Skip to content

Commit 04b42bd

Browse files
committed
Remove WithMaxReadBytes
The more I look at it, the more convinced I am that this option is a bad idea. It's very unclear what it's trying to accomplish, and there are many better options: * Limiting heap usage? Use the upcoming soft memory limit APIs (golang/go#48409). * Limiting network I/O? Use `http.MaxBytesReader` and set a per-stream limit. * Banning "large messages"? Be clear what you mean, and use `unsafe.SizeOf` or `proto.Size` in an interceptor. Basically, the behavior here (and in grpc-go) is an incoherent middle ground between Go runtime settings, HTTP-level settings, and a vague "no large messages" policy. I'm doubly sure we should delete this because we've decided not to expose the metrics to track how close users are to the configured limit :)
1 parent bfc288f commit 04b42bd

10 files changed

+14
-125
lines changed

client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func NewClient[Req, Res any](
5151
CompressionPools: newReadOnlyCompressionPools(config.CompressionPools),
5252
Codec: config.Codec,
5353
Protobuf: config.protobuf(),
54-
MaxResponseBytes: config.MaxResponseBytes,
5554
CompressMinBytes: config.CompressMinBytes,
5655
HTTPClient: httpClient,
5756
URL: url,
@@ -166,7 +165,6 @@ func (c *Client[Req, Res]) newStream(ctx context.Context, streamType StreamType)
166165
type clientConfiguration struct {
167166
Protocol protocol
168167
Procedure string
169-
MaxResponseBytes int64
170168
CompressMinBytes int
171169
Interceptor Interceptor
172170
CompressionPools map[string]compressionPool

handler.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ func (h *Handler) failNegotiation(w http.ResponseWriter, code int) {
244244
type handlerConfiguration struct {
245245
CompressionPools map[string]compressionPool
246246
Codecs map[string]Codec
247-
MaxRequestBytes int64
248247
CompressMinBytes int
249248
Interceptor Interceptor
250249
Procedure string
@@ -293,7 +292,6 @@ func (c *handlerConfiguration) newProtocolHandlers(streamType StreamType) []prot
293292
Spec: c.newSpecification(streamType),
294293
Codecs: codecs,
295294
CompressionPools: compressors,
296-
MaxRequestBytes: c.MaxRequestBytes,
297295
CompressMinBytes: c.CompressMinBytes,
298296
}))
299297
}

handler_example_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ func Example_handler() {
5151
// (for example, net/http's StripPrefix).
5252
mux := http.NewServeMux()
5353
mux.Handle(pingv1connect.NewPingServiceHandler(
54-
&ExamplePingServer{}, // our business logic
55-
connect.WithReadMaxBytes(1024*1024), // limit request size
54+
&ExamplePingServer{}, // our business logic
5655
))
5756
// You can serve gRPC's health and server reflection APIs using
5857
// github.com/bufbuild/connect-grpchealth-go and github.com/bufbuild/connect-grpcreflect-go.

handler_test.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

option.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,6 @@ func WithProtoJSONCodec() Option {
231231
return WithCodec(&protoJSONCodec{})
232232
}
233233

234-
// WithReadMaxBytes limits the performance impact of pathologically large
235-
// messages sent by the other party. For handlers, WithReadMaxBytes limits the size
236-
// of message that the client can send. For clients, WithReadMaxBytes limits the
237-
// size of message that the server can respond with. Limits are applied before
238-
// decompression and apply to each Protobuf message, not to the stream as a
239-
// whole.
240-
//
241-
// Setting WithReadMaxBytes to zero allows any message size. Both clients and
242-
// handlers default to allowing any request size.
243-
func WithReadMaxBytes(n int64) Option {
244-
return &readMaxBytesOption{n}
245-
}
246-
247234
type clientOptionsOption struct {
248235
options []ClientOption
249236
}
@@ -363,18 +350,6 @@ func (o *optionsOption) applyToHandler(config *handlerConfiguration) {
363350
}
364351
}
365352

366-
type readMaxBytesOption struct {
367-
Max int64
368-
}
369-
370-
func (o *readMaxBytesOption) applyToClient(config *clientConfiguration) {
371-
config.MaxResponseBytes = o.Max
372-
}
373-
374-
func (o *readMaxBytesOption) applyToHandler(config *handlerConfiguration) {
375-
config.MaxRequestBytes = o.Max
376-
}
377-
378353
type requestCompressionOption struct {
379354
Name string
380355
}

protocol.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ type protocolHandlerParams struct {
4848
Spec Specification
4949
Codecs readOnlyCodecs
5050
CompressionPools readOnlyCompressionPools
51-
MaxRequestBytes int64
5251
CompressMinBytes int
5352
}
5453

@@ -91,7 +90,6 @@ type protocolClientParams struct {
9190
CompressionName string
9291
CompressionPools readOnlyCompressionPools
9392
Codec Codec
94-
MaxResponseBytes int64
9593
CompressMinBytes int
9694
HTTPClient HTTPClient
9795
URL string

protocol_grpc.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func (g *protocolGRPC) NewHandler(params *protocolHandlerParams) protocolHandler
3535
web: g.web,
3636
codecs: params.Codecs,
3737
compressionPools: params.CompressionPools,
38-
maxRequestBytes: params.MaxRequestBytes,
3938
minCompressBytes: params.CompressMinBytes,
4039
accept: acceptPostValue(g.web, params.Codecs),
4140
}
@@ -57,7 +56,6 @@ func (g *protocolGRPC) NewClient(params *protocolClientParams) (protocolClient,
5756
compressionPools: params.CompressionPools,
5857
codec: params.Codec,
5958
protobuf: params.Protobuf,
60-
maxResponseBytes: params.MaxResponseBytes,
6159
minCompressBytes: params.CompressMinBytes,
6260
httpClient: params.HTTPClient,
6361
procedureURL: params.URL,
@@ -69,7 +67,6 @@ type grpcHandler struct {
6967
web bool
7068
codecs readOnlyCodecs
7169
compressionPools readOnlyCompressionPools
72-
maxRequestBytes int64
7370
minCompressBytes int
7471
accept string
7572
}
@@ -171,7 +168,6 @@ func (g *grpcHandler) NewStream(
171168
g.web,
172169
responseWriter,
173170
request,
174-
g.maxRequestBytes,
175171
g.minCompressBytes,
176172
clientCodec,
177173
g.codecs.Protobuf(), // for errors
@@ -212,7 +208,6 @@ type grpcClient struct {
212208
compressionPools readOnlyCompressionPools
213209
codec Codec
214210
protobuf Codec
215-
maxResponseBytes int64
216211
minCompressBytes int
217212
httpClient HTTPClient
218213
procedureURL string
@@ -255,14 +250,13 @@ func (g *grpcClient) NewStream(
255250
// the response stream.
256251
pipeReader, pipeWriter := io.Pipe()
257252
duplex := &duplexClientStream{
258-
ctx: ctx,
259-
httpClient: g.httpClient,
260-
url: g.procedureURL,
261-
spec: spec,
262-
maxReadBytes: g.maxResponseBytes,
263-
codec: g.codec,
264-
protobuf: g.protobuf,
265-
writer: pipeWriter,
253+
ctx: ctx,
254+
httpClient: g.httpClient,
255+
url: g.procedureURL,
256+
spec: spec,
257+
codec: g.codec,
258+
protobuf: g.protobuf,
259+
writer: pipeWriter,
266260
marshaler: marshaler{
267261
writer: pipeWriter,
268262
compressionPool: g.compressionPools.Get(g.compressionName),

protocol_grpc_client_stream.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ func (cr *clientReceiver) Trailer() http.Header { return cr.stream.ResponseTrail
6464
// request/response code. Since this is the most complex code in connect, it has
6565
// many more comments than usual.
6666
type duplexClientStream struct {
67-
ctx context.Context
68-
httpClient HTTPClient
69-
url string
70-
spec Specification
71-
maxReadBytes int64
72-
codec Codec
73-
protobuf Codec // for errors
67+
ctx context.Context
68+
httpClient HTTPClient
69+
url string
70+
spec Specification
71+
codec Codec
72+
protobuf Codec // for errors
7473

7574
// send: guarded by prepareOnce because we can't initialize this state until
7675
// the first call to Send.
@@ -334,7 +333,6 @@ func (cs *duplexClientStream) makeRequest(prepared chan struct{}) {
334333
mergeHeaders(cs.responseHeader, res.Header)
335334
cs.unmarshaler = unmarshaler{
336335
reader: res.Body,
337-
max: cs.maxReadBytes,
338336
codec: cs.codec,
339337
compressionPool: cs.compressionPools.Get(compression),
340338
web: cs.web,

protocol_grpc_handler_stream.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func newHandlerStream(
2727
web bool,
2828
responseWriter http.ResponseWriter,
2929
request *http.Request,
30-
maxReadBytes int64,
3130
compressMinBytes int,
3231
codec Codec,
3332
protobuf Codec, // for errors
@@ -53,7 +52,6 @@ func newHandlerStream(
5352
unmarshaler: unmarshaler{
5453
web: web,
5554
reader: request.Body,
56-
max: maxReadBytes,
5755
compressionPool: requestCompressionPools,
5856
codec: codec,
5957
},

protocol_grpc_lpm.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ func (m *marshaler) writeGRPCPrefix(compressed, trailer bool, size int) *Error {
119119

120120
type unmarshaler struct {
121121
reader io.Reader
122-
max int64
123122
codec Codec
124123
compressionPool compressionPool
125124

@@ -169,8 +168,6 @@ func (u *unmarshaler) Unmarshal(message any) (retErr *Error) {
169168
size := int(binary.BigEndian.Uint32(prefixes[1:5]))
170169
if size < 0 {
171170
return errorf(CodeInvalidArgument, "message size %d overflowed uint32", size)
172-
} else if u.max > 0 && int64(size) > u.max {
173-
return errorf(CodeInvalidArgument, "message size %d is larger than configured max %d", size, u.max)
174171
}
175172
// OPT: easy opportunity to pool buffers and grab the underlying byte slice
176173
raw := make([]byte, size)

0 commit comments

Comments
 (0)