state: add new state package as a foundation for v2#83
state: add new state package as a foundation for v2#83ecordell wants to merge 2 commits intoauthzed:mainfrom
Conversation
| func OnError(errorHandler, successHandler state.NewStep) state.NewStep { | ||
| return func(next state.Step) state.Step { | ||
| return state.StepFunc(func(ctx context.Context) state.Step { | ||
| if ctx.Err() != nil { |
There was a problem hiding this comment.
Or is the idea that we're using the error field on the context for this? Like where would this be set other than through cancellation?
There was a problem hiding this comment.
this is just for handling context cancellation, we do it a bunch so I wanted to make some helpers for it
There was a problem hiding this comment.
General comment: I still don't understand how error handling works in this - how would an error result of (say) an API call be propagated up to an error-handling step? Are we sticking it on the context? That seems like it'd be a recipe for unhandled errors or error handling that behaves strangely when you introduce parallelism.
There was a problem hiding this comment.
yeah I addressed that here - I think I need a bit more docs / examples to explain it
4cf1007 to
15d7c2d
Compare
|
I've simplified and cleaned up a bit since I opened this, the new things to note are:
|
Description
There's lots of docs in the PR so I'll let those do most of the talking, but there were a few pain points I set out to address with this:
returnfrom a handler, i.e. after callingrequeue. This newstatepackage relies on returning the next step to run (vs. directly calling the next step to run like in v1), so you can't forget. This is the primary low-level difference - building blocks arefunc(Context) Contextinstead offunc(Context). There are some new things in thequeuepackage to demonstrate this use too.v1. The main reason was so that we could "find" the right handler if we needed to branch. The newstatepackage addresses this by giving direct ways to compose handlers (examples will make it clear).And in the back of my head I've always thought about these in terms of monadic structures. I don't think it's for everyone, but I took the time to write down what that actually means in the context of the
statepackage and why it matters (library consumers don't need to worry about this, it's more philosophical)Testing
This is not in practical use yet - there are unit tests and and I have follow on work that uses these new primitives, but wanted to keep the initial PR smaller.