-
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.
skipper: support http2 over cleartext TCP (h2c)
Works around golang/go#26682 by introducing ShutdownListener that tracks active connections and implements graceful shutdown. For #1253 Signed-off-by: Alexander Yastrebov <[email protected]>
- Loading branch information
1 parent
aa11746
commit 3a9dcac
Showing
4 changed files
with
231 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,76 @@ | ||
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 | ||
once 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) Shutdown(ctx context.Context) error { | ||
ticker := time.NewTicker(500 * time.Millisecond) | ||
defer ticker.Stop() | ||
for { | ||
n := l.activeConns.Load() | ||
log.Debugf("ShutdownListener Shutdown: %d active 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.once.Do(c.listener.unregisterConn) | ||
|
||
return err | ||
} | ||
|
||
func (l *ShutdownListener) registerConn() { | ||
n := l.activeConns.Add(1) | ||
log.Debugf("ShutdownListener registerConn: %d active connections", n) | ||
} | ||
|
||
func (l *ShutdownListener) unregisterConn() { | ||
n := l.activeConns.Add(-1) | ||
log.Debugf("ShutdownListener unregisterConn: %d active 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.