luci: Index | Files

package localsrv

import ""

Package localsrv provides helpers for running local TCP servers.

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


Package Files


type ServeFunc Uses

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 Uses

type Server struct {
    // contains filtered or unexported fields

Server runs a local TCP server.

func (*Server) Start Uses

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 Uses

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 localsrv imports 8 packages (graph) and is imported by 10 packages. Updated 2021-01-24. Refresh now. Tools for package owners.