Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #186 from lpabon/gcexec
Browse files Browse the repository at this point in the history
Delete a channel once it is no longer used
  • Loading branch information
Luis Pabón committed Sep 2, 2015
2 parents f88c555 + a044d40 commit 0c5d109
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions executors/sshexec/sshexec.go
Expand Up @@ -53,6 +53,7 @@ var (

func NewSshExecutor(config *SshConfig) *SshExecutor {
godbc.Require(config != nil)
godbc.Require(DEFAULT_MAX_CONNECTIONS > 1)

s := &SshExecutor{}
s.throttlemap = make(map[string]chan bool)
Expand Down Expand Up @@ -92,7 +93,6 @@ func NewSshExecutor(config *SshConfig) *SshExecutor {
}

func (s *SshExecutor) accessConnection(host string) {

var (
c chan bool
ok bool
Expand All @@ -102,18 +102,37 @@ func (s *SshExecutor) accessConnection(host string) {
if c, ok = s.throttlemap[host]; !ok {
c = make(chan bool, DEFAULT_MAX_CONNECTIONS)
s.throttlemap[host] = c

// We know we are not going to pend here
// Do this inside the lock to remove the race
// condition of creating the channel, unlocking,
// then having freeConnection() lock, notice that the channel
// is empty and then remove it. When this function tries
// to do c <- true, it will notice that the channel no longer exists
c <- true

s.lock.Unlock()

// Return here
return
}
s.lock.Unlock()

// Do this outside the locks to pend here
c <- true
}

func (s *SshExecutor) freeConnection(host string) {
s.lock.Lock()
c := s.throttlemap[host]
s.lock.Unlock()

<-c

if len(c) == 0 {
close(c)
delete(s.throttlemap, host)
}
s.lock.Unlock()
}

func (s *SshExecutor) sshExec(host string, commands []string, timeoutMinutes int) ([]string, error) {
Expand Down

0 comments on commit 0c5d109

Please sign in to comment.