Skip to content

Commit 2aa2299

Browse files
mustard-mhgeropl
andcommitted
address feedback
Co-authored-by: Gero Posmyk-Leinemann <[email protected]> Tool: gitpod/catfood.gitpod.cloud
1 parent 46fd420 commit 2aa2299

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

components/server/src/authorization/spicedb.ts

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function spiceDBConfigFromEnv(): SpiceDBClientConfig | undefined {
6565
}
6666

6767
export class SpiceDBClientProvider {
68-
private client: Client | undefined;
68+
private client: Client | undefined = undefined;
6969
private previousClientOptionsString: string = DEFAULT_FEATURE_FLAG_VALUE;
7070
private clientOptions: grpc.ClientOptions;
7171

@@ -74,54 +74,53 @@ export class SpiceDBClientProvider {
7474
private readonly interceptors: grpc.Interceptor[] = [],
7575
) {
7676
this.clientOptions = DefaultClientOptions;
77-
this.reconcileClientOptions()
78-
.then(() => {})
79-
.catch((e) => {
80-
log.error("[spicedb] Failed to reconcile client options", e);
81-
});
77+
this.reconcileClientOptions();
8278
}
8379

84-
private async reconcileClientOptions() {
80+
private reconcileClientOptions(): void {
8581
const doReconcileClientOptions = async () => {
86-
try {
87-
const customClientOptions = await getExperimentsClientForBackend().getValueAsync(
88-
"spicedb_client_options",
89-
DEFAULT_FEATURE_FLAG_VALUE,
90-
{},
91-
);
92-
if (customClientOptions === this.previousClientOptionsString) {
93-
return;
94-
}
95-
let clientOptions = DefaultClientOptions;
96-
if (customClientOptions && customClientOptions != DEFAULT_FEATURE_FLAG_VALUE) {
97-
clientOptions = JSON.parse(customClientOptions);
98-
}
99-
if (this.client != null) {
100-
const newClient = this.createClient(clientOptions);
101-
const oldClient = this.client;
102-
this.client = newClient;
82+
const customClientOptions = await getExperimentsClientForBackend().getValueAsync(
83+
"spicedb_client_options",
84+
DEFAULT_FEATURE_FLAG_VALUE,
85+
{},
86+
);
87+
if (customClientOptions === this.previousClientOptionsString) {
88+
return;
89+
}
90+
let clientOptions = DefaultClientOptions;
91+
if (customClientOptions && customClientOptions != DEFAULT_FEATURE_FLAG_VALUE) {
92+
clientOptions = JSON.parse(customClientOptions);
93+
}
94+
if (this.client !== undefined) {
95+
const newClient = this.createClient(clientOptions);
96+
const oldClient = this.client;
97+
this.client = newClient;
10398

104-
log.info("[spicedb] Client options changes", {
105-
clientOptions: new TrustedValue(clientOptions),
106-
});
99+
log.info("[spicedb] Client options changes", {
100+
clientOptions: new TrustedValue(clientOptions),
101+
});
107102

108-
// close client after 10s to make sure most pending requests on the previous client are finished.
109-
setTimeout(() => {
110-
this.closeClient(oldClient);
111-
}, 10 * 1000);
112-
}
113-
this.clientOptions = clientOptions;
114-
// `createClient` will use the `DefaultClientOptions` to create client if the value on Feature Flag is not able to create a client
115-
// but we will still write `previousClientOptionsString` here to prevent retry loops.
116-
this.previousClientOptionsString = customClientOptions;
117-
} catch (e) {
118-
log.error("[spicedb] Failed to parse custom client options", e);
103+
// close client after 10s to make sure most pending requests on the previous client are finished.
104+
setTimeout(() => {
105+
this.closeClient(oldClient);
106+
}, 10 * 1000);
119107
}
108+
this.clientOptions = clientOptions;
109+
// `createClient` will use the `DefaultClientOptions` to create client if the value on Feature Flag is not able to create a client
110+
// but we will still write `previousClientOptionsString` here to prevent retry loops.
111+
this.previousClientOptionsString = customClientOptions;
120112
};
121-
while (true) {
122-
await doReconcileClientOptions();
123-
await new Promise((resolve) => setTimeout(resolve, 60 * 1000));
124-
}
113+
// eslint-disable-next-line no-void
114+
void (async () => {
115+
while (true) {
116+
try {
117+
await doReconcileClientOptions();
118+
await new Promise((resolve) => setTimeout(resolve, 60 * 1000));
119+
} catch (e) {
120+
log.error("[spicedb] Failed to reconcile client options", e);
121+
}
122+
}
123+
})();
125124
}
126125

127126
private closeClient(client: Client) {

0 commit comments

Comments
 (0)