Skip to content

Commit

Permalink
feat: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroyky committed Nov 17, 2023
1 parent 26514b4 commit a95ca14
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
13 changes: 4 additions & 9 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,8 @@ func (c *Client) onItem(item *Item, fn func(*Client, *bufio.ReadWriter, *Item) e
if err != nil {
return err
}
p, ok := c.pools[addr.String()]
if !ok {
p = c.newPool(addr)
}

p := c.getOrNewPool(addr)
cn, err := p.getConn()
if err != nil {
return err
Expand Down Expand Up @@ -286,7 +284,7 @@ func (c *Client) Touch(key string, seconds int32) (err error) {
})
}

func (c *Client) newPool(addr net.Addr) *pool {
func (c *Client) getOrNewPool(addr net.Addr) *pool {
if c.pools == nil {
c.pools = make(map[string]*pool)
}
Expand All @@ -310,10 +308,7 @@ func (c *Client) withKeyAddr(key string, fn func(net.Addr) error) (err error) {
}

func (c *Client) withAddrRw(addr net.Addr, fn func(*bufio.ReadWriter) error) (err error) {
p, ok := c.pools[addr.String()]
if !ok {
p = c.newPool(addr)
}
p := c.getOrNewPool(addr)
cn, err := p.getConn()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func BenchmarkOnItem(b *testing.B) {

addr := fakeServer.Addr()
c := New(addr.String())
if _, err := c.newPool(addr).getConn(); err != nil {
if _, err := c.getOrNewPool(addr).getConn(); err != nil {
b.Fatal("failed to initialize connection to fake server")
}

Expand Down
3 changes: 1 addition & 2 deletions memcache/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func (p *pool) getConn() (*conn, error) {
}

// return latest freeconn or wait until to become free
cn = <-p.freeconns
return cn, nil
return <-p.freeconns, nil
}

func (p *pool) isNewConnOk() bool {
Expand Down

0 comments on commit a95ca14

Please sign in to comment.