Skip to content

Commit

Permalink
Merge pull request #16 from jumpeiMano/feature/rename
Browse files Browse the repository at this point in the history
Rename functions and variables
  • Loading branch information
jumpeiMano authored Feb 18, 2019
2 parents bd16b07 + 93eed13 commit e1716aa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
58 changes: 29 additions & 29 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Pool struct {
MaxLifetime time.Duration
dial func() (*Client, error)
mu sync.Mutex
freeConns []*PooledConnection
freeConns []*conn
open int
openerCh chan struct{}
connRequests map[uint64]chan connRequest
Expand All @@ -46,12 +46,12 @@ func NewPool(dial func() (*Client, error)) *Pool {
}

type connRequest struct {
pc *PooledConnection
*conn
err error
}

// PooledConnection represents a shared and reusable connection.
type PooledConnection struct {
// conn represents a shared and reusable connection.
type conn struct {
Pool *Pool
Client *Client
t time.Time
Expand Down Expand Up @@ -96,36 +96,36 @@ func (p *Pool) openNewConnection() {
p.maybeOpenNewConnections()
return
}
pc := &PooledConnection{
cn := &conn{
Pool: p,
Client: c,
t: time.Now(),
}
p.mu.Lock()
if !p.putConnLocked(pc, nil) {
if !p.putConnLocked(cn, nil) {
p.open--
p.mu.Unlock()
pc.Client.Close()
cn.Client.Close()
return
}
p.mu.Unlock()
return
}

// putConn is return connetion to the connection pool.
func (p *Pool) putConn(pc *PooledConnection, err error) error {
func (p *Pool) putConn(cn *conn, err error) error {
p.mu.Lock()
if !p.putConnLocked(pc, err) {
if !p.putConnLocked(cn, err) {
p.open--
p.mu.Unlock()
pc.Client.Close()
cn.Client.Close()
return err
}
p.mu.Unlock()
return err
}

func (p *Pool) putConnLocked(pc *PooledConnection, err error) bool {
func (p *Pool) putConnLocked(cn *conn, err error) bool {
if p.closed {
return false
}
Expand All @@ -140,32 +140,32 @@ func (p *Pool) putConnLocked(pc *PooledConnection, err error) bool {
}
delete(p.connRequests, reqKey)
req <- connRequest{
pc: pc,
err: err,
conn: cn,
err: err,
}
} else {
p.freeConns = append(p.freeConns, pc)
p.freeConns = append(p.freeConns, cn)
p.startCleanerLocked()
}
return true
}

// get will return an available pooled connection. Either an idle connection or
// conn will return an available pooled connection. Either an idle connection or
// by dialing a new one if the pool does not currently have a maximum number
// of active connections.
func (p *Pool) get() (*PooledConnection, error) {
func (p *Pool) conn() (*conn, error) {
ctx := context.Background()
cn, err := p.conn(ctx, true)
cn, err := p._conn(ctx, true)
if err == nil {
return cn, nil
}
if errors.Cause(err) == ErrBadConn {
return p.conn(ctx, false)
return p._conn(ctx, false)
}
return cn, err
}

func (p *Pool) conn(ctx context.Context, useFreeConn bool) (*PooledConnection, error) {
func (p *Pool) _conn(ctx context.Context, useFreeConn bool) (*conn, error) {
p.mu.Lock()
if p.closed {
p.mu.Unlock()
Expand All @@ -180,7 +180,7 @@ func (p *Pool) conn(ctx context.Context, useFreeConn bool) (*PooledConnection, e
}
lifetime := p.MaxLifetime

var pc *PooledConnection
var pc *conn
numFree := len(p.freeConns)
if useFreeConn && numFree > 0 {
pc = p.freeConns[0]
Expand Down Expand Up @@ -215,7 +215,7 @@ func (p *Pool) conn(ctx context.Context, useFreeConn bool) (*PooledConnection, e
select {
case ret, ok := <-req:
if ok {
p.putConn(ret.pc, ret.err)
p.putConn(ret.conn, ret.err)
}
default:
}
Expand All @@ -225,9 +225,9 @@ func (p *Pool) conn(ctx context.Context, useFreeConn bool) (*PooledConnection, e
return nil, ErrGraphDBClosed
}
if ret.err != nil {
return ret.pc, errors.Wrap(ret.err, "Response has an error")
return ret.conn, errors.Wrap(ret.err, "Response has an error")
}
return ret.pc, nil
return ret.conn, nil
}
}

Expand All @@ -241,7 +241,7 @@ func (p *Pool) conn(ctx context.Context, useFreeConn bool) (*PooledConnection, e
p.maybeOpenNewConnections()
return nil, errors.Wrap(err, "Failed newConn")
}
return &PooledConnection{
return &conn{
Pool: p,
Client: newCn,
t: time.Now(),
Expand Down Expand Up @@ -286,7 +286,7 @@ func (p *Pool) connectionCleaner() {
}
n := time.Now()
mlExpiredSince := n.Add(-ml)
var closing []*PooledConnection
var closing []*conn
for i := 0; i < len(p.freeConns); i++ {
pc := p.freeConns[i]
if (ml > 0 && pc.t.Before(mlExpiredSince)) ||
Expand Down Expand Up @@ -314,7 +314,7 @@ func (p *Pool) connectionCleaner() {

// ExecuteWithBindings formats a raw Gremlin query, sends it to Gremlin Server, and returns the result.
func (p *Pool) ExecuteWithBindings(query string, bindings, rebindings map[string]string) (resp []Response, err error) {
pc, err := p.get()
pc, err := p.conn()
if err != nil {
return resp, errors.Wrap(err, "Failed p.Get")
}
Expand All @@ -327,7 +327,7 @@ func (p *Pool) ExecuteWithBindings(query string, bindings, rebindings map[string

// Execute formats a raw Gremlin query, sends it to Gremlin Server, and returns the result.
func (p *Pool) Execute(query string) (resp []Response, err error) {
pc, err := p.get()
pc, err := p.conn()
if err != nil {
return resp, errors.Wrap(err, "Failed p.Get")
}
Expand All @@ -340,7 +340,7 @@ func (p *Pool) Execute(query string) (resp []Response, err error) {

// ExecuteFile takes a file path to a Gremlin script, sends it to Gremlin Server, and returns the result.
func (p *Pool) ExecuteFile(path string, bindings, rebindings map[string]string) (resp []Response, err error) {
pc, err := p.get()
pc, err := p.conn()
if err != nil {
return resp, errors.Wrap(err, "Failed p.Get")
}
Expand Down Expand Up @@ -380,7 +380,7 @@ func (p *Pool) Close() {
p.mu.Unlock()
}

func (pc *PooledConnection) expired(timeout time.Duration) bool {
func (pc *conn) expired(timeout time.Duration) bool {
if timeout <= 0 {
return false
}
Expand Down
30 changes: 15 additions & 15 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ func TestConnectionCleaner(t *testing.T) {
n := time.Now()

// invalid has timedout and should be cleaned up
invalid := &PooledConnection{Client: &Client{}, t: n.Add(-1030 * time.Millisecond)}
invalid := &conn{Client: &Client{}, t: n.Add(-1030 * time.Millisecond)}
// valid has not yet timed out and should remain in the freeConns pool
valid := &PooledConnection{Client: &Client{}, t: n.Add(1030 * time.Millisecond)}
valid := &conn{Client: &Client{}, t: n.Add(1030 * time.Millisecond)}

// Pool has a 30 second timeout and an freeConns connection slice containing both
// the invalid and valid freeConns connections
p := NewPool(dummyDialFunc)
defer p.Close()
p.MaxLifetime = time.Second * 1
p.freeConns = []*PooledConnection{invalid, valid}
p.freeConns = []*conn{invalid, valid}

if len(p.freeConns) != 2 {
t.Errorf("Expected 2 freeConns connections, got %d", len(p.freeConns))
Expand Down Expand Up @@ -53,13 +53,13 @@ func TestPurgeErrorClosedConnection(t *testing.T) {
defer p.Close()
p.MaxLifetime = time.Second * 1

valid := &PooledConnection{Client: &Client{}, t: n.Add(1030 * time.Millisecond)}
valid := &conn{Client: &Client{}, t: n.Add(1030 * time.Millisecond)}

client := &Client{}

closed := &PooledConnection{Pool: p, Client: client, t: n.Add(1030 * time.Millisecond)}
closed := &conn{Pool: p, Client: client, t: n.Add(1030 * time.Millisecond)}

freeConns := []*PooledConnection{valid, closed}
freeConns := []*conn{valid, closed}

p.freeConns = freeConns

Expand Down Expand Up @@ -88,7 +88,7 @@ func TestPurgeErrorClosedConnection(t *testing.T) {
func TestPooledConnectionClose(t *testing.T) {
pool := NewPool(dummyDialFunc)
defer pool.Close()
pc := &PooledConnection{Pool: pool}
pc := &conn{Pool: pool}

if len(pool.freeConns) != 0 {
t.Errorf("Expected 0 freeConns connection, got %d", len(pool.freeConns))
Expand All @@ -113,10 +113,10 @@ func TestFirst(t *testing.T) {
defer pool.Close()
pool.MaxOpen = 1
pool.MaxLifetime = 30 * time.Millisecond
freeConnsd := []*PooledConnection{
&PooledConnection{Pool: pool, Client: &Client{}, t: n.Add(-45 * time.Millisecond)}, // expired
&PooledConnection{Pool: pool, Client: &Client{}, t: n.Add(-45 * time.Millisecond)}, // expired
&PooledConnection{Pool: pool, Client: &Client{}}, // valid
freeConnsd := []*conn{
&conn{Pool: pool, Client: &Client{}, t: n.Add(-45 * time.Millisecond)}, // expired
&conn{Pool: pool, Client: &Client{}, t: n.Add(-45 * time.Millisecond)}, // expired
&conn{Pool: pool, Client: &Client{}}, // valid
}
pool.freeConns = freeConnsd

Expand All @@ -142,9 +142,9 @@ func TestGetAndDial(t *testing.T) {
defer pool.Close()
pool.MaxLifetime = time.Millisecond * 30

invalid := &PooledConnection{Pool: pool, Client: &Client{}, t: n.Add(-30 * time.Millisecond)}
invalid := &conn{Pool: pool, Client: &Client{}, t: n.Add(-30 * time.Millisecond)}

freeConns := []*PooledConnection{invalid}
freeConns := []*conn{invalid}
pool.freeConns = freeConns

if len(pool.freeConns) != 1 {
Expand All @@ -160,7 +160,7 @@ func TestGetAndDial(t *testing.T) {
pool.mu.Unlock()
time.Sleep(1010 * time.Millisecond)

conn, err := pool.get()
conn, err := pool.conn()

if err != nil {
t.Error(err)
Expand All @@ -186,7 +186,7 @@ func TestGetAndDial(t *testing.T) {
}

// Get a new connection and ensure that it is the now idling connection
conn, err = pool.get()
conn, err = pool.conn()

if err != nil {
t.Error(err)
Expand Down

0 comments on commit e1716aa

Please sign in to comment.