Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Ale-Cas
Copy link

@Ale-Cas Ale-Cas commented Mar 18, 2025

resolves #292

Screenshot 2025-03-18 at 13 34 47

Copy link
Contributor

@gcatlin gcatlin left a 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)
Copy link
Contributor

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.

Suggested change
ResponseCallback func(*http.Response)
ResponseCallback func(*http.Request, *http.Response)

Copy link
Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gcatlin You're the original reporter of #292. If this solution works for you, I'm fine with it with one minor suggestion: if both the request and the response is in the callback then could you maybe call it HTTPCallback instead of ResponseCallback?

Copy link
Contributor

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.

Copy link
Author

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 👍

@@ -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)
Copy link
Contributor

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 {
Copy link
Collaborator

@gnvk gnvk Mar 19, 2025

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.

Copy link
Contributor

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.

Copy link
Collaborator

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.

Copy link
Contributor

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.

Copy link
Author

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

Copy link
Author

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

Copy link
Contributor

@gcatlin gcatlin left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose rate limit state
3 participants