Skip to content

Instantly share code, notes, and snippets.

@mstrYoda
Created February 5, 2023 19:59
Show Gist options
  • Save mstrYoda/344bd3cdc6146e2b568eb2e3399945d3 to your computer and use it in GitHub Desktop.
Save mstrYoda/344bd3cdc6146e2b568eb2e3399945d3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"os"
"sync"
"time"
)
type connPool struct {
idleConns map[int]net.Conn
idleCount int
lock sync.Mutex
}
func (pool *connPool) getConn() net.Conn {
pool.lock.Lock()
defer pool.lock.Unlock()
if pool.idleCount == 0 {
conn, err := net.Dial("tcp", "127.0.0.1:8080")
if err != nil {
return nil
}
return conn
}
conn := pool.idleConns[pool.idleCount]
delete(pool.idleConns, pool.idleCount)
pool.idleCount--
return conn
}
func (pool *connPool) putIdle(conn net.Conn) {
pool.lock.Lock()
defer pool.lock.Unlock()
pool.idleCount++
pool.idleConns[pool.idleCount] = conn
}
func main() {
pool := connPool{
idleConns: make(map[int]net.Conn),
idleCount: 0,
lock: sync.Mutex{},
}
for {
fmt.Fprintf(os.Stdout, "pool idle count: %d \n", pool.idleCount)
conn := pool.getConn()
n, err := conn.Write([]byte("hello"))
if err != nil {
fmt.Println(err)
}
fmt.Fprintf(os.Stdout, "writted: %d \n", n)
pool.putIdle(conn)
time.Sleep(3 * time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment