Skip to content

Commit

Permalink
feat(client): enable gzip encoding (#44)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hspedro authored Nov 27, 2024
1 parent e5b61ec commit 69e3991
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
13 changes: 10 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ client:
time: 60s
timeout: 15s
permitwithoutstreams: true
gzip:
enabled: false
grpc:
serverAddress: eventsgateway-api:5000 #eventsgateway-api:5000
timeout: 500ms
Expand Down
2 changes: 2 additions & 0 deletions config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ client:
maxRetries: 3
numRoutines: 1
retryInterval: 1s
gzip:
enabled: false
grpc:
serverAddress: eventsgateway-api:5000
timeoutms: 500ms
4 changes: 4 additions & 0 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 69e3991

Please sign in to comment.