Files
archie/internal/infrastructure/staterpc/client.go
T
sam c3471128a9 fix(staterpc): make model usage writes durable through a State Store outage
A failed RecordUsage was logged and dropped, losing chat, title, sampling
and curator usage that cannot be rebuilt from events. The staterpc Client
now keeps a bounded local outbox: a failed write is queued and retried with
backoff (1s doubling to 1m, 10s per attempt) until the store accepts it, so
records land once the store recovers. The queue is capped at 1024 records;
an overflow is refused so the caller logs the drop. The sampling usage
write is bounded to 5s so a dead store cannot hold a model call.
2026-10-30 00:06:38 +11:00

125 lines
5.0 KiB
Go

package staterpc
import (
"context"
"google.golang.org/grpc"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/storepkg"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
// Client is the single remote adapter over the State Store services,
// asserted against whichever narrow Go interfaces its caller needs -- exactly
// as gatewayrpc.Client asserts both messaging.ChatContract and
// messaging.SessionStore.
type Client struct {
packages pb.PackageServiceClient
taskGrant pb.TaskGrantServiceClient
harnessSecret pb.HarnessSecretServiceClient
account pb.AccountServiceClient
identity pb.IdentityServiceClient
task pb.TaskServiceClient
execution pb.ExecutionServiceClient
usageOutbox *usageOutbox
review pb.ReviewServiceClient
taskEvent pb.TaskEventServiceClient
stats pb.StatsServiceClient
taskLog pb.TaskLogServiceClient
status pb.StatusServiceClient
capture pb.CaptureServiceClient
mapping pb.MappingServiceClient
eventType pb.EventTypeServiceClient
binding pb.BindingServiceClient
dispatch pb.DispatchServiceClient
source pb.SourceServiceClient
access pb.AccessServiceClient
org pb.OrgServiceClient
alert pb.AlertServiceClient
audit pb.AuditServiceClient
schedule pb.ScheduleServiceClient
controlPlane controlpb.ControlPlaneServiceClient
}
// NewClient wraps conn's generated clients. The client keeps a local usage
// outbox: a RecordUsage write that fails is retried in the background until
// the store accepts it. Close stops that retry worker.
func NewClient(conn grpc.ClientConnInterface) *Client {
c := &Client{
packages: pb.NewPackageServiceClient(conn),
taskGrant: pb.NewTaskGrantServiceClient(conn),
harnessSecret: pb.NewHarnessSecretServiceClient(conn),
account: pb.NewAccountServiceClient(conn),
identity: pb.NewIdentityServiceClient(conn),
task: pb.NewTaskServiceClient(conn),
execution: pb.NewExecutionServiceClient(conn),
review: pb.NewReviewServiceClient(conn),
taskEvent: pb.NewTaskEventServiceClient(conn),
stats: pb.NewStatsServiceClient(conn),
taskLog: pb.NewTaskLogServiceClient(conn),
status: pb.NewStatusServiceClient(conn),
capture: pb.NewCaptureServiceClient(conn),
mapping: pb.NewMappingServiceClient(conn),
eventType: pb.NewEventTypeServiceClient(conn),
binding: pb.NewBindingServiceClient(conn),
dispatch: pb.NewDispatchServiceClient(conn),
source: pb.NewSourceServiceClient(conn),
access: pb.NewAccessServiceClient(conn),
org: pb.NewOrgServiceClient(conn),
alert: pb.NewAlertServiceClient(conn),
audit: pb.NewAuditServiceClient(conn),
schedule: pb.NewScheduleServiceClient(conn),
controlPlane: controlpb.NewControlPlaneServiceClient(conn),
}
c.usageOutbox = newUsageOutbox(c.writeUsage)
return c
}
// ControlPlane returns the generic control-plane RPC client on the same authenticated connection.
func (c *Client) ControlPlane() controlpb.ControlPlaneServiceClient { return c.controlPlane }
// Close stops the usage retry worker, dropping any records still queued. The
// remote store service owns its own DB lifecycle.
func (c *Client) Close() error {
if c.usageOutbox != nil {
c.usageOutbox.stop()
}
return nil
}
var (
_ storecontract.HarnessSecretStore = (*Client)(nil)
_ task.Store = (*Client)(nil)
_ task.Caller = (*Client)(nil)
_ storecontract.TaskStore = (*Client)(nil)
_ storecontract.TaskLogStore = (*Client)(nil)
_ storecontract.CaptureStore = (*Client)(nil)
_ storecontract.MappingStore = (*Client)(nil)
_ storecontract.BindingStore = (*Client)(nil)
_ storecontract.SourceStore = (*Client)(nil)
_ storecontract.BindingDispatcher = (*Client)(nil)
_ storecontract.MappingMatchRecorder = (*Client)(nil)
_ storecontract.BindingTaskCreator = (*Client)(nil)
_ storecontract.PlaybookDispatcher = (*Client)(nil)
_ storecontract.ConfigSnapshotStore = (*Client)(nil)
_ storecontract.ChannelStatusStore = (*Client)(nil)
_ storecontract.ApplyStatusStore = (*Client)(nil)
_ storecontract.PresenceStore = (*Client)(nil)
_ storecontract.CaptureRefusalStore = (*Client)(nil)
_ storecontract.ExecutionCanceller = (*Client)(nil)
_ identity.Repository = (*Client)(nil)
_ storepkg.Installations = (*Client)(nil)
_ identity.PersonalTokens = (*Client)(nil)
)
// Lifecycle
// Tasks returns the most recently updated tasks.
func (c *Client) Tasks(ctx context.Context, limit int) ([]task.Task, error) {
return c.TasksPage(ctx, storecontract.TaskPage{Limit: limit})
}