gncp

package module
v0.0.0-...-c70df2d Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2017 License: MIT Imports: 6 Imported by: 4

README

gncp Doc License Build

A thread safe connection pool for net.conn interface. Easy to manage, reuse and limit connections in golang.

Install

Use go get to install package:

go get github.com/eternnoir/gncp

In source code:

import "github.com/eternnoir/gncp"

Usage

Full document: https://godoc.org/github.con/eternnoir/gncp


// connCreator let connection know how to create new connection.
func connCreator() (net.Conn, error) {
	return net.Dial("tcp", "127.0.0.1:5566")
}

// Create new connection pool. It will initialize 3 connection in pool when pool created.
// If connection not enough in pool, pool will call creator to create new connection.
// But when total connection number pool created reach 10 connection, pool will not creat
// any new connection until someone call Remove().
pool, err := gncp.NewPool(3, 10, connCreator)

// Get connection from pool. If pool has no connection and total connection reach max number
// of connections, this method will block until someone put back connection to pool.
conn, err := pool.Get()

// Get connection from pool with timeout. It will wait one second, if still cannot get connection
// it will return timeout error.
conn, err := pool.GetWithTimeout(time.Duration(1) * time.Second)

// After you are finished using the connection call Close() method to put connection back to pool.
// It will not close real connection.
err := conn.Close()

// Remove connection from connection pool. The connection will not belong pool anymore.
// And this method will close connection.
err := pool.Remove(conn)

// Close connection pool. All connections in pool will be closed.
err := pool.Close()

License

The MIT License (MIT)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnPool

type ConnPool interface {
	Get() (net.Conn, error)
	GetWithTimeout(timeout time.Duration) (net.Conn, error)
	Close() error
	Remove(conn net.Conn) error
}

type CpConn

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

func (*CpConn) Close

func (conn *CpConn) Close() error

Close will push connection back to connection pool. It will not close the real connection.

func (*CpConn) Destroy

func (conn *CpConn) Destroy() error

Destroy will close connection and release connection from connection pool.

type GncpPool

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

GncpPool implements ConnPool interface. Use channel buffer connections.

func NewPool

func NewPool(minConn, maxConn int, connCreator func() (net.Conn, error)) (*GncpPool, error)

NewPool return new ConnPool. It base on channel. It will init minConn connections in channel first. When Get()/GetWithTimeout called, if channel still has connection it will get connection from channel. Otherwise GncpPool check number of connection which had already created as the number are less than maxConn, it use connCreator function to create new connection.

func (*GncpPool) Close

func (p *GncpPool) Close() error

Close close the connection pool. When close the connection pool it also close all connection already in connection pool. If connection not put back in connection it will not close. But it will close when it put back.

func (*GncpPool) Get

func (p *GncpPool) Get() (net.Conn, error)

Get get connection from connection pool. If connection poll is empty and alreay created connection number less than Max number of connection it will create new one. Otherwise it wil wait someone put connection back.

func (*GncpPool) GetWithContext

func (p *GncpPool) GetWithContext(ctx context.Context) (net.Conn, error)

func (*GncpPool) GetWithTimeout

func (p *GncpPool) GetWithTimeout(timeout time.Duration) (net.Conn, error)

GetWithTimeout can let you get connection wait for a time duration. If cannot get connection in this time. It will return TimeOutError.

func (*GncpPool) Put

func (p *GncpPool) Put(conn net.Conn) error

Put can put connection back in connection pool. If connection has been closed, the conneciton will be close too.

func (*GncpPool) Remove

func (p *GncpPool) Remove(conn net.Conn) error

RemoveConn let connection not belong connection pool.And it will close connection.

Jump to

Keyboard shortcuts

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