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

FEATURE: [max] new method for depth buffer to set snapshot #1835

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions pkg/depth/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ func (b *Buffer) Reset() {
b.mu.Unlock()
}

func (b *Buffer) SetSnapshot(snapshot types.SliceOrderBook, firstUpdateID int64, finalArgs ...int64) error {
Copy link
Owner

Choose a reason for hiding this comment

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

make it private method?

finalUpdateID := firstUpdateID
if len(finalArgs) > 0 {
finalUpdateID = finalArgs[0]
}

b.mu.Lock()

if b.finalUpdateID >= finalUpdateID {
b.mu.Unlock()
return nil
Copy link
Owner

Choose a reason for hiding this comment

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

unlock before return?

}

// set the final update ID so that we will know if there is an update missing
b.finalUpdateID = finalUpdateID

// set the snapshot
b.snapshot = &snapshot

b.mu.Unlock()

// should unlock first then call ready
b.EmitReady(snapshot, nil)
return nil
}

// AddUpdate adds the update to the buffer or push the update to the subscriber
func (b *Buffer) AddUpdate(o types.SliceOrderBook, firstUpdateID int64, finalArgs ...int64) error {
finalUpdateID := firstUpdateID
Expand Down
11 changes: 9 additions & 2 deletions pkg/exchange/max/maxapi/public_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ import (

var ErrIncorrectBookEntryElementLength = errors.New("incorrect book entry element length")

const Buy = 1
const Sell = -1
const (
Buy = 1
Sell = -1
)

const (
BookEventSnapshot string = "snapshot"
BookEventUpdate string = "update"
)

var parserPool fastjson.ParserPool

Expand Down
27 changes: 20 additions & 7 deletions pkg/exchange/max/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,26 @@ func (s *Stream) handleBookEvent(ex *Exchange) func(e max.BookEvent) {
return
}

if err := f.AddUpdate(types.SliceOrderBook{
Symbol: symbol,
Time: e.Time(),
Bids: e.Bids,
Asks: e.Asks,
}, e.FirstUpdateID, e.LastUpdateID); err != nil {
log.WithError(err).Errorf("found missing %s update event", e.Market)
if e.Event == max.BookEventSnapshot {
if err := f.SetSnapshot(types.SliceOrderBook{
Symbol: symbol,
Time: e.Time(),
Bids: e.Bids,
Asks: e.Asks,
LastUpdateId: e.LastUpdateID,
}, e.FirstUpdateID, e.LastUpdateID); err != nil {
log.WithError(err).Errorf("failed to set %s snapshot", e.Market)
}
} else {
if err := f.AddUpdate(types.SliceOrderBook{
Symbol: symbol,
Time: e.Time(),
Bids: e.Bids,
Asks: e.Asks,
LastUpdateId: e.LastUpdateID,
}, e.FirstUpdateID, e.LastUpdateID); err != nil {
log.WithError(err).Errorf("found missing %s update event", e.Market)
}
}
}
}
Expand Down
Loading