-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: add error handling and retry #13
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e5eb9df
feat: add identity module and file storage
qingzhuozhen 763c7cc
patch: clean up
qingzhuozhen f743d0f
patch: fix compile
qingzhuozhen 2722117
patch: rename
qingzhuozhen 58fc72d
patch: add instace name and rename identity store
qingzhuozhen eddf220
patch: adjust to use mutex
qingzhuozhen 0ad1317
patch: rename
qingzhuozhen 0b240ef
patch: rename and move the storage to idenity manager
qingzhuozhen 8767c10
feat: add error handling and retry
qingzhuozhen a24475b
patch: fix build
qingzhuozhen ea068ca
patch: resolve comments
qingzhuozhen e9222fd
patch: resolve conflicts
qingzhuozhen 2b2171b
patch: run ktlint
qingzhuozhen 8b408e7
patch: fix build
qingzhuozhen 09779dd
patch: resolve comments
qingzhuozhen 85d1701
patch: resolve comments
qingzhuozhen 083ee02
patch: resolve conflict
qingzhuozhen 8880b6b
patch: resolve comment
qingzhuozhen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
core/src/main/java/com/amplitude/core/utilities/FileResponseHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.amplitude.core.utilities | ||
|
||
import com.amplitude.core.Configuration | ||
import com.amplitude.core.events.BaseEvent | ||
import com.amplitude.core.platform.EventPipeline | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import org.json.JSONArray | ||
|
||
internal class FileResponseHandler( | ||
private val storage: FileStorage, | ||
private val eventPipeline: EventPipeline, | ||
private val configuration: Configuration, | ||
private val scope: CoroutineScope, | ||
private val dispatcher: CoroutineDispatcher, | ||
private val eventFilePath: String, | ||
private val eventsString: String | ||
) : ResponseHandler { | ||
|
||
override fun handleSuccessResponse(successResponse: SuccessResponse) { | ||
val events = JSONArray(eventsString).toEvents() | ||
triggerEventsCallback(events, HttpStatus.SUCCESS.code, "Event sent success.") | ||
scope.launch(dispatcher) { | ||
storage.removeFile(eventFilePath) | ||
} | ||
} | ||
|
||
override fun handleBadRequestResponse(badRequestResponse: BadRequestResponse) { | ||
val events = JSONArray(eventsString).toEvents() | ||
if (events.size == 1) { | ||
triggerEventsCallback(events, HttpStatus.BAD_REQUEST.code, badRequestResponse.error) | ||
storage.removeFile(eventFilePath) | ||
return | ||
} | ||
val droppedIndices = badRequestResponse.getEventIndicesToDrop() | ||
val eventsToDrop = mutableListOf<BaseEvent>() | ||
val eventsToRetry = mutableListOf<BaseEvent>() | ||
events.forEachIndexed { index, event -> | ||
if (droppedIndices.contains(index) || badRequestResponse.isEventSilenced(event)) { | ||
eventsToDrop.add(event) | ||
} else { | ||
eventsToRetry.add(event) | ||
} | ||
} | ||
triggerEventsCallback(eventsToDrop, HttpStatus.BAD_REQUEST.code, badRequestResponse.error) | ||
eventsToRetry.forEach { | ||
eventPipeline.put(it) | ||
} | ||
scope.launch(dispatcher) { | ||
storage.removeFile(eventFilePath) | ||
} | ||
} | ||
|
||
override fun handlePayloadTooLargeResponse(payloadTooLargeResponse: PayloadTooLargeResponse) { | ||
val rawEvents = JSONArray(eventsString) | ||
if (rawEvents.length() == 1) { | ||
val events = rawEvents.toEvents() | ||
triggerEventsCallback(events, HttpStatus.PAYLOAD_TOO_LARGE.code, payloadTooLargeResponse.error) | ||
scope.launch(dispatcher) { | ||
storage.removeFile(eventFilePath) | ||
} | ||
return | ||
} | ||
// split file into two | ||
scope.launch(dispatcher) { | ||
storage.splitEventFile(eventFilePath, rawEvents) | ||
} | ||
} | ||
|
||
override fun handleTooManyRequestsResponse(tooManyRequestsResponse: TooManyRequestsResponse) { | ||
// wait for next time to pick it up | ||
} | ||
|
||
override fun handleTimeoutResponse(timeoutResponse: TimeoutResponse) { | ||
// wait for next time to try again | ||
} | ||
|
||
override fun handleFailedResponse(failedResponse: FailedResponse) { | ||
// wait for next time to try again | ||
} | ||
|
||
private fun triggerEventsCallback(events: List<BaseEvent>, status: Int, message: String) { | ||
events.forEach { event -> | ||
configuration.callback?.let { | ||
it(event, status, message) | ||
} | ||
event.insertId?.let { insertId -> | ||
storage.getEventCallback(insertId)?.let { | ||
it(event, status, message) | ||
storage.removeEventCallback(insertId) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be nice for us to standarize the messaging here. We can add that to the doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. We can sync up on this