api

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 43 Imported by: 0

Documentation

Overview

Package api implements the TasklaneService gRPC API over store.Store, the API-key interceptor and the HTTP/JSON gateway.

Index

Constants

View Source
const (
	// MaxPayloadBytes caps one job's payload twice: as encoded JSON (what the
	// database stores) and as a protobuf Struct (what Lease sends back). The
	// Struct is the larger of the two, up to about 6x for number-heavy JSON.
	MaxPayloadBytes = 256 << 10
	// MaxMessageBytes is the gRPC send and receive limit on the server and
	// on the gateway's client connection, and the HTTP request body limit.
	MaxMessageBytes = 16 << 20
	// MaxLeaseJobs caps one Lease: larger max_jobs values are clamped to it.
	// Because MaxPayloadBytes bounds the protobuf size, MaxLeaseJobs jobs,
	// each at most MaxPayloadBytes plus up to 16 KiB of other fields
	// (last_error alone can be 4 KiB), fit in MaxMessageBytes (checked by
	// TestFullLeaseFitsInOneMessage): 50 × 272 KiB ≈ 13.3 MiB.
	MaxLeaseJobs = 50
)

Request and response limits (design §6, Limits). They keep every response within the gRPC message size both ends are configured for, and turn values the database can't store into INVALID_ARGUMENT instead of INTERNAL.

View Source
const (
	// MaxWait caps a long-polling Lease (design §6).
	MaxWait = 30 * time.Second
	// DefaultPollInterval is how often a waiting Lease re-checks without a
	// notification: delayed jobs and retries become due silently.
	DefaultPollInterval = time.Second
)

Variables

This section is empty.

Functions

func AuthInterceptor

func AuthInterceptor(keys []string) grpc.UnaryServerInterceptor

AuthInterceptor requires metadata "authorization: Bearer <key>" with one of keys; the scheme is case-insensitive. With no keys configured, every call is allowed.

func GatewayDialOptions

func GatewayDialOptions(tp trace.TracerProvider) []grpc.DialOption

GatewayDialOptions are the options for the gateway's connection to the gRPC server: the message limits, and trace propagation, so a traceparent header on an HTTP call reaches the gRPC server span.

func NewGRPCServer

func NewGRPCServer(srv *Server, keys []string, log *slog.Logger) *grpc.Server

NewGRPCServer returns a gRPC server with the API registered behind the API-key interceptor, sending and receiving messages up to MaxMessageBytes. With no keys it logs a warning: auth is disabled.

func NewHTTPHandler

func NewHTTPHandler(ctx context.Context, conn *grpc.ClientConn, ready func(context.Context) error, keys []string, tp trace.TracerProvider) (http.Handler, error)

NewHTTPHandler serves the HTTP/JSON gateway, which forwards each call over conn to the gRPC server (so the auth interceptor applies to HTTP too; the gateway passes the Authorization header on as "authorization" metadata), plus /healthz (the process is up) and /readyz (ready reports the database reachable).

Gateway request bodies are limited to MaxMessageBytes (413 beyond it). With keys configured, a gateway request without an Authorization header gets a 401 before its body is read; the gRPC interceptor still checks the key.

Types

type Notifier

type Notifier interface {
	Subscribe(queue string) (<-chan struct{}, func())
	Publish(queue string)
}

Notifier wakes waiting leases when a queue gets new jobs; *notify.Publisher implements it. Publish is called after the store write has committed, and only when it made jobs runnable; it must not block.

type Option

type Option func(*Server)

func WithMetrics

func WithMetrics(m *metrics.Metrics) Option

WithMetrics records the server's metrics in m (design §16.2).

func WithPollInterval

func WithPollInterval(d time.Duration) Option

WithPollInterval overrides DefaultPollInterval (tests use it). A d <= 0 is ignored.

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider records the server's spans with tp (design §18). The default is the global provider, a no-op unless the program sets one.

type Server

type Server struct {
	tasklanev1.UnimplementedTasklaneServiceServer
	// contains filtered or unexported fields
}

func NewServer

func NewServer(st store.Store, n Notifier, log *slog.Logger, opts ...Option) *Server

func (*Server) Ack

func (*Server) CancelJob

func (*Server) Enqueue

func (*Server) GetJob

func (*Server) GetQueue

func (*Server) Heartbeat

func (*Server) Lease

Lease tries once; with wait > 0 and nothing runnable, it waits for a notification for the queue, or the next poll tick, and tries again until the wait elapses. It subscribes before the first try, so a job enqueued between that try and the wait still wakes it. max_jobs above MaxLeaseJobs is clamped to it silently, so the response fits in MaxMessageBytes.

func (*Server) ListDeadJobs

ListDeadJobs asks the store for one job more than the page holds, so it knows whether another page exists and the last page never carries a token.

func (*Server) Nack

func (*Server) RedriveJobs

RedriveJobs with all = true runs batch after batch until one comes back short. It is not atomic: if it fails halfway, the committed batches stay redriven, and calling it again finishes the rest.

func (*Server) RunDepthRefresh

func (s *Server) RunDepthRefresh(ctx context.Context)

RunDepthRefresh recounts queue depths until ctx is cancelled, the first time immediately and then after nextDepthRefresh of the last count's duration, so a huge backlog can't make counting eat the database. A failed refresh waits only depthRefreshInterval so recovery is quick. Until it has run once, Enqueue refuses nothing.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown makes waiting leases return their (empty) result now, so a graceful stop doesn't wait out 30 s long polls, and later leases fail with UNAVAILABLE, so workers back off instead of polling a draining server. Call it before grpc.Server.GracefulStop.

Jump to

Keyboard shortcuts

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