stoppableListener

package module
v0.0.0-...-100800c Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2017 License: BSD-2-Clause Imports: 5 Imported by: 17

README

stoppableListener

Go package for graceful shutdown of TCP socket listeners.

Works with go 1.4+.

Documentation Build Status Report Card

About

A cleanly stoppable TCP listener in Go. This library wraps an existing TCP connection object. A goroutine calling Accept() is interrupted with StoppedError whenever the listener is stopped by a call to Stop().

For a fully functional example see example/example.go.

Quick usage overview:

	originalListener, err := net.Listen("tcp", ":8080")
	if err != nil {
		panic(err)
	}

	sl, err := stoppableListener.New(originalListener)
	if err != nil {
		panic(err)
	}

Requirements

  • Go version 1.4 or newer
  • Linux, Mac OS X, or Windows

Running the tests

go test -v

Notes

Graceful shutdown in Go 1.8+

Go 1.8 added built-in support for graceful shutdown of HTTP servers.

See also: Runnable example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultStopCheckTimeout  = time.Duration(1 * time.Millisecond)
	DefaultTimeoutMultiplier = 100   // Default number of timeouts permitted before giving up and returning failed-to-stop error.
	DefaultVerbose           = false // Default value for Verbose field of new StoppableListeners.

	StoppedError      = errors.New("StoppableListener: listener stopped")
	ListenerWrapError = errors.New("StoppableListener: cannot wrap listener")
	NotStoppedError   = errors.New("StoppableListener: listener failed to stop, listener port is still open after timeout")
)

Functions

This section is empty.

Types

type StoppableListener

type StoppableListener struct {
	*net.TCPListener // Wrapped listener.

	StopCheckTimeout  time.Duration // TCP socket timeout value used when a stop-check is run.
	TimeoutMultiplier int           // How many times the StopCheckTimeout duration should the wait-loop allow.
	Verbose           bool          // Activates verbose logging.
	// contains filtered or unexported fields
}

func New

New creates a new stoppable TCP listener.

func (*StoppableListener) Accept

func (sl *StoppableListener) Accept() (net.Conn, error)

func (*StoppableListener) Stop

func (sl *StoppableListener) Stop() (err error)

func (*StoppableListener) StopSafely

func (sl *StoppableListener) StopSafely() (err error)

StopSafely waits until the socket is longer reachable, or returns an error if the check times out.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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