Skip to content

Commit

Permalink
Adds context to api client
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Aug 17, 2024
1 parent 854eb1f commit 5f8ebe6
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ const (

// DerivAPI is the main struct for the DerivAPI client.
type DerivAPI struct { //nolint:revive // don't want to change the name for now

Check failure on line 26 in api.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

fieldalignment: struct with 128 pointer bytes could be 72 (govet)
reqChan chan APIReqest
Endpoint *url.URL
keepAliveOnDisconnect chan bool
Origin *url.URL
ws *websocket.Conn
closingChan chan int
Lang string
TimeOut time.Duration
lastRequestID int64
AppID int
keepAliveInterval time.Duration
connectionLock sync.Mutex
keepAlive bool
debugEnabled bool
reqChan chan APIReqest
Endpoint *url.URL
Origin *url.URL
ws *websocket.Conn
closingChan chan int
Lang string
TimeOut time.Duration
lastRequestID int64
AppID int
keepAliveInterval time.Duration
connectionLock sync.Mutex
keepAlive bool
debugEnabled bool
ctx context.Context
cancel context.CancelFunc
}

// APIReqest is an interface for all API requests.
Expand Down Expand Up @@ -116,12 +117,15 @@ func NewDerivAPI(endpoint string, appID int, lang, origin string, opts ...APIOpt
connectionLock: sync.Mutex{},
closingChan: make(chan int),
keepAliveInterval: keepAliveInterval,
ctx: context.Background(),
}

for _, opt := range opts {
opt(&api)
}

api.ctx, api.cancel = context.WithCancel(api.ctx)

return &api, nil
}

Expand Down Expand Up @@ -182,21 +186,20 @@ func (api *DerivAPI) Connect() error {
go api.requestMapper(respChan, outputChan, api.reqChan, api.closingChan)

if api.keepAlive {

Check failure on line 188 in api.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

unnecessary leading newline (whitespace)
api.keepAliveOnDisconnect = make(chan bool, 1)

go func(interval time.Duration, onDisconnect chan bool) {
go func(interval time.Duration) {
for {
select {
case <-time.After(interval):
_, err := api.Ping(schema.Ping{Ping: 1})
if err != nil {
return
}
case <-onDisconnect:
case <-api.ctx.Done():
return
}
}
}(api.keepAliveInterval, api.keepAliveOnDisconnect)
}(api.keepAliveInterval)
}

return nil
Expand All @@ -216,12 +219,9 @@ func (api *DerivAPI) Disconnect() {

api.logDebugf("Disconnecting from %s", api.Endpoint.String())

close(api.reqChan)
api.cancel()

if api.keepAlive {
api.keepAliveOnDisconnect <- true
close(api.keepAliveOnDisconnect)
}
close(api.reqChan)

api.ws.Close(websocket.StatusNormalClosure, "disconnecting")
api.ws = nil
Expand Down Expand Up @@ -254,8 +254,8 @@ func (api *DerivAPI) handleResponses(wsConn *websocket.Conn, respChan chan []byt
api.Disconnect()
}()

for {
msgType, reader, err := wsConn.Reader(context.TODO())
for api.ctx.Err() == nil {
msgType, reader, err := wsConn.Reader(api.ctx)
if err != nil {
api.logDebugf("Failed to receive response: %s", err.Error())
return
Expand Down

0 comments on commit 5f8ebe6

Please sign in to comment.