Skip to content

Commit 0f60c17

Browse files
committed
[ws-manager-bridge] Improve initial connection error handling
Prior we might return `UNAVAILABLE` which would make some gRPC clients retry.
1 parent 869a8d4 commit 0f60c17

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

components/ws-manager-api/typescript/src/client-provider.ts

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { Disposable } from "@gitpod/gitpod-protocol";
1212
import { WorkspaceClusterWoTls, WorkspaceManagerConnectionInfo } from '@gitpod/gitpod-protocol/lib/workspace-cluster';
1313
import { WorkspaceManagerClientProviderCompositeSource, WorkspaceManagerClientProviderSource } from "./client-provider-source";
1414
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
15-
import { GetWorkspacesRequest } from "./core_pb";
1615

1716
@injectable()
1817
export class WorkspaceManagerClientProvider implements Disposable {
@@ -56,14 +55,14 @@ export class WorkspaceManagerClientProvider implements Disposable {
5655
let client = this.connectionCache.get(name);
5756
if (!client) {
5857
const info = await getConnectionInfo();
59-
client = createClient(info, grpcOptions);
58+
client = this.createClient(info, grpcOptions);
6059
this.connectionCache.set(name, client);
6160
} else if(client.getChannel().getConnectivityState(true) != grpc.connectivityState.READY) {
6261
client.close();
6362

6463
console.warn(`Lost connection to workspace manager \"${name}\" - attempting to reestablish`);
6564
const info = await getConnectionInfo();
66-
client = createClient(info, grpcOptions);
65+
client = this.createClient(info, grpcOptions);
6766
this.connectionCache.set(name, client);
6867
}
6968

@@ -78,39 +77,30 @@ export class WorkspaceManagerClientProvider implements Disposable {
7877
return this.source.getAllWorkspaceClusters();
7978
}
8079

81-
/**
82-
* Tries to establish a gRPC connection to the specified target. Throws an exception if it fails.
83-
* @param info
84-
*/
85-
public async tryConnectTo(info: WorkspaceManagerConnectionInfo) {
86-
const client = new PromisifiedWorkspaceManagerClient(createClient(info));
87-
await client.getWorkspaces({}, new GetWorkspacesRequest());
80+
public createClient(info: WorkspaceManagerConnectionInfo, grpcOptions?: object): WorkspaceManagerClient {
81+
let credentials: grpc.ChannelCredentials;
82+
if (info.tls) {
83+
const rootCerts = Buffer.from(info.tls.ca, "base64");
84+
const privateKey = Buffer.from(info.tls.key, "base64");
85+
const certChain = Buffer.from(info.tls.crt, "base64");
86+
credentials = grpc.credentials.createSsl(rootCerts, privateKey, certChain);
87+
log.debug("using TLS config to connect ws-manager");
88+
} else {
89+
credentials = grpc.credentials.createInsecure();
90+
}
91+
92+
const options = {
93+
...grpcOptions,
94+
'grpc.ssl_target_name_override': "ws-manager", // this makes sure we can call ws-manager with a URL different to "ws-manager"
95+
};
96+
return new WorkspaceManagerClient(info.url, credentials, options);
8897
}
8998

9099
public dispose() {
91100
Array.from(this.connectionCache.values()).map(c => c.close());
92101
}
93102
}
94103

95-
function createClient(info: WorkspaceManagerConnectionInfo, grpcOptions?: object): WorkspaceManagerClient {
96-
let credentials: grpc.ChannelCredentials;
97-
if (info.tls) {
98-
const rootCerts = Buffer.from(info.tls.ca, "base64");
99-
const privateKey = Buffer.from(info.tls.key, "base64");
100-
const certChain = Buffer.from(info.tls.crt, "base64");
101-
credentials = grpc.credentials.createSsl(rootCerts, privateKey, certChain);
102-
log.debug("using TLS config to connect ws-manager");
103-
} else {
104-
credentials = grpc.credentials.createInsecure();
105-
}
106-
107-
const options = {
108-
...grpcOptions,
109-
'grpc.ssl_target_name_override': "ws-manager", // this makes sure we can call ws-manager with a URL different to "ws-manager"
110-
};
111-
return new WorkspaceManagerClient(info.url, credentials, options);
112-
}
113-
114104
/**
115105
*
116106
* @param clusters

components/ws-manager-bridge/src/cluster-service-server.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
UpdateRequest,
2323
UpdateResponse
2424
} from '@gitpod/ws-manager-bridge-api/lib';
25+
import { GetWorkspacesRequest } from '@gitpod/ws-manager/lib';
2526
import { WorkspaceManagerClientProvider } from '@gitpod/ws-manager/lib/client-provider';
2627
import * as grpc from "grpc";
2728
import { inject, injectable } from 'inversify';
@@ -107,7 +108,16 @@ export class ClusterService implements IClusterServiceServer {
107108
};
108109

109110
// try to connect to validate the config. Throws an exception if it fails.
110-
await this.clientProvider.tryConnectTo(newCluster);
111+
await new Promise<void>((resolve, reject) => {
112+
const c = this.clientProvider.createClient(newCluster);
113+
c.getWorkspaces(new GetWorkspacesRequest(), (err, resp) => {
114+
if (err) {
115+
reject(new GRPCError(grpc.status.FAILED_PRECONDITION, `cannot reach ${req.url}: ${err.message}`));
116+
} else {
117+
resolve();
118+
}
119+
});
120+
})
111121

112122
await this.db.save(newCluster);
113123

0 commit comments

Comments
 (0)