batch

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 22 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 batch sends the requests of messages send concurrently.

A BatchProcessor runs send jobs, each one request of up to 100 recipients, on as many workers as its caller allows; messages send caps --max-concurrency at 10. It retries a request that failed with a retryable error after the Retry-After the API gave, up to 5 minutes, or else after one second more per attempt, up to 30 seconds, plus a random part of a second, so that workers whose requests failed together do not retry together. Once a request with attachments was too large for the API (HTTP 413), it sends no more: they carry the same attachments. It reports each finished job to a progress.Reporter, and saves the recipients that failed, and those whose outcome is unknown, to files they can be sent again from. It does no rate limiting of its own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchProcessor

type BatchProcessor struct {

	// Notices, when set, is where a long wait before a retry is announced,
	// such as stderr.
	Notices io.Writer
	// contains filtered or unexported fields
}

BatchProcessor handles batch message sending with concurrency and error handling

func NewBatchProcessor

func NewBatchProcessor(client client.AhaSendClient, maxConcurrency, maxRetries int, progressReporter *progress.Reporter) *BatchProcessor

NewBatchProcessor creates a new batch processor

func (*BatchProcessor) ProcessJobs

func (bp *BatchProcessor) ProcessJobs(ctx context.Context, jobs []*SendJob) (*BatchResult, error)

ProcessJobs processes a batch of send jobs with controlled concurrency

type BatchResult

type BatchResult struct {
	TotalRecipients      int               // Total number of recipients processed
	SuccessfulRecipients int               // Individual recipients sent successfully
	FailedRecipients     []FailedRecipient // Recipients of failed requests and recipients the API did not accept
	NotSentRecipients    []FailedRecipient // Recipients of jobs an interruption or a request too large for the API kept from being sent
	UnknownRecipients    []FailedRecipient // Recipients of jobs whose outcome is unknown
	Interrupted          bool              // Whether an interruption kept a job from being sent or cut its retries short
	TooLarge             bool              // Whether a request was too large for the API, which kept the rest from being sent
	Results              []*SendResult     // One result per job, in BatchIndex order
	Stats                progress.Stats
	// FailedRecipientsFile is where the failed and not sent recipients were
	// saved to be sent again, if they were.
	FailedRecipientsFile string
	// FailedSaveError is why the failed and not sent recipients could not be
	// saved, if they could not.
	FailedSaveError error
	// UnknownRecipientsFile is where the recipients whose outcome is unknown
	// were saved, if they were.
	UnknownRecipientsFile string
	// UnknownSaveError is why the recipients whose outcome is unknown could
	// not be saved, if they could not.
	UnknownSaveError error
}

BatchResult contains the overall batch operation results

type FailedRecipient

type FailedRecipient struct {
	Email            string                 `json:"email"`
	Name             string                 `json:"name,omitempty"`
	SubstitutionData map[string]interface{} `json:"substitution_data,omitempty"`
	Error            string                 `json:"_error"`
	// ErrorCode is the HTTP status of a failed request. It is omitted when
	// the API did not answer, or accepted the request but not the recipient.
	ErrorCode int  `json:"_error_code,omitempty"`
	Retryable bool `json:"_retryable"`
	// IdempotencyKey is the idempotency key of the request of a recipient
	// whose outcome is unknown, which sends it again safely.
	IdempotencyKey string `json:"_idempotency_key,omitempty"`
}

FailedRecipient represents a recipient that failed to send

func RejectedRecipients added in v0.3.0

func RejectedRecipients(result *SendResult) []FailedRecipient

RejectedRecipients returns the recipients that a sent request's response reports with status "error", with the API's error text. Each keeps the name and substitutions it was sent with, so the failed recipients file can be sent again. The API answers with one item per recipient in the order of the request, so an item is matched to the recipient at its position, which tells apart two entries of one address; a response of another length is matched to the recipient each item echoes.

type Outcome added in v0.3.0

type Outcome int

Outcome is what became of a send job.

const (
	// OutcomeSent: the API accepted the request.
	OutcomeSent Outcome = iota
	// OutcomeFailed: the request failed, so none of its recipients was sent.
	OutcomeFailed
	// OutcomeUnknown: the job's last request may have been sent, so its
	// recipients may or may not have been sent: the API was still
	// processing an earlier request with the job's idempotency key, a
	// gateway answered 502 or 504, the request was written but no answer
	// came, or the API accepted it with an answer the CLI cannot read.
	OutcomeUnknown
	// OutcomeNotSent: the job's request did not go out: the send was
	// interrupted before it, or stopped after an earlier request of the send
	// was too large for the API.
	OutcomeNotSent
)

type RetryAfterTooLongError added in v0.3.0

type RetryAfterTooLongError struct {
	Seconds int
	Err     error
}

RetryAfterTooLongError is the failure of a request the API asked to retry after more than maxRetryAfterSeconds. It wraps the API's error, so it exits the same way.

func (*RetryAfterTooLongError) Error added in v0.3.0

func (e *RetryAfterTooLongError) Error() string

func (*RetryAfterTooLongError) NamesTheWait added in v0.3.0

func (e *RetryAfterTooLongError) NamesTheWait()

NamesTheWait marks the error as a printer.WaitNamer: its text names the wait the API asked for.

func (*RetryAfterTooLongError) Unwrap added in v0.3.0

func (e *RetryAfterTooLongError) Unwrap() error

type SendJob

type SendJob struct {
	Request         *requests.CreateMessageRequest
	TemplateRequest *requests.CreateTemplateMessageRequest
	IdempotencyKey  string
	BatchIndex      int                // Batch number (0, 1, 2, ...)
	Recipients      []common.Recipient // Multiple recipients in this batch (max 100)
}

SendJob represents a batch message send operation (up to 100 recipients). A job sets exactly one of Request, for an inline send, and TemplateRequest, for a stored template send.

type SendResult

type SendResult struct {
	Job       *SendJob
	Response  *responses.CreateMessageResponse
	Error     error
	Outcome   Outcome
	Retryable bool
	// Interrupted is whether the send was interrupted before the job's
	// request went out, or while the job waited to retry, so its retries
	// were cut short.
	Interrupted bool
}

SendResult represents the result of a send operation

Jump to

Keyboard shortcuts

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