Skip to content

Commit

Permalink
Add retry for user searches
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Dec 11, 2024
1 parent 590feec commit 52ea164
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions services/tickets/zendesk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,31 @@ type SearchUserResponse struct {
Users []User `json:"users"`
}

// SearchUser returns the user with the given external ID or nil if it doesn't exist
func (c *RESTClient) SearchUser(externalID string) (*User, *httpx.Trace, error) {
endpoint := fmt.Sprintf("users/search.json?query=%s", externalID)
var response SearchUserResponse
trace, err := c.get(endpoint, nil, &response)
if err != nil {
return nil, trace, err
}
if len(response.Users) == 0 {
return nil, trace, nil
// SearchUser returns the user or null if it does not exist, with retry logic for consistency delays.
func (c *RESTClient) SearchUser(query string) (*User, *httpx.Trace, error) {
endpoint := fmt.Sprintf("users/search.json?query=%s", query)
maxRetries := 3
delay := 2 * time.Second
var (
response SearchUserResponse
trace *httpx.Trace
err error
)

for i := 0; i < maxRetries; i++ {
trace, err = c.get(endpoint, nil, &response)
if err != nil {
return nil, trace, err
}

if len(response.Users) > 0 {
return &response.Users[0], trace, nil
}

time.Sleep(delay)
}
return &response.Users[0], trace, nil

return nil, trace, nil
}

// MergeUser merge two users
Expand Down

0 comments on commit 52ea164

Please sign in to comment.