Giter Club home page Giter Club logo

gncp's Introduction

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.