Fix lock problem in server
This commit is contained in:
parent
f1b085fa36
commit
615ceab597
1 changed files with 7 additions and 3 deletions
|
@ -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()
|
||||||
|
|
Loading…
Reference in a new issue