Skip to content

Commit 74d7983

Browse files
committed
http2/h2demo: update bug link, add idle conn timeouts
Updates golang/go#14204 Change-Id: Id2598c77e2677a50988c00adc8751a9b87751202 Reviewed-on: https://go-review.googlesource.com/19159 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent 6c581b9 commit 74d7983

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

http2/h2demo/h2demo.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"time"
2929

3030
"camlistore.org/pkg/googlestorage"
31-
"camlistore.org/pkg/singleflight"
31+
"go4.org/syncutil/singleflight"
3232
"golang.org/x/net/http2"
3333
)
3434

@@ -79,7 +79,7 @@ is used transparently by the Go standard library from Go 1.6 and later.
7979
</p>
8080
8181
<p>Contact info: <i>[email protected]</i>, or <a
82-
href="https://golang.org/issues">file a bug</a>.</p>
82+
href="https://golang.org/s/http2bug">file a bug</a>.</p>
8383
8484
<h2>Handlers for testing</h2>
8585
<ul>
@@ -440,11 +440,43 @@ func serveProd() error {
440440
return <-errc
441441
}
442442

443+
const idleTimeout = 5 * time.Minute
444+
const activeTimeout = 10 * time.Minute
445+
446+
// TODO: put this into the standard library and actually send
447+
// PING frames and GOAWAY, etc: golang.org/issue/14204
448+
func idleTimeoutHook() func(net.Conn, http.ConnState) {
449+
var mu sync.Mutex
450+
m := map[net.Conn]*time.Timer{}
451+
return func(c net.Conn, cs http.ConnState) {
452+
mu.Lock()
453+
defer mu.Unlock()
454+
if t, ok := m[c]; ok {
455+
delete(m, c)
456+
t.Stop()
457+
}
458+
var d time.Duration
459+
switch cs {
460+
case http.StateNew, http.StateIdle:
461+
d = idleTimeout
462+
case http.StateActive:
463+
d = activeTimeout
464+
default:
465+
return
466+
}
467+
m[c] = time.AfterFunc(d, func() {
468+
log.Printf("closing idle conn %v after %v", c.RemoteAddr(), d)
469+
go c.Close()
470+
})
471+
}
472+
}
473+
443474
func main() {
444475
var srv http.Server
445476
flag.BoolVar(&http2.VerboseLogs, "verbose", false, "Verbose HTTP/2 debugging.")
446477
flag.Parse()
447478
srv.Addr = *httpsAddr
479+
srv.ConnState = idleTimeoutHook()
448480

449481
registerHandlers()
450482

0 commit comments

Comments
 (0)