Help us shape the future of Go! Take the 2026 Go Developer Survey

serverless

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package serverless is what Cloud Run and Cloud Functions share: running an HTTP container, waiting until it listens, proxying requests to it and the message-to-HTTP mapping of Hub.Deliver.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDocker = errors.New("no container engine is running (start Docker, Podman, Colima or OrbStack, or set DOCKER_HOST)")

ErrNoDocker is why a container cannot run.

Functions

func CloseIdle

func CloseIdle()

CloseIdle drops the kept-alive connections to containers and processes, which would hold a server open while it shuts down.

func Deliver

func Deliver(addr string, m *core.Message) error

Deliver is the mapping of core.Message onto an HTTP endpoint that Cloud Run and Cloud Functions use for Hub.Deliver (Eventarc, Cloud Tasks, ...):

  • the request is a POST of Message.Body to addr;
  • its path is Message.Subject (default "/"), and may carry a query string;
  • each Message.Attributes key becomes a header, set from StringValue (or BinaryValue), so "ce-type" and "content-type" make a CloudEvent in binary mode; without content-type the body is application/json when it is JSON and text/plain otherwise;
  • a non-2xx response, or none, is an error.

func Env

func Env(h *core.Hub, host, project string) []string

Env is the environment that points Google client libraries in a container (or process) on host at pogi.

func FreePort

func FreePort() int

FreePort returns a port nobody listens on.

func Label

func Label(host, suffix string) (string, bool)

Label is the part of host before suffix: "<label>.<suffix>[:port]".

func Port

func Port(h *core.Hub) string

Port is the port pogi listens on.

func Proxy

func Proxy(w http.ResponseWriter, r *http.Request, addr string)

Proxy passes r to addr with the Host kept. ponytail: the request body is buffered (pogi's gateway reads it anyway).

func WaitReady

func WaitReady(addr string, d time.Duration, alive func() bool) error

WaitReady blocks until something accepts connections on addr and keeps them open (a published container port accepts them before the container listens, then closes them), alive reports false, or d passes.

Types

type Instance

type Instance struct {
	Addr string // host:port that pogi reaches it at
	ID   string
	// contains filtered or unexported fields
}

Instance is a running container.

func Start

func Start(ctx context.Context, h *core.Hub, want int, build func(port int) *docker.Container, logs *Logs, wait time.Duration) (*Instance, error)

Start runs the container build makes and waits until it listens. want is the port it serves in a bridge network; on an engine without a VM (Linux) the container shares the host's network and serves on a free port instead, so build is called with the port to use. Its output goes to logs.

func (*Instance) Stop

func (i *Instance) Stop()

Stop removes the container.

type Logs

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

Logs keeps the last lines a container or process wrote.

func (*Logs) Add

func (l *Logs) Add(s string)

Add records one line.

func (*Logs) Lines

func (l *Logs) Lines() []string

Lines returns a copy of the recorded lines.

func (*Logs) ToLogging

func (l *Logs) ToLogging(h *core.Hub, project, logID, resourceType string, labels map[string]string)

ToLogging also writes every line to Cloud Logging, when it is enabled, as an entry of the log logID (URL-encoded, e.g. "run.googleapis.com%2Fstdout") of a monitored resource.

func (*Logs) Write

func (l *Logs) Write(p []byte) (int, error)

Write makes Logs an io.Writer for processes' output. ponytail: a line split across two writes is recorded as two lines.

Jump to

Keyboard shortcuts

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