server

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package server provides a web server for real-time pgcopy state visualization.

The server implements a WebSocket-based push notification system that broadcasts state changes to connected clients with configurable debouncing to prevent overwhelming clients with high-frequency updates.

Thread Safety: All exported methods are safe for concurrent use. The server uses fine-grained locking to protect client connections and state.

Resource Management: The server implements graceful shutdown via the Shutdown method, which properly closes all WebSocket connections and waits for in-flight requests to complete.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrServerNotStarted indicates an operation was attempted on a server that hasn't been started.
	ErrServerNotStarted = errors.New("server: not started")
	// ErrServerAlreadyStarted indicates Start was called on an already running server.
	ErrServerAlreadyStarted = errors.New("server: already started")
	// ErrNilState indicates a nil CopyState was provided.
	ErrNilState = errors.New("server: nil state provided")
	// ErrInvalidPort indicates an invalid port number was provided.
	ErrInvalidPort = errors.New("server: invalid port number")
)

Sentinel errors for WebServer operations.

Functions

This section is empty.

Types

type WebServer

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

WebServer provides a web interface for monitoring pgcopy operations.

The server exposes the following endpoints:

  • GET / - Main web interface (HTML)
  • GET /api/state - Current state as JSON
  • GET /ws - WebSocket endpoint for real-time updates
  • GET /static/* - Static file serving (reserved for future use)

Thread Safety: All methods are safe for concurrent use.

func NewWebServer

func NewWebServer(copyState *state.CopyState, port int) *WebServer

NewWebServer creates a new web server instance.

Parameters:

  • copyState: The CopyState to observe and broadcast. Must not be nil.
  • port: The TCP port to listen on. Must be between 1024 and 65535.

Returns:

  • *WebServer: The configured server instance, or nil if validation fails.
  • error: ErrNilState if copyState is nil, ErrInvalidPort if port is invalid.

Example:

server, err := NewWebServer(state, 8080)
if err != nil {
    log.Fatal(err)
}
defer server.Shutdown(context.Background())

func (*WebServer) OnStateChange

func (ws *WebServer) OnStateChange(_ *state.CopyState, event state.Event)

OnStateChange implements the Listener interface

func (*WebServer) Shutdown added in v0.5.0

func (ws *WebServer) Shutdown(ctx context.Context) error

Shutdown gracefully stops the web server and releases all resources.

The shutdown process:

  1. Marks server as shutting down (prevents new connections)
  2. Unsubscribes from state changes
  3. Closes all active WebSocket connections with close message
  4. Stops the debounce timer
  5. Gracefully shuts down the HTTP server (waits for in-flight requests)

Parameters:

  • ctx: Context for shutdown deadline. If cancelled, shutdown is forced.

Returns:

  • error: Any error from HTTP server shutdown, or nil on success.

Thread Safety: Safe for concurrent calls; only the first call performs shutdown.

func (*WebServer) Start

func (ws *WebServer) Start() error

Start starts the web server in a background goroutine.

The server listens on the configured port and serves:

  • Web interface at /
  • JSON API at /api/state
  • WebSocket updates at /ws

Returns:

  • error: ErrServerAlreadyStarted if called twice, or underlying bind error.

Thread Safety: Safe for concurrent calls; only the first call starts the server.

func (*WebServer) WaitForCompletionAck

func (ws *WebServer) WaitForCompletionAck(timeout time.Duration) bool

WaitForCompletionAck waits for the web client to acknowledge completion

Jump to

Keyboard shortcuts

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