micro--go-micro
c7657f73f4
goreleaser / goreleaser (push) Has been cancelled
* test(harness): read agent plan from the scoped store
The store-scoping change moved an agent's plan from the default table
key agent/{name}/plan to its own table (database "agent", table {name},
key "plan"). The plan-delegate harness tests still read the old key and
failed with 'not found'; read through store.Scope(mem, "agent", name)
like the agent does.
* docs: orient agents-first across README, landing, and docs overview
Lead with agents (then services and flows), surface MCP + A2A as the
interop story, and frame agents as services. Landing hero and feature
grid reordered agents-first with an A2A gateway card.
* v6: module path go-micro.dev/v6, TLS secure by default, NewService
Cut v6. Three breaking changes, bundled so the major bump is paid once:
- Module path go-micro.dev/v5 -> go-micro.dev/v6 across all imports + go.mod.
- TLS verification on by default (was off). MICRO_TLS_SECURE removed;
MICRO_TLS_INSECURE=true opts out for self-signed/dev.
- micro.NewService(name, opts...) is the canonical service constructor,
symmetric with NewAgent/NewFlow; micro.New kept as a deprecated alias;
the old name-less NewService(opts...) removed. Generators emit NewService.
Also ports the JWT auth token provider in-module (go-micro.dev/v6/auth/jwt/token
on golang-jwt/jwt/v5), dropping the v5-pinned github.com/micro/plugins/v5/auth/jwt
and the deprecated dgrijalva/jwt-go.
Docs/README/landing updated to v6 and @latest; v5->v6 migration guide added;
CHANGELOG cut as [6.0.0]. Blog posts left at their historical versions.
---------
Co-authored-by: Claude <noreply@anthropic.com>
115 行
3.5 KiB
Go
115 行
3.5 KiB
Go
// Durable Flow — a workflow that survives a crash and resumes
|
|
//
|
|
// A flow can be an ordered list of steps (a task with stages) rather than
|
|
// a single LLM turn. Each step is checkpointed before and after through a
|
|
// pluggable Checkpoint (store-backed by default), so if the process dies
|
|
// mid-run, the run resumes at the step it stopped on — without re-running
|
|
// the steps that already completed (and already had their side effects).
|
|
//
|
|
// This example needs no LLM key. It runs a three-step checkout flow whose
|
|
// "charge" step fails the first time (a transient outage). The run is
|
|
// checkpointed as failed at that step; we then "recover" the dependency
|
|
// and Resume — and the already-completed "reserve" step does not run
|
|
// again. A real step would call a service (flow.Call), an agent
|
|
// (flow.Dispatch), or the model (flow.LLM); here they're plain funcs so
|
|
// the durability is the only thing on display.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"go-micro.dev/v6"
|
|
)
|
|
|
|
// Order is the payload carried across steps via State.Set / State.Scan.
|
|
type Order struct {
|
|
ID string `json:"id"`
|
|
Reserved bool `json:"reserved"`
|
|
Charged bool `json:"charged"`
|
|
Confirmed bool `json:"confirmed"`
|
|
}
|
|
|
|
// charged toggles the transient failure: 0 = the payment dependency is
|
|
// down (first run), 1 = recovered (on resume).
|
|
var charged int
|
|
|
|
// reserveCalls proves the completed step is not re-run on resume.
|
|
var reserveCalls int
|
|
|
|
func reserve(_ context.Context, in micro.FlowState) (micro.FlowState, error) {
|
|
reserveCalls++
|
|
var o Order
|
|
in.Scan(&o)
|
|
o.ID = "order-1"
|
|
o.Reserved = true
|
|
fmt.Println(" reserve → inventory reserved")
|
|
return in, in.Set(o)
|
|
}
|
|
|
|
func charge(_ context.Context, in micro.FlowState) (micro.FlowState, error) {
|
|
var o Order
|
|
in.Scan(&o)
|
|
if charged == 0 {
|
|
fmt.Println(" charge → payment dependency unavailable (crash)")
|
|
return in, errors.New("payment gateway timeout")
|
|
}
|
|
o.Charged = true
|
|
fmt.Println(" charge → payment captured")
|
|
return in, in.Set(o)
|
|
}
|
|
|
|
func confirm(_ context.Context, in micro.FlowState) (micro.FlowState, error) {
|
|
var o Order
|
|
in.Scan(&o)
|
|
o.Confirmed = true
|
|
fmt.Println(" confirm → order confirmed")
|
|
return in, in.Set(o)
|
|
}
|
|
|
|
func main() {
|
|
f := micro.NewFlow("checkout",
|
|
micro.FlowSteps(
|
|
micro.FlowStep{Name: "reserve", Run: reserve},
|
|
micro.FlowStep{Name: "charge", Run: charge},
|
|
micro.FlowStep{Name: "confirm", Run: confirm},
|
|
),
|
|
// Durable by default; shown explicitly. Runs are namespaced under
|
|
// the flow name ("flow/checkout/runs/..."), so this flow's state
|
|
// doesn't share a keyspace with other flows. Point the default
|
|
// store at Postgres or NATS KV to survive a real process restart.
|
|
micro.FlowWithCheckpoint(micro.StoreCheckpoint(nil, "checkout")),
|
|
)
|
|
|
|
ctx := context.Background()
|
|
|
|
fmt.Println("first run:")
|
|
if err := f.Execute(ctx, `{}`); err != nil {
|
|
fmt.Printf(" run failed: %v\n", err)
|
|
}
|
|
|
|
pending, _ := f.Pending(ctx)
|
|
if len(pending) == 0 {
|
|
fmt.Println("nothing pending — unexpected")
|
|
return
|
|
}
|
|
run := pending[0]
|
|
fmt.Printf("\ncheckpoint: run %s is at step %q (status %s)\n",
|
|
run.ID[:8], run.State.Stage, run.Status)
|
|
|
|
// The dependency recovers (or a new process picks the run up).
|
|
charged = 1
|
|
|
|
fmt.Println("\nresume:")
|
|
if err := f.Resume(ctx, run.ID); err != nil {
|
|
fmt.Printf(" resume failed: %v\n", err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("\nreserve ran %d time(s) total — completed steps are not repeated on resume\n", reserveCalls)
|
|
if pend, _ := f.Pending(ctx); len(pend) == 0 {
|
|
fmt.Println("no pending runs — the workflow completed durably")
|
|
}
|
|
}
|