Skip to content

Commit

Permalink
retry based on idempotency strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
shaffeeullah committed Jul 29, 2022
1 parent 3896201 commit 80909b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import arrify = require('arrify');

import {Bucket} from './bucket';
import {normalize} from './util';
import {PreconditionOptions} from './storage';
import {IdempotencyStrategy, PreconditionOptions} from './storage';

export interface GetPolicyOptions {
userProject?: string;
Expand Down Expand Up @@ -149,10 +149,12 @@ class Iam {
callback: BodyResponseCallback
) => void;
private resourceId_: string;
private bucket_: Bucket;

constructor(bucket: Bucket) {
this.request_ = bucket.request.bind(bucket);
this.resourceId_ = 'buckets/' + bucket.getId();
this.bucket_ = bucket;
}

getPolicy(options?: GetPolicyOptions): Promise<GetPolicyResponse>;
Expand Down Expand Up @@ -345,7 +347,14 @@ class Iam {
SetPolicyCallback
>(optionsOrCallback, callback);

const maxRetries = 0; // We don't support ETag
let maxRetries = this.bucket_.storage.retryOptions.maxRetries;
// ETag preconditions are not currently supported. Retries should be disabled if the idempotency strategy is not set to RetryAlways
if (
this.bucket_.storage.retryOptions.idempotencyStrategy !==
IdempotencyStrategy.RetryAlways
) {
maxRetries = 0;
}
this.request_(
{
method: 'PUT',
Expand Down
7 changes: 7 additions & 0 deletions test/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as assert from 'assert';
import {describe, it, before, beforeEach} from 'mocha';
import * as proxyquire from 'proxyquire';
import {IAMExceptionMessages} from '../src/iam';
import { IdempotencyStrategy } from '../src';

describe('storage/iam', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -48,6 +49,12 @@ describe('storage/iam', () => {
id,
request: util.noop,
getId: () => id,
storage: {
retryOptions: {
idempotencyStrategy: IdempotencyStrategy.RetryConditional,
maxRetries: 3
}
}
};

iam = new Iam(BUCKET_INSTANCE);
Expand Down

0 comments on commit 80909b5

Please sign in to comment.