pancsta / asyncmachine-go

AOP Actor Model for distributed workflows
https://asyncmachine.dev
MIT License
72 stars 0 forks source link

feat(helpers): add WaitFor* methods #149

Closed pancsta closed 5 days ago

pancsta commented 5 days ago

To tackle select&ctx-pasta, there's new Waiting helpers

Example - wait for bootstrap RPC to become ready.

//
err := amhelp.WaitForAll(ctx, s.ConnTimeout,
    boot.server.Mach.When1(ssrpc.ServerStates.RpcReady, nil))
if ctx.Err() != nil {
    return // expired
}
if err != nil {
    AddErrWorker(s.Mach, err, Pass(argsOut))
    return
}

Example - wait for Foo, Bar, or Exception.

ctx := client.Mach.NewStateCtx("Start")
err := amhelp.WaitForErrAll(ctx, 1*time.Second, mach,
        mach.When1("Foo", nil),
        mach.When1("Bar", nil))
if ctx.Err() != nil {
    // no err
    return nil // expired
}
if err != nil {
    // either timeout happened or Exception has been activated
    return err
}

Theres is more new related stuff in /pkg/helpers.