Documentation
¶
Overview ¶
Package epbinding is an endpoint's binding (#2171): the HTTP route a managed script answers at /endpoints/{name}/..., who may call it, and the limits each request runs under. It holds the record, its validation, the store contract and the matching of a request path against the binding's template; the store is internal/endpoint/epstore and the request surface internal/endpoint/ephttp.
Index ¶
Constants ¶
const ( AuthPublic = "public" AuthSignedIn = "signed_in" )
Auth modes. A public endpoint answers anyone who passes the deployment's bot check; a signed-in one answers a browser session or an API key.
const ( DefaultTimeoutSeconds = 10 MaxTimeoutSeconds = 60 DefaultMaxMemoryBytes int64 = 64 << 20 MinMaxMemoryBytes int64 = 1 << 20 MaxMaxMemoryBytes int64 = 1 << 30 DefaultMaxBodyBytes = 1 << 20 MaxMaxBodyBytes = 10 << 20 DefaultRateLimit = 600 DefaultCallerRateLimit = 120 MaxRateLimit = 100000 )
Limits a binding is held to, and the values one that names none gets.
const FailureRetention = 7 * 24 * time.Hour
FailureRetention is how long a failed request is kept.
const PathPrefix = "/endpoints/"
PathPrefix is where every endpoint is served: /endpoints/{name}/...
Variables ¶
var ( // ErrNotFound reports a script with no binding, or a name no binding has. ErrNotFound = errors.New("this script is not bound to an endpoint") // ErrNameTaken reports a name another script's binding already has. ErrNameTaken = errors.New("another script's endpoint has this name") // ErrNotAllowed reports a caller who may not create or change a binding: // everyone but an administrator, the script's owner included. ErrNotAllowed = errors.New("only an administrator can bind a script to an endpoint") )
Errors a binding is refused or looked up with.
var Methods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}
Methods is every HTTP method a binding may answer.
Functions ¶
Types ¶
type Binding ¶
type Binding struct {
ID string `json:"id"`
ScriptID string `json:"script_id"`
// Name is the endpoint's segment of /endpoints/{name}, unique across the
// deployment.
Name string `json:"name" example:"order-lookup"`
// Methods are the HTTP methods it answers.
Methods []string `json:"methods" example:"GET"`
// Path is the template under /endpoints/{name}: "" for the name alone,
// "/{id}" for one parameter a request reads as req.params["id"].
Path string `json:"path" example:"/{id}"`
// AuthMode is public or signed_in.
AuthMode string `json:"auth_mode" example:"signed_in"`
// OpenReads lets GET and HEAD on a public endpoint through without a bot
// check.
OpenReads bool `json:"open_reads"`
// TimeoutSeconds, MaxMemoryBytes and MaxBodyBytes bound one request.
TimeoutSeconds int `json:"timeout_seconds"`
MaxMemoryBytes int64 `json:"max_memory_bytes"`
MaxBodyBytes int `json:"max_body_bytes"`
// AllowedOrigins are the browser origins a cross-origin request may come
// from; empty answers no preflight.
AllowedOrigins []string `json:"allowed_origins"`
// RateLimit is the endpoint's requests per minute on each replica, and
// CallerRateLimit one signed-in caller's.
RateLimit int `json:"rate_limit"`
CallerRateLimit int `json:"caller_rate_limit"`
Enabled bool `json:"enabled"`
CreatedBy string `json:"created_by,omitempty"`
UpdatedBy string `json:"updated_by,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Binding is one script's endpoint.
func Build ¶
Build is the binding req sets on sc over prev, which is nil for a script not bound yet. It refuses a library, which nothing calls, and everything Validate refuses.
func (Binding) Answers ¶
Answers reports whether the binding answers method. HEAD is answered wherever GET is.
func (Binding) Match ¶
Match reports whether rest, the part of a request path after /endpoints/{name}, is the binding's template, and the parameter values it carries.
type Failure ¶
type Failure struct {
ID string `json:"id"`
EndpointID string `json:"endpoint_id"`
Version int `json:"version"`
Method string `json:"method"`
Status int `json:"status"`
Error string `json:"error"`
DurationMS int64 `json:"duration_ms"`
CreatedAt time.Time `json:"created_at"`
}
Failure is one request that failed, kept for the script's owner to read.
type Request ¶
type Request struct {
Name string
Methods []string
Path *string
AuthMode string
OpenReads *bool
TimeoutSeconds int
MaxMemoryBytes int64
MaxBodyBytes int
AllowedOrigins []string
RateLimit int
CallerRateLimit int
Enabled *bool
Actor string
// PublicAllowed is whether the deployment lets a public endpoint be
// bound: it has a bot check, or an administrator set the override.
PublicAllowed bool
}
Request is a binding set on a script. A nil field keeps what the binding had, or takes the default on a new one.
type Stamp ¶
Stamp is when a binding and its script were last written. Every write of a script row moves its updated_at, a new version included, so two equal stamps mean nothing a request runs has changed.
type Store ¶
type Store interface {
// Set creates or replaces sc's binding, keyed by its script; a name
// another script's binding has is ErrNameTaken.
Set(ctx context.Context, b *Binding) error
// Get is a script's binding, ErrNotFound when it has none.
Get(ctx context.Context, scriptID string) (*Binding, error)
// GetByName is the binding a name serves, ErrNotFound when none does.
GetByName(ctx context.Context, name string) (*Binding, error)
List(ctx context.Context, filter Filter) ([]Binding, error)
// SetEnabled turns a script's binding on or off.
SetEnabled(ctx context.Context, scriptID string, enabled bool, actor string) error
// RecordFailure keeps one failed request.
RecordFailure(ctx context.Context, f Failure) error
// Failures is an endpoint's most recent failed requests, newest first.
Failures(ctx context.Context, endpointID string, limit int) ([]Failure, error)
// SweepFailures deletes the failures recorded before cutoff.
SweepFailures(ctx context.Context, cutoff time.Time) (int64, error)
// Stamp is when the binding a name serves, and the script it binds, were
// last written: one read that tells a replica whether what it compiled
// for the name is still current. ErrNotFound when no binding has it.
Stamp(ctx context.Context, name string) (Stamp, error)
}
Store keeps bindings and the failed requests they answered.