6
6
"fmt"
7
7
"net/http"
8
8
"slices"
9
+ "sync"
9
10
"time"
10
11
11
12
openfga "github.com/openfga/go-sdk"
@@ -26,7 +27,9 @@ type FGA struct {
26
27
apiToken string
27
28
storeID string
28
29
29
- online bool
30
+ onlineMu sync.Mutex
31
+ online bool
32
+
30
33
shutdownCtx context.Context
31
34
shutdownCancel context.CancelFunc
32
35
@@ -116,7 +119,10 @@ func (f *FGA) load(ctx context.Context, certificateCache *certificate.Cache, opt
116
119
logger .Warn ("Connection with OpenFGA established" )
117
120
}
118
121
122
+ f .onlineMu .Lock ()
123
+ defer f .onlineMu .Unlock ()
119
124
f .online = true
125
+
120
126
return
121
127
}
122
128
@@ -276,6 +282,8 @@ func (f *FGA) CheckPermission(ctx context.Context, r *http.Request, object Objec
276
282
}
277
283
278
284
// If offline, return a clear error to the user.
285
+ f .onlineMu .Lock ()
286
+ defer f .onlineMu .Unlock ()
279
287
if ! f .online {
280
288
return api .StatusErrorf (http .StatusForbidden , "The authorization server is currently offline, please try again later" )
281
289
}
@@ -881,6 +889,8 @@ func (f *FGA) DeleteStorageBucket(ctx context.Context, projectName string, stora
881
889
// updateTuples sends an object update to OpenFGA if it's currently online.
882
890
func (f * FGA ) updateTuples (ctx context.Context , writes []client.ClientTupleKey , deletions []client.ClientTupleKeyWithoutCondition ) error {
883
891
// If offline, skip updating as a full sync will happen after connection.
892
+ f .onlineMu .Lock ()
893
+ defer f .onlineMu .Unlock ()
884
894
if ! f .online {
885
895
return nil
886
896
}
@@ -1118,7 +1128,6 @@ func (f *FGA) GetInstanceAccess(ctx context.Context, projectName string, instanc
1118
1128
Relation : relation ,
1119
1129
UserFilters : userFilters ,
1120
1130
}).Execute ()
1121
-
1122
1131
if err != nil {
1123
1132
fgaAPIErr , ok := err .(openfga.FgaApiValidationError )
1124
1133
if ! ok || fgaAPIErr .ResponseCode () != openfga .ERRORCODE_RELATION_NOT_FOUND {
@@ -1173,7 +1182,6 @@ func (f *FGA) GetProjectAccess(ctx context.Context, projectName string) (*api.Ac
1173
1182
Relation : relation ,
1174
1183
UserFilters : userFilters ,
1175
1184
}).Execute ()
1176
-
1177
1185
if err != nil {
1178
1186
fgaAPIErr , ok := err .(openfga.FgaApiValidationError )
1179
1187
if ! ok || fgaAPIErr .ResponseCode () != openfga .ERRORCODE_RELATION_NOT_FOUND {
0 commit comments