[#219] morph: Resolve containedctx linter

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-04-05 16:58:32 +03:00
parent 56282edf02
commit e815b19101
9 changed files with 43 additions and 54 deletions

View file

@ -1,6 +1,7 @@
package client
import (
"context"
"sort"
"time"
@ -32,7 +33,7 @@ func (e *endpoints) init(ee []Endpoint) {
e.list = ee
}
func (c *Client) switchRPC() bool {
func (c *Client) switchRPC(ctx context.Context) bool {
c.switchLock.Lock()
defer c.switchLock.Unlock()
@ -41,7 +42,7 @@ func (c *Client) switchRPC() bool {
// Iterate endpoints in the order of decreasing priority.
for c.endpoints.curr = range c.endpoints.list {
newEndpoint := c.endpoints.list[c.endpoints.curr].Address
cli, act, err := c.newCli(newEndpoint)
cli, act, err := c.newCli(ctx, newEndpoint)
if err != nil {
c.logger.Warn("could not establish connection to the switched RPC node",
zap.String("endpoint", newEndpoint),
@ -56,7 +57,7 @@ func (c *Client) switchRPC() bool {
c.logger.Info("connection to the new RPC node has been established",
zap.String("endpoint", newEndpoint))
subs, ok := c.restoreSubscriptions(cli, newEndpoint, false)
subs, ok := c.restoreSubscriptions(ctx, cli, newEndpoint, false)
if !ok {
// new WS client does not allow
// restoring subscription, client
@ -74,7 +75,7 @@ func (c *Client) switchRPC() bool {
if c.cfg.switchInterval != 0 && !c.switchIsActive.Load() &&
c.endpoints.list[c.endpoints.curr].Priority != c.endpoints.list[0].Priority {
c.switchIsActive.Store(true)
go c.switchToMostPrioritized()
go c.switchToMostPrioritized(ctx)
}
return true
@ -83,7 +84,7 @@ func (c *Client) switchRPC() bool {
return false
}
func (c *Client) notificationLoop() {
func (c *Client) notificationLoop(ctx context.Context) {
var e any
var ok bool
@ -95,7 +96,7 @@ func (c *Client) notificationLoop() {
c.switchLock.RUnlock()
select {
case <-c.cfg.ctx.Done():
case <-ctx.Done():
_ = c.UnsubscribeAll()
c.close()
@ -111,17 +112,17 @@ func (c *Client) notificationLoop() {
}
if ok {
c.routeEvent(e)
c.routeEvent(ctx, e)
continue
}
if !c.reconnect() {
if !c.reconnect(ctx) {
return
}
}
}
func (c *Client) routeEvent(e any) {
func (c *Client) routeEvent(ctx context.Context, e any) {
typedNotification := rpcclient.Notification{Value: e}
switch e.(type) {
@ -135,7 +136,7 @@ func (c *Client) routeEvent(e any) {
select {
case c.notifications <- typedNotification:
case <-c.cfg.ctx.Done():
case <-ctx.Done():
_ = c.UnsubscribeAll()
c.close()
case <-c.closeChan:
@ -144,7 +145,7 @@ func (c *Client) routeEvent(e any) {
}
}
func (c *Client) reconnect() bool {
func (c *Client) reconnect(ctx context.Context) bool {
if closeErr := c.client.GetError(); closeErr != nil {
c.logger.Warn("switching to the next RPC node",
zap.String("reason", closeErr.Error()),
@ -156,7 +157,7 @@ func (c *Client) reconnect() bool {
return true
}
if !c.switchRPC() {
if !c.switchRPC(ctx) {
c.logger.Error("could not establish connection to any RPC node")
// could not connect to all endpoints =>
@ -173,7 +174,7 @@ func (c *Client) reconnect() bool {
return true
}
func (c *Client) switchToMostPrioritized() {
func (c *Client) switchToMostPrioritized(ctx context.Context) {
t := time.NewTicker(c.cfg.switchInterval)
defer t.Stop()
defer c.switchIsActive.Store(false)
@ -181,7 +182,7 @@ func (c *Client) switchToMostPrioritized() {
mainLoop:
for {
select {
case <-c.cfg.ctx.Done():
case <-ctx.Done():
return
case <-t.C:
c.switchLock.RLock()
@ -207,7 +208,7 @@ mainLoop:
tryE := e.Address
cli, act, err := c.newCli(tryE)
cli, act, err := c.newCli(ctx, tryE)
if err != nil {
c.logger.Warn("could not create client to the higher priority node",
zap.String("endpoint", tryE),
@ -216,7 +217,7 @@ mainLoop:
continue
}
if subs, ok := c.restoreSubscriptions(cli, tryE, true); ok {
if subs, ok := c.restoreSubscriptions(ctx, cli, tryE, true); ok {
c.switchLock.Lock()
// higher priority node could have been