@@ -55,23 +55,40 @@ export class SpiceDBAuthorizer {
55
55
}
56
56
57
57
async writeRelationships ( ...updates : v1 . RelationshipUpdate [ ] ) : Promise < v1 . WriteRelationshipsResponse | undefined > {
58
- const timer = spicedbClientLatency . startTimer ( ) ;
59
- let error : Error | undefined ;
60
- try {
61
- const response = await this . client . writeRelationships (
62
- v1 . WriteRelationshipsRequest . create ( {
63
- updates,
64
- } ) ,
65
- this . callOptions ,
66
- ) ;
67
- log . info ( "[spicedb] Successfully wrote relationships." , { response, updates } ) ;
58
+ let tries = 0 ;
59
+ // we do sometimes see INTERNAL errors from SpiceDB, so we retry a few times
60
+ // last time we checked it was 15 times per day (check logs)
61
+ while ( tries ++ < 3 ) {
62
+ const timer = spicedbClientLatency . startTimer ( ) ;
63
+ let error : Error | undefined ;
64
+ try {
65
+ const response = await this . client . writeRelationships (
66
+ v1 . WriteRelationshipsRequest . create ( {
67
+ updates,
68
+ } ) ,
69
+ this . callOptions ,
70
+ ) ;
71
+ log . info ( "[spicedb] Successfully wrote relationships." , { response, updates, tries } ) ;
68
72
69
- return response ;
70
- } catch ( err ) {
71
- error = err ;
72
- log . error ( "[spicedb] Failed to write relationships." , err , { updates : new TrustedValue ( updates ) } ) ;
73
- } finally {
74
- observeSpicedbClientLatency ( "write" , error , timer ( ) ) ;
73
+ return response ;
74
+ } catch ( err ) {
75
+ error = err ;
76
+ if ( err . code === grpc . status . INTERNAL && tries < 3 ) {
77
+ log . warn ( "[spicedb] Failed to write relationships." , err , {
78
+ updates : new TrustedValue ( updates ) ,
79
+ tries,
80
+ } ) ;
81
+ } else {
82
+ log . error ( "[spicedb] Failed to write relationships." , err , {
83
+ updates : new TrustedValue ( updates ) ,
84
+ tries,
85
+ } ) ;
86
+ // we don't try again on other errors
87
+ return ;
88
+ }
89
+ } finally {
90
+ observeSpicedbClientLatency ( "write" , error , timer ( ) ) ;
91
+ }
75
92
}
76
93
}
77
94
0 commit comments