Update request.go to fix concurrency nonce issues (#285)

* Updates nonce generation to adhere to fifo channel buffer before request executes by routine

* removed unused variables, lns etc

* Fix requested changes and added in timer that disengages lock if out of scope error occurs

* Fixed woopsy daisy issue

* Add benchmark, reduce time in force to unlock before stack insertion, add nil check for edge case

* Remove unusued waitgroup field

* use return nonce.Value and method, rm redundant nonce code, fix tests.

* Fix linter issue: unnecessary conversion
This commit is contained in:
Ryan O'Hara-Reid
2019-05-06 13:46:34 +10:00
committed by Adrian Gallagher
parent 1967507d40
commit 35b94268e0
46 changed files with 312 additions and 258 deletions

View File

@@ -534,7 +534,7 @@ func (a *Alphapoint) SendHTTPRequest(method, path string, data map[string]interf
return errors.New("unable to JSON request")
}
return a.SendPayload(method, path, headers, bytes.NewBuffer(PayloadJSON), result, false, a.Verbose)
return a.SendPayload(method, path, headers, bytes.NewBuffer(PayloadJSON), result, false, false, a.Verbose)
}
// SendAuthenticatedHTTPRequest sends an authenticated request
@@ -543,17 +543,14 @@ func (a *Alphapoint) SendAuthenticatedHTTPRequest(method, path string, data map[
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, a.Name)
}
if a.Nonce.Get() == 0 {
a.Nonce.Set(time.Now().UnixNano())
} else {
a.Nonce.Inc()
}
n := a.Requester.GetNonce(true)
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
data["apiKey"] = a.APIKey
data["apiNonce"] = a.Nonce.Get()
hmac := common.GetHMAC(common.HashSHA256, []byte(a.Nonce.String()+a.ClientID+a.APIKey), []byte(a.APISecret))
data["apiNonce"] = n
hmac := common.GetHMAC(common.HashSHA256, []byte(n.String()+a.ClientID+a.APIKey),
[]byte(a.APISecret))
data["apiSig"] = common.StringToUpper(common.HexEncodeToString(hmac))
path = fmt.Sprintf("%s/ajax/v%s/%s", a.APIUrl, alphapointAPIVersion, path)
@@ -562,5 +559,5 @@ func (a *Alphapoint) SendAuthenticatedHTTPRequest(method, path string, data map[
return errors.New("unable to JSON request")
}
return a.SendPayload(method, path, headers, bytes.NewBuffer(PayloadJSON), result, true, a.Verbose)
return a.SendPayload(method, path, headers, bytes.NewBuffer(PayloadJSON), result, true, true, a.Verbose)
}