-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexey Zorkaltsev
committed
Feb 13, 2024
1 parent
6b0b4c8
commit 3cda548
Showing
41 changed files
with
581 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/__tests__/e2e/table-service/graceful-session-close.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// import {Logger} from "../logging"; | ||
// | ||
// const CancelPromise = Symbol('CancelPromise'); | ||
// | ||
// class Context { | ||
// withId(id:) | ||
// withValue | ||
// witghCancel | ||
// withTimeout() | ||
// cancel(), | ||
// getValue(name: string, type: function), | ||
// | ||
// } | ||
// | ||
// | ||
// | ||
// class SomeContext /*extends Context*/ { | ||
// | ||
// constructor(opts: { | ||
// id?, | ||
// ctx?: SomeContext, // parent context | ||
// timeout?: number, // fluxon duration | ||
// // cancellable?: boolean, | ||
// // requestId?: string, | ||
// logger?: Logger, | ||
// // something about spans | ||
// } = {}) { | ||
// | ||
// | ||
// | ||
// | ||
// | ||
// // ensure logger | ||
// } | ||
// | ||
// cancel() { | ||
// | ||
// } | ||
// | ||
// | ||
// [CancelPromise]?: Promise<void>; | ||
// | ||
// | ||
// get cancelPromise() { | ||
// return this[CancelPromise]; | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// // TODO: Ref count | ||
// | ||
// import {Client} from "@grpc/grpc-js"; | ||
// import {IAuthService} from "../credentials"; | ||
// import {Logger} from "../logging"; | ||
// import {ISslCredentials} from "../ssl-credentials"; | ||
// import {ClientOptions} from "../utils"; | ||
// | ||
// class GrpcClientFactory { | ||
// | ||
// constructor(database: string, logger: Logger, sslCredentials?: ISslCredentials, clientOptions?: ClientOptions) { | ||
// } | ||
// | ||
// // TODO: Consider to close clients af some period of time of inactivity | ||
// // TODO: We may close clients that are no longer in discovery list | ||
// private cache = new WeakMap<String, Client>(); | ||
// | ||
// aquire(): Client { | ||
// | ||
// } | ||
// | ||
// release(client: Client) { | ||
// | ||
// } | ||
// } | ||
// | ||
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import * as $protobuf from "protobufjs"; | ||
import {ISslCredentials} from "../ssl-credentials"; | ||
import * as grpc from "@grpc/grpc-js"; | ||
import _ from "lodash"; | ||
import {IAuthService} from "../credentials"; | ||
import {getVersionHeader} from "../version"; | ||
import {StreamEnd} from "../table/utils"; | ||
import {ClientOptions, getDatabaseHeader, MetadataHeaders, removeProtocol, ServiceFactory} from "../utils"; | ||
import {TableAuthenticatedService} from "../table/table-authenticated-service"; | ||
|
||
export abstract class QueryAuthenticatedService<Api extends $protobuf.rpc.Service> { | ||
protected api: Api; // TODO: Consider remove | ||
protected metadata: grpc.Metadata; | ||
private responseMetadata: WeakMap<object, grpc.Metadata>; | ||
private lastRequest!: object; | ||
|
||
protected readonly headers: MetadataHeaders; | ||
|
||
static isServiceAsyncMethod(target: object, prop: string | number | symbol, receiver: any) { | ||
return ( | ||
Reflect.has(target, prop) && | ||
typeof Reflect.get(target, prop, receiver) === 'function' && | ||
prop !== 'create' | ||
); | ||
} | ||
|
||
public getResponseMetadata(request: object) { | ||
return this.responseMetadata.get(request); | ||
} | ||
|
||
/** | ||
* | ||
* @param host | ||
* @param database | ||
* @param name | ||
* @param apiCtor | ||
* @param authService | ||
* @param sslCredentials | ||
* @param clientOptions | ||
* @param streamMethods In query service API unlike table service API methods that return stream | ||
* are not labeled with the word Stream in the name. So they must be listed explicitly | ||
* @protected | ||
*/ | ||
protected constructor( | ||
host: string, | ||
database: string, | ||
private name: string, | ||
private apiCtor: ServiceFactory<Api>, | ||
protected authService: IAuthService, | ||
protected sslCredentials?: ISslCredentials, | ||
protected clientOptions?: ClientOptions, | ||
private streamMethods?: string[], | ||
) { | ||
this.headers = new Map([getVersionHeader(), getDatabaseHeader(database)]); | ||
this.metadata = new grpc.Metadata(); | ||
this.responseMetadata = new WeakMap(); | ||
this.api = new Proxy( | ||
this.getClient(removeProtocol(host), this.sslCredentials, clientOptions), | ||
{ | ||
get: (target, prop, receiver) => { | ||
const property = Reflect.get(target, prop, receiver); | ||
return TableAuthenticatedService.isServiceAsyncMethod(target, prop, receiver) ? | ||
async (...args: any[]) => { | ||
if (!['emit', 'rpcCall', 'rpcImpl'].includes(String(prop))) { | ||
if (args.length) { | ||
this.lastRequest = args[0]; | ||
} | ||
} | ||
|
||
this.metadata = await this.authService.getAuthMetadata(); | ||
for (const [name, value] of this.headers) { | ||
if (value) { | ||
this.metadata.add(name, value); | ||
} | ||
} | ||
|
||
return property.call(receiver, ...args); | ||
} : | ||
property; | ||
} | ||
} | ||
); | ||
} | ||
|
||
protected getClient(host: string, sslCredentials?: ISslCredentials, clientOptions?: ClientOptions): Api { | ||
const client = sslCredentials ? | ||
new grpc.Client(host, grpc.credentials.createSsl(sslCredentials.rootCertificates, sslCredentials.clientCertChain, sslCredentials.clientPrivateKey), clientOptions) : | ||
new grpc.Client(host, grpc.credentials.createInsecure(), clientOptions); | ||
const rpcImpl: $protobuf.RPCImpl = (method, requestData, callback) => { | ||
const path = `/${this.name}/${method.name}`; | ||
if (method.name.startsWith('Stream') || this.streamMethods?.findIndex((v) => v === method.name)) { | ||
client.makeServerStreamRequest(path, _.identity, _.identity, requestData, this.metadata) | ||
.on('data', (data) => callback(null, data)) | ||
.on('end', () => callback(new StreamEnd(), null)) | ||
.on('error', (error) => callback(error, null)); | ||
} else { | ||
const req = client.makeUnaryRequest(path, _.identity, _.identity, requestData, this.metadata, callback); | ||
const lastRequest = this.lastRequest; | ||
req.on('status', ({metadata}: grpc.StatusObject) => { | ||
if (lastRequest) { | ||
this.responseMetadata.set(lastRequest, metadata); | ||
} | ||
}); | ||
} | ||
}; | ||
return this.apiCtor.create(rpcImpl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.