websocket icon indicating copy to clipboard operation
websocket copied to clipboard

Add ReadLimit to Option structs

Open ash2k opened this issue 5 years ago • 9 comments

Would it be possible to add a field to AcceptOptions and to DialOptions that sets the read limit on the created connection? It's not as convenient to have to call the SetReadLimit(). If the field is 0 then a default limit can be applied.

Is there a need to change the limit dynamically? I mean, why is it a method (i.e. can be called multiple times) vs a field in those option setting structs?

ash2k avatar May 25 '20 06:05 ash2k

I considered such an option initially but my thinking was that it's possible you'd want to adjust the read limit depending on the user and whether they're verified/logged in but now that I think about it, it does stick out like a sore thumb in the API.

Maybe I should deprecate that method and then add ReadLimit to the options. Will sit on this.

Thanks for the suggestion!

nhooyr avatar May 25 '20 06:05 nhooyr

I also wanted to avoid duplication in the option structs but I don't think it's a big deal now, the compression options are duplicated anyway and it wouldn't make sense to turn those into a function.

nhooyr avatar May 25 '20 06:05 nhooyr

Maybe Options to contain the shared options for both Accept/Dial and then DialOptions and AcceptOptions embed Options.

nhooyr avatar May 25 '20 06:05 nhooyr

that it's possible you'd want to adjust the read limit depending on the user and whether they're verified/logged in

That sounds like a very specific requirement that can be met without any library support - just get the stream and wrap it in a custom reader, which is trivial to implement. Anybody actually asked for this?

Maybe Options to contain the shared options for both Accept/Dial and then DialOptions and AcceptOptions embed Options.

👍

ash2k avatar May 25 '20 09:05 ash2k

That sounds like a very specific requirement that can be met without any library support - just get the stream and wrap it in a custom reader, which is trivial to implement. Anybody actually asked for this?

I agree, for such a situation it's best to implement your own message limiting. No one asked. In retrospect it was a mistake compared to putting it Options but it seemed like a good idea at the time to keep things flexible. Also means you can still use wjson and wspb.

The reason read limit is in the library at all is so that a proper error message is written to the peer. Using io.LimitReader for example will return an EOF on the limit being hit which is confusing.

nhooyr avatar May 25 '20 09:05 nhooyr

The reason read limit is in the library at all is so that a proper error message is written to the peer. Using io.LimitReader for example will return an EOF on the limit being hit which is confusing.

That makes sense, but would it not be better to allow for composition and allowing users to use this piece of code when they actually need it? e.g. still provide it in the library, but just as a helper.

I understand that changing things in a backward-incompatible way may not be an option, it's just a thought experiment.

ash2k avatar May 26 '20 00:05 ash2k

Another idea: make HTTPClient field in DialOptions an interface to allow to inject custom client implementation/wrapper/mock.

ash2k avatar May 26 '20 04:05 ash2k

That makes sense, but would it not be better to allow for composition and allowing users to use this piece of code when they actually need it? e.g. still provide it in the library, but just as a helper.

Just wouldn't seem right to have websocket.LimitReader.

Another idea: make HTTPClient field in DialOptions an interface to allow to inject custom client implementation/wrapper/mock.

http.Transport is an interface so you can already do that!

nhooyr avatar May 26 '20 23:05 nhooyr

Has there been an update to the ReadLimit? I have an active use case for this.

felbit avatar Mar 09 '23 18:03 felbit