aptos-labs / aptos-go-sdk

Aptos Go SDK
https://aptos.dev/sdks/go-sdk
Apache License 2.0
11 stars 14 forks source link

[Feature Request] Per-Request Context Support #95

Open Exca-DK opened 3 weeks ago

Exca-DK commented 3 weeks ago

🚀 Feature Request Description

Enable passing a context for each client method, instead of relying on a global timeout for the entire client.

Motivation

Is your feature request related to a problem? Please describe you use case. A global timeout for all requests isn’t flexible enough. Some requests only need a few seconds, while others may take much longer. I need to handle short-duration requests without blocking event loops, while also supporting longer ones when necessary.

Pitch

Describe the solution you would like Solution: Follow Go’s standard pattern by adding a context.Context as the first argument for each method. This would allow us to remove the global timeout on the HTTP client, relying on the context to manage request durations. Use context.Background() when no specific context is needed.

Describe alternatives you've considered None at the moment.

Are you willing to open a pull request? Yes, I’m happy to implement this if the feature is accepted.

Additional context

None

gregnazario commented 2 weeks ago

Interesting, do you have an example of this pattern?

As a relatively new Go developer, I don't have all the patterns down to be fully idiomatic.

Exca-DK commented 2 weeks ago

Glad you found it interesting! Here are some official resources for understanding the context pattern in Go: Go context package documentation Go blog: Understanding context

You may also find this example useful for seeing the pattern in action with the AWS SDK for Go: AWS SDK for Go: Context pattern example

For example, using http.NewRequestWithContext here enables the use of the provided ctx, allowing the user to directly manage the request lifecycle.