simplified mutex handling and defer statements

This commit is contained in:
tigerbot 2017-03-22 16:47:53 -06:00
parent 92f951544b
commit 9acf50c2ff
2 changed files with 10 additions and 21 deletions

View File

@ -23,7 +23,7 @@ func NewTrack(conn net.Conn, domain string) (p *Track) {
//Tracking --
type Tracking struct {
mutex *sync.Mutex
mutex sync.Mutex
connections map[string]*Track
register chan *Track
unregister chan net.Conn
@ -32,7 +32,6 @@ type Tracking struct {
//NewTracking -- Constructor
func NewTracking() (p *Tracking) {
p = new(Tracking)
p.mutex = &sync.Mutex{}
p.connections = make(map[string]*Track)
p.register = make(chan *Track)
p.unregister = make(chan net.Conn)
@ -80,10 +79,8 @@ func (p *Tracking) list() {
//Lookup --
// - get connection from key
func (p *Tracking) Lookup(key string) (*Track, error) {
defer func() {
p.mutex.Unlock()
}()
p.mutex.Lock()
defer p.mutex.Unlock()
if _, ok := p.connections[key]; ok {
return p.connections[key], nil

View File

@ -20,7 +20,7 @@ var upgrader = websocket.Upgrader{
// Connection track websocket and faciliates in and out data
type Connection struct {
mutex *sync.Mutex
mutex sync.Mutex
// The main connection table (should be just one of these created at startup)
connectionTable *Table
@ -75,7 +75,6 @@ func NewConnection(connectionTable *Table, conn *websocket.Conn, remoteAddress s
connectionID = connectionID + 1
p = new(Connection)
p.mutex = &sync.Mutex{}
p.connectionTable = connectionTable
p.conn = conn
p.source = remoteAddress
@ -158,30 +157,25 @@ func (c *Connection) ConnectionTable() *Table {
//GetState -- Get state of Socket...this is a high level state.
func (c *Connection) GetState() bool {
defer func() {
c.mutex.Unlock()
}()
c.mutex.Lock()
defer c.mutex.Unlock()
return c.wssState
}
//State -- Set the set of the high level connection
func (c *Connection) State(state bool) {
defer func() {
c.mutex.Unlock()
}()
c.mutex.Lock()
defer c.mutex.Unlock()
c.wssState = state
}
//Update -- updates the lastUpdate property tracking idle time
func (c *Connection) Update() {
defer func() {
c.mutex.Unlock()
}()
c.mutex.Lock()
defer c.mutex.Unlock()
c.lastUpdate = time.Now()
}
@ -275,9 +269,7 @@ func (c *Connection) Reader(ctx context.Context) {
//Writer -- expoer the writer function
func (c *Connection) Writer() {
defer func() {
c.conn.Close()
}()
defer c.conn.Close()
loginfo.Println("Writer Start ", c)