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

Consolidate orderbook updates into a single stream update (backport #1634) #1635

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 53 additions & 31 deletions protocol/streaming/grpc/grpc_streaming_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,41 @@ func (sm *GrpcStreamingManagerImpl) SendOrderbookUpdates(
}

// Unmarshal each per-clob pair message to v1 updates.
updatesByClobPairId := make(map[uint32][]clobtypes.StreamUpdate)
updatesByClobPairId := make(map[uint32][]ocutypes.OffChainUpdateV1)
for clobPairId, update := range updates {
v1updates, err := GetOffchainUpdatesV1(update)
if err != nil {
panic(err)
}
updatesByClobPairId[clobPairId] = []clobtypes.StreamUpdate{
{
UpdateMessage: &clobtypes.StreamUpdate_OrderbookUpdate{
OrderbookUpdate: &clobtypes.StreamOrderbookUpdate{
Updates: v1updates,
Snapshot: snapshot,
updatesByClobPairId[clobPairId] = v1updates
}

updatesBySubscriptionId := make(map[uint32][]clobtypes.StreamUpdate)
for id, subscription := range sm.orderbookSubscriptions {
// Consolidate orderbook updates into a single `StreamUpdate`.
v1updates := make([]ocutypes.OffChainUpdateV1, 0)
for _, clobPairId := range subscription.clobPairIds {
if update, ok := updatesByClobPairId[clobPairId]; ok {
v1updates = append(v1updates, update...)
}
}

if len(v1updates) > 0 {
updatesBySubscriptionId[id] = []clobtypes.StreamUpdate{
{
UpdateMessage: &clobtypes.StreamUpdate_OrderbookUpdate{
OrderbookUpdate: &clobtypes.StreamOrderbookUpdate{
Updates: v1updates,
Snapshot: snapshot,
},
},
},
},
}
}
}

sm.sendStreamUpdate(
updatesByClobPairId,
updatesBySubscriptionId,
blockHeight,
execMode,
)
Expand Down Expand Up @@ -170,16 +185,28 @@ func (sm *GrpcStreamingManagerImpl) SendOrderbookFillUpdates(
updatesByClobPairId[clobPairId] = append(updatesByClobPairId[clobPairId], streamUpdate)
}

updatesBySubscriptionId := make(map[uint32][]clobtypes.StreamUpdate)
for id, subscription := range sm.orderbookSubscriptions {
streamUpdatesForSubscription := make([]clobtypes.StreamUpdate, 0)
for _, clobPairId := range subscription.clobPairIds {
if update, ok := updatesByClobPairId[clobPairId]; ok {
streamUpdatesForSubscription = append(streamUpdatesForSubscription, update...)
}
}

updatesBySubscriptionId[id] = streamUpdatesForSubscription
}

sm.sendStreamUpdate(
updatesByClobPairId,
updatesBySubscriptionId,
blockHeight,
execMode,
)
}

// sendStreamUpdate takes in a map of clob pair id to stream updates and emits them to subscribers.
func (sm *GrpcStreamingManagerImpl) sendStreamUpdate(
updatesByClobPairId map[uint32][]clobtypes.StreamUpdate,
updatesBySubscriptionId map[uint32][]clobtypes.StreamUpdate,
blockHeight uint32,
execMode sdk.ExecMode,
) {
Expand All @@ -194,26 +221,21 @@ func (sm *GrpcStreamingManagerImpl) sendStreamUpdate(
// Send updates to subscribers.
idsToRemove := make([]uint32, 0)
for id, subscription := range sm.orderbookSubscriptions {
streamUpdatesForSubscription := make([]clobtypes.StreamUpdate, 0)
for _, clobPairId := range subscription.clobPairIds {
if update, ok := updatesByClobPairId[clobPairId]; ok {
streamUpdatesForSubscription = append(streamUpdatesForSubscription, update...)
}
}

if len(streamUpdatesForSubscription) > 0 {
metrics.IncrCounter(
metrics.GrpcSendResponseToSubscriberCount,
1,
)
if err := subscription.srv.Send(
&clobtypes.StreamOrderbookUpdatesResponse{
Updates: streamUpdatesForSubscription,
BlockHeight: blockHeight,
ExecMode: uint32(execMode),
},
); err != nil {
idsToRemove = append(idsToRemove, id)
if streamUpdatesForSubscription, ok := updatesBySubscriptionId[id]; ok {
if len(streamUpdatesForSubscription) > 0 {
metrics.IncrCounter(
metrics.GrpcSendResponseToSubscriberCount,
1,
)
if err := subscription.srv.Send(
&clobtypes.StreamOrderbookUpdatesResponse{
Updates: streamUpdatesForSubscription,
BlockHeight: blockHeight,
ExecMode: uint32(execMode),
},
); err != nil {
idsToRemove = append(idsToRemove, id)
}
}
}
}
Expand Down
Loading