Skip to content

Commit

Permalink
Enable HTTP/2 connection health checking
Browse files Browse the repository at this point in the history
This limits the effect of a connection dropout by detecting it and
reconnecting after ~30s. This is added to Kubernetes in v1.19.4, but
I think we need to switch to Go modules before updating to that.

Fixes googlecloudrobotics#64.

Change-Id: I709b6bcecc35fd28bd904e6047bf479b0609117d
  • Loading branch information
drigz committed Feb 8, 2021
1 parent 38e9c21 commit b260340
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ go_repository(

go_repository(
name = "org_golang_x_net",
commit = "4cb1c02c05b0e749b0365f61ae859a8e0cfceed9",
commit = "5f4716e94777e714bc2fb3e3a44599cb40817aac",
importpath = "golang.org/x/net",
)

Expand Down
5 changes: 3 additions & 2 deletions src/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/go/cmd/cr-syncer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"@io_opencensus_go//tag:go_default_library",
"@io_opencensus_go//zpages:go_default_library",
"@org_golang_x_net//context:go_default_library",
"@org_golang_x_net//http2:go_default_library",
"@org_golang_x_oauth2//:go_default_library",
"@org_golang_x_oauth2//google:go_default_library",
],
Expand Down
22 changes: 22 additions & 0 deletions src/go/cmd/cr-syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"go.opencensus.io/tag"
"go.opencensus.io/zpages"
"golang.org/x/net/context"
"golang.org/x/net/http2"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
crdtypes "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
Expand Down Expand Up @@ -152,6 +153,23 @@ func (r *ctxRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return r.base.RoundTrip(req.WithContext(r.ctx))
}

// Configure HTTP/2 liveness checking on the connection, so that dropouts are
// noticed and handled appropriately. Errors are ignored, as this is not
// essential in normal conditions.
func configureTransport(base http.RoundTripper) {
t1, ok := base.(*http.Transport)
if !ok {
log.Printf("failed to configure transport: expected http.Transport, got %T", base)
}
t2, err := http2.ConfigureTransports(t1)
if err != nil {
log.Printf("failed to enable HTTP/2 on transport: %v", err)
}
t2.ReadIdleTimeout = 30 * time.Second
t2.PingTimeout = 15 * time.Second
// The transport has been modified in-place, no need to return it.
}

// restConfigForRemote assembles the K8s REST config for the remote server.
func restConfigForRemote(ctx context.Context) (*rest.Config, error) {
tokenSource, err := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform")
Expand All @@ -163,6 +181,10 @@ func restConfigForRemote(ctx context.Context) (*rest.Config, error) {
return nil, err
}
transport := func(base http.RoundTripper) (rt http.RoundTripper) {
// Configure the transport to better handle dropped connections.
// TODO(rodrigoq): remove when updating to client-go kubernetes-1.19.4
configureTransport(base)

rt = &oauth2.Transport{
Source: tokenSource,
Base: base,
Expand Down

0 comments on commit b260340

Please sign in to comment.