gracenet

package module
v0.0.0-...-7e0e6f3 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2020 License: MIT Imports: 2 Imported by: 1

README

gracenet

Wrapper around net.Listener with network activity monitoring

Example usage

In this example application exits after 5 minutes without any network activity


...

func main() {
    ln, err := net.Listen("tcp", "localhost:8080")
    defer ln.Close()
    if err != nil {
        return errors.Wrap(err, "Failed to bind address")
    }
    agln := gracenet.NewGraceListener(ln, 5 * time.Minute) // setting grace period
    httpError := make(chan error, 1)
    go func() {
        mux := http.NewServeMux()
        err = http.Serve(agln, mux)
        httpError <- err
    }()
    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

    select {
    case <-agln.Expire():
        log.Info("Nobody here for so long!")
    case sig := <-sigs:
        log.WithField("signal", sig).Info("Got syscall")
    case err := <-httpError:
        return errors.Wrap(err, "Got http error")
    }
    log.Info("Shooting down... at last!")
    return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GraceConn

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

func NewGraceConn

func NewGraceConn(c net.Conn, ch chan bool) *GraceConn

func (*GraceConn) Close

func (c *GraceConn) Close() error

func (*GraceConn) LocalAddr

func (c *GraceConn) LocalAddr() net.Addr

func (*GraceConn) Read

func (c *GraceConn) Read(b []byte) (n int, err error)

func (*GraceConn) RemoteAddr

func (c *GraceConn) RemoteAddr() net.Addr

func (*GraceConn) SetDeadline

func (c *GraceConn) SetDeadline(t time.Time) error

func (*GraceConn) SetReadDeadline

func (c *GraceConn) SetReadDeadline(t time.Time) error

func (*GraceConn) SetWriteDeadline

func (c *GraceConn) SetWriteDeadline(t time.Time) error

func (*GraceConn) Write

func (c *GraceConn) Write(b []byte) (n int, err error)

type GraceListener

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

func NewGraceListener

func NewGraceListener(l net.Listener, d time.Duration) *GraceListener

func (*GraceListener) Accept

func (ln *GraceListener) Accept() (net.Conn, error)

func (*GraceListener) Addr

func (ln *GraceListener) Addr() net.Addr

func (*GraceListener) Close

func (ln *GraceListener) Close() error

func (*GraceListener) Expire

func (ln *GraceListener) Expire() <-chan bool

func (*GraceListener) Revoke

func (ln *GraceListener) Revoke() <-chan bool

func (*GraceListener) Start

func (ln *GraceListener) Start()

func (*GraceListener) Stop

func (ln *GraceListener) Stop()

Jump to

Keyboard shortcuts

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