Skip to content

Commit bdcce47

Browse files
committed
refactor: replace buraksezer/pool with buraksezer/connpool
1 parent b5a0f44 commit bdcce47

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/buraksezer/olric
33
go 1.15
44

55
require (
6+
github.com/buraksezer/connpool v0.4.0
67
github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72
7-
github.com/buraksezer/pool v3.0.0+incompatible
88
github.com/cespare/xxhash v1.1.0
99
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
1010
github.com/hashicorp/go-multierror v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZ
33
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
44
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
55
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
6+
github.com/buraksezer/connpool v0.4.0 h1:fNLvWu0FOtJxL7Sqetm6+040mhZWVs8c6vrke14SEKw=
7+
github.com/buraksezer/connpool v0.4.0/go.mod h1:qPiG7gKXo+EjrwG/yqn2StZM4ek6gcYnnGgFIVKN6b0=
68
github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72 h1:fUmDBbSvv1uOzo/t8WaxZMVb7BxJ8JECo5lGoR9c5bA=
79
github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72/go.mod h1:OEE5igu/CDjGegM1Jn6ZMo7R6LlV/JChAkjfQQIRLpg=
810
github.com/buraksezer/pool v3.0.0+incompatible h1:MXcI3YkBnElnbJ8ZPIYqa0dia3qqHDNto1E004YxseA=

internal/transport/client.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
package transport
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"net"
2021
"os"
2122
"sync"
2223

24+
"github.com/buraksezer/connpool"
2325
"github.com/buraksezer/olric/config"
2426
"github.com/buraksezer/olric/internal/protocol"
25-
"github.com/buraksezer/pool"
2627
)
2728

2829
// Client is the client implementation for the internal TCP server.
@@ -32,7 +33,7 @@ type Client struct {
3233

3334
dialer *net.Dialer
3435
config *config.Client
35-
pools map[string]pool.Pool
36+
pools map[string]connpool.Pool
3637
}
3738

3839
// NewClient returns a new Client.
@@ -49,7 +50,7 @@ func NewClient(cc *config.Client) *Client {
4950
c := &Client{
5051
dialer: dialer,
5152
config: cc,
52-
pools: make(map[string]pool.Pool),
53+
pools: make(map[string]connpool.Pool),
5354
}
5455
return c
5556
}
@@ -62,7 +63,7 @@ func (c *Client) Close() {
6263
p.Close()
6364
}
6465
// Reset pool
65-
c.pools = make(map[string]pool.Pool)
66+
c.pools = make(map[string]connpool.Pool)
6667
}
6768

6869
// ClosePool closes the underlying connections in a pool,
@@ -81,34 +82,36 @@ func (c *Client) ClosePool(addr string) {
8182
}
8283

8384
// pool creates a new pool for a given addr or returns an exiting one.
84-
func (c *Client) pool(addr string) (pool.Pool, error) {
85+
func (c *Client) pool(addr string) (connpool.Pool, error) {
8586
factory := func() (net.Conn, error) {
8687
return c.dialer.Dial("tcp", addr)
8788
}
8889

8990
c.mu.Lock()
9091
defer c.mu.Unlock()
9192

92-
cpool, ok := c.pools[addr]
93+
p, ok := c.pools[addr]
9394
if ok {
94-
return cpool, nil
95+
return p, nil
9596
}
9697

97-
cpool, err := pool.NewChannelPool(c.config.MinConn, c.config.MaxConn, factory)
98+
p, err := connpool.NewChannelPool(c.config.MinConn, c.config.MaxConn, factory)
9899
if err != nil {
99100
return nil, err
100101
}
101-
c.pools[addr] = cpool
102-
return cpool, nil
102+
c.pools[addr] = p
103+
return p, nil
103104
}
104105

105106
func (c *Client) conn(addr string) (net.Conn, error) {
106-
cpool, err := c.pool(addr)
107+
p, err := c.pool(addr)
107108
if err != nil {
108109
return nil, err
109110
}
110111

111-
conn, err := cpool.Get()
112+
// Use context.Background here because we dont want to change the default
113+
// behaviour.
114+
conn, err := p.Get(context.Background())
112115
if err != nil {
113116
return nil, err
114117
}
@@ -138,7 +141,7 @@ func (c *Client) teardownConn(rawConn net.Conn, dead bool) {
138141
c.teardownConnWithTimeout(rawConn.(*ConnWithTimeout), dead)
139142
return
140143
}
141-
pc, _ := rawConn.(*pool.PoolConn)
144+
pc, _ := rawConn.(*connpool.PoolConn)
142145
pc.MarkUnusable()
143146
err := pc.Close()
144147
if err != nil {

internal/transport/stream.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"io"
2121
"os"
2222

23+
"github.com/buraksezer/connpool"
2324
"github.com/buraksezer/olric/internal/protocol"
24-
"github.com/buraksezer/pool"
2525
)
2626

2727
func readFromStream(conn io.ReadWriteCloser, bufCh chan<- protocol.EncodeDecoder, errCh chan<- error) {
@@ -62,14 +62,14 @@ func (c *Client) CreateStream(ctx context.Context, addr string, read chan<- prot
6262
return err
6363
}
6464

65-
conn, err := p.Get()
65+
conn, err := p.Get(ctx)
6666
if err != nil {
6767
return err
6868
}
6969

7070
defer func() {
7171
// marks the connection not usable any more, to let the pool close it instead of returning it to pool.
72-
pc, _ := conn.(*pool.PoolConn)
72+
pc, _ := conn.(*connpool.PoolConn)
7373
pc.MarkUnusable()
7474
if err = pc.Close(); err != nil {
7575
_, _ = fmt.Fprintf(os.Stderr, "[ERROR] Failed to close connection: %v", err)

internal/transport/timeout.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"net"
1919
"time"
2020

21-
"github.com/buraksezer/pool"
21+
"github.com/buraksezer/connpool"
2222
)
2323

2424
// ConnWithTimeout denotes a composite type which can used to implement i/o timeout feature for TCP sockets.
@@ -60,17 +60,17 @@ func (c *ConnWithTimeout) UnsetDeadline() error {
6060
return c.Conn.SetDeadline(time.Time{})
6161
}
6262

63-
// MarkUnusable() marks the connection not usable any more, to let the pool close it instead of returning it to pool.
63+
// MarkUnusable marks the connection not usable any more, to let the pool close it instead of returning it to pool.
6464
// Wrapper around pool.PoolConn.MarkUnusable
6565
func (c *ConnWithTimeout) MarkUnusable() {
66-
if conn, ok := c.Conn.(*pool.PoolConn); ok {
66+
if conn, ok := c.Conn.(*connpool.PoolConn); ok {
6767
conn.MarkUnusable()
6868
}
6969
}
7070

7171
// Close closes the connection.
7272
func (c *ConnWithTimeout) Close() error {
73-
conn, ok := c.Conn.(*pool.PoolConn)
73+
conn, ok := c.Conn.(*connpool.PoolConn)
7474
if ok {
7575
return conn.Close()
7676
}

0 commit comments

Comments
 (0)