-
Notifications
You must be signed in to change notification settings - Fork 104
Add user defined callback on http response #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the callback should also take a *http.Request
for maximum flexibility. Otherwise, I think this is a reasonable simple solution. Thanks for doing this work!
I don't work at Alpaca, so it's up to them to decide if they want to accept the PR.
alpaca/rest.go
Outdated
@@ -30,6 +30,8 @@ type ClientOpts struct { | |||
RetryDelay time.Duration | |||
// HTTPClient to be used for each http request. | |||
HTTPClient *http.Client | |||
// ResponseCallback is a user defined callback to execute on the raw http.Response | |||
ResponseCallback func(*http.Response) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful to also provide the request.
ResponseCallback func(*http.Response) | |
ResponseCallback func(*http.Request, *http.Response) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I agree it can also be useful, I'll wait for the maintainers feedback and implement this accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this solution works for me. HTTPCallback
works too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add the request and rename to HTTPCallback than 👍
marketdata/rest.go
Outdated
@@ -42,6 +42,8 @@ type ClientOpts struct { | |||
HTTPClient *http.Client | |||
// Host used to set the http request's host | |||
RequestHost string | |||
// ResponseCallback is a user defined callback to execute on the raw http.Response | |||
ResponseCallback func(*http.Response) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
alpaca/rest.go
Outdated
@@ -114,6 +116,10 @@ func defaultDo(c *Client, req *http.Request) (*http.Response, error) { | |||
time.Sleep(c.opts.RetryDelay) | |||
} | |||
|
|||
if c.opts.ResponseCallback != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original issue (#292) is explicitly about rate limit. However, in this implementation the callback function is not called when the rate limit is hit (L110), only when either we reach the retry limit or succeed. This means there's no way to delay your next request after hitting 429, making the callback unusable for #292.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking at this @gnvk!
The main goal of #292 is to expose the rate limit state so we can, for example, make full use of the available request rate for polling (balances, positions, orders, fills) and placing orders, and adjust the polling rate dynamically as needed. This is especially useful when the Alpaca account is accessed from multiple client connections concurrently (e.g. TradingView integration and a locally running trading bot). This PR satisfies that goal.
It looks like it will still call the callback on 429s since L110 only breaks from the for loop but does not return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even so, I'd call the callback when a 429 happens to cover at least one other use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, are you saying we should call the callback the first time we get a 429, rather than waiting until the retry limit is met? If so, I agree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I'll adjust the implementation, thanks for spotting it @gnvk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the implementation and now I'm invoking the method right after receiving the response instead of deferring the execution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good. It would also be good to add a test to alpaca/rest_test.go. You could maybe have a flag that gets set when the callback is called and then test against the flag.
resolves #292