server

package
v2.303.2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package server provides transport-agnostic server lifecycle helpers used by go-service.

This package defines small primitives that higher-level transport packages (for example HTTP and gRPC) can use to run servers consistently and integrate with process shutdown logic.

Concepts

The main abstractions are:

  • Server: a minimal interface describing a runnable server that can be gracefully shut down.

  • Service: a small lifecycle manager that starts a Server asynchronously, logs start/stop events, and triggers application shutdown when the underlying Server.Serve returns an error.

Typical usage

Transport-specific packages construct a concrete implementation of Server (wrapping a net/http.Server or grpc.Server), then wrap it in a Service and call Service.Start during application startup. On shutdown, the application stops the underlying server by calling Service.Stop with a context.

Start with `Server` and `Service`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server interface {
	fmt.Stringer

	// Serve starts serving requests.
	//
	// Serve is expected to block until the server stops. It should return a non-nil error when serving
	// terminates unexpectedly (for example due to a listener failure). Graceful shutdown should typically
	// cause Serve to return nil or a well-understood sentinel error, depending on the underlying server.
	Serve() error

	// Shutdown stops the server gracefully.
	//
	// Shutdown should attempt to stop the server without abruptly dropping active requests, respecting ctx
	// for deadlines/cancellation. It should return any error encountered during shutdown.
	Shutdown(ctx context.Context) error
}

Server defines the minimal interface required by Service to manage a transport server.

Implementations are typically thin adapters over concrete servers such as `net/http.Server` or `grpc.Server`. The embedded fmt.Stringer is expected to return a human-readable address or identifier (used for logging).

type Service

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

Service manages starting and stopping a Server with optional logging and shutdown integration.

Concurrency/lifecycle expectations:

  • Start launches the server in a new goroutine.
  • Stop requests a graceful shutdown and should be called during application shutdown.
  • If Server.Serve returns a non-nil error, Service triggers application shutdown via di.Shutdowner.

func NewService

func NewService(name string, server Server, logger *logger.Logger, sh di.Shutdowner) *Service

NewService constructs a Service that manages the lifetime of an underlying Server.

name is used for attribution in logs (meta.SystemKey). server is the concrete server implementation (e.g. HTTP or gRPC). logger may be nil to disable logging. sh is used to trigger application shutdown when the underlying Server terminates unexpectedly.

NewService does not start the server; call (*Service).Start to begin serving.

func (*Service) Start

func (s *Service) Start()

Start launches the underlying server asynchronously.

Start returns immediately. The underlying Server.Serve runs in a separate goroutine.

func (*Service) Stop

func (s *Service) Stop(ctx context.Context)

Stop requests a graceful shutdown of the underlying server and logs the stop event.

Stop calls Server.Shutdown with ctx and ignores the returned error. The provided context controls shutdown deadlines/cancellation.

Jump to

Keyboard shortcuts

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