Skip to content

sokil/go-connection-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

go-connection-pool

Connection pool is a thread safe list of net.Conn

Go Report Card GoDoc Code Climate

Basic usage

socket, err := net.Listen("tcp", "127.0.0.1:8080")
  
// prepare connection pool
connectionPool := connectionPool.NewConnectionPool()
  
// accept connection
connection, err := socket.Accept()
    
// add connection to pool
connectionId := connectionPool.Add(connection)

// get connection and read
reader := bufio.NewReader(connectionPool.Get(connectionId))

// count of connections in pool
size := connectionPool.Size()

// send message to all connections in pool
connectionPool.Range(func(targetConnection net.Conn, targetConnectionId int) {
    writer := bufio.NewWriter(targetConnection)
    writer.WriteString("Some message\n")
    writer.Flush()
})

// remove connection from bool
connectionPool.Remove(connectionId)