localsrv

package
v0.0.0-...-c912a3c Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package localsrv provides helpers for running local TCP servers.

It is used by various machine-local authentication protocols to launch local listening servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ServeFunc

type ServeFunc func(c context.Context, l net.Listener, wg *sync.WaitGroup) error

ServeFunc is called from internal goroutine to run the server loop.

When server stops, the given listener will be closed and the given context will be canceled. The wait group is used to wait for pending requests: increment it when starting processing a request, and decrement when done.

If ServeFunc returns after the listener is closed, the returned error is ignored (it is most likely caused by the closed listener).

type Server

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

Server runs a local TCP server.

func (*Server) Start

func (s *Server) Start(ctx context.Context, name string, port int, serve ServeFunc) (*net.TCPAddr, error)

Start launches background goroutine with the serving loop 'serve'.

Returns the address the listening socket is bound to.

The provided context is used as base context for request handlers and for logging. 'name' identifies this server in logs, and 'port' specifies a TCP port number to bind to (or 0 to auto-pick one).

The server must be eventually stopped with Stop().

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop closes the listening socket, notifies pending requests to abort and stops the internal serving goroutine.

Safe to call multiple times. Once stopped, the server cannot be started again (make a new instance of Server instead).

Uses the given context for the deadline when waiting for the serving loop to stop.

Jump to

Keyboard shortcuts

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