Implement method to gracefully destroy sessionManager

This commit is contained in:
Hendrik Hofstadt 2018-09-08 20:26:59 +02:00
parent 99b6b2ec67
commit 692452fd03
1 changed files with 14 additions and 0 deletions

View File

@ -21,6 +21,7 @@ type (
poolSize uint
creationWait sync.WaitGroup
destroyed bool
}
)
@ -35,6 +36,7 @@ func NewSessionManager(connector connector.Connector, authKeyID uint16, password
authKeyID: authKeyID,
password: password,
poolSize: poolSize,
destroyed: false,
}
manager.household()
@ -93,6 +95,9 @@ func (s *SessionManager) household() {
}
func (s *SessionManager) GetSession() (*securechannel.SecureChannel, error) {
if s.destroyed {
return nil, errors.New("sessionmanager has already been destroyed")
}
if len(s.sessions) == 0 {
return nil, errors.New("no sessions available")
}
@ -101,3 +106,12 @@ func (s *SessionManager) GetSession() (*securechannel.SecureChannel, error) {
defer s.lock.Unlock()
return s.sessions[rand.Intn(len(s.sessions))], nil
}
func (s *SessionManager) Destroy() {
s.lock.Lock()
defer s.lock.Unlock()
for _, session := range s.sessions {
session.Close()
}
}