-
Notifications
You must be signed in to change notification settings - Fork 5
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
Pass ctx from func to func #2
Comments
Hmm, I wasn't aware of that interface. I don't think what I'm doing is well served by that interface and maybe it makes sense to rename |
@sjwiesman If you want to follow Go's practices you can easily re-use the already provided context (not sure why it does not work for you), otherwise you need to rename the custom one because it it a bit confusing. |
Maybe I'm missing something, but looking at this interface: type Context interface {
//It retures a channel when a context is cancelled, timesout (either when deadline is reached or timeout time has finished)
Done() <-chan struct{}
//Err will tell why this context was cancelled. A context is cancelled in three scenarios.
// 1. With explicit cancellation signal
// 2. Timeout is reached
// 3. Deadline is reached
Err() error
//Used for handling deallines and timeouts
Deadline() (deadline time.Time, ok bool)
//Used for passing request scope values
Value(key interface{}) interface{}
} I don't see a use for these methods in an implementation of the |
@sjwiesman you could use a method from the API like WithValue to store key,value pairs. I saw that you keep some values around as state so this is one option of how to use this. Immutability is a concern if you copy a lot of data, but from what I see you keep pointers in the custom context struct, so copying pointers does not seem too bad on the average case anyway.
can be re-written as:
Note that if you want to keep state beyond the scope of a request this is not suitable. Maybe the latter was not clear from my side ;) |
Hi @skonto, Let me take a step back and describe what is going on:
So, I do think that there is a need for a class like that to exist, to capture all of these concerns. Now for the
It seems like (1) is pretty idiomatic in Go, especially If the user function would spawn different go-routines, so it seems logical to provide a child context to the stateful functions.
The end result might look like this: and This would give us both idiomatic usage of the Go context, and we keep managing the previous responsibilities. |
Resolved in aca3da2 |
Right now a custom ctx is used in order to call the API funcs and keep state internally. In Golang ctx is expected to be passed from func to func. I think Golang ctx could be initiated at the http.serve side and then pass it down to process etc.
This would be a more idiomatic use and more aligned on how context is used in the other sdks.
The text was updated successfully, but these errors were encountered: