-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: encoding/json support context in marshal/unmarshal. like MarshalWithContext(ctx context.Context,v interface{}) #50422
Comments
Usually, a context or timeout/deadline is required for asynchronous work where it's practically impossible to know how long an operation will take. I also imagine it would be useful for libraries that interpret or execute code, such as interpreting a script that might loop forever. My first impression is that encoding/json falls under neither of those categories. The amount of time a Marshal or Unmarshal call takes is fairly predictable, as it only scales with the size of the input, and shouldn't block or loop for a long time unless you're using some special MarshalJSON/UnmarshalJSON methods. For example, for Unmarshal, why is it not enough to have a limit on the number of bytes being decoded? I realise that for Marshal it's not quite as easy as |
This may be related to #20280. I don't think the |
partially agree on this. a context or timeout/deadline is required to control the expectation of some work(sync or async) and the work can continue or drop by how the work respects the context. a there are customized marshal/unmarshal methods, too, as you described as
right. i know how large the data is, and also i know how much time it takes to do marshal. the problem is the request has 30 seconds as timeout and the response is already done and leaving marshal work as dangling until it finishes the work. it slows down the web server(our k8s control plane a lot). |
Those methods do not receive a context though, so if your problem is that they may consume an unexpected amount of time, this proposal won't help.
Okay, that makes more sense to me. I can't say I've ever marshalled or unmarshalled such huge chunks of data, but I can understand wanting to cancel when done. I share @dsnet's thoughts, though - besides MarshalJSON and UnmarshalJSON, which don't take contexts, the only other blocking operation in encoding/json is reading and writing, which is currently not cancellable. So it seems wrong to add Context to the json APIs. Even if they might improve your situation in some cases, in some others cancelling the context would accomplish nothing at all, and that would be a very confusing and unhelpful API. |
For your particular use case, another approach could be to use the |
from this perspective, it makes sense not to add the context parameter for the tidy and cleanliness of package encoding/xxx . also thank you for the approatch of using io.Writer, buffer solution as we need a way to parse the context into the writer and control it. |
Proposals
Proposing to support a context.Context as an argument to encoding/json package to do interaction while marshalling/unmarshalling payloads. e.g.
Use Cases
kubernetes response sometimes can be very large(e.g. >300MB) when listing cluster wide objects. the response can be very heavy for server to encode/decode. there are timeout configurations to exit the request with timeout failure because the object encoding doesn't finish within the time limitation. the response already returned but the marshalling still goes on and consumes CPU/Memory.
with a context parsed to marshal/unmarshal, it is possible to respect the context and cancel the encoding/decoding as soon as possible rather than exhausting unnecessary resources.
The text was updated successfully, but these errors were encountered: