-
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
f35389e
commit ff6f242
Showing
4 changed files
with
236 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.