Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: retry with backoff on transient DB write failures

riley02277a8e4bd4ce10
diff --git a/memory-layer/src/main/scala/ix/memory/db/BulkWriteApi.scala b/memory-layer/src/main/scala/ix/memory/db/BulkWriteApi.scala
index 865ba853..982308c6 100644
--- a/memory-layer/src/main/scala/ix/memory/db/BulkWriteApi.scala
+++ b/memory-layer/src/main/scala/ix/memory/db/BulkWriteApi.scala
@@ -8,6 +8,8 @@ import cats.syntax.traverse._
import io.circe.Json
import io.circe.syntax._
+import scala.concurrent.duration._
+
import ix.memory.model._
/**
@@ -16,6 +18,13 @@ import ix.memory.model._
*/
class BulkWriteApi(client: ArangoClient) {
+ private def withRetry[A](action: IO[A], maxAttempts: Int = 3, delay: FiniteDuration = 500.millis): IO[A] = {
+ action.handleErrorWith { err =>
+ if (maxAttempts <= 1) IO.raiseError(err)
+ else IO.sleep(delay) *> withRetry(action, maxAttempts - 1, delay * 2)
+ }
+ }
+
def commitBatch(
fileBatches: Vector[FileBatch],
baseRev: Long
@@ -35,10 +44,10 @@ class BulkWriteApi(client: ArangoClient) {
for {
_ <- tombstoneExistingNodes(logicalIds, newRev)
_ <- retireOldClaims(fileBatches, newRev)
- _ <- client.bulkInsert("nodes", allNodes)
- _ <- client.bulkInsertEdges("edges", allEdges)
- _ <- client.bulkInsert("claims", allClaims)
- _ <- client.bulkInsert("patches", allPatches)
+ _ <- withRetry(client.bulkInsert("nodes", allNodes))
+ _ <- withRetry(client.bulkInsertEdges("edges", allEdges))
+ _ <- withRetry(client.bulkInsert("claims", allClaims))
+ _ <- withRetry(client.bulkInsert("patches", allPatches))
_ <- updateRevision(newRev)
} yield CommitResult(Rev(newRev), CommitStatus.Ok)
}