Skip to content

Commit

Permalink
[price-feeder] gracefully handle nil response for new provider
Browse files Browse the repository at this point in the history
  • Loading branch information
udpatil committed Feb 27, 2023
1 parent ccd0a48 commit 47187d9
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 14 deletions.
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func NewBinanceProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Binance websocket: %w", err)
}
Expand Down Expand Up @@ -366,7 +370,11 @@ func (p *BinanceProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnect to binance websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/coinbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ func NewCoinbaseProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Coinbase websocket: %w", err)
}
Expand Down Expand Up @@ -486,7 +490,11 @@ func (p *CoinbaseProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Coinbase websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ func NewGateProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Gate websocket: %w", err)
}
Expand Down Expand Up @@ -551,7 +555,11 @@ func (p *GateProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Gate websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ func NewHuobiProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Huobi websocket: %w", err)
}
Expand Down Expand Up @@ -388,7 +392,11 @@ func (p *HuobiProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Huobi websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/kraken.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ func NewKrakenProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to websocket: %w", err)
}
Expand Down Expand Up @@ -492,7 +496,11 @@ func (p *KrakenProvider) reconnect() error {
p.logger.Debug().Msg("trying to reconnect")

wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error connecting to Kraken websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/mexc.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func NewMexcProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to mexc websocket: %w", err)
}
Expand Down Expand Up @@ -396,7 +400,11 @@ func (p *MexcProvider) reconnect() error {

p.logger.Debug().Msg("mexc: reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("mexc: error reconnect to mexc websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/okx.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func NewOkxProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Okx websocket: %w", err)
}
Expand Down Expand Up @@ -430,7 +434,11 @@ func (p *OkxProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Okx websocket: %w", err)
}
Expand Down

0 comments on commit 47187d9

Please sign in to comment.