Skip to content

Commit

Permalink
Fix #52
Browse files Browse the repository at this point in the history
  • Loading branch information
jhead committed Jan 5, 2020
1 parent af1bd53 commit 9a76c58
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions internal/clientmap/clientmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type ClientMap struct {
IdleTimeout time.Duration
IdleCheckInterval time.Duration
clients map[string]clientEntry
clients map[string]*clientEntry
dead *abool.AtomicBool
mutex *sync.RWMutex
}
Expand All @@ -33,7 +33,7 @@ func New(idleTimeout time.Duration, idleCheckInterval time.Duration) *ClientMap
clientMap := ClientMap{
idleTimeout,
idleCheckInterval,
make(map[string]clientEntry),
make(map[string]*clientEntry),
abool.New(),
&sync.RWMutex{},
}
Expand Down Expand Up @@ -97,26 +97,22 @@ func (cm *ClientMap) Get(
key := clientAddr.String()

// Check if connection exists
cm.mutex.RLock()
cm.mutex.Lock()
defer cm.mutex.Unlock()

if client, ok := cm.clients[key]; ok {
cm.mutex.RUnlock()
client.lastActive = time.Now()
return client.conn, nil
}

cm.mutex.RUnlock()
cm.mutex.Lock()
defer cm.mutex.Unlock()

// New connection needed
logger.Printf("Opening connection to %s for new client %s!\n", remote, clientAddr)
newServerConn, err := newServerConnection(remote)
if err != nil {
return nil, err
}

cm.clients[key] = clientEntry{
cm.clients[key] = &clientEntry{
newServerConn,
time.Now(),
}
Expand Down

0 comments on commit 9a76c58

Please sign in to comment.