watch

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 20 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 watch is `email watch`: a long-running process that waits for new mail and wakes the agent with one HTTP notification per batch, so the agent spends no tokens while nothing arrives.

Each account runs on its own: IMAP accounts wait with IDLE on INBOX (falling back to polling), the others poll their provider's sync cursor. New messages go through the watch policy filters into a per-account buffer; a batch is sent once the debounce window after its first message ends. Watch reads metadata only and never changes or sends mail.

Index

Constants

View Source
const MaxPayloadMessages = 20

MaxPayloadMessages is how many messages one notification lists; Count still gives the real number.

Variables

View Source
var DefaultRetries = []time.Duration{5 * time.Second, 30 * time.Second, 2 * time.Minute}

DefaultRetries are the waits before the 2nd, 3rd and 4th attempt.

Functions

This section is empty.

Types

type Account

type Account struct {
	Alias    string
	Provider string
	// Open connects. It runs again after every failure, so it should read
	// the account and its credentials afresh (a new login heals a
	// degraded account without a restart).
	Open func(ctx context.Context) (Source, error)
}

type Config

type Config struct {
	Accounts     []Account
	Filter       policy.WatchSection
	Notifier     Notifier
	Debounce     time.Duration
	PollInterval time.Duration
	StateDir     string
	HealthAddr   string // "" = no health endpoint
	Log          *slog.Logger
	Audit        *audit.Logger

	// NoIdle polls every account, also those whose server offers IDLE (for
	// servers that accept IDLE but never report new mail; see doctor).
	NoIdle bool

	// Zero values get the defaults below; tests shorten them.
	IdleMax       time.Duration // renew IDLE this often (Gmail drops it at ~29 min)
	BackoffMin    time.Duration
	BackoffMax    time.Duration
	DegradedRetry time.Duration // how often a degraded account is retried and logged
	IdlePause     time.Duration // polling time after IdleFailures failed IDLEs
	IdleFailures  int
	OfflineLimit  time.Duration // /healthz is 503 once every account is offline this long
}

type HTTPNotifier

type HTTPNotifier struct {
	URL     string
	Token   string
	Client  *http.Client    // nil: a client with a 30 s timeout
	Retries []time.Duration // nil: DefaultRetries
	Log     *slog.Logger
}

HTTPNotifier POSTs the payload as JSON, with the token in X-Gitlab-Token (the header the Hermes webhook adapter checks).

func (*HTTPNotifier) Notify

func (n *HTTPNotifier) Notify(ctx context.Context, p Payload) error

type Idler

type Idler interface {
	Idle(ctx context.Context, maxWait time.Duration) (bool, error)
}

Idler is a Source that can wait for new mail on the server (IMAP IDLE). It returns errs.Unsupported when the server cannot.

type Item

type Item struct {
	ID             string           `json:"id"`
	ThreadID       string           `json:"thread_id,omitempty"`
	From           provider.Address `json:"from"`
	Subject        string           `json:"subject"`
	Date           time.Time        `json:"date"`
	WebLink        string           `json:"web_link,omitempty"`
	HasAttachments bool             `json:"has_attachments"`
	Unread         bool             `json:"unread"`
}

Item is the metadata of one new message, as the notification carries it. Bodies are never read: the agent fetches one with `email get`.

type Notifier

type Notifier interface {
	Notify(ctx context.Context, p Payload) error
}

Notifier delivers a batch. An error means the batch is lost.

type Payload

type Payload struct {
	Source   string `json:"source"`
	Account  string `json:"account"`
	Count    int    `json:"count"`
	Messages []Item `json:"messages"`
}

Payload is the body of one notification. It covers one account.

type Source

type Source interface {
	Sync(ctx context.Context, state string) (*provider.SyncResult, error)
	Close() error
}

Source is one account's mailbox, as watch uses it. Sync returns the mail that arrived after state ("" sets a baseline).

type State added in v0.8.0

type State struct {
	Provider string
	Cursor   string
	Notified []string
	Pending  int
}

State is an account's watch state, as `email doctor` reports it.

func ReadState added in v0.8.0

func ReadState(path string) (*State, error)

ReadState reads a watch state file. A missing file is fs.ErrNotExist.

type Watcher

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

func New

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

func (*Watcher) Handler

func (w *Watcher) Handler() http.Handler

func (*Watcher) Health

func (w *Watcher) Health() (int, any)

Health is 200 while at least one account is online, and also while all are offline for less than OfflineLimit (starting up, a short outage); then 503. The body lists every account's state.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context) error

Run watches until ctx ends, then closes the connections, saves state and returns nil. It fails only when it cannot start (state dir, health port).

type WriterNotifier

type WriterNotifier struct {
	W io.Writer
	// contains filtered or unexported fields
}

WriterNotifier prints each payload as one JSON line (--dry-run).

func (*WriterNotifier) Notify

func (n *WriterNotifier) Notify(_ context.Context, p Payload) error

Jump to

Keyboard shortcuts

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