Skip to content

Commit

Permalink
feat: import closepool and errclass from rbmk-project/x (#9)
Browse files Browse the repository at this point in the history
These two packages are well tested and, IMHO, stable enough at this
point to justify putting them into the common libs.

I will follow up and deprecate them inside the `x` collection.
  • Loading branch information
bassosimone authored Nov 30, 2024
1 parent 96f3995 commit cc92766
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 1 deletion.
52 changes: 52 additions & 0 deletions closepool/closepool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: GPL-3.0-or-later

// Package closepool allows pooling [io.Closer] instances
// and closing them in a single operation.
package closepool

import (
"errors"
"io"
"slices"
"sync"
)

// Pool allows pooling a set of [io.Closer].
//
// The zero value is ready to use.
type Pool struct {
// handles contains the [io.Closer] to close.
handles []io.Closer

// mu provides mutual exclusion.
mu sync.Mutex
}

// Add adds a given [io.Closer] to the pool.
func (p *Pool) Add(conn io.Closer) {
p.mu.Lock()
p.handles = append(p.handles, conn)
p.mu.Unlock()
}

// Close closes all the [io.Closer] inside the pool iterating
// in backward order. Therefore, if one registers a TCP connection
// and then the corresponding TLS connection, the TLS connection
// is closed first. The returned error is the join of all the
// errors that occurred when closing connections.
func (p *Pool) Close() error {
// Lock and copy the [io.Closer] to close.
p.mu.Lock()
handles := p.handles
p.handles = nil
p.mu.Unlock()

// Close all the [io.Closer].
var errv []error
for _, handle := range slices.Backward(handles) {
if err := handle.Close(); err != nil {
errv = append(errv, err)
}
}
return errors.Join(errv...)
}
120 changes: 120 additions & 0 deletions closepool/closepool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package closepool_test

import (
"errors"
"sync/atomic"
"testing"
"time"

"github.com/rbmk-project/common/closepool"
)

// mockCloser implements io.Closer for testing
type mockCloser struct {
closed atomic.Int64
err error
}

// t0 is the time when we started running
var t0 = time.Now()

func (m *mockCloser) Close() error {
m.closed.Add(int64(time.Since(t0)))
return m.err
}

func TestPool(t *testing.T) {
t.Run("successful close", func(t *testing.T) {
pool := closepool.Pool{}
m1 := &mockCloser{}
m2 := &mockCloser{}

pool.Add(m1)
pool.Add(m2)

err := pool.Close()
if err != nil {
t.Errorf("expected no error, got %v", err)
}

if m1.closed.Load() <= 0 {
t.Error("first closer was not closed")
}
if m2.closed.Load() <= 0 {
t.Error("second closer was not closed")
}
})

t.Run("close order", func(t *testing.T) {
pool := closepool.Pool{}

m1 := &mockCloser{
err: nil,
}
m2 := &mockCloser{
err: nil,
}

pool.Add(m1) // Added first
pool.Add(m2) // Added second

// Should close in reverse order
err := pool.Close()
if err != nil {
t.Errorf("expected no error, got %v", err)
}

if m1.closed.Load() <= m2.closed.Load() {
t.Error("expected m1 to be closed after m2")
}
})

t.Run("error handling", func(t *testing.T) {
pool := closepool.Pool{}
expectedErr1 := errors.New("close error #1")
expectedErr2 := errors.New("close error #2")

m1 := &mockCloser{err: expectedErr1}
m2 := &mockCloser{err: expectedErr2}

pool.Add(m1)
pool.Add(m2)

err := pool.Close()
if err == nil {
t.Fatalf("expected error, got nil")
}

t.Log(err)
if errors.Join(expectedErr2, expectedErr1).Error() != err.Error() {
t.Errorf("expected error to include both errors, got %v", err)
}
})

t.Run("concurrent usage", func(t *testing.T) {
pool := closepool.Pool{}
done := make(chan struct{})

// Concurrently add closers
go func() {
for i := 0; i < 100; i++ {
pool.Add(&mockCloser{})
}
close(done)
}()

// Add more closers from main goroutine
for i := 0; i < 100; i++ {
pool.Add(&mockCloser{})
}

<-done // Wait for goroutine to finish

err := pool.Close()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
})
}
Loading

0 comments on commit cc92766

Please sign in to comment.