-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work around golang/go#26682 by introducing ShutdownListener that tracks active connections and implements graceful shutdown. For #1253
- Loading branch information
1 parent
f35389e
commit 55045ae
Showing
4 changed files
with
233 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type ( | ||
ShutdownListener struct { | ||
net.Listener | ||
activeConns atomic.Int64 | ||
} | ||
|
||
shutdownListenerConn struct { | ||
net.Conn | ||
listener *ShutdownListener | ||
closeOnce sync.Once | ||
} | ||
) | ||
|
||
var _ net.Listener = &ShutdownListener{} | ||
|
||
func NewShutdownListener(l net.Listener) *ShutdownListener { | ||
return &ShutdownListener{Listener: l} | ||
} | ||
|
||
func (l *ShutdownListener) Accept() (net.Conn, error) { | ||
c, err := l.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l.registerConn() | ||
|
||
return &shutdownListenerConn{Conn: c, listener: l}, nil | ||
} | ||
|
||
func (l *ShutdownListener) Close() error { | ||
err := l.Listener.Close() | ||
return err | ||
} | ||
|
||
func (l *ShutdownListener) Shutdown(ctx context.Context) error { | ||
ticker := time.NewTicker(500 * time.Millisecond) | ||
defer ticker.Stop() | ||
for { | ||
n := l.activeConns.Load() | ||
log.Debugf("ShutdownListener Shutdown: %d connections", n) | ||
if n == 0 { | ||
return nil | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-ticker.C: | ||
} | ||
} | ||
} | ||
|
||
func (c *shutdownListenerConn) Close() error { | ||
err := c.Conn.Close() | ||
|
||
c.closeOnce.Do(func() { c.listener.unregisterConn() }) | ||
|
||
return err | ||
} | ||
|
||
func (l *ShutdownListener) registerConn() { | ||
n := l.activeConns.Add(1) | ||
log.Debugf("ShutdownListener registerConn: %d connections", n) | ||
} | ||
|
||
func (l *ShutdownListener) unregisterConn() { | ||
n := l.activeConns.Add(-1) | ||
log.Debugf("ShutdownListener unregisterConn: %d connections", n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.