simplified mutex handling and defer statements
This commit is contained in:
parent
92f951544b
commit
9acf50c2ff
|
@ -23,7 +23,7 @@ func NewTrack(conn net.Conn, domain string) (p *Track) {
|
||||||
|
|
||||||
//Tracking --
|
//Tracking --
|
||||||
type Tracking struct {
|
type Tracking struct {
|
||||||
mutex *sync.Mutex
|
mutex sync.Mutex
|
||||||
connections map[string]*Track
|
connections map[string]*Track
|
||||||
register chan *Track
|
register chan *Track
|
||||||
unregister chan net.Conn
|
unregister chan net.Conn
|
||||||
|
@ -32,7 +32,6 @@ type Tracking struct {
|
||||||
//NewTracking -- Constructor
|
//NewTracking -- Constructor
|
||||||
func NewTracking() (p *Tracking) {
|
func NewTracking() (p *Tracking) {
|
||||||
p = new(Tracking)
|
p = new(Tracking)
|
||||||
p.mutex = &sync.Mutex{}
|
|
||||||
p.connections = make(map[string]*Track)
|
p.connections = make(map[string]*Track)
|
||||||
p.register = make(chan *Track)
|
p.register = make(chan *Track)
|
||||||
p.unregister = make(chan net.Conn)
|
p.unregister = make(chan net.Conn)
|
||||||
|
@ -80,10 +79,8 @@ func (p *Tracking) list() {
|
||||||
//Lookup --
|
//Lookup --
|
||||||
// - get connection from key
|
// - get connection from key
|
||||||
func (p *Tracking) Lookup(key string) (*Track, error) {
|
func (p *Tracking) Lookup(key string) (*Track, error) {
|
||||||
defer func() {
|
|
||||||
p.mutex.Unlock()
|
|
||||||
}()
|
|
||||||
p.mutex.Lock()
|
p.mutex.Lock()
|
||||||
|
defer p.mutex.Unlock()
|
||||||
|
|
||||||
if _, ok := p.connections[key]; ok {
|
if _, ok := p.connections[key]; ok {
|
||||||
return p.connections[key], nil
|
return p.connections[key], nil
|
||||||
|
|
|
@ -20,7 +20,7 @@ var upgrader = websocket.Upgrader{
|
||||||
|
|
||||||
// Connection track websocket and faciliates in and out data
|
// Connection track websocket and faciliates in and out data
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
mutex *sync.Mutex
|
mutex sync.Mutex
|
||||||
|
|
||||||
// The main connection table (should be just one of these created at startup)
|
// The main connection table (should be just one of these created at startup)
|
||||||
connectionTable *Table
|
connectionTable *Table
|
||||||
|
@ -75,7 +75,6 @@ func NewConnection(connectionTable *Table, conn *websocket.Conn, remoteAddress s
|
||||||
connectionID = connectionID + 1
|
connectionID = connectionID + 1
|
||||||
|
|
||||||
p = new(Connection)
|
p = new(Connection)
|
||||||
p.mutex = &sync.Mutex{}
|
|
||||||
p.connectionTable = connectionTable
|
p.connectionTable = connectionTable
|
||||||
p.conn = conn
|
p.conn = conn
|
||||||
p.source = remoteAddress
|
p.source = remoteAddress
|
||||||
|
@ -158,30 +157,25 @@ func (c *Connection) ConnectionTable() *Table {
|
||||||
|
|
||||||
//GetState -- Get state of Socket...this is a high level state.
|
//GetState -- Get state of Socket...this is a high level state.
|
||||||
func (c *Connection) GetState() bool {
|
func (c *Connection) GetState() bool {
|
||||||
defer func() {
|
|
||||||
c.mutex.Unlock()
|
|
||||||
}()
|
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
return c.wssState
|
return c.wssState
|
||||||
}
|
}
|
||||||
|
|
||||||
//State -- Set the set of the high level connection
|
//State -- Set the set of the high level connection
|
||||||
func (c *Connection) State(state bool) {
|
func (c *Connection) State(state bool) {
|
||||||
defer func() {
|
|
||||||
c.mutex.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
c.wssState = state
|
c.wssState = state
|
||||||
}
|
}
|
||||||
|
|
||||||
//Update -- updates the lastUpdate property tracking idle time
|
//Update -- updates the lastUpdate property tracking idle time
|
||||||
func (c *Connection) Update() {
|
func (c *Connection) Update() {
|
||||||
defer func() {
|
|
||||||
c.mutex.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
c.lastUpdate = time.Now()
|
c.lastUpdate = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,9 +269,7 @@ func (c *Connection) Reader(ctx context.Context) {
|
||||||
|
|
||||||
//Writer -- expoer the writer function
|
//Writer -- expoer the writer function
|
||||||
func (c *Connection) Writer() {
|
func (c *Connection) Writer() {
|
||||||
defer func() {
|
defer c.conn.Close()
|
||||||
c.conn.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
loginfo.Println("Writer Start ", c)
|
loginfo.Println("Writer Start ", c)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue