pool

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2023 License: Apache-2.0 Imports: 4 Imported by: 9

README

Pool

Doc License

A simple connection pool for Go.

Features:

  • Thread-safe (obviously)
  • Stack based (rather than queue based) - connections that have been used recently are more likely to be re-used again
  • Supports shirinking - idle pool connections can be reaped

Credits

Documentation

Overview

Package pool is a generic, high-performance pool for net.Conn objects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

type Factory func() (net.Conn, error)

Factory must returns new connections

type Options

type Options struct {
	// InitialSize creates a number of connection on pool initialization
	// Default: 0
	InitialSize int

	// MaxCap sets the maximum pool capacity. Will be automatically adjusted when InitialSize
	// is larger.
	// Default: 10
	MaxCap int

	// IdleTimeout timeout after which connections are reaped and
	// automatically removed from the pool.
	// Default: 0 (= never)
	IdleTimeout time.Duration

	// ReapInterval determines the frequency of reap cycles
	// Default: 1 minute
	ReapInterval time.Duration
}

Options can tweak Pool configuration

type Pool

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

Pool contains a number of connections

func New

func New(opt *Options, factory Factory) (*Pool, error)

New creates a pool with an initial number of connection and a maximum cap

func (*Pool) Close

func (s *Pool) Close() error

Close closes all connections and the pool

func (*Pool) Get

func (s *Pool) Get() (net.Conn, error)

Get returns a connection from the pool or creates a new one

func (*Pool) Len

func (s *Pool) Len() int

Len returns the number of available connections in the pool

func (*Pool) Put

func (s *Pool) Put(cn net.Conn) bool

Put adds/returns a connection to the pool

Jump to

Keyboard shortcuts

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