Skip to content

Commit

Permalink
create internal context instead use the global directly
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Apr 12, 2024
1 parent 1b07463 commit 9461b1d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion scanner/providers/gitcoin/gitcoin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ func (g *GitcoinPassport) updateScores() error {
downloading.Store(true)
defer downloading.Store(false)
// download de json from API endpoint
req, err := http.NewRequestWithContext(g.ctx, http.MethodGet, g.apiEndpoint, nil)
internalCtx, cancel := context.WithCancel(g.ctx)
defer cancel()
req, err := http.NewRequestWithContext(internalCtx, http.MethodGet, g.apiEndpoint, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
Expand Down
14 changes: 11 additions & 3 deletions scanner/providers/poap/poap_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"net/http"
"net/url"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/vocdoni/census3/scanner/providers"
"go.vocdoni.io/dvote/log"
)

const (
defaultRequestTimeout = 30 * time.Second
// POAP_SYMBOL_PREFIX is the prefix of the POAP token symbol to be used in
// with the eventID to compose the token symbol.
POAP_SYMBOL_PREFIX = "POAP"
Expand Down Expand Up @@ -62,6 +64,7 @@ type POAPSnapshot struct {
// of the token holders from the last snapshot.
type POAPHolderProvider struct {
ctx context.Context
cancel context.CancelFunc
apiEndpoint string
accessToken string
snapshots map[string]*POAPSnapshot
Expand All @@ -88,7 +91,7 @@ func (p *POAPHolderProvider) Init(globalCtx context.Context, iconf any) error {
if conf.AccessToken == "" {
return fmt.Errorf("no POAP access token defined")
}
p.ctx = globalCtx
p.ctx, p.cancel = context.WithCancel(globalCtx)
p.apiEndpoint = conf.APIEndpoint
p.accessToken = conf.AccessToken
p.snapshots = make(map[string]*POAPSnapshot)
Expand Down Expand Up @@ -162,6 +165,7 @@ func (p *POAPHolderProvider) HoldersBalances(_ context.Context, id []byte, delta
// Close method is not implemented in the POAP external provider. By default it
// returns nil error.
func (p *POAPHolderProvider) Close() error {
p.cancel()
return nil
}

Expand Down Expand Up @@ -350,7 +354,9 @@ func (p *POAPHolderProvider) holdersPage(eventID string, offset int) (*POAPAPIRe
q.Add("offset", fmt.Sprint(offset))
endpoint.RawQuery = q.Encode()
// create request and add headers
req, err := http.NewRequestWithContext(p.ctx, http.MethodGet, endpoint.String(), nil)
internalCtx, cancel := context.WithTimeout(p.ctx, defaultRequestTimeout)
defer cancel()
req, err := http.NewRequestWithContext(internalCtx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -393,7 +399,9 @@ func (p *POAPHolderProvider) getEventInfo(eventID string) (*EventAPIResponse, er
return nil, err
}
// create request and add headers
req, err := http.NewRequestWithContext(p.ctx, http.MethodGet, endpoint.String(), nil)
internalCtx, cancel := context.WithTimeout(p.ctx, defaultRequestTimeout)
defer cancel()
req, err := http.NewRequestWithContext(internalCtx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9461b1d

Please sign in to comment.