Documentation
¶
Overview ¶
Package api implements the TasklaneService gRPC API over store.Store, the API-key interceptor and the HTTP/JSON gateway.
Index ¶
- Constants
- func AuthInterceptor(keys []string) grpc.UnaryServerInterceptor
- func GatewayDialOptions(tp trace.TracerProvider) []grpc.DialOption
- func NewGRPCServer(srv *Server, keys []string, log *slog.Logger) *grpc.Server
- func NewHTTPHandler(ctx context.Context, conn *grpc.ClientConn, ready func(context.Context) error, ...) (http.Handler, error)
- type Notifier
- type Option
- type Server
- func (s *Server) Ack(ctx context.Context, req *tasklanev1.AckRequest) (*tasklanev1.AckResponse, error)
- func (s *Server) CancelJob(ctx context.Context, req *tasklanev1.CancelJobRequest) (*tasklanev1.CancelJobResponse, error)
- func (s *Server) CreateQueue(ctx context.Context, req *tasklanev1.CreateQueueRequest) (*tasklanev1.CreateQueueResponse, error)
- func (s *Server) Enqueue(ctx context.Context, req *tasklanev1.EnqueueRequest) (*tasklanev1.EnqueueResponse, error)
- func (s *Server) EnqueueBatch(ctx context.Context, req *tasklanev1.EnqueueBatchRequest) (*tasklanev1.EnqueueBatchResponse, error)
- func (s *Server) GetJob(ctx context.Context, req *tasklanev1.GetJobRequest) (*tasklanev1.GetJobResponse, error)
- func (s *Server) GetQueue(ctx context.Context, req *tasklanev1.GetQueueRequest) (*tasklanev1.GetQueueResponse, error)
- func (s *Server) Heartbeat(ctx context.Context, req *tasklanev1.HeartbeatRequest) (*tasklanev1.HeartbeatResponse, error)
- func (s *Server) Lease(ctx context.Context, req *tasklanev1.LeaseRequest) (*tasklanev1.LeaseResponse, error)
- func (s *Server) ListDeadJobs(ctx context.Context, req *tasklanev1.ListDeadJobsRequest) (*tasklanev1.ListDeadJobsResponse, error)
- func (s *Server) Nack(ctx context.Context, req *tasklanev1.NackRequest) (*tasklanev1.NackResponse, error)
- func (s *Server) RedriveJobs(ctx context.Context, req *tasklanev1.RedriveJobsRequest) (*tasklanev1.RedriveJobsResponse, error)
- func (s *Server) RunDepthRefresh(ctx context.Context)
- func (s *Server) Shutdown()
Constants ¶
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.
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 ¶
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 ¶
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 ¶
WithMetrics records the server's metrics in m (design §16.2).
func WithPollInterval ¶
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 (*Server) Ack ¶
func (s *Server) Ack(ctx context.Context, req *tasklanev1.AckRequest) (*tasklanev1.AckResponse, error)
func (*Server) CancelJob ¶
func (s *Server) CancelJob(ctx context.Context, req *tasklanev1.CancelJobRequest) (*tasklanev1.CancelJobResponse, error)
func (*Server) CreateQueue ¶
func (s *Server) CreateQueue(ctx context.Context, req *tasklanev1.CreateQueueRequest) (*tasklanev1.CreateQueueResponse, error)
func (*Server) Enqueue ¶
func (s *Server) Enqueue(ctx context.Context, req *tasklanev1.EnqueueRequest) (*tasklanev1.EnqueueResponse, error)
func (*Server) EnqueueBatch ¶
func (s *Server) EnqueueBatch(ctx context.Context, req *tasklanev1.EnqueueBatchRequest) (*tasklanev1.EnqueueBatchResponse, error)
func (*Server) GetJob ¶
func (s *Server) GetJob(ctx context.Context, req *tasklanev1.GetJobRequest) (*tasklanev1.GetJobResponse, error)
func (*Server) GetQueue ¶
func (s *Server) GetQueue(ctx context.Context, req *tasklanev1.GetQueueRequest) (*tasklanev1.GetQueueResponse, error)
func (*Server) Heartbeat ¶
func (s *Server) Heartbeat(ctx context.Context, req *tasklanev1.HeartbeatRequest) (*tasklanev1.HeartbeatResponse, error)
func (*Server) Lease ¶
func (s *Server) Lease(ctx context.Context, req *tasklanev1.LeaseRequest) (*tasklanev1.LeaseResponse, error)
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 ¶
func (s *Server) ListDeadJobs(ctx context.Context, req *tasklanev1.ListDeadJobsRequest) (*tasklanev1.ListDeadJobsResponse, error)
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 (s *Server) Nack(ctx context.Context, req *tasklanev1.NackRequest) (*tasklanev1.NackResponse, error)
func (*Server) RedriveJobs ¶
func (s *Server) RedriveJobs(ctx context.Context, req *tasklanev1.RedriveJobsRequest) (*tasklanev1.RedriveJobsResponse, error)
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 ¶
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.