10BC0 fix: Eagerly establish gRPC connection to avoid initial delay (#2105) · cerbos/cerbos@4929745 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4929745

Browse files
authored
fix: Eagerly establish gRPC connection to avoid initial delay (#2105)
The Go gRPC library has changed its behaviour to lazily establish a connection when the first request is sent. This makes the first request to Cerbos quite slow to return a response. Before: ``` DNS Lookup TCP Connection Server Processing Content Transfer [ 0ms | 0ms | 20023ms | 0ms ] | | | | namelookup:0ms | | | connect:0ms | | starttransfer:20038ms | total:20038ms ``` After: ``` DNS Lookup TCP Connection Server Processing Content Transfer [ 0ms | 0ms | 1ms | 0ms ] | | | | namelookup:0ms | | | connect:0ms | | starttransfer:7ms | total:7ms ``` Signed-off-by: Charith Ellawala <charith@cerbos.dev> Signed-off-by: Charith Ellawala <charith@cerbos.dev>
1 parent b3109b3 commit 4929745

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

cmd/cerbos/healthcheck/healthcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (gc grpcCheck) check(ctx context.Context, out io.Writer) error {
203203
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(gc.tlsConf)))
204204
}
205205

206-
conn, err := grpc.NewClient(gc.addr, dialOpts...)
206+
conn, err := util.EagerGRPCClient(gc.addr, dialOpts...)
207207
if err != nil {
208208
return fmt.Errorf("failed to connect to gRPC service at %q: %w", gc.addr, err)
209209
}

internal/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func (s *Server) mkGRPCConn() (*grpc.ClientConn, error) {
573573
opts = append(opts, grpc.WithTransportCredentials(local.NewCredentials()))
574574
}
575575

576-
grpcConn, err := grpc.NewClient(s.conf.GRPCListenAddr, opts...)
576+
grpcConn, err := util.EagerGRPCClient(s.conf.GRPCListenAddr, opts...)
577577
if err != nil {
578578
return nil, fmt.Errorf("failed to dial gRPC: %w", err)
579579
}

internal/server/tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func mkGRPCConn(t *testing.T, addr string, opts ...grpc.DialOption) *grpc.Client
118118

119119
dialOpts := append(defaultGRPCDialOpts(), opts...)
120120

121-
grpcConn, err := grpc.NewClient(addr, dialOpts...)
121+
grpcConn, err := util.EagerGRPCClient(addr, dialOpts...)
122122
require.NoError(t, err, "Failed to dial gRPC server")
123123

124124
return grpcConn

internal/util/network.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ package util
55

66
import (
77
"crypto/tls"
8+
"fmt"
89
"net"
910
"strconv"
1011
"strings"
12+
13+
"google.golang.org/grpc"
1114
)
1215

1316
// ParseListenAddress parses an address and returns the network type and the address to dial.
@@ -73,3 +76,14 @@ func GetFreePort() (int, error) {
7376

7477
return strconv.Atoi(p)
7578
}
79+
80+
// EagerGRPCClient creates a gRPC client and establishes a connection immediately.
81+
func EagerGRPCClient(target string, dialOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
82+
client, err := grpc.NewClient("passthrough:///"+target, dialOpts...)
83+
if err != nil {
84+
return nil, fmt.Errorf("failed to create gRPC client: %w", err)
85+
}
86+
87+
client.Connect()
88+
return client, nil
89+
}

0 commit comments

Comments
 (0)
0