-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Refactor socketioxide parsing #376
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Totodore
force-pushed
the
crate-refactor-parsers
branch
from
October 5, 2024 09:32
05c56c5
to
b097705
Compare
Totodore
force-pushed
the
crate-refactor-parsers
branch
from
October 5, 2024 09:33
b097705
to
3fc6b47
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Currently, the serialization/deserialization system of socketioxide works as follows:
Deserialization
[event, ...data]
) is immediately parsed into a dynamic value (serde_json::Value
). We do this to:serde_json::Value::Array
] and skip the first element (the event).Serialization
serde_json::Value
.Drawbacks
This approach is the simplest way to handle the data payload, but it has several drawbacks:
Vec
emitted as a single value and a variadic number of arguments (Emitting with an array/vector data incorrectly serialized as separate arguments. #225).Solution
First, the codebase is split into multiple crates:
socketioxide_core
crate contains all the core types used by the other crates (mainly the parser implementations):Packet
,Parse
,Sid
,Str
.parser_common
crate contains all the parsing code for the default parser.parser_msgpack
crate contains all the parsing code for the new msgpack parser.socketioxide
crate contains the rest of the codebase.The core crate defines a
Parse
trait that all parsers must implement. TheParse
design focuses on deferred parsing and keeping things as simple as possible. Here is the new serde flow with this solution:Deserialization
read_event
method to simply retrieve a string reference to the event in the payload.serde
implementation, we check if the user-provided value is a tuple based on theserde
model. If it is, we deserialize it directly. Otherwise, we deserialize the first element of the array (solves Emitting with an array/vector data incorrectly serialized as separate arguments. #225).serde::{Serialize, Deserialize}
, which wrapserde_json
andrmp_serde
. These wrappers handle the following:Serialization
Drawbacks of this solution
serde
model usesVec<u8>
rather thanBytes
or other structs that are cheap to clone. This is the primary issue with the current solution, though it only affectsserde_json
;msgpack
handles binary data natively.socket.emit(...new Array(Math.random(1000)))
).To Do
serde_json::Value
when deserializing payloads with binariesread_event
, ...)Closes :
Bin
and the structure ofData
can be ambiguous for binary packets #276 (Bin payload is removed)