From 9acf50c2ff30225e51dcb14d0d9c0a5c18e37d59 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Wed, 22 Mar 2017 16:47:53 -0600 Subject: [PATCH] simplified mutex handling and defer statements --- rvpn/genericlistener/conn_tracking.go | 7 ++----- rvpn/genericlistener/connection.go | 24 ++++++++---------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/rvpn/genericlistener/conn_tracking.go b/rvpn/genericlistener/conn_tracking.go index ed9ed71..a89de9e 100644 --- a/rvpn/genericlistener/conn_tracking.go +++ b/rvpn/genericlistener/conn_tracking.go @@ -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 diff --git a/rvpn/genericlistener/connection.go b/rvpn/genericlistener/connection.go index 93cd988..882c8f7 100755 --- a/rvpn/genericlistener/connection.go +++ b/rvpn/genericlistener/connection.go @@ -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)