Skip to content

Commit

Permalink
request: adds WithVerbose function to package to add verbosity to req…
Browse files Browse the repository at this point in the history
…uest context (thrasher-corp#950)

* request: adds WithVerbose function to package to add verbosity to request context

* request: add t.Parr....

* thrasher: nits
  • Loading branch information
shazbert authored and samuael committed Aug 8, 2022
1 parent f7d8913 commit 4bbac58
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
35 changes: 32 additions & 3 deletions exchanges/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/log"
)

const contextVerboseFlag verbosity = "verbose"

var (
// ErrRequestSystemIsNil defines and error if the request system has not
// been set up yet.
Expand Down Expand Up @@ -146,7 +148,9 @@ func (r *Requester) doRequest(ctx context.Context, endpoint EndpointLimit, newRe
return err
}

if p.Verbose {
verbose := isVerbose(ctx, p.Verbose)

if verbose {
log.Debugf(log.RequestSys, "%s attempt %d request path: %s", r.name, attempt, p.Path)
for k, d := range req.Header {
log.Debugf(log.RequestSys, "%s request header [%s]: %s", r.name, k, d)
Expand Down Expand Up @@ -194,7 +198,7 @@ func (r *Requester) doRequest(ctx context.Context, endpoint EndpointLimit, newRe
return fmt.Errorf("deadline would be exceeded by retry, status: %s", resp.Status)
}

if p.Verbose {
if verbose {
log.Errorf(log.RequestSys,
"%s request has failed. Retrying request in %s, attempt %d",
r.name,
Expand Down Expand Up @@ -255,7 +259,7 @@ func (r *Requester) doRequest(ctx context.Context, endpoint EndpointLimit, newRe
r.name,
err)
}
if p.Verbose {
if verbose {
log.Debugf(log.RequestSys,
"HTTP status: %s, Code: %v",
resp.Status,
Expand Down Expand Up @@ -367,3 +371,28 @@ func (r *Requester) Shutdown() error {
}
return r._HTTPClient.release()
}

// WithVerbose adds verbosity to a request context so that specific requests
// can have distinct verbosity without impacting all requests.
func WithVerbose(ctx context.Context) context.Context {
return context.WithValue(ctx, contextVerboseFlag, true)
}

// isVerbose checks main verbosity first then checks context verbose values
// for specific request verbosity.
func isVerbose(ctx context.Context, verbose bool) bool {
if verbose {
return true
}

val := ctx.Value(contextVerboseFlag)
if val == nil {
return false
}

isCtxVerbose, ok := val.(bool)
if !ok {
return false
}
return isCtxVerbose
}
29 changes: 29 additions & 0 deletions exchanges/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,32 @@ func TestGetHTTPClientUserAgent(t *testing.T) {
t.Fatal("unexpected value")
}
}

func TestContextVerbosity(t *testing.T) {
t.Parallel()
if isVerbose(context.Background(), false) {
t.Fatal("unexpected value")
}

if !isVerbose(context.Background(), true) {
t.Fatal("unexpected value")
}

ctx := context.Background()
ctx = WithVerbose(ctx)
if !isVerbose(ctx, false) {
t.Fatal("unexpected value")
}

ctx = context.Background()
ctx = context.WithValue(ctx, contextVerboseFlag, false)
if isVerbose(ctx, false) {
t.Fatal("unexpected value")
}

ctx = context.Background()
ctx = context.WithValue(ctx, contextVerboseFlag, "bruh")
if isVerbose(ctx, false) {
t.Fatal("unexpected value")
}
}
2 changes: 2 additions & 0 deletions exchanges/request/request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ type RequesterOption func(*Requester)
// being outside of receive window if application rate limiting reduces outbound
// requests.
type Generate func() (*Item, error)

type verbosity string

0 comments on commit 4bbac58

Please sign in to comment.