idletiming

package
v0.0.0-...-8b1022e Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2016 License: Apache-2.0, Apache-2.0 Imports: 7 Imported by: 0

README

idletiming Travis CI Status Coverage Status GoDoc

Provides mechanisms for adding idle timeouts to net.Conn and net.Listener.

GoDoc

Documentation

Overview

package idletiming provides mechanisms for adding idle timeouts to net.Conn and net.Listener.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// ErrIdled is return when attempting to use a network connection that was
	// closed because of idling.
	ErrIdled = errors.New("Use of idled network connection")
)

Functions

func Listener

func Listener(listener net.Listener, idleTimeout time.Duration, onIdle func(conn net.Conn)) net.Listener

Listener creates a net.Listener that wraps the connections obtained from an original net.Listener with idle timing connections that time out after the specified duration.

idleTimeout specifies how long to wait for inactivity before considering connection idle. Note - the actual timeout may be up to twice idleTimeout, depending on timing.

If onIdle is specified, it will be called to indicate when the connection has idled and been closed.

Example
l, err := net.Listen("tcp", "127.0.0.1:8080")
if err != nil {
	log.Fatalf("Unable to listen %s", err)
}

il := Listener(l, 5*time.Second, func(conn net.Conn) {
	log.Debugf("Connection was idled")
})

if _, err := il.Accept(); err != nil {
	log.Fatalf("Unable to accept connections: %v", err)
}
Output:

Types

type IdleTimingConn

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

IdleTimingConn is a net.Conn that wraps another net.Conn and that times out if idle for more than idleTimeout.

func Conn

func Conn(conn net.Conn, idleTimeout time.Duration, onIdle func()) *IdleTimingConn

Conn creates a new net.Conn wrapping the given net.Conn that times out after the specified period. Once a connection has timed out, any pending reads or writes will return io.EOF and the underlying connection will be closed.

idleTimeout specifies how long to wait for inactivity before considering connection idle.

If onIdle is specified, it will be called to indicate when the connection has idled and been closed.

Example
c, err := net.Dial("tcp", "127.0.0.1:8080")
if err != nil {
	log.Fatalf("Unable to dial %s", err)
}

ic := Conn(c, 5*time.Second, func() {
	log.Debugf("Connection was idled")
})

if _, err := ic.Write([]byte("My data")); err != nil {
	log.Fatalf("Unable to write to connection: %v", err)
}
Output:

func (*IdleTimingConn) Close

func (c *IdleTimingConn) Close() error

Close this IdleTimingConn. This will close the underlying net.Conn as well, returning the error from calling its Close method.

func (*IdleTimingConn) LocalAddr

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

func (*IdleTimingConn) Read

func (c *IdleTimingConn) Read(b []byte) (int, error)

Read implements the method from io.Reader

func (*IdleTimingConn) RemoteAddr

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

func (*IdleTimingConn) SetDeadline

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

func (*IdleTimingConn) SetReadDeadline

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

func (*IdleTimingConn) SetWriteDeadline

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

func (*IdleTimingConn) TimesOutAt

func (c *IdleTimingConn) TimesOutAt() time.Time

TimesOutAt returns the time at which this connection will time out, assuming there is no further activity

func (*IdleTimingConn) TimesOutIn

func (c *IdleTimingConn) TimesOutIn() time.Duration

TimesOutIn returns how much time is left before this connection will time out, assuming there is no further activity.

func (*IdleTimingConn) Write

func (c *IdleTimingConn) Write(b []byte) (int, error)

Write implements the method from io.Reader

Jump to

Keyboard shortcuts

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