Documentation
¶
Overview ¶
Package agent implements the outbound-only WebSocket worker loop connecting EdgeFetch standby nodes (Docker containers, Home Assistant OS add-ons, Raspberry Pis, or bare-metal binaries) to Cloudflare's Hibernating WebSocket Durable Object (AgentHubDO).
Architecture & Security Invariants ¶
- Zero inbound ports: nodes initiate outbound TLS 1.3 WebSockets (`wss://<hub>/agent/ws`) authenticated via `Authorization: Bearer <agent_token>` (>= 24 characters) and self-identify capabilities (`http2-v1`, `scrape-v2`, `llms-v1`, `browser-v1`, `interact-v1`, `pdf-v1`) via `X-Agent-*` headers and `agent_hello`.
- Hibernation keepalive: sends lightweight `"ping"` text frames so Cloudflare's edge answers `"pong"` via `setWebSocketAutoResponse` at $0 Durable Object wall-clock duration cost while idle.
- Out-of-band R2 streaming: when `artifacts.mode == "r2"`, response bodies are gzipped, SHA-256 hashed, and uploaded directly via `PUT /agent/artifacts/:blobSha256` before the metadata envelope is sent to `POST /agent/reply`, preventing multi-megabyte payloads from passing through Durable Object memory.
- Graceful SIGTERM drain: active jobs continue up to `ShutdownDrain` (default 25s) before container exit.
Index ¶
Constants ¶
const ( DefaultPingInterval = 30 * time.Second DefaultReadWatchdog = 50 * time.Second DefaultHealthyResetDur = 30 * time.Second DefaultShutdownDrain = 25 * time.Second DefaultUploadTimeout = 45 * time.Second DefaultShutdownUploadTimeout = 10 * time.Second )
const ( DefaultHAOptionsPath = "/data/options.json" DefaultPauseMs = 3000 DefaultPauseJitterMs = 1500 MinTokenLength = 24 )
Variables ¶
var Version = "2.7.8"
Version is the EdgeFetch binary/add-on release version reported in X-Agent-Version and agent_hello. Overridable at build time via -ldflags="-X github.com/bartrumb/edgefetch/v2/internal/agent.Version=...".
Functions ¶
func Run ¶
func Run( ctx context.Context, cfg Config, crawler *crawl.Crawler, allow func(*url.URL) error, hubTransport http.RoundTripper, log *slog.Logger, rtOpts RuntimeOpts, ) error
Run maintains a persistent outbound WebSocket session to `cfg.HubURL/agent/ws`, automatically reconnecting with full-jitter exponential backoff on network drops and draining in-flight work cleanly on `SIGTERM`.
func RunAll ¶
func RunAll( ctx context.Context, cfg Config, crawler *crawl.Crawler, allow func(*url.URL) error, hubTransport http.RoundTripper, log *slog.Logger, rtOpts RuntimeOpts, ) error
RunAll maintains persistent outbound WebSocket sessions to every controller in `cfg.HubURLs` (falling back to `cfg.HubURL`), sharing one `*crawl.Crawler` and `*politeness.Registry` across active-active controllers with zero coordination overhead.
Types ¶
type Config ¶
type Config struct {
HubURL string `json:"hub_url"`
HubURLs []string `json:"hub_urls,omitempty"`
AgentToken string `json:"agent_token"`
SiteID string `json:"site_id"`
UserAgent string `json:"user_agent"`
PauseMs int `json:"pause_ms"`
PauseJitterMs int `json:"pause_jitter_ms"`
AllowedHosts []string `json:"allowed_hosts"`
RobotsBypassHosts []string `json:"robots_bypass_hosts,omitempty"`
AllowNoSandboxBrowser bool `json:"allow_no_sandbox_browser,omitempty"`
}
Config holds validated settings for outbound `agent` mode (including multi-controller HA via `HubURLs`).
func LoadConfig ¶
LoadConfig loads `/data/options.json` (when present) and overlays `EDGEFETCH_*` (or `CRAWLER_*`) environment variables.
type RuntimeOpts ¶
type RuntimeOpts struct {
PingInterval time.Duration
ReadWatchdog time.Duration
HealthyResetDur time.Duration
ShutdownDrain time.Duration
UploadTimeout time.Duration
ShutdownUploadTimeout time.Duration
WarmUpOnStart bool
OnConnected func()
}
RuntimeOpts allows integration tests to shorten keepalive/drain timers without altering production defaults.