Skip to content

Commit

Permalink
feat(transport): configure HTTP/2 ReadIdleTimeout (#882)
Browse files Browse the repository at this point in the history
Setting this config causes the HTTP/2 transport to send a ping on idle connections every 15s and prune ones that have been severed. This should give increased reliability during network issues between the client and CFE.
  • Loading branch information
tritone committed Apr 16, 2021
1 parent d08bd71 commit c2ff762
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -8,6 +8,7 @@ require (
github.com/googleapis/gax-go/v2 v2.0.5
go.opencensus.io v0.23.0
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4
golang.org/x/oauth2 v0.0.0-20210413134643-5e61552d6c78
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210412220455-f1c623a9e750
Expand Down
25 changes: 25 additions & 0 deletions transport/http/configure_http2_go116.go
@@ -0,0 +1,25 @@
// Copyright 2021 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.16

package http

import (
"net/http"
"time"

"golang.org/x/net/http2"
)

// configureHTTP2 configures the ReadIdleTimeout HTTP/2 option for the
// transport. This allows broken idle connections to be pruned more quickly,
// preventing the client from attempting to re-use connections that will no
// longer work.
func configureHTTP2(trans *http.Transport) {
http2Trans, err := http2.ConfigureTransports(trans)
if err == nil {
http2Trans.ReadIdleTimeout = time.Second * 15
}
}
16 changes: 16 additions & 0 deletions transport/http/configure_http2_not_go116.go
@@ -0,0 +1,16 @@
// Copyright 2021 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !go1.16

package http

import (
"net/http"
)

// configureHTTP2 configures the ReadIdleTimeout HTTP/2 option for the
// transport. The interface to do this is only available in Go 1.16 and up, so
// this performs a no-op.
func configureHTTP2(trans *http.Transport) {}
4 changes: 4 additions & 0 deletions transport/http/dial.go
Expand Up @@ -175,6 +175,10 @@ func defaultBaseTransport(ctx context.Context, clientCertSource cert.Source) htt
}
}

// If possible, configure http2 transport in order to use ReadIdleTimeout
// setting. This can only be done in Go 1.16 and up.
configureHTTP2(trans)

return trans
}

Expand Down

0 comments on commit c2ff762

Please sign in to comment.