eventaccel

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package eventaccel is the optional event accelerator of the polling flag source: an S3 event notification that names the watched object, delivered through an SQS queue, makes the reader poll now instead of at the end of its interval.

The accelerator only asks. The reader polls on its own schedule whatever it hears, through the one loop that polls (the same reader, the same client of the connection); a notification that is lost, duplicated, malformed, about another object or another bucket, or a queue that is down, changes nothing of the polling.

Index

Constants

View Source
const (
	// StateStarting is the state before the first answer of the queue.
	StateStarting = "starting"
	// StateListening is the state while the queue answers.
	StateListening = "listening"
	// StateUnreachable is the state while the queue does not answer: the polling goes on as it is.
	StateUnreachable = "unreachable"
	// StateDegraded is the state while the queue answers but the messages that were read can not be deleted: they come
	// back after the visibility timeout, and the reader turns them down. The polling goes on as it is.
	StateDegraded = "degraded"
	// StateStopped is the state after [Accelerator.Run] returned, or after a failure inside the accelerator that
	// stopped it.
	StateStopped = "stopped"
)

The state of the accelerator, in Status.State.

View Source
const (
	// ResultMatched is a message about a write of the watched object.
	ResultMatched = "matched"
	// ResultSkipped is a valid message that asks for nothing: another object or bucket, another kind of event, a test.
	ResultSkipped = "skipped"
	// ResultMalformed is a message that is not an S3 event notification.
	ResultMalformed = "malformed"
)

The results of a message, in Status.Messages.

View Source
const (
	// PollRequested is a poll that the reader was asked for.
	PollRequested = "requested"
	// PollUnneeded is a request that the reader turned down: it had read that object already, it was waiting after
	// failed polls, or it was not polling.
	PollUnneeded = "unneeded"
)

The results of the request of a poll, in Status.Polls.

View Source
const (
	// DefaultMinInterval is the shortest time between two requests of a poll, and the window in which the
	// notifications of a burst are one.
	DefaultMinInterval = 10 * time.Second
	// MinMinInterval is the shortest interval of [Config.MinInterval].
	MinMinInterval = time.Second
	// MaxMinInterval is the longest interval of [Config.MinInterval].
	MaxMinInterval = time.Hour
)

Variables

View Source
var ErrAlreadyRunning = errors.New("eventaccel: already running")

ErrAlreadyRunning is returned by a second call of Accelerator.Run.

View Source
var ErrDeleteFailed = errors.New("eventaccel: the queue did not delete some messages")

ErrDeleteFailed is the error of a delete that the queue answered but did not do for some of the messages.

View Source
var ErrInvalidConfig = errors.New("eventaccel: invalid configuration")

ErrInvalidConfig is wrapped by the error of New for a configuration the accelerator can not run with.

Functions

This section is empty.

Types

type Accelerator

type Accelerator struct {
	// contains filtered or unexported fields
}

Accelerator turns the notifications of a queue into requests of a poll. Build it with New, run it once with Accelerator.Run. It never changes how the reader polls: it asks, and the reader decides.

func New

func New(cfg *Config) (*Accelerator, error)

New builds an accelerator.

func (*Accelerator) Run

func (a *Accelerator) Run(ctx context.Context) error

Run reads the queue until ctx ends, and returns nil then; no goroutine outlives it. A queue that is down, a message that does not parse or a failure inside the accelerator never reach the reader: the accelerator logs, counts, and goes on (or stops, for a failure inside it). It can be called once.

func (*Accelerator) Status

func (a *Accelerator) Status() Status

Status returns the state and the counts. It never blocks.

type Config

type Config struct {
	// Tracing are the OpenTelemetry providers of the accelerator: with them enabled every batch of messages that a
	// receive brings has a span of its own ("eventaccel.messages"), the parent of the delete request; an empty long
	// poll has none. The zero value records nothing.
	Tracing tracing.Providers
	// Queue delivers the notifications.
	Queue Queue
	// Poller is the change source.
	Poller Poller
	// Logger receives the logs; nil discards them.
	Logger *slog.Logger
	// Wait is the one timer of the accelerator: it blocks for d or until ctx ends, and returns the error of ctx then.
	// A test replaces it. nil is a timer.
	Wait func(ctx context.Context, d time.Duration) error
	// Jitter returns a value in [0, 1) that spreads the wait after a failed receive; nil is a source of the runtime.
	Jitter func() float64
	// Watches name the places to look at: a notification about an object that no watch holds is skipped.
	Watches []Watch
	// MinInterval is the window of a burst and the shortest time between two requests of a poll; zero is
	// [DefaultMinInterval].
	MinInterval time.Duration
}

Config configures an Accelerator.

type Message

type Message struct {
	// Body is the text of the message.
	Body string
	// Handle identifies the message for [Queue.Delete].
	Handle string
}

Message is one message of the queue.

type Poller

type Poller interface {
	// Nudge asks for a poll now. It answers false when the source is waiting after failed polls.
	Nudge() bool
}

Poller is the change source as the accelerator sees it: the one way to ask for a poll.

type Queue

type Queue interface {
	// Receive waits for messages (long polling) and returns them; it returns when ctx ends.
	Receive(ctx context.Context) ([]Message, error)
	// Delete removes the messages that were read, so that the queue does not deliver them again.
	Delete(ctx context.Context, messages []Message) error
}

Queue is the queue that the notifications arrive in.

type SQS

type SQS interface {
	ReceiveMessage(
		ctx context.Context, in *sqs.ReceiveMessageInput, opts ...func(*sqs.Options),
	) (*sqs.ReceiveMessageOutput, error)
	DeleteMessageBatch(
		ctx context.Context, in *sqs.DeleteMessageBatchInput, opts ...func(*sqs.Options),
	) (*sqs.DeleteMessageBatchOutput, error)
}

SQS is the part of the SQS client that the queue uses; the client of the SDK is one.

type SQSQueue

type SQSQueue struct {
	// contains filtered or unexported fields
}

SQSQueue is a Queue on an SQS queue.

func NewSQSQueue

func NewSQSQueue(client SQS, queueURL string) *SQSQueue

NewSQSQueue returns the queue at queueURL, read with client.

func (*SQSQueue) Delete

func (q *SQSQueue) Delete(ctx context.Context, messages []Message) error

Delete removes the messages from the queue.

func (*SQSQueue) Receive

func (q *SQSQueue) Receive(ctx context.Context) ([]Message, error)

Receive long polls the queue for up to ten messages.

type Status

type Status struct {
	LastMessage time.Time `json:"lastMessage,omitzero"`
	// LastDeleteError is the time of the last failed delete of messages, zero for none.
	LastDeleteError time.Time         `json:"lastDeleteError,omitzero"`
	Messages        map[string]uint64 `json:"messages"`
	Polls           map[string]uint64 `json:"polls"`
	State           string            `json:"state"`
	ReceiveErrors   uint64            `json:"receiveErrors"`
	// DeleteErrors is the number of deletes of messages that failed.
	DeleteErrors uint64        `json:"deleteErrors"`
	MinInterval  time.Duration `json:"minInterval"`
}

Status is what the accelerator reports: its state and counts. It holds no queue address, no key and no secret.

type Watch added in v1.3.0

type Watch struct {
	Bucket string
	Prefix string
}

Watch is a place a notification may be about: the objects whose key starts with Prefix in the bucket.

Jump to

Keyboard shortcuts

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