Skip to content

Commit

Permalink
Merge pull request processout#8 from processout/improve-max-duration-…
Browse files Browse the repository at this point in the history
…comments

Improve maximum duration logic comments & more test
  • Loading branch information
manuel-huez authored Jul 26, 2018
2 parents 6f31a97 + 756b16c commit 0835618
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ func (c *ClientConn) Close() error {
if c.pool.IsClosed() {
return ErrClosed
}
// If the wrapper connection has become too old, we want to recycle it
// If the wrapper connection has become too old, we want to recycle it. To
// clarify the logic: if the sum of the initialization time and the max
// duration is before Now(), it means the initialization is so old adding
// the maximum duration couldn't put in the future. This sum therefore
// corresponds to the cut-off point: if it's in the future we still have
// time, if it's in the past it's too old
maxDuration := c.pool.maxLifeDuration
if maxDuration > 0 && c.timeInitiated.Add(maxDuration).Before(time.Now()) {
c.Unhealthy()
Expand Down
22 changes: 22 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,26 @@ func TestMaxLifeDuration(t *testing.T) {
if !c.unhealthy {
t.Errorf("the connection should've been marked as unhealthy")
}

// Let's also make sure we don't prematurely close the connection
p, err = New(func() (*grpc.ClientConn, error) {
return grpc.Dial("example.com", grpc.WithInsecure())
}, 1, 1, 0, time.Minute)
if err != nil {
t.Errorf("The pool returned an error: %s", err.Error())
}

c, err = p.Get(context.Background())
if err != nil {
t.Errorf("Get returned an error: %s", err.Error())
}

// The max life of the connection was very low (1ns), so when we close
// the connection it should get marked as unhealthy
if err := c.Close(); err != nil {
t.Errorf("Close returned an error: %s", err.Error())
}
if c.unhealthy {
t.Errorf("the connection shouldn't have been marked as unhealthy")
}
}

0 comments on commit 0835618

Please sign in to comment.