@@ -16,10 +16,12 @@ import { Database } from "./database";
1616import {
1717 ArangoError ,
1818 HttpError ,
19+ isArangoError ,
1920 isArangoErrorResponse ,
2021 isSystemError ,
2122} from "./error" ;
2223import { btoa } from "./lib/btoa" ;
24+ import { ERROR_ARANGO_CONFLICT } from "./lib/codes" ;
2325import { Errback } from "./lib/errback" ;
2426import { normalizeUrl } from "./lib/normalizeUrl" ;
2527import {
@@ -254,6 +256,13 @@ export type RequestOptions = {
254256 * a non-authoritative server.
255257 */
256258 allowDirtyRead ?: boolean ;
259+ /**
260+ * If set to a positive number, the request will automatically be retried at
261+ * most this many times if it results in a write-write conflict.
262+ *
263+ * Default: `0`
264+ */
265+ retryOnConflict ?: number ;
257266 /**
258267 * HTTP headers to pass along with this request in addition to the default
259268 * headers generated by arangojs.
@@ -288,6 +297,7 @@ type Task = {
288297 host ?: number ;
289298 stack ?: ( ) => string ;
290299 allowDirtyRead : boolean ;
300+ retryOnConflict : number ;
291301 resolve : ( res : ArangojsResponse ) => void ;
292302 reject : ( error : Error ) => void ;
293303 retries : number ;
@@ -632,6 +642,13 @@ export class Connection {
632642 this . _activeHost = ( this . _activeHost + 1 ) % this . _hosts . length ;
633643 }
634644 if (
645+ isArangoError ( err ) &&
646+ err . errorNum === ERROR_ARANGO_CONFLICT &&
647+ task . retryOnConflict > 0
648+ ) {
649+ task . retryOnConflict -= 1 ;
650+ this . _queue . push ( task ) ;
651+ } else if (
635652 ! task . host &&
636653 this . _shouldRetry &&
637654 task . retries < ( this . _maxRetries || this . _hosts . length - 1 ) &&
@@ -878,6 +895,7 @@ export class Connection {
878895 expectBinary = false ,
879896 isBinary = false ,
880897 allowDirtyRead = false ,
898+ retryOnConflict = 0 ,
881899 timeout = 0 ,
882900 headers,
883901 ...urlInfo
@@ -911,6 +929,7 @@ export class Connection {
911929 retries : 0 ,
912930 host,
913931 allowDirtyRead,
932+ retryOnConflict,
914933 options : {
915934 url : this . _buildUrl ( urlInfo ) ,
916935 headers : { ...extraHeaders , ...headers } ,
0 commit comments