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>
147 行
3.2 KiB
Go
147 行
3.2 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
micro "go-micro.dev/v6"
|
|
"go-micro.dev/v6/client"
|
|
grpcclient "go-micro.dev/v6/client/grpc"
|
|
"go-micro.dev/v6/registry"
|
|
"go-micro.dev/v6/server"
|
|
)
|
|
|
|
type SleepRequest struct {
|
|
DelayMS int `json:"delay_ms"`
|
|
}
|
|
|
|
type SleepResponse struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type SleepHandler struct {
|
|
started chan struct{}
|
|
}
|
|
|
|
func (h *SleepHandler) Sleep(ctx context.Context, req *SleepRequest, rsp *SleepResponse) error {
|
|
select {
|
|
case h.started <- struct{}{}:
|
|
default:
|
|
}
|
|
|
|
timer := time.NewTimer(time.Duration(req.DelayMS) * time.Millisecond)
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
}
|
|
|
|
rsp.Message = "ok"
|
|
return nil
|
|
}
|
|
|
|
func TestGracefulStopRejectsNewRPCsButAllowsInFlightRPCs(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("listen: %v", err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
addr := listener.Addr().String()
|
|
reg := registry.NewMemoryRegistry()
|
|
handler := &SleepHandler{started: make(chan struct{}, 1)}
|
|
|
|
svc := micro.NewService("grace-demo",
|
|
micro.HandleSignal(false),
|
|
micro.Registry(reg),
|
|
micro.Server(NewServer(
|
|
server.Registry(reg),
|
|
server.Name("grace-demo"),
|
|
server.Address(addr),
|
|
Listener(listener),
|
|
GracefulStopTimeout(3*time.Second),
|
|
)),
|
|
micro.Client(grpcclient.NewClient(
|
|
client.Registry(reg),
|
|
client.ContentType("application/grpc+json"),
|
|
client.DialTimeout(200*time.Millisecond),
|
|
client.RequestTimeout(5*time.Second),
|
|
)),
|
|
)
|
|
|
|
if err := svc.Handle(handler); err != nil {
|
|
t.Fatalf("handle: %v", err)
|
|
}
|
|
if err := svc.Start(); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
|
|
stopped := false
|
|
defer func() {
|
|
if !stopped {
|
|
_ = svc.Stop()
|
|
}
|
|
}()
|
|
|
|
longDone := make(chan error, 1)
|
|
go func() {
|
|
req := svc.Client().NewRequest("grace-demo", "SleepHandler.Sleep", &SleepRequest{DelayMS: 1000})
|
|
rsp := &SleepResponse{}
|
|
longDone <- svc.Client().Call(context.Background(), req, rsp, client.WithAddress(addr))
|
|
}()
|
|
|
|
select {
|
|
case <-handler.started:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("timed out waiting for long RPC to start")
|
|
}
|
|
|
|
stopDone := make(chan error, 1)
|
|
go func() {
|
|
stopDone <- svc.Stop()
|
|
}()
|
|
|
|
freshReq := svc.Client().NewRequest("grace-demo", "SleepHandler.Sleep", &SleepRequest{DelayMS: 10})
|
|
freshRsp := &SleepResponse{}
|
|
var rejectErr error
|
|
|
|
deadline := time.Now().Add(300 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
callCtx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
|
|
err := svc.Client().Call(callCtx, freshReq, freshRsp, client.WithAddress(addr))
|
|
cancel()
|
|
if err != nil {
|
|
rejectErr = err
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
|
|
if rejectErr == nil {
|
|
t.Fatal("expected a new RPC to be rejected shortly after shutdown started")
|
|
}
|
|
|
|
select {
|
|
case err := <-longDone:
|
|
if err != nil {
|
|
t.Fatalf("long RPC failed during graceful stop: %v", err)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("timed out waiting for in-flight RPC to finish")
|
|
}
|
|
|
|
select {
|
|
case err := <-stopDone:
|
|
if err != nil {
|
|
t.Fatalf("stop: %v", err)
|
|
}
|
|
stopped = true
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("timed out waiting for server stop")
|
|
}
|
|
}
|