asynkron / protoactor-go

Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
http://proto.actor
Apache License 2.0
4.96k stars 514 forks source link

Context propagation #1115

Open waeljammal opened 1 month ago

waeljammal commented 1 month ago

Is your feature request related to a problem? Please describe.

we have actors calling other actors in the cluster and those actors might do things like starting a database transaction, or something else that takes a context.Context, it would be great if a future request for example that has a timeout set would propagate and make the context available to the actor like actorCtx.Context() so that it can be passed along the chain or to fail a transaction if the requestor is not going to get an error because the request timed out mid way through handling it.

Another use for this would also be for us to propagate trace info in the context when using otel so we can get better trace output. Simply passing this context to the mongo client for eg. would include the db operation in the trace, doing this any other way is resulting in a lot of boiler plate code, the send, future etc. functions should take a context.

This will also allow developers to add their own traces to those the library might generate more easily.

Also tracing does not seem to work when using cluster kinds, I registered the middleware but no traces. It looks like the placement actor ignores all middlewares when using cluster kinds.

Describe the solution you'd like Access to a go context on the actor context, grpc already supports propagating metadata using md so it should be possible to propagate timeout, trace headers etc.

Describe alternatives you've considered Creating a message envelope to wrap the info or middleware to replace the default actor context but this won't work because it changes the signature of the Receive function.

solemncn commented 1 month ago
// RequestFuture sends a message to a given PID and returns a Future.
func (rc *RootContext) RequestFuture(pid *PID, message interface{}, timeout time.Duration) *Future {
    future := NewFuture(rc.actorSystem, timeout)
    env := &MessageEnvelope{
        Header:  rc.headers,
        Message: message,
        Sender:  future.PID(),
    }
    rc.sendUserMessage(pid, env)

    return future
}

The methods in root_context.go modify Handler. You can look into it. In the official implementation, nil is passed in.