goperconn

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2018 License: MIT Imports: 4 Imported by: 3

README

goperconn

Goperconn is a Go library for maintaining pseudo-persitent client network connections.

Usage

Documentation is available via GoDoc.

Basic Example

Only the Address of the remote host is required. All other parameters have reasonalbe defaults and may be elided.

        package main

        import (
            "log"
            "os"
            "time"

            "github.com/karrick/goperconn"
        )

        func main() {
            // NOTE: Address is required, but all other parameters have defaults.
            conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"))
            if err != nil {
                log.Fatal(err)
            }

            // later ...

            _, err = conn.Write([]byte("hello, world"))
            if err != nil {
                log.Fatal(err)
            }

            buf := make([]byte, 512)
            _, err = conn.Read(buf)
            if err != nil {
                log.Fatal(err)
            }
        }
DialTimeout

By default the library uses net.Dial to establish a new network connection. If you specify a custom dial timeout, it will use net.DialTimeout instead.

        package main

        import (
            "log"
            "os"
            "time"

            "github.com/karrick/goperconn"
        )

        func main() {
            // NOTE: Address is required, but all other parameters have defaults.
            conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"),
                goperconn.DialTimeout(5*time.Second))
            if err != nil {
                log.Fatal(err)
            }

            // later ...

            _, err = conn.Write([]byte("hello, world"))
            if err != nil {
                log.Fatal(err)
            }

            buf := make([]byte, 512)
            _, err = conn.Read(buf)
            if err != nil {
                log.Fatal(err)
            }
        }
Logger

If the library receives an error when attempting to dial the remote host, or if there is an error that takes place during an I/O operation, the library handles the error. Additionally, when the error takes place during an I/O operation, the library will return the error to the client just like the Read or Write methods are expected to. However, when a custom Logger is provided, the library will also call the logger's Print function.

        package main

        import (
            "log"
            "os"
            "time"

            "github.com/karrick/goperconn"
        )

        func main() {
            printer := log.New(os.Stderr, "WARNING: ", 0)

            // NOTE: Address is required, but all other parameters have defaults.
            conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"),
                goperconn.Logger(printer))
            if err != nil {
                log.Fatal(err)
            }

            // later ...

            _, err = conn.Write([]byte("hello, world"))
            if err != nil {
                log.Fatal(err)
            }

            buf := make([]byte, 512)
            _, err = conn.Read(buf)
            if err != nil {
                log.Fatal(err)
            }
        }
RetryMin and RetryMax

The library will attempt to reestablish the network connection if it breaks down. It uses an exponential backoff retry approach, bounded by RetryMin and RetryMax. You can override the minimum and maximum amount of time between connection retry attempts.

        package main

        import (
            "log"
            "os"
            "time"

            "github.com/karrick/goperconn"
        )

        func main() {
            // NOTE: Address is required, but all other parameters have defaults.
            conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"),
                goperconn.RetryMin(time.Second),
                goperconn.RetryMax(30*time.Second))
            if err != nil {
                log.Fatal(err)
            }

            // later ...

            _, err = conn.Write([]byte("hello, world"))
            if err != nil {
                log.Fatal(err)
            }

            buf := make([]byte, 512)
            _, err = conn.Read(buf)
            if err != nil {
                log.Fatal(err)
            }
        }
The Whole Enchalata

The Address of the remote connection must be specified, but all other customizable parameters are optional, and may be given in any order, in the form of a slice of function calls in the goperconn.New invocation.

        package main

        import (
            "log"
            "os"
            "time"

            "github.com/karrick/goperconn"
        )

        func main() {
            printer := log.New(os.Stderr, "WARNING: ", 0)

            // NOTE: Address is required, but all other parameters have defaults.
            conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"),
                goperconn.DialTimeout(5*time.Second),
                goperconn.Logger(printer),
                goperconn.RetryMin(time.Second),
                goperconn.RetryMax(30*time.Second))
            if err != nil {
                log.Fatal(err)
            }

            // later ...

            _, err = conn.Write([]byte("hello, world"))
            if err != nil {
                log.Fatal(err)
            }

            buf := make([]byte, 512)
            _, err = conn.Read(buf)
            if err != nil {
                log.Fatal(err)
            }
        }

Documentation

Index

Constants

View Source
const DefaultJobQueueSize = 10

DefaultJobQueueSize specifies the size of the job queue created to support IO operations on the Conn.

View Source
const DefaultRetryMax = time.Minute

DefaultRetryMax is the default maximum amount of time the client will wait to reconnect to a remote host if the connection drops.

View Source
const DefaultRetryMin = time.Second

DefaultRetryMin is the default minimum amount of time the client will wait to reconnect to a remote host if the connection drops.

Variables

This section is empty.

Functions

This section is empty.

Types

type Configurator

type Configurator func(*Conn) error

Configurator is a function that modifies a Conn structure during initialization.

func Address

func Address(address string) Configurator

Address changes the network address used by a .

func DialTimeout

func DialTimeout(duration time.Duration) Configurator

DialTimeout specifies the timeout to use when establishing the connection to the remote host.

func Logger

func Logger(printer Printer) Configurator

Logger specifies an optional logger to invoke to log warning messages.

func RetryMax

func RetryMax(duration time.Duration) Configurator

RetryMax controls the maximum amount of time a Conn will wait between connection attempts to the remote host.

func RetryMin

func RetryMin(duration time.Duration) Configurator

RetryMin controls the minimum amount of time a Conn will wait between connection attempts to the remote host.

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn wraps a net.Conn, providing a pseudo-persistent network connection.

func New

func New(setters ...Configurator) (*Conn, error)

New returns a Conn structure that wraps the net.Conn connection, and attempts to provide a pseudo-persistent connection to a remote host.

package main

import (
	"log"
	"os"
	"time"

	"github.com/karrick/goperconn"
)

func main() {
	printer := log.New(os.Stderr, "WARNING: ", 0)

	// NOTE: Address is required, but all other parameters have defaults.
	conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"),
		goperconn.DialTimeout(5*time.Second),
		goperconn.Logger(printer),
		goperconn.RetryMin(time.Second),
		goperconn.RetryMax(30*time.Second))
	if err != nil {
		log.Fatal(err)
	}

	// later ...

	_, err = conn.Write([]byte("hello, world"))
	if err != nil {
		log.Fatal(err)
	}

	buf := make([]byte, 512)
	_, err = conn.Read(buf)
	if err != nil {
		log.Fatal(err)
	}
}

func (*Conn) Close

func (client *Conn) Close() error

Close closes the connection.

func (*Conn) Read

func (client *Conn) Read(data []byte) (int, error)

Read reads data from the connection.

func (*Conn) Write

func (client *Conn) Write(data []byte) (int, error)

Write writes data to the connection.

type ErrClosedConnection

type ErrClosedConnection struct{}

ErrClosedConnection is returned when I/O operation attempted on closed connection.

func (ErrClosedConnection) Error

func (e ErrClosedConnection) Error() string

type ErrDialFailure

type ErrDialFailure struct {
	Address string
	Err     error
}

ErrDialFailure is optionally sent to the configured warning hookback when net.DialTimeout fails. The library will continue to attempt to reestablish the connection, but this error is useful for client application logging purposes.

func (ErrDialFailure) Error

func (e ErrDialFailure) Error() string

type ErrIOError

type ErrIOError struct {
	Op  opcode
	Err error
}

ErrIOError is optionally sent to the configured warning hookback when an I/O operation fails. The library will close and attempt to reestablish the connection, but this error is useful for client application logging purposes.

func (ErrIOError) Error

func (e ErrIOError) Error() string

type Printer

type Printer interface {
	Print(...interface{})
}

Printer is the interface implemented by an object that allows printing of arbitrary information. It is provided to allow goperconn to accept and use any logging library that implements this interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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