diff --git a/CHANGELOG b/CHANGELOG index c35101a7b..829d9e6a7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +3.0.4 +- Features + - adding subscriptions rate limiting + 3.0.3 - Features - new websocket manager. Please refer to `/examples/ws` for usage examples. diff --git a/pkg/mux/mux.go b/pkg/mux/mux.go index 3d6712bd6..5772d68f8 100644 --- a/pkg/mux/mux.go +++ b/pkg/mux/mux.go @@ -5,12 +5,18 @@ import ( "fmt" "log" "sync" + "time" "github.com/bitfinexcom/bitfinex-api-go/pkg/models/event" "github.com/bitfinexcom/bitfinex-api-go/pkg/mux/client" "github.com/bitfinexcom/bitfinex-api-go/pkg/mux/msg" ) +const ( + rateLimit = 60 * 1000 / 20 // 20 connections per minute + pubClientSubsLimit = 30 +) + // Mux will manage all connections and subscriptions. Will check if subscriptions // limit is reached and spawn new connection when that happens. It will also listen // to all incomming client messages and reconnect client with all its subscriptions @@ -105,6 +111,9 @@ func (m *Mux) Subscribe(sub event.Subscribe) *Mux { m.mtx.Lock() defer m.mtx.Unlock() + // make sure we don't hit the rate limit + time.Sleep(rateLimit * time.Millisecond) + if subscribed := m.publicClients[m.cid].SubAdded(sub); subscribed { return m } @@ -286,7 +295,7 @@ func (m *Mux) addPublicClient() *Mux { c, err := client. New(). WithID(m.cid). - WithSubsLimit(20). + WithSubsLimit(pubClientSubsLimit). Public(m.publicURL) if err != nil { m.Err = err