globalrouter

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeTimeRequest     = "time.request"
	TypeTimeResponse    = "time.response"
	TypeOrgInfoRequest  = "org.info.request"
	TypeOrgInfoResponse = "org.info.response"

	// TypeConnectionRequest is sent from cloud to cluster when a POP
	// server needs the cluster to establish an HTTP/3 connection.
	TypeConnectionRequest = "connection.request"
)

Message type constants matching the cloud's wire protocol.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client maintains a persistent WebSocket connection to the cloud coordination service with automatic reconnection.

func NewClient

func NewClient(cloudURL string, authClient *cloudauth.AuthClient, router *MessageRouter, log *slog.Logger) *Client

NewClient creates a new WebSocket client.

func (*Client) OnConnect

func (c *Client) OnConnect(fn func(ctx context.Context))

OnConnect registers a callback invoked each time a WebSocket connection is established. The handler can use Send to queue messages for the new connection.

func (*Client) Run

func (c *Client) Run(ctx context.Context) error

Run maintains the WebSocket connection with reconnection. It blocks until the context is cancelled.

func (*Client) Send

func (c *Client) Send(env *Envelope)

Send queues an envelope for delivery to the cloud. Non-blocking; drops the message if the outbox is full.

func (*Client) SendMessage

func (c *Client) SendMessage(msgType string, data any) error

SendMessage marshals data and queues it for delivery.

type Config

type Config struct {
	CloudURL   string
	ClusterXID string
	AuthClient *cloudauth.AuthClient
	Ingress    *httpingress.Server
	Log        *slog.Logger
}

Config holds the configuration for the global router.

type ConnectionRequest

type ConnectionRequest struct {
	POPXID       string `json:"pop_xid"`
	POPAddress   string `json:"pop_address"`
	Hostname     string `json:"hostname"`
	RequestID    string `json:"request_id"`
	ConnectToken string `json:"connect_token"`
}

ConnectionRequest is the payload for connection.request messages.

type Envelope

type Envelope struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

Envelope is the wire format for all messages on the WebSocket.

type GlobalRouter

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

GlobalRouter connects the cluster to the cloud coordination service and manages POP connections for inbound traffic forwarding.

func New

func New(cfg Config) *GlobalRouter

New creates a new GlobalRouter.

func (*GlobalRouter) OrganizationID

func (gr *GlobalRouter) OrganizationID() string

OrganizationID returns the organization ID reported by the cloud.

func (*GlobalRouter) Run

func (gr *GlobalRouter) Run(ctx context.Context) error

Run starts the global router. It blocks until the context is cancelled.

func (*GlobalRouter) TimeOffset

func (gr *GlobalRouter) TimeOffset() time.Duration

TimeOffset returns the estimated clock offset between this cluster and the cloud, computed via simplified NTP.

type MessageHandler

type MessageHandler func(ctx context.Context, data json.RawMessage) error

MessageHandler processes an inbound message.

type MessageRouter

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

MessageRouter dispatches inbound messages by type.

func NewMessageRouter

func NewMessageRouter() *MessageRouter

NewMessageRouter creates a new MessageRouter.

func (*MessageRouter) Dispatch

func (r *MessageRouter) Dispatch(ctx context.Context, env Envelope) error

Dispatch routes a message to the appropriate handler.

func (*MessageRouter) Handle

func (r *MessageRouter) Handle(msgType string, handler MessageHandler)

Handle registers a handler for a given message type.

type OrgInfoResponse

type OrgInfoResponse struct {
	OrganizationID string `json:"organization_id"`
}

OrgInfoResponse is the payload for org.info.response messages.

type POPManager

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

POPManager manages HTTP/3 connections to POP servers. When the cloud sends a connection.request, the manager establishes two QUIC connections to the POP:

  1. Control plane (ALPN "h3"): POST /connect with the connect_token
  2. Data plane (ALPN "pop-data"): authenticated by IP match, then serves incoming HTTP/3 requests forwarded by the POP

func NewPOPManager

func NewPOPManager(clusterXID string, ingress *httpingress.Server, log *slog.Logger) *POPManager

NewPOPManager creates a new POP connection manager.

func (*POPManager) Close

func (m *POPManager) Close()

Close shuts down all POP connections.

func (*POPManager) ConnectedPOPs

func (m *POPManager) ConnectedPOPs() []string

ConnectedPOPs returns the XIDs of all currently connected POPs.

func (*POPManager) HandleConnectionRequest

func (m *POPManager) HandleConnectionRequest(ctx context.Context, req ConnectionRequest) error

HandleConnectionRequest processes a connection.request from the cloud. It establishes connections to the specified POP if one does not already exist.

func (*POPManager) RemovePOP

func (m *POPManager) RemovePOP(popXID string)

RemovePOP closes and removes the connection to a specific POP.

type TimeRequest

type TimeRequest struct {
	ClientTransmitTime time.Time `json:"client_transmit_time"`
}

TimeRequest is the payload for time.request messages.

type TimeResponse

type TimeResponse struct {
	ClientTransmitTime time.Time `json:"client_transmit_time"`
	ServerReceiveTime  time.Time `json:"server_receive_time"`
	ServerTransmitTime time.Time `json:"server_transmit_time"`
}

TimeResponse carries the four timestamps needed for simplified NTP.

Jump to

Keyboard shortcuts

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