diff --git a/components/usage/pkg/server/dialer.go b/components/usage/pkg/server/dialer.go new file mode 100644 index 00000000000000..1ed1ba96848290 --- /dev/null +++ b/components/usage/pkg/server/dialer.go @@ -0,0 +1,25 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the GNU Affero General Public License (AGPL). +// See License-AGPL.txt in the project root for license information. + +package server + +import ( + "context" + "net" + "time" + + "google.golang.org/grpc" +) + +func grpcDialerWithInitialDelay(delay time.Duration) grpc.DialOption { + return grpc.WithContextDialer(func(_ context.Context, addr string) (net.Conn, error) { + <-time.After(delay) + + conn, err := net.Dial("tcp", addr) + if err != nil { + return nil, err + } + return conn, nil + }) +} diff --git a/components/usage/pkg/server/server.go b/components/usage/pkg/server/server.go index 414e2024827778..3d75723551c9b8 100644 --- a/components/usage/pkg/server/server.go +++ b/components/usage/pkg/server/server.go @@ -6,12 +6,13 @@ package server import ( "fmt" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "net" "os" "time" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "github.com/gitpod-io/gitpod/common-go/baseserver" "github.com/gitpod-io/gitpod/common-go/log" v1 "github.com/gitpod-io/gitpod/usage-api/v1" @@ -58,7 +59,8 @@ func Start(cfg Config) error { return fmt.Errorf("failed to initialize usage server: %w", err) } - selfConnection, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials())) + dialerOpt := grpcDialerWithInitialDelay(1 * time.Second) + selfConnection, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()), dialerOpt) if err != nil { return fmt.Errorf("failed to create self-connection to grpc server: %w", err) }