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

websocket/gateio: Support multi connection management and integrate with GateIO #1580

Merged
merged 91 commits into from
Oct 10, 2024

Conversation

shazbert
Copy link
Collaborator

@shazbert shazbert commented Jul 15, 2024

WebSocket

Multiple Connections for Asset Endpoints

  • Feature: Implemented the ability to spawn multiple WebSocket connections for different asset endpoints.
  • Design Decision:
    • Opted for a separate subscription holder per connection due to import cycle issues with a unified subscription manager.
    • This allows for future scalability when exchange constraints limit subscriptions per connection.

Connection-Coupled Functionality

  • Approach: Coupled specific functionality with the connection itself, along with its own subscription holder.
  • Benefit: Enables tracking multiple connections efficiently, facilitating scaling when exchanges restrict subscriptions per connection.

Reader Routine in Streaming Package

  • Implementation: Created a reader routine in the streaming package to avoid code duplication.
  • Future Work: This will initiate a purge across wrappers to standardize this routine.

Context Awareness

  • Progress: Began integrating context awareness throughout the WebSocket functionality.
  • Enhancement: Added a DialContext method to WebsocketConnection to leverage the supported context.

Gate.io Enhancements

WebSocket Support

  • Added Support:
    • Futures
    • Dated Futures
    • Options
  • Note: Margin, cross margin, and BTC-margined dated futures not included due to PR size and complexity.

Bug Fixes

  • Delivery Futures Handling:
    • Fixed issue where all available pairs were fetched instead of only enabled pairs.
  • Payload Generation:
    • Addressed issues in generateDeliveryFuturesPayload where different settlements were handled incorrectly.
    • Separated connections for different settlements to flatten functions for consistency.

🐛 fix:

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.

  • [I ran it 🏃 instead] 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 review me This pull request is ready for review pending response awaiting a response from the author blocked high priority labels Jul 15, 2024
@shazbert shazbert requested review from gbjk and a team July 15, 2024 05:56
@shazbert shazbert self-assigned this Jul 15, 2024
Copy link

codecov bot commented Jul 15, 2024

Codecov Report

Attention: Patch coverage is 70.05848% with 256 lines in your changes missing coverage. Please review.

Project coverage is 36.93%. Comparing base (910cdaa) to head (2a62643).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
exchanges/stream/websocket.go 84.13% 55 Missing and 24 partials ⚠️
exchanges/gateio/gateio_ws_futures.go 43.54% 33 Missing and 2 partials ⚠️
exchanges/gateio/gateio_ws_delivery_futures.go 11.76% 30 Missing ⚠️
exchanges/gateio/gateio_websocket.go 60.00% 14 Missing and 6 partials ⚠️
exchanges/gateio/gateio_wrapper.go 67.74% 14 Missing and 6 partials ⚠️
exchanges/gateio/gateio_ws_option.go 23.80% 15 Missing and 1 partial ⚠️
exchanges/stream/websocket_connection.go 50.00% 14 Missing and 1 partial ⚠️
exchanges/okx/okx_websocket.go 0.00% 6 Missing ⚠️
engine/rpcserver.go 0.00% 5 Missing ⚠️
exchanges/bitfinex/bitfinex_websocket.go 40.00% 1 Missing and 2 partials ⚠️
... and 14 more
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1580      +/-   ##
==========================================
+ Coverage   36.83%   36.93%   +0.09%     
==========================================
  Files         413      414       +1     
  Lines      180457   180478      +21     
==========================================
+ Hits        66472    66654     +182     
+ Misses     106185   105999     -186     
- Partials     7800     7825      +25     
Files with missing lines Coverage Δ
exchanges/binance/binance_wrapper.go 38.48% <100.00%> (+0.07%) ⬆️
exchanges/binanceus/binanceus_wrapper.go 40.89% <100.00%> (ø)
exchanges/bitfinex/bitfinex_wrapper.go 37.66% <100.00%> (ø)
exchanges/bithumb/bithumb_wrapper.go 29.68% <100.00%> (ø)
exchanges/bitmex/bitmex_wrapper.go 38.85% <100.00%> (ø)
exchanges/bitstamp/bitstamp_wrapper.go 53.23% <100.00%> (ø)
exchanges/btcmarkets/btcmarkets_wrapper.go 32.77% <100.00%> (ø)
exchanges/btse/btse_wrapper.go 43.72% <100.00%> (ø)
exchanges/bybit/bybit_wrapper.go 43.20% <100.00%> (ø)
exchanges/coinbasepro/coinbasepro_wrapper.go 34.41% <100.00%> (ø)
... and 37 more

... and 9 files with indirect coverage changes

@gloriousCode gloriousCode removed the pending response awaiting a response from the author label Jul 15, 2024
…larity on purpose. Change connections map to point to candidate to track subscriptions for future dynamic connections holder and drop struct ConnectionDetails.
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.

I'll only offer very light commentary right now.

There are things I'd do differently from this first look, but it doesn't work to comment in that way because that could blow out having an implementation where this looks to try and minimise the amount of changes while having backwards compatability. Also, this works

I'll try and focus on things to fix up and any holes I find

exchanges/stream/websocket_types.go Outdated Show resolved Hide resolved
exchanges/stream/websocket_connection.go Outdated Show resolved Hide resolved
exchanges/gateio/gateio_ws_delivery_futures.go Outdated Show resolved Hide resolved
exchanges/binance/binance_websocket.go Outdated Show resolved Hide resolved
exchanges/gateio/gateio_websocket.go Outdated Show resolved Hide resolved
exchanges/stream/websocket.go Outdated Show resolved Hide resolved
exchanges/stream/websocket.go Outdated Show resolved Hide resolved
exchanges/stream/websocket.go Outdated Show resolved Hide resolved
exchanges/stream/websocket.go Outdated Show resolved Hide resolved
exchanges/stream/websocket_connection.go Outdated Show resolved Hide resolved
@shazbert shazbert removed the blocked label Jul 16, 2024
@shazbert shazbert requested a review from gloriousCode July 16, 2024 04:51
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.

Thanks for addressing those issues! I think there's a potential double-shut/close happening


// monitorTraffic monitors to see if there has been traffic within the trafficTimeout time window. If there is no traffic
// the connection is shutdown and will be reconnected by the connectionMonitor routine.
func (w *Websocket) monitorTraffic() func() bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I tried simulating a disconnect on Binance and got the following:

panic: close of closed channel

goroutine 950 [running]:
github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*WebsocketConnection).Shutdown(0x14000b12000)
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket_connection.go:275 +0x70
github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*Websocket).shutdown(0x14000264800)
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket.go:553 +0x364
github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*Websocket).Shutdown(0x0?)
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket.go:512 +0xb8
github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*Websocket).observeTraffic.func1()
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket.go:1233 +0x24
created by github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*Websocket).observeTraffic in goroutine 427
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket.go:1232 +0x1b4
exit status 2

This does not occur on master branch. I have not been able to re-do this

Copy link
Collaborator

@gloriousCode gloriousCode Sep 30, 2024

Choose a reason for hiding this comment

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

I had a different recreation on closing the application. In this instance I had both GateIO and Binance enabled

^C[INFO]  | SYNC | 30/09/2024 12:09:56 | GateIO websocket BTC-USDT SPOT ORDERBOOK: Bids len: 1 Amount: 2.933270 BTC. Total value: 189864.407233 Asks len: 1 Amount: 0.045910 BTC. Total value: 2971.66248
[INFO]  | LOG | 30/09/2024 12:09:56 | Captured interrupt, shutdown requested.
[DEBUG] | LOG | 30/09/2024 12:09:56 | Engine shutting down..
[DEBUG] | ORDER | 30/09/2024 12:09:56 | Order manager shutting down...
[DEBUG] | PORTFOLIO | 30/09/2024 12:09:56 | Portfolio manager shutting down...
[DEBUG] | CONNECTION | 30/09/2024 12:09:56 | Connection manager shutting down...
[DEBUG] | CONNECTION | 30/09/2024 12:09:56 | Connection manager stopped.
[DEBUG] | PORTFOLIO | 30/09/2024 12:09:56 | Portfolio manager shutdown.
[DEBUG] | ORDER | 30/09/2024 12:09:56 | Order manager shutdown.
[DEBUG] | DISPATCH | 30/09/2024 12:09:56 | Dispatch manager shutting down...
[DEBUG] | DISPATCH | 30/09/2024 12:09:56 | Dispatch manager shutdown
[DEBUG] | EXCHANGE | 30/09/2024 12:09:56 | Currency state manager shutting down...
[DEBUG] | EXCHANGE | 30/09/2024 12:09:56 | Currency state manager shutdown.
panic: close of closed channel

goroutine 677 [running]:
github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*WebsocketConnection).Shutdown(0x14000c06000)
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket_connection.go:275 +0x70
github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*Websocket).shutdown(0x140001fc800)
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket.go:553 +0x364
github.com/thrasher-corp/gocryptotrader/exchanges/stream.(*Websocket).Shutdown(0x14000406be0?)
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/stream/websocket.go:512 +0xb8
github.com/thrasher-corp/gocryptotrader/exchanges.(*Base).Shutdown(0x1400035a008)
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/exchanges/exchange.go:1644 +0x2c
github.com/thrasher-corp/gocryptotrader/engine.(*ExchangeManager).Shutdown.func1(0x14000cfe040, 0x14000cfe030, {0x1017f2e20, 0x1400035a008})
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/engine/exchange_manager.go:159 +0x3c
created by github.com/thrasher-corp/gocryptotrader/engine.(*ExchangeManager).Shutdown in goroutine 1
	/Users/WIF/go/src/github.com/thrasher-corp/gocryptotrader/engine/exchange_manager.go:158 +0x1f8
exit status 2

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!

@gloriousCode gloriousCode added the gcrc GloriousCode Review Complete label Oct 9, 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.

Been testing this for a few hours and appears to be working well! Just some unused vars with the changes of this PR

exchanges/stream/websocket.go Outdated Show resolved Hide resolved
exchanges/stream/websocket.go Outdated Show resolved Hide resolved
@thrasher- thrasher- changed the title websocket/gateio: Multiple connection management and gateio integration. websocket/gateio: Support multiple connection management and integrate with GateIO Oct 10, 2024
@thrasher- thrasher- changed the title websocket/gateio: Support multiple connection management and integrate with GateIO websocket/gateio: Support multi connection management and integrate with GateIO Oct 10, 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!

@thrasher- thrasher- merged commit ac731ce into thrasher-corp:master Oct 10, 2024
10 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug gcrc GloriousCode Review Complete high priority review me This pull request is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants