@@ -23,6 +23,7 @@ import {
2323import { tableFromIPC } from "apache-arrow" ;
2424import * as http2 from "http2" ;
2525import type * as net from "net" ;
26+ import { setTimeout } from "node:timers/promises" ;
2627
2728// Prepare the restate server
2829async function prepareRestateEndpoint (
@@ -303,37 +304,46 @@ export class StateProxy<TState extends TypedState> {
303304 entries : [ key : string , value : Uint8Array ] [ ] ,
304305 version ?: string
305306 ) {
306- const res = await fetch (
307- `${ this . adminAPIBaseUrl } /services/${ this . service } /state` ,
308- {
309- method : "POST" ,
310- headers : {
311- "Content-Type" : "application/json" ,
312- } ,
313- body : JSON . stringify (
314- {
315- version,
316- object_key : this . serviceKey ,
317- new_state : Object . fromEntries ( entries ) ,
307+ let lastFailure : Error | undefined = undefined ;
308+ for ( let i = 0 ; i < 10 ; i ++ ) {
309+ const res = await fetch (
310+ `${ this . adminAPIBaseUrl } /services/${ this . service } /state` ,
311+ {
312+ method : "POST" ,
313+ headers : {
314+ "Content-Type" : "application/json" ,
318315 } ,
319- ( key , value ) => {
320- if ( value instanceof Uint8Array ) {
321- return Array . from ( value ) ;
322- } else {
323- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
324- return value ;
316+ body : JSON . stringify (
317+ {
318+ version,
319+ object_key : this . serviceKey ,
320+ new_state : Object . fromEntries ( entries ) ,
321+ } ,
322+ ( key , value ) => {
323+ if ( value instanceof Uint8Array ) {
324+ return Array . from ( value ) ;
325+ } else {
326+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
327+ return value ;
328+ }
325329 }
326- }
327- ) ,
330+ ) ,
331+ }
332+ ) ;
333+
334+ if ( res . ok ) {
335+ return ;
328336 }
329- ) ;
330337
331- if ( ! res . ok ) {
332338 const badResponse = await res . text ( ) ;
333- throw new Error (
339+ lastFailure = new Error (
334340 `Error ${ res . status } during modify state: ${ badResponse } `
335341 ) ;
342+
343+ await setTimeout ( 1000 ) ;
336344 }
345+
346+ throw lastFailure ;
337347 }
338348}
339349
0 commit comments