110

GitHub - Broadroad/gpool: A go tcp connection pool

 5 years ago
source link: https://github.com/Broadroad/gpool
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

gpoolGoDoc Build Status

A go tcp connection pool

Function

  • gpool support net.Conn interface
  • reuse connection in gpool
  • get connection from gpool will error when pool is full

TODO

  • get connection will block until timeout or a idle connection return
  • connection will be closed when idle for some time duration

Usage

install with this command:

go get github.com/broadroad/gpool

and then use like this bellow:

// create factory to create connection
factory    = func() (net.Conn, error) { return net.Dial(network, address) }

// create poolConfig
poolConfig = &PoolConfig{
	InitCap:     5,
	MaxCap:      30,
	Factory:     factory,
	IdleTimeout: 15 * time.Second,
}

// create a new gpool
p, err := NewGPool(poolConfig)
if err != nil {
    fmt.Println(err)
}

// get a connection from gpool
conn, err := p.Get()
if err != nil {
	t.Errorf("Get error: %s", err)
}

// return a connection to gpool
conn.Close()

// release all connection in gpool
p.Close()

License

The Apache License 2.0 - see LISENCE for more details

Issue

It will be very pleasure if you give some issue or pr. Feel free to contact [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK