-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add connection pooling for controller-runtime controllers
- Loading branch information
1 parent
e3bf14d
commit 7ad2712
Showing
12 changed files
with
332 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package controller | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
type pooledConnection struct { | ||
nc *nats.Conn | ||
pool *connectionPool | ||
hash string | ||
refCount int | ||
} | ||
|
||
func (pc *pooledConnection) Close() { | ||
if pc.pool != nil { | ||
pc.pool.release(pc.hash) | ||
} else if pc.nc != nil { | ||
pc.nc.Close() // Close directly if not pool-managed | ||
} | ||
} | ||
|
||
type connectionPool struct { | ||
connections map[string]*pooledConnection | ||
gracePeriod time.Duration | ||
mu sync.Mutex | ||
} | ||
|
||
func newConnPool(gracePeriod time.Duration) *connectionPool { | ||
return &connectionPool{ | ||
connections: make(map[string]*pooledConnection), | ||
gracePeriod: gracePeriod, | ||
} | ||
} | ||
|
||
func (p *connectionPool) Get(c *NatsConfig, pedantic bool) (*pooledConnection, error) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
hash, err := c.Hash() | ||
if err != nil { | ||
// If hash fails, create a new non-pooled connection | ||
nc, err := createNatsConn(c, pedantic) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &pooledConnection{nc: nc}, nil | ||
} | ||
|
||
if pc, ok := p.connections[hash]; ok && !pc.nc.IsClosed() { | ||
pc.refCount++ | ||
return pc, nil | ||
} | ||
|
||
nc, err := createNatsConn(c, pedantic) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pc := &pooledConnection{ | ||
nc: nc, | ||
pool: p, | ||
hash: hash, | ||
refCount: 1, | ||
} | ||
p.connections[hash] = pc | ||
|
||
return pc, nil | ||
} | ||
|
||
func (p *connectionPool) release(hash string) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
pc, ok := p.connections[hash] | ||
if !ok { | ||
return | ||
} | ||
|
||
pc.refCount-- | ||
if pc.refCount < 1 { | ||
go func() { | ||
if p.gracePeriod > 0 { | ||
time.Sleep(p.gracePeriod) | ||
} | ||
|
||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
if pc, ok := p.connections[hash]; ok && pc.refCount < 1 { | ||
pc.nc.Close() | ||
delete(p.connections, hash) | ||
} | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package controller | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
natsservertest "github.com/nats-io/nats-server/v2/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConnPool(t *testing.T) { | ||
t.Parallel() | ||
|
||
s := natsservertest.RunRandClientPortServer() | ||
defer s.Shutdown() | ||
|
||
c1 := &NatsConfig{ | ||
ClientName: "Client 1", | ||
ServerURL: s.ClientURL(), | ||
} | ||
|
||
c2 := &NatsConfig{ | ||
ClientName: "Client 1", | ||
ServerURL: s.ClientURL(), | ||
} | ||
|
||
c3 := &NatsConfig{ | ||
ClientName: "Client 2", | ||
ServerURL: s.ClientURL(), | ||
} | ||
|
||
pool := newConnPool(0) | ||
|
||
var conn1, conn2, conn3 *pooledConnection | ||
var err1, err2, err3 error | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(3) | ||
|
||
go func() { | ||
conn1, err1 = pool.Get(c1, true) | ||
wg.Done() | ||
}() | ||
go func() { | ||
conn2, err2 = pool.Get(c2, true) | ||
wg.Done() | ||
}() | ||
go func() { | ||
conn3, err3 = pool.Get(c3, true) | ||
wg.Done() | ||
}() | ||
wg.Wait() | ||
|
||
require := require.New(t) | ||
|
||
require.NoError(err1) | ||
require.NoError(err2) | ||
require.NoError(err3) | ||
|
||
require.Same(conn1, conn2) | ||
require.NotSame(conn1, conn3) | ||
require.NotSame(conn2, conn3) | ||
|
||
conn1.Close() | ||
conn3.Close() | ||
|
||
time.Sleep(time.Second) | ||
|
||
require.False(conn1.nc.IsClosed()) | ||
require.False(conn2.nc.IsClosed()) | ||
require.True(conn3.nc.IsClosed()) | ||
|
||
conn4, err4 := pool.Get(c1, true) | ||
require.NoError(err4) | ||
require.Same(conn1, conn4) | ||
require.Same(conn2, conn4) | ||
|
||
conn2.Close() | ||
conn4.Close() | ||
|
||
time.Sleep(time.Second) | ||
|
||
require.True(conn1.nc.IsClosed()) | ||
require.True(conn2.nc.IsClosed()) | ||
require.True(conn3.nc.IsClosed()) | ||
require.True(conn4.nc.IsClosed()) | ||
|
||
conn5, err5 := pool.Get(c1, true) | ||
require.NoError(err5) | ||
require.NotSame(conn1, conn5) | ||
|
||
conn5.Close() | ||
|
||
time.Sleep(time.Second) | ||
|
||
require.True(conn5.nc.IsClosed()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.