Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry HTTP 429 status codes. #763

Merged
merged 1 commit into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Analytics/Classes/Internal/SEGHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,38 @@ - (NSURLSessionUploadTask *)upload:(NSDictionary *)batch forWriteKey:(NSString *

NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:gzippedPayload completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
if (error) {
// Network error. Retry.
SEGLog(@"Error uploading request %@.", error);
completionHandler(YES);
return;
}

NSInteger code = ((NSHTTPURLResponse *)response).statusCode;
if (code < 300) {
// 2xx response codes.
// 2xx response codes. Don't retry.
completionHandler(NO);
return;
}
if (code < 400) {
// 3xx response codes.
// 3xx response codes. Retry.
SEGLog(@"Server responded with unexpected HTTP code %d.", code);
completionHandler(YES);
return;
}
if (code == 429) {
// 429 response codes. Retry.
SEGLog(@"Server limited client with response code %d.", code);
completionHandler(YES);
return;
}
if (code < 500) {
// 4xx response codes.
// non-429 4xx response codes. Don't retry.
SEGLog(@"Server rejected payload with HTTP code %d.", code);
completionHandler(NO);
return;
}

// 5xx response codes.
// 5xx response codes. Retry.
SEGLog(@"Server error with HTTP code %d.", code);
completionHandler(YES);
}];
Expand Down
47 changes: 31 additions & 16 deletions AnalyticsTests/HTTPClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ import Analytics

class HTTPClientTest: QuickSpec {
override func spec() {

var client: SEGHTTPClient!

beforeEach {
LSNocilla.sharedInstance().start()
client = SEGHTTPClient(requestFactory: nil)
}
afterEach {
afterEach {
LSNocilla.sharedInstance().clearStubs()
LSNocilla.sharedInstance().stop()
}
describe("defaultRequestFactory") {

describe("defaultRequestFactory") {
it("preserves url") {
let factory = SEGHTTPClient.defaultRequestFactory()
let url = URL(string: "https://api.segment.io/v1/batch")
let request = factory(url!)
expect(request.url) == url
}
}
describe("settingsForWriteKey") {

describe("settingsForWriteKey") {
it("succeeds for 2xx response") {
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
Expand All @@ -61,7 +61,7 @@ class HTTPClientTest: QuickSpec {
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
expect(done).toEventually(beTrue())
}

it("fails for non 2xx response") {
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
Expand All @@ -77,7 +77,7 @@ class HTTPClientTest: QuickSpec {
})
expect(done).toEventually(beTrue())
}

it("fails for json error") {
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
Expand Down Expand Up @@ -111,10 +111,10 @@ class HTTPClientTest: QuickSpec {
expect(task).to(beNil())
expect(done).toEventually(beTrue())
}

let batch: [String: Any] = ["sentAt":"2016-07-19'T'19:25:06Z", "batch":[["type":"track", "event":"foo"]]]


it("does not ask to retry for 2xx response") {
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
Expand Down Expand Up @@ -160,6 +160,21 @@ class HTTPClientTest: QuickSpec {
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
}

it("asks to retry for 429 response") {
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withJsonGzippedBody(batch as AnyObject)
.withWriteKey("bar")
.andReturn(429)
var done = false
let task = client.upload(batch, forWriteKey: "bar") { retry in
expect(retry) == true
done = true
}
expect(done).toEventually(beTrue())
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
}

it("asks to retry for 5xx response") {
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
Expand All @@ -175,7 +190,7 @@ class HTTPClientTest: QuickSpec {
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
}
}

describe("attribution") {
it("fails for json error") {
let device = [
Expand All @@ -190,22 +205,22 @@ class HTTPClientTest: QuickSpec {
expect(task).to(beNil())
expect(done).toEventually(beTrue())
}

let context: [String: Any] = [
"os": [
"name": "iPhone OS",
"version" : "8.1.3",
],
"ip": "8.8.8.8",
]

it("succeeds for 2xx response") {
_ = stubRequest("POST", "https://mobile-service.segment.com/v1/attribution" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withWriteKey("foo")
.andReturn(200)!
.withBody("{\"provider\": \"mock\"}" as NSString)

var done = false
let task = client.attribution(withWriteKey: "foo", forDevice: context) { success, properties in
expect(success) == true
Expand All @@ -217,7 +232,7 @@ class HTTPClientTest: QuickSpec {
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
expect(done).toEventually(beTrue())
}

it("fails for non 2xx response") {
_ = stubRequest("POST", "https://mobile-service.segment.com/v1/attribution" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
Expand Down