-
Notifications
You must be signed in to change notification settings - Fork 55
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
Raise publish serialization exception early #140
Conversation
When publishing, if there are serialization errors we need to throw the exception at the point of publish call being made. This changes buffer handling quite a bit.
@jasper-d @caleblloyd this resolves the exception #138 issue but changes serialization buffer location which must be considered in serialization / API discussions #137 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasper-d @caleblloyd this resolves the exception #138 issue but changes serialization buffer location which must be considered in serialization / API discussions #137
I think serializing early is beneficial, not only because it fixes the exception being observed here. It also enables serialization work to happen concurrently (for multiple publish operations) instead of serializing all serialization work inside WriteLoopAsync
. This should positively impact throughput.
return result; | ||
} | ||
|
||
public override void Write(ProtocolWriter writer) | ||
{ | ||
writer.WritePublish(_subject!, _replyTo, _headers, _value, _serializer!); | ||
writer.WritePublish(_subject!, _replyTo, _headers, new ReadOnlySequence<byte>(_buffer.WrittenMemory)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change, _serializer
and _value
aren't needed anymore and PublishCommand
doesn't need to be generic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, _buffer
or rather it's underlying array should be freed here. Otherwise, we would keep a potentially huge buffer allocated after sending a large message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also enables serialization work to happen concurrently (for multiple publish operations) instead of serializing all serialization work inside WriteLoopAsync
Good point.
Also, _buffer or rather it's underlying array should be freed here.
The idea of keeping the _buffer around to avoid GC since it'd be pooled with the command object.
On the other hand we could leave out the buffer management to end developer and just accept ReadOnlySequence or Memory or something or even an IMemoryOwner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, @jasper-d I think I misunderstand your freeing buffer comment (wasn't looking at where in code you commented). You have a point. I thought about this as well. We could resize the underlying array to be smaller or enforce a size limit as it would be by the server anyway.
Would need to see benchmarks on what this does to allocations. But this puts us in an odd spot. As it stands without this PR we have:
After this PR, 2 becomes exactly the same as 3 and we introduce an allocation per serialized model. Would rather use some sort of pipeline with a serialization buffer size, and maybe an option for |
Introduced a buffer pool to soften the blow on allocations when serialization is done early but didn't seem to help.
|
If I understand this correctly, the issue is that So for So maybe it would be better to fix the send logic in a way that On top of that, |
I think that is a good idea and I also would like to wrap this PR before it grows too big and place these ideas in a new issue or under the existing serialization improvements issue. My proposal to have progress is to solve the issue at hand, which is handling serialization exceptions when publishing.
|
@jasper-d @caleblloyd closing this PR as an experiment. I've referenced this PR from relate issues but please feel free to create other issues etc. Thank you for the input and ideas. Changes are copied to #144 and #145 please review them instead. |
When publishing, if there are serialization errors we need to throw the exception at the point of publish call being made.
This changes buffer handling quite a bit.