Back to godoc.org

Package localsrv

v0.0.0-...-1557bcf
Latest Go to latest

The latest major version is .

Published: Aug 13, 2020 | License: Apache-2.0 | Module: github.com/luci/luci-go

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

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.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier