Skip to content

Commit 3ff274f

Browse files
committed
use internal transport retry api
1 parent 918935d commit 3ff274f

File tree

1 file changed

+15
-50
lines changed

1 file changed

+15
-50
lines changed

internal/services/iam/group_membership.go

Lines changed: 15 additions & 50 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
187-
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
185+
retryInterval := 100 * time.Millisecond
186+
187+
group, err := transport.RetryOnTransientStateError(func() (*iam.Group, error) {
188+
switch req := request.(type) {
189+
case *iam.AddGroupMemberRequest:
190+
return api.AddGroupMember(req, scw.WithContext(ctx))
191+
case *iam.RemoveGroupMemberRequest:
192+
return api.RemoveGroupMember(req, scw.WithContext(ctx))
193+
default:
194+
return nil, fmt.Errorf("invalid request type: %T", req)
205195
}
196+
}, func() (string, error) {
197+
time.Sleep(retryInterval)
206198

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-
}
219-
220-
return response, nil
221-
}
222-
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-
}
199+
return "", nil
200+
})
236201

237-
return false
202+
return group, err
238203
}

0 commit comments

Comments
 (0)