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

fix: batch event payloads into chunks of 100 to reduce errors on event processing #193

Merged
merged 5 commits into from Apr 10, 2024

Conversation

kaushalkapasi
Copy link
Collaborator

@kaushalkapasi kaushalkapasi commented Apr 1, 2024

  • send events payload in batches of 100 events to reduce errors
  • switches the GH Actions image to macos-13-large

@jsalaber
Copy link
Collaborator

jsalaber commented Apr 2, 2024

would be good to add some tests for this

@kaushalkapasi kaushalkapasi force-pushed the batch-events-payload branch 3 times, most recently from f3f8605 to dfb60ed Compare April 10, 2024 17:23
Comment on lines 121 to 141
let requestBody: [String: Any] = [
"events": batchEvents,
"user": userBody
]

let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted)
Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")")
eventsRequest.httpBody = jsonBody

self.makeRequest(request: eventsRequest) { data, response, error in
if error != nil || data == nil {
return completion((data, response, error))
}
// Continue with next batch
startIndex = endIndex
endIndex = min(endIndex + self.maxBatchSize, totalEventsCount)

if startIndex >= totalEventsCount {
return completion((data, response, nil))
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this request code to its own method to clean this up a bit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to split the request generation into its own private function

let data = "{\"config\":\"key}".data(using: .utf8)
let config = processConfig(data)
XCTAssertNil(config)
}

func testFlushingEvents() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make sure there is a test that if there are less than 100 events it still gets eventually flushed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, this test is only generating 20 events :) the next one batches them with 205 events

@kaushalkapasi kaushalkapasi enabled auto-merge (squash) April 10, 2024 20:31
@kaushalkapasi kaushalkapasi merged commit 377779a into main Apr 10, 2024
10 checks passed
@kaushalkapasi kaushalkapasi deleted the batch-events-payload branch April 10, 2024 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants