Websocket: Restructure files and types (#1859)

* Websocket: Rename stream package

* Websocket: Rename Websocket to Manager

* Websocket: Replace explicit errs with common.NilGuard

* Websocket: Move websocket_types.go to types.go

* Websocket: Minor field comment and alignment in types

* Webosocket: Rename WebsocketConnection to Connection

* Alphapoint: Make gorilla ws import explicit

Just to avoid confusion with our own packages.

* Websocket: Move stream_match to match

* Websocket: Move websocket_connection to connection

* Websocket: Move websocket.go to manager.go

* Websocket: Break out all subscription methods into subscriptions.go

* Websocket: Move connection type into its file

* Websocket: Remove PositionUpdated

Type is not used anywhere

* Kraken: Use local constant for pong

Was the only use of websocket.Pong and doesn't really feel right to
represent kraken's api resp in one of our packages

* Websocket: Move connection sub-types to connection package

* Websocket: Move manager types into manager

* Websocket: Move ConnectionWrapper into manager

* Websocket: Move websocket_test to manager_test

* Websocket: Privatise connectionWrapper

* Websocket: Remaining types into types.go

These really belong somewhere else mostly, but this will do for now

* Websocket: Tidy up connection method vars

* Gofumpt: Moving package imports around

* Websocket: Rename errDuplicateConnectionSetup

* Websocket: Fix duplicate import of gws

* Websocket: Fix gofumpt -extra

* Websocket: Standardise import of gws across other pkgs

* Kraken: Remove unused sub conf consts

These were replaced by the generic Levels and Depth fields on all subs

* Websocket: Privitise ConnectioWrapper fields

* Websocket: inline single use var WebsocketNotAuthenticatedUsingRest

* Websocket: Move documentation to template

* Bithumb: Assertify TestWsHandleData
This commit is contained in:
Gareth Kirwan
2025-04-10 08:25:02 +02:00
committed by GitHub
parent 676b2e0367
commit b4e45e9a1b
119 changed files with 3169 additions and 3056 deletions

View File

@@ -23,10 +23,10 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/internal/exchange/websocket"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
testsubs "github.com/thrasher-corp/gocryptotrader/internal/testing/subscriptions"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
@@ -1105,7 +1105,7 @@ func TestGetDepositAddress(t *testing.T) {
// TestWSAuth dials websocket, sends login request.
func TestWSAuth(t *testing.T) {
if !b.Websocket.IsEnabled() {
t.Skip(stream.ErrWebsocketNotEnabled.Error())
t.Skip(websocket.ErrWebsocketNotEnabled.Error())
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
if !b.API.AuthenticatedWebsocketSupport {
@@ -1333,8 +1333,8 @@ func TestWSSubscribedResponse(t *testing.T) {
assert.NoError(t, err, "Setting a matcher should not error")
err = b.wsHandleData([]byte(`{"event":"subscribed","channel":"ticker","chanId":224555,"subId":"waiter1","symbol":"tBTCUSD","pair":"BTCUSD"}`))
if assert.Error(t, err, "Should error if sub is not registered yet") {
assert.ErrorIs(t, err, stream.ErrSubscriptionFailure, "Should error SubFailure if sub isn't registered yet")
assert.ErrorIs(t, err, stream.ErrSubscriptionFailure, "Should error SubNotFound if sub isn't registered yet")
assert.ErrorIs(t, err, websocket.ErrSubscriptionFailure, "Should error SubFailure if sub isn't registered yet")
assert.ErrorIs(t, err, subscription.ErrNotFound, "Should error SubNotFound if sub isn't registered yet")
assert.ErrorContains(t, err, "waiter1", "Should error containing subID if")
}

View File

@@ -16,7 +16,7 @@ import (
"github.com/Masterminds/sprig/v3"
"github.com/buger/jsonparser"
"github.com/gorilla/websocket"
gws "github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
@@ -27,10 +27,10 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/internal/exchange/websocket"
"github.com/thrasher-corp/gocryptotrader/log"
)
@@ -100,7 +100,7 @@ var defaultSubscriptions = subscription.List{
{Enabled: true, Channel: subscription.OrderbookChannel, Asset: asset.All, Levels: 100, Params: map[string]any{"prec": "R0"}},
}
var comms = make(chan stream.Response)
var comms = make(chan websocket.Response)
type checksum struct {
Token uint32
@@ -123,9 +123,9 @@ var subscriptionNames = map[string]string{
// WsConnect starts a new websocket connection
func (b *Bitfinex) WsConnect() error {
if !b.Websocket.IsEnabled() || !b.IsEnabled() {
return stream.ErrWebsocketNotEnabled
return websocket.ErrWebsocketNotEnabled
}
var dialer websocket.Dialer
var dialer gws.Dialer
err := b.Websocket.Conn.Dial(&dialer, http.Header{})
if err != nil {
return fmt.Errorf("%v unable to connect to Websocket. Error: %s",
@@ -162,7 +162,7 @@ func (b *Bitfinex) WsConnect() error {
}
// wsReadData receives and passes on websocket messages for processing
func (b *Bitfinex) wsReadData(ws stream.Connection) {
func (b *Bitfinex) wsReadData(ws websocket.Connection) {
defer b.Websocket.Wg.Done()
for {
resp := ws.ReadMessage()
@@ -193,7 +193,7 @@ func (b *Bitfinex) WsDataHandler() {
}
return
case resp := <-comms:
if resp.Type != websocket.TextMessage {
if resp.Type != gws.TextMessage {
continue
}
err := b.wsHandleData(resp.Raw)
@@ -494,8 +494,8 @@ func (b *Bitfinex) wsHandleData(respRaw []byte) error {
b.Websocket.DataHandler <- wsFundingTrade
}
default:
b.Websocket.DataHandler <- stream.UnhandledMessageWarning{
Message: b.Name + stream.UnhandledMessage + string(respRaw),
b.Websocket.DataHandler <- websocket.UnhandledMessageWarning{
Message: b.Name + websocket.UnhandledMessage + string(respRaw),
}
return nil
}
@@ -581,12 +581,12 @@ func (b *Bitfinex) handleWSSubscribed(respRaw []byte) error {
c := b.Websocket.GetSubscription(subID)
if c == nil {
return fmt.Errorf("%w: %w subID: %s", stream.ErrSubscriptionFailure, subscription.ErrNotFound, subID)
return fmt.Errorf("%w: %w subID: %s", websocket.ErrSubscriptionFailure, subscription.ErrNotFound, subID)
}
chanID, err := jsonparser.GetInt(respRaw, "chanId")
if err != nil {
return fmt.Errorf("%w: %w 'chanId': %w; Channel: %s Pair: %s", stream.ErrSubscriptionFailure, common.ErrParsingWSField, err, c.Channel, c.Pairs)
return fmt.Errorf("%w: %w 'chanId': %w; Channel: %s Pair: %s", websocket.ErrSubscriptionFailure, common.ErrParsingWSField, err, c.Channel, c.Pairs)
}
// Note: chanID's int type avoids conflicts with the string type subID key because of the type difference
@@ -596,7 +596,7 @@ func (b *Bitfinex) handleWSSubscribed(respRaw []byte) error {
// subscribeToChan removes the old subID keyed Subscription
err = b.Websocket.AddSuccessfulSubscriptions(b.Websocket.Conn, c)
if err != nil {
return fmt.Errorf("%w: %w subID: %s", stream.ErrSubscriptionFailure, err, subID)
return fmt.Errorf("%w: %w subID: %s", websocket.ErrSubscriptionFailure, err, subID)
}
if b.Verbose {
@@ -799,7 +799,7 @@ func (b *Bitfinex) handleWSCandleUpdate(c *subscription.Subscription, d []any) e
return errors.New("invalid candleBundle length")
}
var err error
var klineData stream.KlineData
var klineData websocket.KlineData
if klineData.Timestamp, err = convert.TimeFromUnixTimestampFloat(element[0]); err != nil {
return fmt.Errorf("unable to convert candle timestamp: %w", err)
}
@@ -828,7 +828,7 @@ func (b *Bitfinex) handleWSCandleUpdate(c *subscription.Subscription, d []any) e
return errors.New("invalid candleBundle length")
}
var err error
var klineData stream.KlineData
var klineData websocket.KlineData
if klineData.Timestamp, err = convert.TimeFromUnixTimestampFloat(candleData); err != nil {
return fmt.Errorf("unable to convert candle timestamp: %w", err)
}

View File

@@ -25,10 +25,10 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream/buffer"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/internal/exchange/websocket"
"github.com/thrasher-corp/gocryptotrader/internal/exchange/websocket/buffer"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
@@ -167,7 +167,7 @@ func (b *Bitfinex) SetDefaults() {
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.Websocket = stream.NewWebsocket()
b.Websocket = websocket.NewManager()
b.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
b.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
b.WebsocketOrderbookBufferLimit = exchange.DefaultWebsocketOrderbookBufferLimit
@@ -193,7 +193,7 @@ func (b *Bitfinex) Setup(exch *config.Exchange) error {
return err
}
err = b.Websocket.Setup(&stream.WebsocketSetup{
err = b.Websocket.Setup(&websocket.ManagerSetup{
ExchangeConfig: exch,
DefaultURL: publicBitfinexWebsocketEndpoint,
RunningURL: wsEndpoint,
@@ -210,7 +210,7 @@ func (b *Bitfinex) Setup(exch *config.Exchange) error {
return err
}
err = b.Websocket.SetupNewConnection(&stream.ConnectionSetup{
err = b.Websocket.SetupNewConnection(&websocket.ConnectionSetup{
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
URL: publicBitfinexWebsocketEndpoint,
@@ -219,7 +219,7 @@ func (b *Bitfinex) Setup(exch *config.Exchange) error {
return err
}
return b.Websocket.SetupNewConnection(&stream.ConnectionSetup{
return b.Websocket.SetupNewConnection(&websocket.ConnectionSetup{
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
URL: authenticatedBitfinexWebsocketEndpoint,