Documentation
¶
Index ¶
- Variables
- func Close() error
- func Init(ctx context.Context, backendName string) error
- func Publish(ctx context.Context, topic string, payload any, opts ...PublishOption) error
- func PublishTx(ctx context.Context, tx any, topic string, payload any, opts ...PublishOption) error
- func SetBackend(b Backend)
- func Start(ctx context.Context) error
- func Subscribe(topic string, handler func(ctx context.Context, msg []byte) error)
- type Backend
- type PublishConfig
- type PublishOption
- type Starter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotInitialized — Publish/Subscribe/Start called before Init. ErrNotInitialized = errors.New("queue: not initialized, call Init first") // ErrUnknownBackend — unsupported backend name passed to Init. ErrUnknownBackend = errors.New("queue: unknown backend") )
var ErrTxUnsupported = errors.New("queue: tx-bound publish not supported by current backend")
ErrTxUnsupported is returned by Backend.PublishTx when the active backend does not support transaction-bound publishing (memory backend).
Functions ¶
func Init ¶
Init initializes the queue with the memory backend. For durable backends (postgres, redis, etc.) callers use SetBackend(b) after yongol-generated code constructs the Backend implementation against the user's sqlc Queries.
Signature accepts backendName for backward-compatible call sites — only "memory" is accepted from inside ssac. Any other name (including "postgres") returns ErrUnknownBackend; the caller must instead call SetBackend(externalImpl) directly.
func Publish ¶
Publish serializes payload to JSON and delegates to the active Backend. Returns ErrNotInitialized if neither Init nor SetBackend has run.
func PublishTx ¶
PublishTx enqueues payload on topic inside the caller's transaction. The tx parameter is driver-neutral (any); the active Backend asserts the expected concrete type — typically pgx.Tx for the postgres backend or *sql.Tx for legacy database/sql. The memory backend returns ErrTxUnsupported.
Atomicity: on Commit the row becomes visible to pollers; on Rollback no trace remains. The caller is responsible for the commit/rollback.
func SetBackend ¶
func SetBackend(b Backend)
SetBackend installs an externally constructed Backend (e.g. a yongol- generated postgres implementation). Use this from main.go after building the backend from the user's sqlc Queries:
q := db.New(pool) queue.SetBackend(postgresqueue.NewPostgres(q))
Handlers registered via Subscribe before SetBackend survive the swap.
Types ¶
type Backend ¶
type Backend interface {
// Publish enqueues a serialized payload on topic with the supplied
// delivery config. The memory backend dispatches handlers synchronously;
// durable backends persist the row and return.
Publish(ctx context.Context, topic string, data []byte, cfg PublishConfig) error
// PublishTx enqueues inside the caller's transaction. tx is driver-
// specific; backends that do not support transactional publishing
// (e.g. memory) return ErrTxUnsupported.
PublishTx(ctx context.Context, tx any, topic string, data []byte, cfg PublishConfig) error
}
Backend is the interface implemented by queue backends. The package-level Publish/PublishTx functions delegate to the currently installed Backend.
memory backend ships in ssac for tests and zero-config dev. A postgres (or other durable) backend is provided by yongol codegen from the user's sqlc Queries via pkg/queue/interface.yaml ports (QueuePublish / QueuePoll / QueueAck). ssac itself never imports database/sql or pgx.
PublishTx accepts tx as `any` so both database/sql (*sql.Tx) and jackc/pgx (pgx.Tx) implementations are representable without ssac binding to a specific driver. The concrete Backend asserts the expected type.
type PublishConfig ¶
PublishConfig holds options applied to a single Publish / PublishTx call. Exported because Backend implementations provided outside the queue package (e.g. yongol-generated postgres adapter) must be able to name the config type in their method signatures.
Fields are exported for the same reason — external implementations need read access to Priority / Delay when translating into their own driver calls (e.g. priority ordering, deliver_at offset). Callers still configure via WithPriority / WithDelay options; direct struct literals are reserved for tests and in-package wiring.
type PublishOption ¶
type PublishOption func(*PublishConfig)
PublishOption configures a Publish call.
func WithDelay ¶
func WithDelay(seconds int) PublishOption
WithDelay sets the delivery delay in seconds.
func WithPriority ¶
func WithPriority(p string) PublishOption
WithPriority sets the message priority ("high", "normal", "low").