diff --git a/ldap/pool.go b/ldap/pool.go index 560d639b7091334e83d290b5c0ac1ff56b1261ee..520dcf2bdcdb719873315324477cbae09aa902ca 100644 --- a/ldap/pool.go +++ b/ldap/pool.go @@ -7,11 +7,30 @@ import ( "net/url" "time" - "git.autistici.org/ai3/go-common/clientutil" "github.com/cenkalti/backoff" "gopkg.in/ldap.v2" ) +// Parameters that define the exponential backoff algorithm used. +var ( + ExponentialBackOffInitialInterval = 100 * time.Millisecond + ExponentialBackOffMultiplier = 1.4142 +) + +// newExponentialBackOff creates a backoff.ExponentialBackOff object +// with our own default values. +func newExponentialBackOff() *backoff.ExponentialBackOff { + b := backoff.NewExponentialBackOff() + b.InitialInterval = ExponentialBackOffInitialInterval + b.Multiplier = ExponentialBackOffMultiplier + + // Set MaxElapsedTime to 0 because we expect the overall + // timeout to be dictated by the request Context. + b.MaxElapsedTime = 0 + + return b +} + // ConnectionPool provides a goroutine-safe pool of long-lived LDAP // connections that will reconnect on errors. type ConnectionPool struct { @@ -129,14 +148,14 @@ func NewConnectionPool(uri, bindDN, bindPw string, cacheSize int) (*ConnectionPo } func (p *ConnectionPool) doRequest(ctx context.Context, fn func(*ldap.Conn) error) error { - return clientutil.Retry(func() error { + return backoff.Retry(func() error { conn, err := p.Get(ctx) if err != nil { // Here conn is nil, so we don't need to Release it. if isTemporaryLDAPError(err) { - return clientutil.TempError(err) + return err } - return err + return backoff.Permanent(err) } if deadline, ok := ctx.Deadline(); ok { @@ -144,13 +163,12 @@ func (p *ConnectionPool) doRequest(ctx context.Context, fn func(*ldap.Conn) erro } err = fn(conn) - if err != nil && isTemporaryLDAPError(err) { - p.Release(conn, err) - return clientutil.TempError(err) - } p.Release(conn, err) + if err != nil && !isTemporaryLDAPError(err) { + err = backoff.Permanent(err) + } return err - }, backoff.WithContext(clientutil.NewExponentialBackOff(), ctx)) + }, backoff.WithContext(newExponentialBackOff(), ctx)) } // Search performs the given search request. It will retry the request