From 69e3991f5a34912ad485086925fad158f7b75179 Mon Sep 17 00:00:00 2001 From: Pedro Soares Date: Wed, 27 Nov 2024 15:14:39 -0300 Subject: [PATCH] feat(client): enable gzip encoding (#44) If client enables gzip via env var client.gzip.enabled (string representing bool) then gzip will be enabled by default in all GRPC calls via DialOptions. Servers created via grpc.NewServer() will have gzip compressor by default if imported as per: https://github.com/grpc/grpc-go/blob/master/examples/features/compression/server/main.go#L33 --- client/client.go | 13 ++++++++++--- config/local.yaml | 2 ++ config/test.yaml | 2 ++ server/app/app.go | 4 ++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index 31b2258..29e145a 100644 --- a/client/client.go +++ b/client/client.go @@ -10,15 +10,17 @@ package client import ( "context" "fmt" + "strings" + "sync" + "time" + "github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc" "github.com/opentracing/opentracing-go" "github.com/topfreegames/eventsgateway/v4/metrics" "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" "go.opentelemetry.io/otel" + "google.golang.org/grpc/encoding/gzip" "google.golang.org/grpc/keepalive" - "strings" - "sync" - "time" uuid "github.com/satori/go.uuid" "github.com/sirupsen/logrus" @@ -101,6 +103,11 @@ func New( opts..., ) + gzipEnabled := c.config.GetBool(fmt.Sprintf("%sclient.gzip.enabled", configPrefix)) + if gzipEnabled { + dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name))) + } + c.logger = c.logger.WithFields(map[string]interface{}{ "serverAddress": c.serverAddress, "async": async, diff --git a/config/local.yaml b/config/local.yaml index 87879b0..cf6a9ad 100644 --- a/config/local.yaml +++ b/config/local.yaml @@ -19,6 +19,8 @@ client: time: 60s timeout: 15s permitwithoutstreams: true + gzip: + enabled: false grpc: serverAddress: eventsgateway-api:5000 #eventsgateway-api:5000 timeout: 500ms diff --git a/config/test.yaml b/config/test.yaml index 7854b33..6a6fcc6 100644 --- a/config/test.yaml +++ b/config/test.yaml @@ -11,6 +11,8 @@ client: maxRetries: 3 numRoutines: 1 retryInterval: 1s + gzip: + enabled: false grpc: serverAddress: eventsgateway-api:5000 timeoutms: 500ms diff --git a/server/app/app.go b/server/app/app.go index 72814a0..0549050 100644 --- a/server/app/app.go +++ b/server/app/app.go @@ -39,6 +39,10 @@ import ( "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/propagation" + // Installing the gzip encoding registers it as an available compressor. + // gRPC will automatically negotiate and use gzip if the client supports it. + _ "google.golang.org/grpc/encoding/gzip" + goMetrics "github.com/rcrowley/go-metrics" "github.com/topfreegames/eventsgateway/v4/server/logger" "github.com/topfreegames/eventsgateway/v4/server/metrics"