Fire-and-Forget Invocations in Go (Trigger)
Overview
Method.Trigger(client, in) enqueues an invocation without waiting for its result and returns an InvocationID, or an error if the invocation could not be enqueued (MustTrigger panics instead). Use it for notifications, fan-out, or handing work to a background/worker agent. Unlike a synchronous Call, Trigger does not block — so it is also safe for an agent to message itself (a synchronous self-Call would deadlock, since one instance handles one invocation at a time).
Steps
- Import the target agent’s definition package.
- Get a client with
Agent.Get(id). - Trigger the method:
Target.Method.MustTrigger(client, in)— returns immediately.
Example
package impl
import (
"myapp/agents/coordinator"
"myapp/agents/worker"
"github.com/golemcloud/golem/sdks/go/golem"
)
type state struct{}
var agent = coordinator.Agent.Implement(func(coordinator.ID) *state { return &state{} })
func init() {
agent.Handle(coordinator.Start, func(_ *golem.Context[state], in coordinator.StartIn) golem.Unit {
// Hand each unit of work to a worker without awaiting the outcome.
for _, w := range in.Jobs {
c := worker.Agent.Get(worker.ID{Name: w.Worker})
worker.Dispatch.MustTrigger(c, worker.DispatchIn{Job: w.Job})
}
return golem.Unit{}
})
}Fanning out is just a loop of Trigger calls — each runs independently on its target agent.
Trigger vs Call
Call(c, in) (Out, error)/MustCall(c, in) Out— blocks, returns the result. Use when you need the value. Never self-Call.Trigger(c, in) (InvocationID, error)/MustTrigger(c, in) InvocationID— returns immediately, no result. Use for notifications, fan-out, and self-messaging.CallAsync(c, in) *Future[Out]— start now, await later withGet()orMustGet(). Use when you want the result but not to block yet (seegolem-call-another-agent-go).
The returned InvocationID is an opaque handle to the enqueued invocation; you can ignore it for pure fire-and-forget.
Key Constraints
Triggerdelivers a value-less enqueue: there is no way to read a return value from it. If you need the result, useCall/CallAsync.- As with all RPC,
Triggeris called on the method descriptor (worker.Dispatch.Trigger(client, in)), and the client comes from the callee’s definition (worker.Agent.Get(id)).
Related Guides
| Guide | Description |
|---|---|
golem-call-another-agent-go | You need the callee’s result (sync Call / async CallAsync) |
golem-recurring-task-go | Schedule a future/self invocation at a specific time |
golem-add-agent-go | Define the agent being triggered |