Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go): use createIterable method like in other clients #3216

Merged
merged 9 commits into from
Jun 21, 2024
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
Loading