Skip to content

Commit

Permalink
If we hit max retries and still error, return an error instead of nil.
Browse files Browse the repository at this point in the history
This fixes a bug where when running in service mode, if Validate() succeeds on startup (the LDAP server is up), but then goes down or permissions change, we return a successful sync with no data.

Also remove a TODO that I did in a previous pull request.
  • Loading branch information
ggreer committed Nov 20, 2024
1 parent 89bddd0 commit a8206cc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/ldap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ func isNetworkError(err error) bool {
func (c *Client) getConnection(ctx context.Context, isModify bool, f func(client *ldapConn) error) error {
l := ctxzap.Extract(ctx)

var err error
connectAttempts := 0
for connectAttempts < maxConnectAttempts {
if connectAttempts > 0 {
l.Warn("baton-ldap: retrying connection", zap.Int("attempts", connectAttempts), zap.Int("maxAttempts", maxConnectAttempts))
time.Sleep(time.Duration(connectAttempts) * time.Second)
}
cp, err := c.pool.Acquire(ctx)
var cp *puddle.Resource[*ldapConn]
cp, err = c.pool.Acquire(ctx)
if err != nil {
if isNetworkError(err) {
l.Warn("baton-ldap: network error acquiring connection. retrying", zap.Error(err), zap.Int("attempts", connectAttempts), zap.Int("maxAttempts", maxConnectAttempts))
Expand Down Expand Up @@ -99,7 +101,7 @@ func (c *Client) getConnection(ctx context.Context, isModify bool, f func(client
cp.Release()
break
}
return nil
return err
}

func parsePageToken(pageToken string) (string, []byte, error) {
Expand Down Expand Up @@ -172,8 +174,6 @@ func (c *Client) _ldapSearch(ctx context.Context,
var ret []*ldap.Entry
var nextPageToken string

// TODO (ggreer): Reconnecting with a pageToken doesn't work because the ldap cookie is per-connection
// To fix this, we should restart the query with no pageToken
err := c.getConnection(ctx, false, func(client *ldapConn) error {
if pageSize <= 0 {
pageSize = defaultPageSize
Expand Down

0 comments on commit a8206cc

Please sign in to comment.