Skip to content

Commit

Permalink
Use retry based make bucket with region
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx committed Jun 16, 2022
1 parent 66b7b9c commit 8b7ec65
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 134 deletions.
25 changes: 24 additions & 1 deletion src/main/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,4 +792,27 @@ export class SelectResults {

}

export const DEFAULT_REGION = 'us-east-1'
export const DEFAULT_REGION = 'us-east-1'

export const retryableS3Codes=[
"RequestError",
"RequestTimeout",
"Throttling",
"ThrottlingException",
"RequestLimitExceeded",
"RequestThrottled",
"InternalError",
"ExpiredToken",
"ExpiredTokenException",
"SlowDown",
]


export const retryableHTTPStatusCodes = [
429,
499,
500,
502,
503,
504
]
26 changes: 18 additions & 8 deletions src/main/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import { parseSelectObjectContentResponse} from "./xml-parsers"

var Package = require('../../package.json')


export class Client {
constructor(params) {
if (typeof params.secure !== 'undefined') throw new Error('"secure" option deprecated, "useSSL" should be used instead')
Expand Down Expand Up @@ -134,7 +133,6 @@ export class Client {
this.secretKey = params.secretKey
this.sessionToken = params.sessionToken
this.userAgent = `${libraryAgent}`
this.region = DEFAULT_REGION

// Default path style is true
if (params.pathStyle === undefined) {
Expand Down Expand Up @@ -548,7 +546,7 @@ export class Client {
// obtained region.
var pathStyle = this.pathStyle && typeof window === 'undefined'

this.makeRequest({method, bucketName, query, pathStyle}, '', [200], this.region, true, (e, response) => {
this.makeRequest({method, bucketName, query, pathStyle}, '', [200], DEFAULT_REGION, true, (e, response) => {
if (e) {
if (e.name === 'AuthorizationHeaderMalformed') {
var region = e.Region
Expand All @@ -572,7 +570,7 @@ export class Client {
// * `region` _string_ - region valid values are _us-west-1_, _us-west-2_, _eu-west-1_, _eu-central-1_, _ap-southeast-1_, _ap-northeast-1_, _ap-southeast-2_, _sa-east-1_.
// * `makeOpts` _object_ - Options to create a bucket. e.g {ObjectLocking:true} (Optional)
// * `callback(err)` _function_ - callback function with `err` as the error argument. `err` is null if the bucket is successfully created.
makeBucket(bucketName, region="", makeOpts={}, cb) {
makeBucket(bucketName, region, makeOpts={}, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
Expand Down Expand Up @@ -636,8 +634,20 @@ export class Client {
headers["x-amz-bucket-object-lock-enabled"]=true
}

if (!region) region = this.region
this.makeRequest({method, bucketName, headers}, payload, [200], region, false, cb)
if (!region) region = DEFAULT_REGION

const processWithRetry = (err) =>{
if (err && (region === "" || region === DEFAULT_REGION)) {
if(err.code === "AuthorizationHeaderMalformed" && err.region !== ""){
// Retry with region returned as part of error
this.makeRequest({method, bucketName, headers}, payload, [200], err.region, false, cb)
}
return
}
cb && cb()
}

this.makeRequest({method, bucketName, headers}, payload, [200], region, false, processWithRetry)
}

// List of buckets created.
Expand All @@ -653,7 +663,7 @@ export class Client {
throw new TypeError('callback should be of type "function"')
}
var method = 'GET'
this.makeRequest({method}, '', [200], this.region, true, (e, response) => {
this.makeRequest({method}, '', [200], DEFAULT_REGION, true, (e, response) => {
if (e) return cb(e)
var transformer = transformers.getListBucketTransformer()
var buckets
Expand Down Expand Up @@ -765,7 +775,7 @@ export class Client {
throw new TypeError('callback should be of type "function"')
}
var method = 'DELETE'
this.makeRequest({method, bucketName}, '', [204], this.region, false, (e) => {
this.makeRequest({method, bucketName}, '', [204], '', false, (e) => {
// If the bucket was successfully removed, remove the region map entry.
if (!e) delete(this.regionMap[bucketName])
cb(e)
Expand Down

0 comments on commit 8b7ec65

Please sign in to comment.