Check if Listener exists before trying to close it

This commit is contained in:
Sarah Jamie Lewis 2020-10-29 15:45:07 -07:00
parent c0b675b011
commit 9fba459adc
2 changed files with 11 additions and 2 deletions

View File

@ -21,6 +21,7 @@ type BaseOnionService struct {
privateKey ed25519.PrivateKey
ls connectivity.ListenService
lock sync.Mutex
port int
}
// Metrics provides a report of useful information about the status of the service e.g. the number of active
@ -51,6 +52,12 @@ func (s *BaseOnionService) Init(acn connectivity.ACN, sk ed25519.PrivateKey, id
s.acn = acn
s.id = id
s.privateKey = sk
s.port = 9878
}
// SetPort configures the port that the service uses.
func (s *BaseOnionService) SetPort(port int) {
s.port = port
}
// WaitForCapabilityOrClose blocks until the connection has the given capability or the underlying connection is closed
@ -146,7 +153,7 @@ func (s *BaseOnionService) Listen(app tapir.Application) error {
// accepts a new connection
// spins off to a connection struct
s.lock.Lock()
ls, err := s.acn.Listen(s.privateKey, 9878)
ls, err := s.acn.Listen(s.privateKey, s.port)
s.ls = ls
log.Debugf("Starting a service on %v ", ls.AddressFull())
s.lock.Unlock()
@ -172,7 +179,9 @@ func (s *BaseOnionService) Listen(app tapir.Application) error {
func (s *BaseOnionService) Shutdown() {
s.lock.Lock()
defer s.lock.Unlock()
s.ls.Close()
if s.ls != nil {
s.ls.Close()
}
s.connections.Range(func(key, value interface{}) bool {
connection := value.(tapir.Connection)
connection.Close()

0
scratch/main.go Normal file
View File