Skip to content

Commit cf1e3a6

Browse files
committed
refactor: move gRPC channelOptions to a separate field
Signed-off-by: Vladislav Polyakov <[email protected]>
1 parent 5bd5958 commit cf1e3a6

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

.changeset/cold-books-show.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ydbjs/core': patch
3+
---
4+
5+
Move gprc channelOptions to separate field

packages/core/src/conn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
createChannel,
77
} from 'nice-grpc'
88

9-
import { dbg } from './dbg.ts'
9+
import { dbg } from './dbg.js'
1010

1111
export interface Connection {
1212
readonly nodeId: bigint

packages/core/src/driver.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ import { dbg } from './dbg.js'
2727
import { ConnectionPool } from './pool.js'
2828
import { debug } from './middleware.js'
2929

30-
export type DriverOptions = Omit<ChannelOptions, keyof any> & {
30+
export type DriverOptions = {
31+
/**
32+
* SSL/TLS options for secure connections.
33+
*
34+
* @deprecated Use `secureOptions` instead.
35+
*/
3136
ssl?: tls.SecureContextOptions
37+
secureOptions?: tls.SecureContextOptions | undefined
38+
channelOptions?: ChannelOptions
3239
credentialsProvider?: CredentialsProvider
3340

3441
'ydb.sdk.application'?: string
@@ -81,8 +88,9 @@ export class Driver implements Disposable {
8188
throw new Error('Invalid connection string. Must be a non-empty string')
8289
}
8390

84-
this.options = Object.assign({}, defaultOptions, options)
8591
this.cs = new URL(connectionString.replace(/^grpc/, 'http'))
92+
this.options = Object.assign({}, defaultOptions, options)
93+
this.options.secureOptions ??= this.options.ssl
8694

8795
if (['grpc:', 'grpcs:', 'http:', 'https:'].includes(this.cs.protocol) === false) {
8896
throw new Error('Invalid connection string protocol. Must be one of grpc, grpcs, http, https')
@@ -113,13 +121,13 @@ export class Driver implements Disposable {
113121
ssl: this.isSecure,
114122
})
115123

116-
let channelCredentials = this.options.ssl
117-
? credentials.createFromSecureContext(tls.createSecureContext(this.options.ssl))
124+
let channelCredentials = this.options.secureOptions
125+
? credentials.createFromSecureContext(tls.createSecureContext(this.options.secureOptions))
118126
: this.isSecure
119127
? credentials.createSsl()
120128
: credentials.createInsecure()
121129

122-
this.#connection = new LazyConnection(endpoint, channelCredentials, this.options)
130+
this.#connection = new LazyConnection(endpoint, channelCredentials, this.options.channelOptions)
123131

124132
this.#middleware = debug
125133
this.#middleware = composeClientMiddleware(this.#middleware, (call, options) => {
@@ -135,7 +143,7 @@ export class Driver implements Disposable {
135143
this.#middleware = composeClientMiddleware(this.#middleware, this.#credentialsProvider.middleware)
136144
}
137145

138-
this.#pool = new ConnectionPool(channelCredentials, this.options)
146+
this.#pool = new ConnectionPool(channelCredentials, this.options.channelOptions)
139147

140148
this.#discoveryClient = createClientFactory()
141149
.use(this.#middleware)
@@ -186,7 +194,7 @@ export class Driver implements Disposable {
186194
}
187195

188196
get isSecure(): boolean {
189-
return this.cs.protocol === 'https:'
197+
return this.cs.protocol === 'https:' || this.cs.protocol === 'grpcs:'
190198
}
191199

192200
async #discovery(signal?: AbortSignal): Promise<void> {
@@ -260,7 +268,7 @@ export class Driver implements Disposable {
260268
},
261269
}),
262270
{
263-
'*': this.options,
271+
'*': this.options.channelOptions,
264272
}
265273
)
266274
}

packages/core/src/pool.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { type EndpointInfo } from '@ydbjs/api/discovery';
22
import type { ChannelCredentials, ChannelOptions } from 'nice-grpc';
33

4-
import { type Connection, LazyConnection } from './conn.ts';
4+
import { type Connection, LazyConnection } from './conn.js';
55
import { dbg } from './dbg.js';
66

77
export class ConnectionPool implements Disposable {
88
protected connections: Set<Connection> = new Set();
99
protected pessimized: Set<Connection> = new Set();
1010

11-
#pessimizationTimeoutMs = 60000;
11+
#channelOptions?: ChannelOptions;
1212
#channelCredentials: ChannelCredentials;
13-
#channelOptions: ChannelOptions;
13+
#pessimizationTimeoutMs = 60000;
1414

15-
constructor(channelCredentials: ChannelCredentials, channelOptions: ChannelOptions) {
15+
constructor(channelCredentials: ChannelCredentials, channelOptions?: ChannelOptions) {
1616
this.#channelCredentials = channelCredentials
1717
this.#channelOptions = channelOptions
1818
}

0 commit comments

Comments
 (0)