Various performance and correctness improvements.
This commit is contained in:
@@ -13,6 +13,9 @@ type WorkerFactory func(ctx context.Context, id string) (Worker, error)
|
||||
// Pool Config
|
||||
|
||||
type PoolConfig struct {
|
||||
InboxBufferSize int
|
||||
EventsBufferSize int
|
||||
ErrorsBufferSize int
|
||||
ConnectionConfig *transport.ConnectionConfig
|
||||
WorkerFactory WorkerFactory
|
||||
WorkerConfig *WorkerConfig
|
||||
@@ -33,6 +36,9 @@ func NewPoolConfig(options ...PoolOption) (*PoolConfig, error) {
|
||||
|
||||
func GetDefaultPoolConfig() *PoolConfig {
|
||||
return &PoolConfig{
|
||||
InboxBufferSize: 256,
|
||||
EventsBufferSize: 10,
|
||||
ErrorsBufferSize: 10,
|
||||
ConnectionConfig: nil,
|
||||
WorkerFactory: nil,
|
||||
WorkerConfig: nil,
|
||||
@@ -68,6 +74,43 @@ func ValidatePoolConfig(config *PoolConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBufferSize(value int) error {
|
||||
if value < 1 {
|
||||
return InvalidBufferSize
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WithInboxBufferSize(value int) PoolOption {
|
||||
return func(c *PoolConfig) error {
|
||||
if err := validateBufferSize(value); err != nil {
|
||||
return err
|
||||
}
|
||||
c.InboxBufferSize = value
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithEventsBufferSize(value int) PoolOption {
|
||||
return func(c *PoolConfig) error {
|
||||
if err := validateBufferSize(value); err != nil {
|
||||
return err
|
||||
}
|
||||
c.EventsBufferSize = value
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithErrorsBufferSize(value int) PoolOption {
|
||||
return func(c *PoolConfig) error {
|
||||
if err := validateBufferSize(value); err != nil {
|
||||
return err
|
||||
}
|
||||
c.ErrorsBufferSize = value
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithConnectionConfig(cc *transport.ConnectionConfig) PoolOption {
|
||||
return func(c *PoolConfig) error {
|
||||
err := transport.ValidateConnectionConfig(cc)
|
||||
|
||||
@@ -12,6 +12,9 @@ func TestNewPoolConfig(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, conf, &PoolConfig{
|
||||
InboxBufferSize: 256,
|
||||
EventsBufferSize: 10,
|
||||
ErrorsBufferSize: 10,
|
||||
ConnectionConfig: nil,
|
||||
WorkerConfig: nil,
|
||||
WorkerFactory: nil,
|
||||
@@ -22,6 +25,9 @@ func TestDefaultPoolConfig(t *testing.T) {
|
||||
conf := GetDefaultPoolConfig()
|
||||
|
||||
assert.Equal(t, conf, &PoolConfig{
|
||||
InboxBufferSize: 256,
|
||||
EventsBufferSize: 10,
|
||||
ErrorsBufferSize: 10,
|
||||
ConnectionConfig: nil,
|
||||
WorkerConfig: nil,
|
||||
WorkerFactory: nil,
|
||||
@@ -39,6 +45,19 @@ func TestApplyPoolOptions(t *testing.T) {
|
||||
assert.Equal(t, 0*time.Second, conf.ConnectionConfig.WriteTimeout)
|
||||
}
|
||||
|
||||
func TestWithBufferSizes(t *testing.T) {
|
||||
conf := &PoolConfig{}
|
||||
|
||||
err := applyPoolOptions(conf,
|
||||
WithInboxBufferSize(100),
|
||||
WithEventsBufferSize(20),
|
||||
WithErrorsBufferSize(20),
|
||||
)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100, conf.InboxBufferSize)
|
||||
assert.Equal(t, 20, conf.EventsBufferSize)
|
||||
}
|
||||
|
||||
func TestWithConnectionConfig(t *testing.T) {
|
||||
conf := &PoolConfig{}
|
||||
opt := WithConnectionConfig(&transport.ConnectionConfig{WriteTimeout: 1 * time.Second})
|
||||
|
||||
@@ -7,6 +7,7 @@ var (
|
||||
// Config errors
|
||||
InvalidKeepaliveTimeout = errors.New("keepalive timeout cannot be negative")
|
||||
InvalidMaxQueueSize = errors.New("maximum queue size cannot be negative")
|
||||
InvalidBufferSize = errors.New("buffer size must be greater than zero")
|
||||
|
||||
// Pool errors
|
||||
ErrPoolClosed = errors.New("pool is closed")
|
||||
|
||||
+3
-3
@@ -89,9 +89,9 @@ func NewPool(ctx context.Context, config *PoolConfig, logger *slog.Logger,
|
||||
ctx: pctx,
|
||||
cancel: cancel,
|
||||
peers: make(map[string]*Peer),
|
||||
inbox: make(chan InboxMessage, 256),
|
||||
events: make(chan PoolEvent, 10),
|
||||
errors: make(chan error, 10),
|
||||
inbox: make(chan InboxMessage, config.InboxBufferSize),
|
||||
events: make(chan PoolEvent, config.EventsBufferSize),
|
||||
errors: make(chan error, config.ErrorsBufferSize),
|
||||
dialer: transport.NewDialer(),
|
||||
config: config,
|
||||
logger: logger,
|
||||
|
||||
+29
-43
@@ -1,9 +1,10 @@
|
||||
package outbound
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"context"
|
||||
"git.wisehodl.dev/jay/go-honeybee/queue"
|
||||
"git.wisehodl.dev/jay/go-honeybee/transport"
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@@ -17,11 +18,6 @@ type Worker interface {
|
||||
Send(data []byte) error
|
||||
}
|
||||
|
||||
type ReceivedMessage struct {
|
||||
data []byte
|
||||
receivedAt time.Time
|
||||
}
|
||||
|
||||
type DefaultWorker struct {
|
||||
id string
|
||||
conn atomic.Pointer[transport.Connection]
|
||||
@@ -59,11 +55,12 @@ func NewWorker(
|
||||
func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
dial := make(chan struct{}, 1)
|
||||
newConn := make(chan *transport.Connection, 1)
|
||||
messages := make(chan ReceivedMessage, 256)
|
||||
toQueue := make(chan types.ReceivedMessage, 256)
|
||||
toForwarder := make(chan types.ReceivedMessage, 256)
|
||||
keepalive := make(chan struct{}, 1)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(4)
|
||||
wg.Add(5)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
@@ -77,7 +74,12 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
RunForwarder(w.id, w.ctx, messages, pool.Inbox, w.config.MaxQueueSize)
|
||||
queue.RunQueue(w.id, w.ctx, toQueue, toForwarder, w.config.MaxQueueSize)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
RunForwarder(w.id, w.ctx, toForwarder, pool.Inbox)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
@@ -85,7 +87,7 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
session := &Session{
|
||||
id: w.id,
|
||||
connPtr: &w.conn,
|
||||
messages: messages,
|
||||
messages: toQueue,
|
||||
heartbeat: w.heartbeat,
|
||||
dial: dial,
|
||||
keepalive: keepalive,
|
||||
@@ -124,7 +126,7 @@ type Session struct {
|
||||
id string
|
||||
connPtr *atomic.Pointer[transport.Connection]
|
||||
|
||||
messages chan<- ReceivedMessage
|
||||
messages chan<- types.ReceivedMessage
|
||||
heartbeat chan<- struct{}
|
||||
dial chan<- struct{}
|
||||
|
||||
@@ -203,7 +205,7 @@ func RunReader(
|
||||
ctx context.Context,
|
||||
onStop func(),
|
||||
conn *transport.Connection,
|
||||
messages chan<- ReceivedMessage,
|
||||
messages chan<- types.ReceivedMessage,
|
||||
heartbeat chan<- struct{},
|
||||
) {
|
||||
defer func() {
|
||||
@@ -222,7 +224,7 @@ func RunReader(
|
||||
}
|
||||
|
||||
// send message forward
|
||||
messages <- ReceivedMessage{data: data, receivedAt: time.Now()}
|
||||
messages <- types.ReceivedMessage{Data: data, ReceivedAt: time.Now()}
|
||||
|
||||
// send heartbeat
|
||||
select {
|
||||
@@ -254,43 +256,27 @@ func RunStopMonitor(
|
||||
func RunForwarder(
|
||||
id string,
|
||||
ctx context.Context,
|
||||
messages <-chan ReceivedMessage,
|
||||
messages <-chan types.ReceivedMessage,
|
||||
inbox chan<- InboxMessage,
|
||||
maxQueueSize int,
|
||||
) {
|
||||
queue := list.New()
|
||||
|
||||
for {
|
||||
var out chan<- InboxMessage
|
||||
var next ReceivedMessage
|
||||
|
||||
// enable inbox if it is populated
|
||||
if queue.Len() > 0 {
|
||||
out = inbox
|
||||
|
||||
// read the first message in the queue
|
||||
next = queue.Front().Value.(ReceivedMessage)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case msg := <-messages:
|
||||
// limit queue size if maximum is configured
|
||||
if maxQueueSize > 0 && queue.Len() >= maxQueueSize {
|
||||
// drop oldest message
|
||||
queue.Remove(queue.Front())
|
||||
case msg, ok := <-messages:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
|
||||
case inbox <- InboxMessage{
|
||||
ID: id,
|
||||
Data: msg.Data,
|
||||
ReceivedAt: msg.ReceivedAt,
|
||||
}:
|
||||
}
|
||||
// add new message
|
||||
queue.PushBack(msg)
|
||||
// send next message to inbox
|
||||
case out <- InboxMessage{
|
||||
ID: id,
|
||||
Data: next.data,
|
||||
ReceivedAt: next.receivedAt,
|
||||
}:
|
||||
// drop message from queue
|
||||
queue.Remove(queue.Front())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package outbound
|
||||
import (
|
||||
"context"
|
||||
"git.wisehodl.dev/jay/go-honeybee/honeybeetest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -11,14 +11,14 @@ import (
|
||||
func TestRunForwarder(t *testing.T) {
|
||||
t.Run("message passes through to inbox", func(t *testing.T) {
|
||||
id := "wss://test"
|
||||
messages := make(chan ReceivedMessage, 1)
|
||||
messages := make(chan types.ReceivedMessage, 1)
|
||||
inbox := make(chan InboxMessage, 1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go RunForwarder(id, ctx, messages, inbox, 0)
|
||||
go RunForwarder(id, ctx, messages, inbox)
|
||||
|
||||
messages <- ReceivedMessage{data: []byte("hello"), receivedAt: time.Now()}
|
||||
messages <- types.ReceivedMessage{Data: []byte("hello"), ReceivedAt: time.Now()}
|
||||
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
select {
|
||||
@@ -29,75 +29,4 @@ func TestRunForwarder(t *testing.T) {
|
||||
}
|
||||
}, "expected message")
|
||||
})
|
||||
|
||||
t.Run("oldest message dropped when queue is full", func(t *testing.T) {
|
||||
id := "wss://test"
|
||||
messages := make(chan ReceivedMessage, 1)
|
||||
inbox := make(chan InboxMessage, 1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
gate := make(chan struct{})
|
||||
gatedInbox := make(chan InboxMessage)
|
||||
|
||||
// gate the inbox from receiving messages until the gate is opened
|
||||
go func() {
|
||||
<-gate
|
||||
for msg := range gatedInbox {
|
||||
inbox <- msg
|
||||
}
|
||||
}()
|
||||
|
||||
go RunForwarder(id, ctx, messages, gatedInbox, 2)
|
||||
|
||||
// send three messages while the gated inbox is blocked
|
||||
messages <- ReceivedMessage{data: []byte("first"), receivedAt: time.Now()}
|
||||
messages <- ReceivedMessage{data: []byte("second"), receivedAt: time.Now()}
|
||||
messages <- ReceivedMessage{data: []byte("third"), receivedAt: time.Now()}
|
||||
|
||||
// allow time for the first message to be dropped
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
|
||||
// close the gate, draining messages into the inbox
|
||||
close(gate)
|
||||
|
||||
// receive messages from the inbox
|
||||
var received []string
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
select {
|
||||
case msg := <-inbox:
|
||||
received = append(received, string(msg.Data))
|
||||
default:
|
||||
}
|
||||
return len(received) == 2
|
||||
}, "expected messages")
|
||||
|
||||
// first message was dropped
|
||||
assert.Equal(t, []string{"second", "third"}, received)
|
||||
|
||||
})
|
||||
|
||||
t.Run("exits on context cancellation", func(t *testing.T) {
|
||||
id := "wss://test"
|
||||
messages := make(chan ReceivedMessage, 1)
|
||||
inbox := make(chan InboxMessage, 1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
RunForwarder(id, ctx, messages, inbox, 0)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
cancel()
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
select {
|
||||
case <-done:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}, "expected done signal")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"git.wisehodl.dev/jay/go-honeybee/honeybeetest"
|
||||
"git.wisehodl.dev/jay/go-honeybee/transport"
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
@@ -18,7 +19,7 @@ func TestRunReader(t *testing.T) {
|
||||
conn, _, incomingData, _ := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
messages := make(chan ReceivedMessage, 1)
|
||||
messages := make(chan types.ReceivedMessage, 1)
|
||||
heartbeat := make(chan struct{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@@ -38,7 +39,7 @@ func TestRunReader(t *testing.T) {
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
select {
|
||||
case msg := <-messages:
|
||||
return string(msg.data) == "hello" && msg.receivedAt.After(before)
|
||||
return string(msg.Data) == "hello" && msg.ReceivedAt.After(before)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
@@ -49,7 +50,7 @@ func TestRunReader(t *testing.T) {
|
||||
conn, _, incomingData, _ := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
messages := make(chan ReceivedMessage, 10)
|
||||
messages := make(chan types.ReceivedMessage, 10)
|
||||
heartbeat := make(chan struct{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@@ -82,7 +83,7 @@ func TestRunReader(t *testing.T) {
|
||||
t.Run("incoming channel close calls conn.Close and onStop", func(t *testing.T) {
|
||||
conn, _, incomingData, _ := setupTestConnection(t)
|
||||
|
||||
messages := make(chan ReceivedMessage, 1)
|
||||
messages := make(chan types.ReceivedMessage, 1)
|
||||
heartbeat := make(chan struct{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@@ -120,7 +121,7 @@ func TestRunReader(t *testing.T) {
|
||||
t.Run("sessionDone close calls conn.Close and onStop", func(t *testing.T) {
|
||||
conn, _, _, _ := setupTestConnection(t)
|
||||
|
||||
messages := make(chan ReceivedMessage, 1)
|
||||
messages := make(chan types.ReceivedMessage, 1)
|
||||
heartbeat := make(chan struct{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"git.wisehodl.dev/jay/go-honeybee/honeybeetest"
|
||||
"git.wisehodl.dev/jay/go-honeybee/transport"
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
@@ -28,7 +29,7 @@ type testVars struct {
|
||||
keepalive chan struct{}
|
||||
heartbeat chan struct{}
|
||||
newConn chan *transport.Connection
|
||||
messages chan ReceivedMessage
|
||||
messages chan types.ReceivedMessage
|
||||
|
||||
conn *transport.Connection
|
||||
mockSocket *honeybeetest.MockSocket
|
||||
@@ -52,7 +53,7 @@ func setup(t *testing.T) (
|
||||
keepalive: make(chan struct{}, 1),
|
||||
heartbeat: make(chan struct{}, 1),
|
||||
newConn: make(chan *transport.Connection, 1),
|
||||
messages: make(chan ReceivedMessage, 256),
|
||||
messages: make(chan types.ReceivedMessage, 256),
|
||||
conn: conn,
|
||||
mockSocket: mockSocket,
|
||||
incomingData: incomingData,
|
||||
|
||||
Reference in New Issue
Block a user