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

common/gateio/stream: add thread-safe counter and overide default GenerateMessageID with connection specific implementation #1615

Merged
merged 8 commits into from
Aug 24, 2024

Conversation

shazbert
Copy link
Collaborator

@shazbert shazbert commented Aug 16, 2024

PR Description

  • GenerateMessageID does not ensure atomicity collision resistance, this update adds a basic counter for routines that require low cost message matching signatures.
  • Gateio is converted by using bespoke ID generation in connection setup to override default GenerateMessageID
  • NOTE: The overhead of default GenerateMessageID is quite high

Fixes # (issue)

  • Signature collisions in outbound requests for Gateio.

Type of change

Please delete options that are not relevant and add an x in [] as item is complete.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration and
also consider improving test coverage whilst working on a certain feature or package.

  • go test ./... -race
  • golangci-lint run
  • Test X

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and regenerated documentation via the documentation tool
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally and on Github Actions with my changes
  • Any dependent changes have been merged and published in downstream modules

@shazbert shazbert added bug review me This pull request is ready for review labels Aug 16, 2024
@shazbert shazbert requested a review from a team August 16, 2024 02:11
@shazbert shazbert self-assigned this Aug 16, 2024
Copy link

codecov bot commented Aug 16, 2024

Codecov Report

Attention: Patch coverage is 94.87179% with 2 lines in your changes missing coverage. Please review.

Project coverage is 36.38%. Comparing base (17c2ef2) to head (8a31eec).
Report is 2 commits behind head on master.

Files Patch % Lines
exchanges/gateio/gateio_websocket.go 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1615      +/-   ##
==========================================
+ Coverage   36.37%   36.38%   +0.01%     
==========================================
  Files         422      422              
  Lines      183113   183135      +22     
==========================================
+ Hits        66602    66636      +34     
+ Misses     108466   108452      -14     
- Partials     8045     8047       +2     
Files Coverage Δ
common/common.go 94.32% <100.00%> (+0.11%) ⬆️
exchanges/gateio/gateio.go 14.18% <ø> (ø)
exchanges/gateio/gateio_wrapper.go 39.71% <100.00%> (-0.56%) ⬇️
exchanges/stream/websocket.go 84.69% <100.00%> (+0.13%) ⬆️
exchanges/stream/websocket_connection.go 62.11% <100.00%> (+0.85%) ⬆️
exchanges/gateio/gateio_websocket.go 49.64% <0.00%> (+1.42%) ⬆️

... and 19 files with indirect coverage changes

Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

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

Only one request, this is fine 🐶

exchanges/gateio/gateio.go Outdated Show resolved Hide resolved
// IncrementAndGet returns the next count after incrementing.
func (c *Counter) IncrementAndGet() int64 {
newID := atomic.AddInt64(&c.n, 1)
// Handle overflow by resetting the counter to 1 if it becomes negative
Copy link
Collaborator

Choose a reason for hiding this comment

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

Had thoughts of suggesting uint64 instead, but there are many unknowns and I doubt anyone is going to subscribe to WIF more 9,223,372,036,854,775,807 times on GateIO in a single session

Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

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

tACK!

🌞

[DEBUG] | WEBSOCKET | 16/08/2024 13:37:42 | GateIO websocket connection: sending message [{"time":1723779462,"id":1,"channel":"spot.tickers","event":"subscribe","payload":["BTC_USDT"]}]
[DEBUG] | WEBSOCKET | 16/08/2024 13:37:42 | GateIO websocket connection: message received: {"time":1723779462,"time_ms":1723779462323,"id":1,"conn_id":"1b73153490cc89f8","trace_id":"daa79b26d14a54b172e11012230c1c78","channel":"spot.tickers","event":"subscribe","payload":["BTC_USDT"],"result":{"status":"success"},"requestId":"daa79b26d14a54b172e11012230c1c78"}
[DEBUG] | WEBSOCKET | 16/08/2024 13:37:42 | GateIO websocket connection: sending message [{"time":1723779462,"id":2,"channel":"spot.candlesticks","event":"subscribe","payload":["5m","BTC_USDT"]}]
[DEBUG] | WEBSOCKET | 16/08/2024 13:37:42 | GateIO websocket connection: message received: {"time":1723779462,"time_ms":1723779462670,"id":2,"conn_id":"1b73153490cc89f8","trace_id":"daa79b26d14a54b172e11012230c1c78","channel":"spot.candlesticks","event":"subscribe","payload":["5m","BTC_USDT"],"result":{"status":"success"},"requestId":"daa79b26d14a54b172e11012230c1c78"}
[DEBUG] | WEBSOCKET | 16/08/2024 13:37:42 | GateIO websocket connection: sending message [{"time":1723779462,"id":3,"channel":"spot.book_ticker","event":"subscribe","payload":["BTC_USDT"]}]
[DEBUG] | WEBSOCKET | 16/08/2024 13:37:43 | GateIO websocket connection: message received: {"time":1723779463,"time_ms":1723779463017,"id":3,"conn_id":"1b73153490cc89f8","trace_id":"daa79b26d14a54b172e11012230c1c78","channel":"spot.book_ticker","event":"subscribe","payload":["BTC_USDT"],"result":{"status":"success"},"requestId":"daa79b26d14a54b172e11012230c1c78"}

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

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

Rather than deviating from the websocket GenerateMessageID helper and rolling out exchange specific counters, I'd opt for the ability to override the default behavior so it still remains unified but configurable

@shazbert shazbert changed the title common/gateio: add thread-safe counter common/gateio/stream: add thread-safe counter and overide default GenerateMessageID with connection specific implementation Aug 19, 2024
Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

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

Thanks for making those changes 🌞 tACK:
image

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

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

Just realised after tACK'ing that this requires test coverage and then should be good to go

@shazbert shazbert requested a review from thrasher- August 23, 2024 09:47
Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

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

Changes ACK, thanks!

@thrasher- thrasher- merged commit 1cabba7 into thrasher-corp:master Aug 24, 2024
5 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug review me This pull request is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants