netpool test. wip
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 32s
Build and Publish / BuildAndDeploy (push) Successful in 3m2s

This commit is contained in:
matst80
2024-11-21 21:23:38 +01:00
parent 1f7f161e62
commit 5348c33f3b
11 changed files with 140 additions and 89 deletions

View File

@@ -6,10 +6,13 @@ import (
"log"
"net"
"time"
"github.com/yudhasubki/netpool"
)
type Connection struct {
address string
pool netpool.Netpooler
count uint64
}
@@ -56,9 +59,10 @@ type FrameData interface {
FromBytes([]byte) error
}
func NewConnection(address string) *Connection {
func NewConnection(address string, pool netpool.Netpooler) *Connection {
return &Connection{
count: 0,
pool: pool,
address: address,
}
}
@@ -75,7 +79,8 @@ func SendFrame(conn net.Conn, data *FrameWithPayload) error {
}
func (c *Connection) CallAsync(msg FrameType, payload []byte, ch chan<- FrameWithPayload) (net.Conn, error) {
conn, err := net.Dial("tcp", c.address)
conn, err := c.pool.Get()
//conn, err := net.Dial("tcp", c.address)
if err != nil {
return conn, err
}
@@ -102,7 +107,7 @@ func (c *Connection) Call(msg FrameType, data []byte) (*FrameWithPayload, error)
return nil, err
}
defer conn.Close()
defer c.pool.Put(conn, nil) // conn.Close()
defer close(ch)
select {
@@ -154,7 +159,8 @@ func (c *Connection) Listen() (*GenericListener, error) {
for !ret.StopListener {
connection, err := l.Accept()
if err != nil {
log.Fatalf("Error accepting connection: %v\n", err)
log.Printf("Error accepting connection: %v\n", err)
continue
}
go ret.HandleConnection(connection)
}
@@ -194,12 +200,12 @@ func (l *GenericListener) HandleFrame(conn net.Conn, frame *FrameWithPayload) er
defer close(resultChan)
err := handler(frame, resultChan)
if err != nil {
log.Fatalf("Error handling frame: %v\n", err)
log.Fatalf("Error handling frame: %s\n", err)
}
result := <-resultChan
err = SendFrame(conn, &result)
if err != nil {
log.Fatalf("Error sending frame: %v\n", err)
log.Fatalf("Error sending frame: %s\n", err)
}
}()
} else {