Documentation
¶
Overview ¶
Package eventaccel is the optional event accelerator of the manifest reader: an S3 event notification that names the manifest 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 ¶
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.
const ( // ResultMatched is a message about a write of the manifest 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.
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 manifest 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.
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 ¶
var ErrAlreadyRunning = errors.New("eventaccel: already running")
ErrAlreadyRunning is returned by a second call of Accelerator.Run.
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.
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 (*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 {
// Queue delivers the notifications.
Queue Queue
// Poller is the manifest reader.
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
// Bucket and Key name the manifest object: a notification about another object is skipped.
Bucket string
Key string
// 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. etag is the ETag the notification names, "" when it names none; the reader answers
// false when it has read that ETag already, when it is waiting after failed polls, or when it is not polling.
Nudge(etag string) bool
}
Poller is the manifest reader 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 ¶
NewSQSQueue returns the queue at queueURL, read with client.
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.