系统列表
Distributed Systems in Go:生产级分布式平台 SDK 与微服务实战
专栏《分布式 Go 系统工程》配套完整代码实战工程,涵盖并发模型、消息队列适配、Saga 编排、弹性容错与可观测性等生产级模式。
README.mdSYSTEM.mdcmd/auth/main.gocmd/gateway/main.gocmd/orders/main.goconfig/grafana/datasources/datasources.yamlconfig/prometheus.yamlconfig/tempo.yamldeploy/kube/orderflow-infra.yamldocker-compose.ymlgo.modgo.suminternal/auth/handler.gointernal/auth/service.gointernal/domain/events.gointernal/inventory/handler.gointernal/inventory/service.gointernal/notifications/service.gointernal/orders/handler.gointernal/orders/service.gointernal/payment/service.gomigrations/000001_init_schema.up.sqlpkg/platform/auth/auth_test.gopkg/platform/auth/claims.gopkg/platform/auth/jwks.gopkg/platform/auth/middleware.gopkg/platform/auth/password.gopkg/platform/auth/token.gopkg/platform/cli/cli_test.gopkg/platform/cli/health.gopkg/platform/concurrency/context.gopkg/platform/concurrency/shutdown.gopkg/platform/concurrency/shutdown_test.gopkg/platform/concurrency/workerpool.gopkg/platform/concurrency/workerpool_test.gopkg/platform/config/config_test.gopkg/platform/config/loader.gopkg/platform/consistency/consistency_test.gopkg/platform/consistency/idempotency.gopkg/platform/consistency/optimistic.gopkg/platform/consistency/saga.gopkg/platform/ctxutil/context.gopkg/platform/ctxutil/context_test.gopkg/platform/ctxutil/keys.gopkg/platform/database/database_test.gopkg/platform/database/errors.gopkg/platform/database/migrate.gopkg/platform/database/pool.gopkg/platform/database/retry.gopkg/platform/database/tx.gopkg/platform/messaging/fake.gopkg/platform/messaging/fake_test.gopkg/platform/messaging/interfaces.gopkg/platform/messaging/kafka/consumer.gopkg/platform/messaging/kafka/producer.gopkg/platform/messaging/nats/publisher.gopkg/platform/messaging/nats/subscriber.gopkg/platform/messaging/schema.gopkg/platform/middleware/bodyclose.gopkg/platform/middleware/chain.gopkg/platform/middleware/cors.gopkg/platform/middleware/errors.gopkg/platform/middleware/health.gopkg/platform/middleware/logging.gopkg/platform/middleware/middleware_test.gopkg/platform/middleware/recover.gopkg/platform/middleware/requestid.gopkg/platform/middleware/timeout.gopkg/platform/observability/context.gopkg/platform/observability/logger.gopkg/platform/observability/metrics.gopkg/platform/observability/observability_test.gopkg/platform/observability/tracing.gopkg/platform/orchestration/budget.gopkg/platform/orchestration/degrade.gopkg/platform/orchestration/fanout.gopkg/platform/orchestration/orchestration_test.gopkg/platform/resilience/circuit.gopkg/platform/resilience/resilience_test.gopkg/platform/resilience/retry.gotests/live_infra_test.gotests/orderflow_integration_test.go
pkg/platform/consistency/consistency_test.go
| package consistency |
| import ( |
| "context" |
| "errors" |
| "testing" |
| "time" |
| "distributed-systems-in-go/pkg/platform/database" |
| ) |
| func TestRunSaga_Success(t *testing.T) { |
| var step1Done, step2Done bool |
| steps := []SagaStep{ |
| { |
| Name: "ReserveInventory", |
| Action: func(ctx context.Context) error { |
| step1Done = true |
| return nil |
| }, |
| Compensate: func(ctx context.Context) error { |
| step1Done = false |
| return nil |
| }, |
| }, |
| { |
| Name: "ChargePayment", |
| Action: func(ctx context.Context) error { |
| step2Done = true |
| return nil |
| }, |
| Compensate: func(ctx context.Context) error { |
| step2Done = false |
| return nil |
| }, |
| }, |
| } |
| err := RunSaga(context.Background(), steps) |
| if err != nil { |
| t.Fatalf("expected saga to succeed, got %v", err) |
| } |
| if !step1Done || !step2Done { |
| t.Fatal("expected all steps to have executed") |
| } |
| } |
| func TestRunSaga_CompensationOnFailure(t *testing.T) { |
| var inventoryReserved bool |
| steps := []SagaStep{ |
| { |
| Name: "ReserveInventory", |
| Action: func(ctx context.Context) error { |
| inventoryReserved = true |
| return nil |
| }, |
| Compensate: func(ctx context.Context) error { |
| inventoryReserved = false |
| return nil |
| }, |
| }, |
| { |
| Name: "ChargePayment", |
| Action: func(ctx context.Context) error { |
| return errors.New("card declined") |
| }, |
| Compensate: func(ctx context.Context) error { |
| return nil |
| }, |
| }, |
| } |
| err := RunSaga(context.Background(), steps) |
| if err == nil { |
| t.Fatal("expected saga to fail") |
| } |
| // Inventory must be compensated (released) |
| if inventoryReserved { |
| t.Fatal("expected inventory reservation to be undone by compensation") |
| } |
| } |
| func TestIdempotencyStore_CacheAndExpiry(t *testing.T) { |
| store := NewMemoryIdempotencyStore() |
| ctx := context.Background() |
| key := "idemp-key-1" |
| res := &StoredResult{ |
| StatusCode: 201, |
| Body: []byte(`{"order_id":"123"}`), |
| ExpiresAt: time.Now().Add(50 * time.Millisecond), |
| } |
| _ = store.Set(ctx, key, res) |
| got, err := store.Get(ctx, key) |
| if err != nil || got == nil { |
| t.Fatalf("expected stored result, got %v (err=%v)", got, err) |
| } |
| if got.StatusCode != 201 { |
| t.Fatalf("expected 201 status, got %d", got.StatusCode) |
| } |
| // After expiry |
| time.Sleep(60 * time.Millisecond) |
| expired, err := store.Get(ctx, key) |
| if err != nil || expired != nil { |
| t.Fatalf("expected nil after expiration, got %v", expired) |
| } |
| } |
| func TestOptimisticLock_Retry(t *testing.T) { |
| attempts := 0 |
| err := WithOptimisticRetry(context.Background(), 3, func(ctx context.Context) error { |
| attempts++ |
| if attempts < 2 { |
| return database.ErrConflict |
| } |
| return nil |
| }) |
| if err != nil { |
| t.Fatalf("expected optimistic retry to succeed, got %v", err) |
| } |
| if attempts != 2 { |
| t.Fatalf("expected 2 attempts, got %d", attempts) |
| } |
| } |