Skip to content

Commit a22e83d

Browse files
authored
fix(go): use createIterable method like in other clients (#3216)
1 parent a91f475 commit a22e83d

File tree

4 files changed

+361
-311
lines changed

4 files changed

+361
-311
lines changed

clients/algoliasearch-client-go/algolia/errs/wait_err.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package errs
22

3-
type WaitError struct{}
3+
type WaitError struct {
4+
msg string
5+
}
6+
7+
func NewWaitError(msg string) *WaitError {
8+
return &WaitError{
9+
msg: msg,
10+
}
11+
}
412

513
func (e *WaitError) Error() string {
6-
return "wait error"
14+
return e.msg
715
}
816

917
type WaitKeyUpdateError struct{}

clients/algoliasearch-client-go/algolia/utils/utils.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,41 +65,49 @@ func IsNilOrEmpty(i any) bool {
6565
}
6666
}
6767

68-
func RetryUntil[T any](
69-
retry func() (*T, error),
70-
until func(*T, error) bool,
71-
maxRetries *int,
72-
initialDelay *time.Duration,
73-
maxDelay *time.Duration,
68+
type IterableError[T any] struct {
69+
Validate func(*T, error) bool
70+
Message func(*T, error) string
71+
}
72+
73+
func CreateIterable[T any](
74+
execute func(*T, error) (*T, error),
75+
validate func(*T, error) bool,
76+
aggregator func(*T, error),
77+
timeout func() time.Duration,
78+
iterableErr *IterableError[T],
7479
) (*T, error) {
75-
if maxRetries == nil {
76-
maxRetries = new(int)
77-
*maxRetries = 50
78-
}
80+
var executor func(*T, error) (*T, error)
7981

80-
if initialDelay == nil {
81-
initialDelay = new(time.Duration)
82-
*initialDelay = 200 * time.Millisecond
83-
}
82+
executor = func(previousResponse *T, previousError error) (*T, error) {
83+
response, responseErr := execute(previousResponse, previousError)
8484

85-
if maxDelay == nil {
86-
maxDelay = new(time.Duration)
87-
*maxDelay = 5 * time.Second
88-
}
85+
if aggregator != nil {
86+
aggregator(response, responseErr)
87+
}
88+
89+
if validate(response, responseErr) {
90+
return response, responseErr
91+
}
8992

90-
for i := 0; i < *maxRetries; i++ {
91-
res, err := retry()
93+
if iterableErr != nil && iterableErr.Validate(response, responseErr) {
94+
if iterableErr.Message != nil {
95+
return nil, errs.NewWaitError(iterableErr.Message(response, responseErr))
96+
}
9297

93-
if ok := until(res, err); ok {
94-
return res, nil
98+
return nil, errs.NewWaitError("an error occurred")
9599
}
96100

97-
time.Sleep(*initialDelay)
98-
*initialDelay *= 2
99-
if *initialDelay > *maxDelay {
100-
*initialDelay = *maxDelay
101+
if timeout == nil {
102+
timeout = func() time.Duration {
103+
return 1 * time.Second
104+
}
101105
}
106+
107+
time.Sleep(timeout())
108+
109+
return executor(response, responseErr)
102110
}
103111

104-
return nil, &errs.WaitError{}
112+
return executor(nil, nil)
105113
}

clients/algoliasearch-client-swift/Sources/Core/Helpers/Iterable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public func createIterable<T>(
3939

4040
if let error, error.validate(response) {
4141
guard let errorMessage = error.message else {
42-
throw AlgoliaError.wait("An error occured")
42+
throw AlgoliaError.wait("An error occurred")
4343
}
4444

4545
throw AlgoliaError.wait(errorMessage(response))

0 commit comments

Comments
 (0)