dtmx

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package dtmx defines Kernel's distributed transaction abstraction.

dtmx is intentionally provider-neutral. Business packages depend on this package only, while concrete implementations live in subpackages such as dtmx/dtm. The first production implementation is backed by dtm-labs/dtm via the official Go HTTP SDK.

Import an implementation once during bootstrap:

import _ "github.com/aisphereio/kernel/dtmx/dtm"

Then open it from config:

manager, err := dtmx.New(dtmx.Config{
    Enabled: true,
    Driver:  "dtm",
    Server:  "http://127.0.0.1:36789/api/dtmsvr",
})

Kernel services should model cross-resource workflows as Saga branches with idempotent action and compensation endpoints. Large binary payloads must not be put into DTM request bodies; put objects into object storage first and pass only object keys, hashes, sizes, and metadata through Saga payloads.

Index

Constants

View Source
const (
	DriverDTM = "dtm"

	ProtocolHTTP = "http"
)
View Source
const (
	CodeDisabled            = errorx.Code("DTMX_DISABLED")
	CodeInvalidConfig       = errorx.Code("DTMX_INVALID_CONFIG")
	CodeUnknownDriver       = errorx.Code("DTMX_UNKNOWN_DRIVER")
	CodeInvalidSaga         = errorx.Code("DTMX_INVALID_SAGA")
	CodeUnsupportedProtocol = errorx.Code("DTMX_UNSUPPORTED_PROTOCOL")
	CodeSubmitFailed        = errorx.Code("DTMX_SUBMIT_FAILED")
	CodeNewGIDFailed        = errorx.Code("DTMX_NEW_GID_FAILED")
	CodeTimeout             = errorx.Code("DTMX_TIMEOUT")
	CodeCanceled            = errorx.Code("DTMX_CANCELED")
	CodeBranchUnauthorized  = errorx.Code("DTMX_BRANCH_UNAUTHORIZED")
)
View Source
const (
	// BranchAuthHeader is the default header used to authenticate DTM -> service
	// branch callbacks. dtmx/dtm sends this header when Config.BranchSecret is
	// configured, and BranchAuthMiddleware validates it on callback handlers.
	BranchAuthHeader = "X-Kernel-DTM-Branch-Token"
)

Variables

View Source
var (
	ErrDisabled            = errors.New("dtmx: distributed transaction is disabled")
	ErrInvalidConfig       = errors.New("dtmx: invalid config")
	ErrUnknownDriver       = errors.New("dtmx: unknown driver")
	ErrNoSteps             = errors.New("dtmx: saga has no steps")
	ErrInvalidBranch       = errors.New("dtmx: invalid saga branch")
	ErrGIDRequired         = errors.New("dtmx: gid is required")
	ErrUnsupportedProtocol = errors.New("dtmx: unsupported transaction protocol")
	ErrSubmitFailed        = errors.New("dtmx: submit transaction failed")
	ErrNewGIDFailed        = errors.New("dtmx: create gid failed")
)
View Source
var ErrBranchUnauthorized = errorx.Unauthorized(CodeBranchUnauthorized, "dtm branch callback is unauthorized")

Functions

func BranchAuthMiddleware added in v0.0.9

func BranchAuthMiddleware(secret string, next http.Handler) http.Handler

BranchAuthMiddleware wraps internal DTM branch endpoints with shared-secret validation. It writes 401 and does not call next when validation fails.

func BuildBranchURL added in v0.0.9

func BuildBranchURL(base, prefix, branchPath string) string

func Inject added in v0.0.9

func Inject(ctx context.Context, manager Manager) context.Context

func IsDriverRegistered added in v0.0.9

func IsDriverRegistered(name string) bool

func RegisterDriver added in v0.0.9

func RegisterDriver(name string, opener DriverOpener)

func RegisteredDrivers added in v0.0.9

func RegisteredDrivers() []string

func ValidateBranchRequest added in v0.0.9

func ValidateBranchRequest(r *http.Request, secret string) error

ValidateBranchRequest validates the default branch-auth header on an HTTP request. It is intentionally net/http only so kernel transport adapters and business services can use it without importing the DTM SDK.

func ValidateBranchToken added in v0.0.9

func ValidateBranchToken(expected, got string) error

ValidateBranchToken validates a received DTM branch token using constant-time comparison. Empty expected means branch authentication is disabled; this is allowed for local development, but production services should normally set a BranchSecret or protect branch endpoints with mTLS/private networking.

Types

type Config

type Config struct {
	// Enabled controls whether distributed transactions are active. When false,
	// New returns a disabled no-op Manager that fails fast on SubmitSaga.
	Enabled bool `json:"enabled" yaml:"enabled"`

	// Driver selects the registered implementation. The production driver is
	// "dtm" from package github.com/aisphereio/kernel/dtmx/dtm.
	Driver string `json:"driver" yaml:"driver"`

	// Protocol selects how DTM calls branch endpoints. Currently "http" is
	// implemented; gRPC can be added without changing business code.
	Protocol string `json:"protocol" yaml:"protocol"`

	// Server is the DTM HTTP API endpoint, e.g.
	// http://127.0.0.1:36789/api/dtmsvr.
	Server string `json:"server" yaml:"server"`

	// ServiceBaseURL is the externally reachable base URL for this service.
	// Helpers use it to build branch callback URLs.
	ServiceBaseURL string `json:"service_base_url" yaml:"service_base_url"`

	// BranchPrefix is the prefix for internal branch endpoints, e.g.
	// /internal/dtm. It is optional and used only by BranchURL helper.
	BranchPrefix string `json:"branch_prefix" yaml:"branch_prefix"`

	// WaitResult asks DTM to wait for Saga execution result on Submit. Use true
	// in synchronous user-facing flows; false in fire-and-retry background flows.
	WaitResult bool `json:"wait_result" yaml:"wait_result"`

	// Timeout bounds the local SubmitSaga call. DTM itself still owns branch
	// retry/timeout behavior.
	Timeout time.Duration `json:"timeout_ns" yaml:"timeout_ns"`

	// BranchSecret is propagated to DTM as a branch header. DTM then forwards it
	// to branch action/compensate endpoints, where ValidateBranchRequest or
	// BranchAuthMiddleware can verify the callback. Keep it empty only when the
	// branch endpoints are protected by stronger network controls such as mTLS
	// and private networking.
	BranchSecret string `json:"branch_secret" yaml:"branch_secret"`

	// Logger is the component logger. If nil, dtmx uses logx.DefaultLogger().
	Logger logx.Logger `json:"-" yaml:"-"`

	// Metrics is the optional metrics manager.
	Metrics metricsx.Manager `json:"-" yaml:"-"`

	// MetricsEnabled controls whether dtmx records metrics.
	MetricsEnabled bool `json:"metrics_enabled" yaml:"metrics_enabled"`
}

Config configures Kernel distributed transactions.

This config is for the application-side transaction manager client. The DTM server itself is an external process managed by deployment; kernel only needs the DTM API endpoint and service callback address.

func (Config) Validate

func (c Config) Validate() error

type DriverOpener added in v0.0.9

type DriverOpener func(Config) (Manager, error)

type Manager added in v0.0.9

type Manager interface {
	Enabled() bool
	DriverName() string
	NewGID(ctx context.Context) (string, error)
	SubmitSaga(ctx context.Context, saga Saga) (TransactionResult, error)
	BranchURL(path string) string
	Close() error
}

Manager is the business-facing distributed transaction interface.

Business modules should depend on Manager, Saga, and SagaStep only. They should not import github.com/dtm-labs/client directly.

func Disabled added in v0.0.9

func Disabled() Manager

func FromContext added in v0.0.9

func FromContext(ctx context.Context) Manager

func FromContextOr added in v0.0.9

func FromContextOr(ctx context.Context, fallback Manager) Manager

func New

func New(cfg Config) (Manager, error)

type Saga

type Saga struct {
	GID   string
	Name  string
	Steps []SagaStep

	// Options override Config defaults for a single Saga.
	Options SagaOptions
}

func NewSaga

func NewSaga(gid, name string, opts ...SagaOption) Saga

func (Saga) Add

func (s Saga) Add(step SagaStep) Saga

func (Saga) AddHTTP added in v0.0.9

func (s Saga) AddHTTP(name, action, compensate string, payload any) Saga

func (Saga) Validate added in v0.0.9

func (s Saga) Validate() error

type SagaOption added in v0.0.9

type SagaOption func(*Saga)

func WithSagaBranchHeader added in v0.0.9

func WithSagaBranchHeader(key, value string) SagaOption

WithSagaBranchHeader adds a header that DTM forwards to every branch action/compensate request for this Saga. Prefer Config.BranchSecret for the common shared-secret case; use this for per-transaction metadata only.

func WithSagaBranchHeaders added in v0.0.9

func WithSagaBranchHeaders(headers map[string]string) SagaOption

WithSagaBranchHeaders adds headers that DTM forwards to every branch action/compensate request for this Saga.

func WithSagaTimeout added in v0.0.9

func WithSagaTimeout(timeout time.Duration) SagaOption

func WithSagaWaitResult added in v0.0.9

func WithSagaWaitResult(wait bool) SagaOption

type SagaOptions added in v0.0.9

type SagaOptions struct {
	WaitResult    *bool
	Timeout       time.Duration
	BranchHeaders map[string]string
}

type SagaStep added in v0.0.9

type SagaStep struct {
	Name       string
	Action     string
	Compensate string
	Payload    any
}

type TransactionResult added in v0.0.9

type TransactionResult struct {
	GID       string
	Protocol  string
	Pattern   string
	Submitted bool
	StartedAt time.Time
	Elapsed   time.Duration
}

Directories

Path Synopsis
Package dtm registers the dtm-labs/dtm implementation for dtmx.
Package dtm registers the dtm-labs/dtm implementation for dtmx.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL