From 196c609d1ed72e4ca16fddb0884f1a5b2ecb7338 Mon Sep 17 00:00:00 2001
From: Andrey Smirnov <andrey.smirnov@siderolabs.com>
Date: Tue, 28 May 2024 14:27:14 +0400
Subject: [PATCH] fix: use shared gRPC buffers, lower buffer size

As Discovery Service handles lots of connections with relatively low
traffic on each connection, lower the buffer size and re-use the
read/write buffers.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
---
 cmd/discovery-service/main.go | 5 +++++
 pkg/server/server_test.go     | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/cmd/discovery-service/main.go b/cmd/discovery-service/main.go
index b9dce91..58f9ac3 100644
--- a/cmd/discovery-service/main.go
+++ b/cmd/discovery-service/main.go
@@ -31,6 +31,7 @@ import (
 	"golang.org/x/sync/errgroup"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/codes"
+	"google.golang.org/grpc/experimental"
 	"google.golang.org/grpc/keepalive"
 	"google.golang.org/grpc/status"
 
@@ -192,6 +193,10 @@ func run(ctx context.Context, logger *zap.Logger) error {
 		grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
 			MinTime: 10 * time.Second,
 		}),
+		grpc.SharedWriteBuffer(true),
+		experimental.RecvBufferPool(grpc.NewSharedBufferPool()),
+		grpc.ReadBufferSize(16 * 1024),
+		grpc.WriteBufferSize(16 * 1024),
 	}
 
 	state := state.NewState(logger)
diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go
index 89c24f4..0e90a19 100644
--- a/pkg/server/server_test.go
+++ b/pkg/server/server_test.go
@@ -25,6 +25,7 @@ import (
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/credentials/insecure"
+	"google.golang.org/grpc/experimental"
 	"google.golang.org/grpc/metadata"
 	"google.golang.org/grpc/status"
 	"google.golang.org/protobuf/proto"
@@ -98,6 +99,10 @@ func setupServer(t *testing.T, rateLimit rate.Limit, redirectEndpoint string) *t
 			server.AddLoggingFieldsStreamServerInterceptor(),
 			server.RateLimitStreamServerInterceptor(limiter),
 		),
+		grpc.SharedWriteBuffer(true),
+		experimental.RecvBufferPool(grpc.NewSharedBufferPool()),
+		grpc.ReadBufferSize(16 * 1024),
+		grpc.WriteBufferSize(16 * 1024),
 	}
 
 	testServer.s = grpc.NewServer(testServer.serverOptions...)