Skip to content

Commit 7a02daa

Browse files
committed
use internal transport retry api
1 parent 918935d commit 7a02daa

File tree

1 file changed

+14
-49
lines changed

1 file changed

+14
-49
lines changed

internal/services/iam/group_membership.go

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -182,57 +182,22 @@ func ExpandGroupMembershipID(id string) (groupID string, userID string, applicat
182182
}
183183

184184
func MakeGroupRequest(ctx context.Context, api *iam.API, request any) (*iam.Group, error) {
185-
retryInterval := 50 * time.Millisecond
186-
maxRetries := 10
185+
retryInterval := 100 * time.Millisecond
187186

188-
if transport.DefaultWaitRetryInterval != nil {
189-
retryInterval = *transport.DefaultWaitRetryInterval
190-
}
191-
192-
switch req := request.(type) {
193-
case *iam.AddGroupMemberRequest:
194-
for i := range maxRetries {
195-
response, err := api.AddGroupMember(req, scw.WithContext(ctx))
196-
if err != nil {
197-
if handleTransientError(err, req.GroupID, retryInterval, i) {
198-
continue
199-
}
200-
201-
return nil, err
202-
}
203-
204-
return response, nil
205-
}
206-
207-
return nil, fmt.Errorf("failed to add group member after %d retries", maxRetries)
208-
209-
case *iam.RemoveGroupMemberRequest:
210-
for i := range maxRetries {
211-
response, err := api.RemoveGroupMember(req, scw.WithContext(ctx))
212-
if err != nil {
213-
if handleTransientError(err, req.GroupID, retryInterval, i) {
214-
continue
215-
}
216-
217-
return nil, err
218-
}
187+
group, err := transport.RetryOnTransientStateError(func() (*iam.Group, error) {
219188

220-
return response, nil
189+
switch req := request.(type) {
190+
case *iam.AddGroupMemberRequest:
191+
return api.AddGroupMember(req, scw.WithContext(ctx))
192+
case *iam.RemoveGroupMemberRequest:
193+
return api.RemoveGroupMember(req, scw.WithContext(ctx))
194+
default:
195+
return nil, fmt.Errorf("invalid request type: %T", req)
221196
}
197+
}, func() (string, error) {
198+
time.Sleep(retryInterval)
199+
return "", nil
200+
})
222201

223-
return nil, fmt.Errorf("failed to remove group member after %d retries", maxRetries)
224-
225-
default:
226-
return nil, fmt.Errorf("invalid request type: %T", req)
227-
}
228-
}
229-
230-
func handleTransientError(err error, groupID string, retryInterval time.Duration, maxRetries int) bool {
231-
if httperrors.Is409(err) && strings.Contains(err.Error(), fmt.Sprintf("resource group with ID %s is in a transient state: updating", groupID)) {
232-
time.Sleep(retryInterval * time.Duration(maxRetries)) // lintignore: R018
233-
234-
return true
235-
}
236-
237-
return false
202+
return group, err
238203
}

0 commit comments

Comments
 (0)