Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable gzip grpc-accept-encoding #349

Merged
merged 8 commits into from
Aug 17, 2022
14 changes: 9 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (c *Client[Req, Res]) newConn(ctx context.Context, streamType StreamType) S
}

type clientConfig struct {
GzipAcceptEncoding bool
Protocol protocol
Procedure string
CompressMinBytes int
Expand All @@ -185,16 +186,19 @@ type clientConfig struct {
func newClientConfig(url string, options []ClientOption) (*clientConfig, *Error) {
protoPath := extractProtoPath(url)
config := clientConfig{
Protocol: &protocolConnect{},
Procedure: protoPath,
CompressionPools: make(map[string]*compressionPool),
BufferPool: newBufferPool(),
Protocol: &protocolConnect{},
Procedure: protoPath,
CompressionPools: make(map[string]*compressionPool),
BufferPool: newBufferPool(),
GzipAcceptEncoding: true,
}
withProtoBinaryCodec().applyToClient(&config)
withGzip().applyToClient(&config)
for _, opt := range options {
opt.applyToClient(&config)
}
if config.GzipAcceptEncoding {
withGzip().applyToClient(&config)
}
if err := config.validate(); err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func WithAcceptCompression(
}
}

// WithGzipAcceptEncoding sets the Grpc-Accept-Encoding header to gzip when
// enabled.
// Defaults to true in connect.Client config.
func WithGzipAcceptEncoding(enabled bool) ClientOption {
return &defaultAcceptEncodingOption{
gzip: enabled,
}
}

// WithClientOptions composes multiple ClientOptions into one.
func WithClientOptions(options ...ClientOption) ClientOption {
return &clientOptionsOption{options}
Expand Down Expand Up @@ -367,6 +376,14 @@ func (o *grpcOption) applyToClient(config *clientConfig) {
config.Protocol = &protocolGRPC{web: o.web}
}

type defaultAcceptEncodingOption struct {
gzip bool
}

func (o *defaultAcceptEncodingOption) applyToClient(config *clientConfig) {
config.GzipAcceptEncoding = o.gzip
}

type interceptorsOption struct {
Interceptors []Interceptor
}
Expand Down