-
Notifications
You must be signed in to change notification settings - Fork 38
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
Jun 27, 2024
1 parent
3622d03
commit 258f37f
Showing
3 changed files
with
132 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import DiscoveryService from "../../../discovery/discovery-service"; | ||
import {ENDPOINT_DISCOVERY_PERIOD} from "../../../constants"; | ||
import {AnonymousAuthService} from "../../../credentials/anonymous-auth-service"; | ||
import {getDefaultLogger} from "../../../logger/get-default-logger"; | ||
import {InternalTopicService} from "../../../topic"; | ||
|
||
const DATABASE = '/local'; | ||
const ENDPOINT = 'grpc://localhost:2136'; | ||
|
||
describe('Query.execute()', () => { | ||
let discoveryService: DiscoveryService; | ||
let topicService: InternalTopicService; | ||
|
||
beforeEach(async () => { | ||
await testOnOneSessionWithoutDriver(); | ||
}); | ||
|
||
afterEach(async () => { | ||
discoveryService.destroy(); | ||
await topicService.dispose(); | ||
}); | ||
|
||
it('write: simple', async () => { | ||
// topicService.dispose(); | ||
|
||
}); | ||
|
||
it('read: simple', async () => { | ||
|
||
}); | ||
|
||
async function testOnOneSessionWithoutDriver() { | ||
const logger = getDefaultLogger(); | ||
const authService = new AnonymousAuthService(); | ||
discoveryService = new DiscoveryService({ | ||
endpoint: ENDPOINT, | ||
database: DATABASE, | ||
authService, | ||
discoveryPeriod: ENDPOINT_DISCOVERY_PERIOD, | ||
logger, | ||
}); | ||
await discoveryService.ready(ENDPOINT_DISCOVERY_PERIOD); | ||
topicService = new InternalTopicService( | ||
await discoveryService.getEndpoint(), // TODO: Should be one per endpoint | ||
DATABASE, | ||
authService, | ||
logger, | ||
); | ||
} | ||
}); |
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 |
---|---|---|
@@ -1,33 +1,81 @@ | ||
import {Logger} from "../logger/simple-logger"; | ||
import {Ydb} from "ydb-sdk-proto"; | ||
import {InternalTopicService} from "./internal-topic-service"; | ||
import StreamWriteMessage = Ydb.Topic.StreamWriteMessage; | ||
import {ClientWritableStream, ServiceError} from "@grpc/grpc-js/src/call"; | ||
import FromClient = Ydb.Topic.StreamWriteMessage.FromClient; | ||
import FromServer = Ydb.Topic.StreamWriteMessage.FromServer; | ||
import {ClientWritableStream/*, ServiceError*/} from "@grpc/grpc-js/src/call"; | ||
import EventEmitter from "events"; | ||
|
||
export interface InternalTopicWriteOpts { | ||
} | ||
|
||
export class InternalTopicWrite { | ||
type InitArgs = | ||
Ydb.Topic.StreamWriteMessage.IInitRequest | ||
& Required<Pick<Ydb.Topic.StreamWriteMessage.IInitRequest, 'path'>>; | ||
|
||
type WriteArgs = | ||
Ydb.Topic.StreamWriteMessage.IWriteRequest | ||
& Required<Pick<Ydb.Topic.StreamWriteMessage.IWriteRequest, 'messages'>>; | ||
|
||
type UpdateTokenArgs = Ydb.Topic.UpdateTokenRequest & Required<Pick<Ydb.Topic.UpdateTokenRequest, 'token'>>; | ||
|
||
export const STREAM_DISPOSED = 'stream-disposed'; | ||
|
||
export class InternalTopicWrite extends EventEmitter { | ||
// @ts-ignore | ||
private writeStream: ClientWritableStream<StreamWriteMessage.FromClient>; | ||
private writeStream?: ClientWritableStream<FromClient>; | ||
|
||
constructor( | ||
private topicService: InternalTopicService, | ||
// @ts-ignore | ||
private logger: Logger, | ||
// @ts-ignore | ||
private opts: InternalTopicWriteOpts) { | ||
this.writeStream = this.topicService.grpcClient! | ||
.makeClientStreamRequest( | ||
super(); | ||
const stream = this.topicService.grpcClient! | ||
.makeClientStreamRequest<FromClient, FromServer>( | ||
'/Ydb.Topic.V1.TopicService/StreamWrite', | ||
(v: StreamWriteMessage.FromClient) => StreamWriteMessage.FromServer.encode(v).finish() as Buffer, | ||
StreamWriteMessage.FromServer.decode, | ||
(v: FromClient) => FromClient.encode(v).finish() as Buffer, | ||
FromServer.decode, | ||
this.topicService.metadata, | ||
(err: ServiceError | null, value?: StreamWriteMessage.FromServer) => { | ||
(_err: any /* ServiceError */, _value?: FromServer) => { | ||
// TODO: process | ||
} | ||
) | ||
console.info(1000, _value); | ||
}); | ||
this.writeStream = stream as ClientWritableStream<FromClient>; // 'as' is here as a quick solution of the fact that TS generates error here | ||
} | ||
|
||
public init(opts: InitArgs) { | ||
if (this.writeStream) | ||
this.writeStream.write( | ||
FromClient.create({ | ||
initRequest: Ydb.Topic.StreamWriteMessage.InitRequest.create(opts), | ||
})); | ||
} | ||
|
||
public write(opts: WriteArgs) { | ||
if (this.writeStream) | ||
this.writeStream.write( | ||
FromClient.create({ | ||
writeRequest: Ydb.Topic.StreamWriteMessage.WriteRequest.create(opts), | ||
})); | ||
} | ||
|
||
public updateToken(opts: UpdateTokenArgs) { | ||
if (this.writeStream) | ||
this.writeStream.write( | ||
FromClient.create({ | ||
updateTokenRequest: Ydb.Topic.UpdateTokenRequest.create(opts), | ||
})); | ||
} | ||
|
||
public dispose() { | ||
if (this.writeStream) { | ||
this.writeStream.end(); | ||
delete this.writeStream; | ||
this.emit(STREAM_DISPOSED, this); | ||
} | ||
} | ||
|
||
// TODO: Regular update token | ||
// TODO: Update token when the auth provider returns a new one | ||
} |