Skip to content
Golang net.Conn Connection Pool
Branch: master
Clone or download
sabey added support for IdleTimeout
if IdleTimeout is above 0 support for removing idle connections is enabled
once a connection is moved to the available map, ticker will start and will watch and remove idle connections from our pool
when there are no available connections (ie: all connections are active or there are no connections at all) our ticker will not run
Latest commit 972bd0a Sep 5, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE init Sep 4, 2018
README.md init Sep 4, 2018
buffer.go init Sep 4, 2018
config.go added support for IdleTimeout Sep 5, 2018
connection.go added support for IdleTimeout Sep 5, 2018
error.go init Sep 4, 2018
lagoon.go added support for IdleTimeout Sep 5, 2018
lagoon_test.go added support for IdleTimeout Sep 5, 2018
lagoon_tick.go added support for IdleTimeout Sep 5, 2018

README.md

Lagoon

Golang net.Conn Connection Pool

GoDoc

This package was inspired by github.com/fatih/pool - This is NOT a fork, it will function differently!!!

The main differences are:

  • The connection pool is capped and Dial will block once the pool is full!
  • Connections are capped with a Buffer that can be optionally shared between pools.
  • Dial can Timeout if we fail to acquire a spot from the Buffer queue! (DialTimeout or similar should be used within the Dial function)
  • DialInitial can not garauntee that we will always dial the initial amount due to the possibility of a shared Buffer.

Install

go get -t -u sabey.co/lagoon

Usage

// create a shared buffer
buffer := CreateBuffer(10, time.Second*2)

// create a config for our lagoon instance
config := &Config{
	Dial: func() (net.Conn, error) {
		return net.DialTimeout("tcp", "service.local:25", time.Second*30)
	},
	DialInitial: 5,
	Buffer:      buffer,
}

// create a lagoon instance
l, err := CreateLagoon(config)

// dial
c, err := l.Dial()

// return connection back to the pool
c.Close()

// dial
c, err := l.Dial()

// remove connection from the pool on close
c.(*Connection).Disable()

// close connection
c.Close()
You can’t perform that action at this time.