Skip to content
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

[caplin] event source server #8865

Merged
merged 3 commits into from
Nov 30, 2023
Merged

[caplin] event source server #8865

merged 3 commits into from
Nov 30, 2023

Conversation

elee1766
Copy link
Contributor

@elee1766 elee1766 commented Nov 30, 2023

eventsource is required for the validator api. this implements the eventsource sink/server handler

the implementation is based off of this document: https://html.spec.whatwg.org/multipage/server-sent-events.html

note that this is a building block for the full eventsource server. there still needs to be work done

prysm has their own custom solution based off of protobuf/grpc: https://hackmd.io/@prysmaticlabs/eventstream-api using that would be not good

existing eventsource implementations for golang are not good for our situation. options are:

  1. https://github.com/r3labs/sse - has most stars - this is the best contender, since it uses []byte and not string, but it allocates and copies extra times in the server (because of use of fprintf) and makes an incorrect assumption about Last-Event-ID needing to be a number (i can't find this in the specification).
  2. https://github.com/antage/eventsource -requires full buffers, copies many times, does not provide abstraction for headers. relatively unmaintained
  3. https://github.com/donovanhide/eventsource - missing functionality around sending ids, requires full buffers, etc
  4. https://github.com/bernerdschaefer/eventsource - 10 years old, unmaintained.

additionally, implemetations other than r3labs/sse are very incorrect because they do not split up the data field correctly when newlines are sent. (parsers by specification will fail to encode messages sent by most of these implementations that have newlines, as i understand it). the implementation by r3labs/sse is also incorrect because it does not respect \r

finally, all these implementations have very heavy implementation of the server, which we do not need since we will use fixed sequence ids. r3labs/sse for instance hijacks the entire handler and ties that to the server, losing a lot of flexiblity in how we implement our server

for the beacon api, we need to stream:

head, block, attestation, voluntary_exit, bls_to_execution_change, finalized_checkpoint, chain_reorg, contribution_and_proof, light_client_finality_update, light_client_optimistic_update, payload_attributes

some of these are rather big json payloads, and the ability to simultaneously stream them from io.Readers instead of making a full copy of the payload every time we wish to rebroadcast it will save a lot of heap size for both resource constrained environments and serving at scale.

the protocol itself is relatively simple, there are just a few gotchas

Copy link
Contributor

@Giulio2002 Giulio2002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Giulio2002 Giulio2002 merged commit 98c57e7 into devel Nov 30, 2023
6 checks passed
@Giulio2002 Giulio2002 deleted the sse branch November 30, 2023 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants