idletiming

package module
v0.0.0-...-6767b09 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: Apache-2.0 Imports: 9 Imported by: 43

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 returned when attempting to use a network connection that was
	// closed because of idling.
	ErrIdled = errors.New("Use of idled network connection")

	// ErrClosed is returned when attempting to use a network connections that was
	// already manually closed.
	ErrClosed = errors.New("Use of closed network connection")
)

Functions

func IsIdled

func IsIdled(conn net.Conn) bool

IsIdled indicates whether the given conn represents an idletiming conn that has idled.

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:

func NewReader

func NewReader(conn net.Conn, timeout time.Duration) io.Reader

NewReader creates a Reader whose Read method only blocks up to timeout. If timeout is hit, it returns whatever was read and no error.

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) Idled

func (c *IdleTimingConn) Idled() bool

func (*IdleTimingConn) LocalAddr

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

func (*IdleTimingConn) Pause

func (c *IdleTimingConn) Pause() func()

Pause pauses idle timing and returns a function that can be called to unpause it

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) 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) Wrapped

func (c *IdleTimingConn) Wrapped() net.Conn

Wrapped implements the interface netx.WrappedConn

func (*IdleTimingConn) Write

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

Write implements the method from io.Writer

Jump to

Keyboard shortcuts

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