-
Notifications
You must be signed in to change notification settings - Fork 113
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
When reusing the decoder, gojay does not flush the buffer #108
Comments
Gojay is used in production on a highly instrumented system handling thousands of requests per second. In the context it's being used it does not leak memory, if it would, we would have noticed. It could be linked with something you're doing specifically. Let me try to reproduce what you are noticing. |
So the reason is you are reusing the same decoder. It's actually a duplicate of this issue: #79 At the time I suggested to add a reset method. But the OP implemented his own solution. As I want gojay to stick more to the behaviour of the standard API, I will implement the reset after each decode so that the buffer doesn't grow forever if you reuse the Decoder, in the meantime you can get a new decoder between each loop and test. |
@francoispqt I cannot get new decoder because I have a byte stream of JSON messages and on dropping the old decoder I will crop a part of the next message (I've already tried that before creating the issue). |
Yes, of course, because it reads the bytes available in the reader into the decoder's internal buffer. So basically, what we'd need to do, is everytime an item is read, drop the bytes already read by resetting the cursor, this is actually what the stream api already does. But you're not using the stream api. I will implement that in the regular API and evaluate the perf impact. |
In my quick-research I did not understand how to use the stream API for my case. If that is the API that I was supposed to use for my case then don't waste time on me: I will just try to use it. So am I understand correctly that I just should use the stream API? :) |
Yes, the best for you would be to use the stream api, it will basically push messages to a channel whenever it's there, so you just have to read from that channel. But if you want, I have just implemented the reset to reuse the If it's ok for you, I will merge it to master and tag a new release tomorrow. |
I will test it tomorrow, thank you very much :) |
Well, I've switched the branch and it did not help.
I will try to use the steam API. Anyway, thanks :) |
Are you running it in a unit test (mocking your udp connection) or you're really testing your stream? |
The second one:
|
Ok. Let me setup an environment quickly. It shouldn't grow like that. I know it doesn't with the stream api, as we are using it, and you can try the example in |
Ok, so I have pushed updates to the branch I've added an example which replicates what you are doing, you can find it here: https://github.com/francoispqt/gojay/blob/9df89bce7ea024f84eb4d44efb460bbf755e7b58/examples/reuse-decoder/main.go You can run the example doing: |
Hello. I've tried to replace the standard JSON decoder to gojay's. The result:
Here's the code:
https://github.com/trafficstars/put2ch/blob/79f2e1d100bbd181df3cb54e8199995d5f868ded/input_raw_json.go
The text was updated successfully, but these errors were encountered: