pool

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2023 License: MIT Imports: 4 Imported by: 4

README

TCP pooling

tag Go Version GoDoc Build Status Go report Coverage Contributors License

✨ Drop-in replacement to net.Conn with pooling and auto-reconnect.

🚀 Install

go get github.com/samber/go-tcp-pool

This library is v1 and follows SemVer strictly. No breaking changes will be made to exported APIs before v2.0.0.

💡 Usage

GoDoc: https://pkg.go.dev/github.com/samber/go-tcp-pool

Create a TCP connection pool
# Start a tcp server
ncat -l 9999 -k
import pool "github.com/samber/go-tcp-pool"

conn, err := pool.Dial("tcp", "localhost:9999")
if err != nil {
    log.Fatal(err)
}

conn.SetPoolSize(10)
conn.SetMaxRetries(10)
conn.SetRetryInterval(10 * time.Millisecond)

// a tcp connection will be used in a round-robin manner
n, err := conn.Write([]byte("Hello, world!\n"))
if err != nil {
    log.Fatal(err)
}

// will always return an error
conn.Read(...)

🚀 @TODO

  • Implement round-robin connection pool
  • Implement auto-reconnect
  • Implement Read()
  • Implement other load-balancing strategies
    • Max idle time
    • MinConn + MaxConn

🤝 Contributing

Don't hesitate ;)

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

👤 Contributors

Contributors

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

Copyright © 2023 Samuel Berthe.

This project is MIT licensed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TCPClient

type TCPClient struct {
	*net.TCPConn
	// contains filtered or unexported fields
}

func (*TCPClient) SetMaxRetries

func (c *TCPClient) SetMaxRetries(maxRetries int)

func (*TCPClient) SetRetryInterval

func (c *TCPClient) SetRetryInterval(retryInterval time.Duration)

func (*TCPClient) Write

func (c *TCPClient) Write(b []byte) (int, error)

type TCPPool

type TCPPool struct {
	// contains filtered or unexported fields
}

func Dial

func Dial(network, addr string) (*TCPPool, error)

Dial returns a new *TCPPool.

The new client connects to the remote address `raddr` on the network `network`, which must be "tcp", "tcp4", or "tcp6".

func DialTCP

func DialTCP(network string, laddr, raddr *net.TCPAddr) (*TCPPool, error)

DialTCP returns a new *TCPPool.

The new client connects to the remote address `raddr` on the network `network`, which must be "tcp", "tcp4", or "tcp6". If `laddr` is not nil, it is used as the local address for the connection.

func (*TCPPool) Close

func (p *TCPPool) Close() error

implements net.Conn

func (*TCPPool) LocalAddr

func (p *TCPPool) LocalAddr() net.Addr

implements net.Conn

func (*TCPPool) Read

func (p *TCPPool) Read(b []byte) (int, error)

implements net.Conn

func (*TCPPool) RemoteAddr

func (p *TCPPool) RemoteAddr() net.Addr

implements net.Conn

func (*TCPPool) SetDeadline

func (p *TCPPool) SetDeadline(t time.Time) error

implements net.Conn

func (*TCPPool) SetMaxRetries

func (p *TCPPool) SetMaxRetries(maxRetries int)

func (*TCPPool) SetPoolSize

func (p *TCPPool) SetPoolSize(size uint) error

func (*TCPPool) SetReadDeadline

func (p *TCPPool) SetReadDeadline(t time.Time) error

implements net.Conn

func (*TCPPool) SetRetryInterval

func (p *TCPPool) SetRetryInterval(retryInterval time.Duration)

func (*TCPPool) SetWriteDeadline

func (p *TCPPool) SetWriteDeadline(t time.Time) error

implements net.Conn

func (*TCPPool) Write

func (p *TCPPool) Write(b []byte) (int, error)

implements net.Conn

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL