ConnPool is a thread safe connection pool for net.Conn interface.

Related tags

Network connpool
Overview

ConnPool Go Reference Tests

ConnPool is a thread safe connection pool for net.Conn interface. It can be used to manage and reuse connections.

This package is a fork of fatih/pool. ConnPool is a vital part of buraksezer/olric. So I need to maintain it myself.

Fixed bugs:

Install and Usage

Install the package with:

go get github.com/buraksezer/connpool

Example

// create a factory() to be used with channel based pool
factory := func() (net.Conn, error) { return net.Dial("tcp", "127.0.0.1:4000") }

// create a new channel based pool with an initial capacity of 5 and maximum
// capacity of 30. The factory will create 5 initial connections and put it
// into the pool.
p, err := connpool.NewChannelPool(5, 30, factory)

// now you can get a connection from the pool, if there is no connection
// available it will create a new one via the factory function.
conn, err := p.Get(context.Background())

// do something with conn and put it back to the pool by closing the connection
// (this doesn't close the underlying connection instead it's putting it back
// to the pool).
conn.Close()

// close the underlying connection instead of returning it to pool
// it is useful when acceptor has already closed connection and conn.Write() returns error
if pc, ok := conn.(*connpool.PoolConn); ok {
  pc.MarkUnusable()
  pc.Close()
}

// close pool any time you want, this closes all the connections inside a pool
p.Close()

// currently available connections in the pool
current := p.Len()

Credits

License

The MIT License (MIT) - see LICENSE for more details

You might also like...
Make TCP connection storm between server and client for benchmarking network stuff

Make TCP connection storm between server and client for benchmarking network stuff

A simple proxy to work with tcp connection

Proxy It is simple proxy to work with tcp connection HTTP TCP Getting Started pr

Resolved the issue that Windows cannot detect the Internet even if it does have an Internet connection.

win-connect 中文文档 Background This program is built to resolved the issue that Windows cannot detect the Internet even if it does have an Internet conne

SSHWaiterUtil - Wait for file to appear over an SSH connection

SSHWaiterUtil Simple util to wait for a remote file to appear, over SSH using pr

Gsshrun - Running commands via ssh on the server/hosting (if ssh support) specified in the connection file

Gsshrun - Running commands via ssh on the server/hosting (if ssh support) specified in the connection file

Go-db-connection-api - API REST in Go that connect to SQL DB and manage task of projects

Go Todo REST API Example A RESTful API example for simple application with Go It

“Dear Port80” is a zero-config TCP proxy server that hides SSH connection behind a HTTP server!

Dear Port80 About The Project: “Dear Port80” is a zero-config TCP proxy server that hides SSH connection behind a HTTP server! +---------------------

A golang universal connections pool.

gopool A golang universal connection pool. Features: Using arrays instead of channels to improve performance Automatically detect live server nodes an

HTTP IP Proxy  Pool
HTTP IP Proxy Pool

HTTP IP Proxy Pool

Comments
  • Factory failure drains available connection max from pool

    Factory failure drains available connection max from pool

    Issue

    If the provided factory returns an error, the pool's total connection limit will permanently decrease by one. Provided this happens enough times, the pool can become deadlocked, with zero capacity.

    Here is a playground example of the issue: https://go.dev/play/p/lAy0_uoKMFj

    Notice after the first two failures (the number of max connections in the pool) the errors change, due to the pool itself never being able to attempt to fetch new connections.

    Expected Outcome

    When the factory fails, the reserved slot in the pool should be returned to the pool.

    opened by randall-fulton 2
  • Marking connections unavailable eventually drains the pool

    Marking connections unavailable eventually drains the pool

    Whenever a PoolConn.MarkUnusable() is called, the channelPool will never refresh that connection. This is due to the semaphore channel not being read once that connection is eventually closed.

    Here is a test to reproduce the issue:

    func TestPool_UnavailableConnections(t *testing.T) {
    	go func() {
    		_ = http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    			w.Write([]byte("hello"))
    			w.WriteHeader(http.StatusOK)
    		}))
    	}()
    
    	time.Sleep(3 * time.Second)
    
    	factory := func() (net.Conn, error) {
    		return net.DialTimeout("tcp", ":8080", 500*time.Millisecond)
    	}
    	pool, err := NewChannelPool(1, 1, factory)
    	if err != nil {
    		t.Fatal(err)
    	}
    
    	conn, err := pool.Get(context.Background())
    	if err != nil {
    		t.Fatal(err)
    	}
    	if conn == nil {
    		t.Fatal("invalid conn")
    	}
    
    	conn.(*PoolConn).MarkUnusable()
    	conn.Close()
    
    	ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
    	conn, err = pool.Get(ctx)
    	if err != nil {
    		t.Fatal(err)
    	}
    	if conn == nil {
    		t.Fatal("invalid conn")
    	}
    }
    

    If you run that same test without the MarkUnusable(), there is no issue.

    Should be a simple fix. We just need to pair a <-c.semaphore with the connection close.

    bug 
    opened by randall-fulton 2
  • Whether the conn should be restored deadline(Read or Write) when it is put in the channel pool

    Whether the conn should be restored deadline(Read or Write) when it is put in the channel pool

    like this

    func (p *PoolConn) Close() error {
    	p.mu.RLock()
    	defer p.mu.RUnlock()
    
    	if p.unusable {
    		if p.Conn != nil {
    			<-p.c.semaphore
    			return p.Conn.Close()
    		}
    		return nil
    	}
    	p.Conn.SetDeadline(time.Time{}) // not time out
    	return p.c.put(p.Conn)
    }
    
    opened by munding 0
  • use this package with grpc connections

    use this package with grpc connections

    What you think about generic connection pool? Some "connections" not provide net.Conn interface https://pkg.go.dev/google.golang.org/grpc?utm_source=godoc#ClientConn but will be very useful to make pool of such connections

    opened by vtolstov 0
Releases(v0.6.0)
Owner
Burak Sezer
Distributed systems and DevOps things
Burak Sezer
Record and replay a go net.Conn, mosting for testing.

fakeconn Record and replay a go net.Conn, mostly for testing and debugging. This package isn't finished, so you probably shouldn't use it. Recording f

null 0 Dec 5, 2021
wire protocol for multiplexing connections or streams into a single connection, based on a subset of the SSH Connection Protocol

qmux qmux is a wire protocol for multiplexing connections or streams into a single connection. It is based on the SSH Connection Protocol, which is th

Jeff Lindsay 205 Dec 26, 2022
Send network packets over a TCP or UDP connection.

Packet is the main class representing a single network message. It has a byte code indicating the type of the message and a []byte type payload.

Aero 71 Nov 28, 2022
Package socket provides a low-level network connection type which integrates with Go's runtime network poller to provide asynchronous I/O and deadline support. MIT Licensed.

socket Package socket provides a low-level network connection type which integrates with Go's runtime network poller to provide asynchronous I/O and d

Matt Layher 49 Dec 14, 2022
🤘 The native golang ssh client to execute your commands over ssh connection. 🚀🚀

Golang SSH Client. Fast and easy golang ssh client module. Goph is a lightweight Go SSH client focusing on simplicity! Installation ❘ Features ❘ Usage

Mohamed El Bahja 1.2k Dec 24, 2022
Golang connection multiplexing library

Yamux Yamux (Yet another Multiplexer) is a multiplexing library for Golang. It relies on an underlying connection to provide reliability and ordering,

HashiCorp 1.9k Jan 7, 2023
WebSocket Connection Smuggler

ws-smuggler ws-smuggler is websocket connection smuggling testing tool. It is similar to the this project, but it has been rewritten based on the web

HAHWUL 42 Jan 3, 2023
This package helps establish a websocket connection to the bilibili streaming server.

biliStreamClient This package helps establish a websocket connection to the bilibili streaming server. bilibili直播弹幕的WebSocket协议分析请参考:https://blog.csdn

JINGWEI ZHANG 24 Oct 25, 2022
Uses the Finger user information protocol to open a TCP connection that makes a request to a Finger server

Finger Client This client uses the Finger user information protocol to open a TCP connection that makes a request to a Finger server. Build and Run Ru

Linda Xiao 0 Oct 7, 2021