pool

package module
v0.0.0-...-5c8eb4f Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2016 License: MIT Imports: 5 Imported by: 7

README

connection-pool

A golang based connection pool for net.Conn connections

##Documentation See godoc

##Installation

go get github.com/go-home-iot/connection-pool

##Package

import "github.com/go-home-iot/pool"

##Usage See connection_pool_test.go for examples of how to use this library. The basic concepts are that you create a pool, in the config you pass in a NewConnection function that allows the pool to create new connections. If at any point a connection is found to be bad the connection is thrown away and a new one created in its place.

##Version History ###0.1.0 Initial release

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeout = errors.New("timeout")

ErrTimeout represents a timeout error, for example you called Get and couldn't get a connection within the timeout period.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Name is a friendly name associated with the pool, cab be useful for debugging
	Name string

	// Size is the number of connections to open
	Size int

	// RetryDuration specifies how long the pool will wait to try to create a new connection
	// if the previous new conneciton attempt failed
	RetryDuration time.Duration

	// NewConnection takes in the pool config information and returns an open net.Conn connection
	// that can be added to the pool.
	NewConnection func(Config) (net.Conn, error)
}

Config contains all of the configuration parameters for the connection pool

type Connection

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

Connection represents a connection to a network resource

func NewConnection

func NewConnection(c net.Conn, p *ConnectionPool) *Connection

NewConnection returns an initialized Connection instance

func (*Connection) Close

func (c *Connection) Close() error

Close returns the connection to the pool, the connection stays open

type ConnectionPool

type ConnectionPool struct {
	Config Config
	// contains filtered or unexported fields
}

ConnectionPool provides the ability to pool connections

func NewPool

func NewPool(config Config) *ConnectionPool

NewPool creates a new ConnectionPool. The pool which is returned will still need to have Init() called in it before it can be used

func (*ConnectionPool) Close

func (p *ConnectionPool) Close() chan bool

Close closes all of the underlying connections, this is non blocking but you can wait on the returned channel if you need to know all the connections have closed

func (*ConnectionPool) Get

func (p *ConnectionPool) Get(timeout time.Duration, flush bool) (*Connection, error)

Get is a blocking function that waits to get an available connection. If after the timeout duration a connection could not be fetched, the function returns with ErrTimeout. The flush parameter if set to true will read all of the outstanding data from the connection before returning it to the caller. Note there is a possible 100ms delay for this function to return if you set flush==true while the pool tries to read any existing content from the connection

func (*ConnectionPool) Init

func (p *ConnectionPool) Init() chan bool

Init should be called before using the pool, the call is non blocking, but you can wait on the returned channel if you want to know when all of the underlying connections have been created and are ready to use

func (*ConnectionPool) Release

func (p *ConnectionPool) Release(c *Connection, err error)

Release returns the connection back to the pool. err is any error that was returned by the connection while it was being used, if there was an error the pool will then throw this connection away and create a new one

Jump to

Keyboard shortcuts

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