Skip to content

Commit

Permalink
support grpc gzip compression
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Oct 7, 2023
1 parent 0c5c2ae commit c253bb1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/proxy/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/url"
"strings"

"google.golang.org/grpc/encoding/gzip"

"github.com/centrifugal/centrifugo/v5/internal/middleware"
"github.com/centrifugal/centrifugo/v5/internal/proxyproto"

Expand Down Expand Up @@ -64,6 +66,9 @@ func getDialOpts(p Proxy) ([]grpc.DialOption, error) {
} else {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
if p.GrpcCompression {
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
}

if p.testGrpcDialer != nil {
dialOpts = append(dialOpts, grpc.WithContextDialer(p.testGrpcDialer))
Expand Down
2 changes: 2 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Proxy struct {
GrpcCredentialsKey string `mapstructure:"grpc_credentials_key" json:"grpc_credentials_key,omitempty"`
// GrpcCredentialsValue is a custom value for GrpcCredentialsKey.
GrpcCredentialsValue string `mapstructure:"grpc_credentials_value" json:"grpc_credentials_value,omitempty"`
// GrpcCompression enables compression for outgoing calls (gzip).
GrpcCompression bool `mapstructure:"grpc_compression" json:"grpc_compression,omitempty"`

testGrpcDialer func(context.Context, string) (net.Conn, error)
}
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import (
"golang.org/x/crypto/acme/autocert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
_ "google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
)
Expand Down Expand Up @@ -271,6 +272,7 @@ var defaults = map[string]any{
"proxy_binary_encoding": false,
"proxy_include_connection_meta": false,
"proxy_grpc_cert_file": "",
"proxy_grpc_compression": false,

"tarantool_mode": "standalone",
"tarantool_address": "tcp://127.0.0.1:3301",
Expand Down Expand Up @@ -1746,6 +1748,7 @@ func proxyMapConfig() (*client.ProxyMap, bool) {
p.GrpcCertFile = v.GetString("proxy_grpc_cert_file")
p.GrpcCredentialsKey = v.GetString("proxy_grpc_credentials_key")
p.GrpcCredentialsValue = v.GetString("proxy_grpc_credentials_value")
p.GrpcCompression = v.GetBool("proxy_grpc_compression")

connectEndpoint := v.GetString("proxy_connect_endpoint")
connectTimeout := GetDuration("proxy_connect_timeout")
Expand Down

0 comments on commit c253bb1

Please sign in to comment.