Fix lock problem in server

This commit is contained in:
Julien Salleyron 2019-03-15 10:04:05 +01:00 committed by Traefiker Bot
parent f1b085fa36
commit 615ceab597

View file

@ -288,16 +288,20 @@ func (c *connectionTracker) RemoveConnection(conn net.Conn) {
delete(c.conns, conn) delete(c.conns, conn)
} }
func (c *connectionTracker) isEmpty() bool {
c.lock.RLock()
defer c.lock.RUnlock()
return len(c.conns) == 0
}
// Shutdown wait for the connection closing // Shutdown wait for the connection closing
func (c *connectionTracker) Shutdown(ctx context.Context) error { func (c *connectionTracker) Shutdown(ctx context.Context) error {
ticker := time.NewTicker(500 * time.Millisecond) ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop() defer ticker.Stop()
for { for {
c.lock.RLock() if c.isEmpty() {
if len(c.conns) == 0 {
return nil return nil
} }
c.lock.RUnlock()
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()