Skip to content

Commit

Permalink
fix(go): use createIterable method like in other clients (#3216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 authored Jun 21, 2024
1 parent a91f475 commit a22e83d
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 311 deletions.
12 changes: 10 additions & 2 deletions clients/algoliasearch-client-go/algolia/errs/wait_err.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package errs

type WaitError struct{}
type WaitError struct {
msg string
}

func NewWaitError(msg string) *WaitError {
return &WaitError{
msg: msg,
}
}

func (e *WaitError) Error() string {
return "wait error"
return e.msg
}

type WaitKeyUpdateError struct{}
Expand Down
62 changes: 35 additions & 27 deletions clients/algoliasearch-client-go/algolia/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,49 @@ func IsNilOrEmpty(i any) bool {
}
}

func RetryUntil[T any](
retry func() (*T, error),
until func(*T, error) bool,
maxRetries *int,
initialDelay *time.Duration,
maxDelay *time.Duration,
type IterableError[T any] struct {
Validate func(*T, error) bool
Message func(*T, error) string
}

func CreateIterable[T any](
execute func(*T, error) (*T, error),
validate func(*T, error) bool,
aggregator func(*T, error),
timeout func() time.Duration,
iterableErr *IterableError[T],
) (*T, error) {
if maxRetries == nil {
maxRetries = new(int)
*maxRetries = 50
}
var executor func(*T, error) (*T, error)

if initialDelay == nil {
initialDelay = new(time.Duration)
*initialDelay = 200 * time.Millisecond
}
executor = func(previousResponse *T, previousError error) (*T, error) {
response, responseErr := execute(previousResponse, previousError)

if maxDelay == nil {
maxDelay = new(time.Duration)
*maxDelay = 5 * time.Second
}
if aggregator != nil {
aggregator(response, responseErr)
}

if validate(response, responseErr) {
return response, responseErr
}

for i := 0; i < *maxRetries; i++ {
res, err := retry()
if iterableErr != nil && iterableErr.Validate(response, responseErr) {
if iterableErr.Message != nil {
return nil, errs.NewWaitError(iterableErr.Message(response, responseErr))
}

if ok := until(res, err); ok {
return res, nil
return nil, errs.NewWaitError("an error occurred")
}

time.Sleep(*initialDelay)
*initialDelay *= 2
if *initialDelay > *maxDelay {
*initialDelay = *maxDelay
if timeout == nil {
timeout = func() time.Duration {
return 1 * time.Second
}
}

time.Sleep(timeout())

return executor(response, responseErr)
}

return nil, &errs.WaitError{}
return executor(nil, nil)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public func createIterable<T>(

if let error, error.validate(response) {
guard let errorMessage = error.message else {
throw AlgoliaError.wait("An error occured")
throw AlgoliaError.wait("An error occurred")
}

throw AlgoliaError.wait(errorMessage(response))
Expand Down
Loading

1 comment on commit a22e83d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.