Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make keep_alive configurable in the Web version #220

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/client-web/__tests__/integration/web_connection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createClient } from '../../src'
import type { WebClickHouseClient } from '../../src/client'

describe('[Web] Connection', () => {
describe('KeepAlive setting', () => {
let fetchSpy: jasmine.Spy<typeof window.fetch>
beforeEach(() => {
fetchSpy = spyOn(window, 'fetch').and.returnValue(
Promise.resolve(new Response())
)
})

it('should be enabled by default', async () => {
const client = createClient()
const fetchParams = await pingAndGetRequestInit(client)
expect(fetchParams.keepalive).toBeTruthy()
})

it('should be possible to disable it', async () => {
const client = createClient({ keep_alive: { enabled: false } })
const fetchParams = await pingAndGetRequestInit(client)
expect(fetchParams!.keepalive).toBeFalsy()
})

it('should be enabled with an explicit setting', async () => {
const client = createClient({ keep_alive: { enabled: true } })
const fetchParams = await pingAndGetRequestInit(client)
expect(fetchParams.keepalive).toBeTruthy()
})

async function pingAndGetRequestInit(client: WebClickHouseClient) {
await client.ping()
expect(fetchSpy).toHaveBeenCalledTimes(1)
const [, fetchParams] = fetchSpy.calls.mostRecent().args
return fetchParams!
}
})
})
16 changes: 14 additions & 2 deletions packages/client-web/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import { WebConnection } from './connection'
import { ResultSet } from './result_set'
import { WebValuesEncoder } from './utils'

export type WebClickHouseClientConfigOptions =
BaseClickHouseClientConfigOptions<ReadableStream> & {
keep_alive?: {
/** Enable or disable HTTP Keep-Alive mechanism. Default: true */
enabled: boolean
}
}

export type WebClickHouseClient = Omit<
ClickHouseClient<ReadableStream>,
'insert' | 'query'
Expand All @@ -30,11 +38,15 @@ export type WebClickHouseClient = Omit<
}

export function createClient(
config?: BaseClickHouseClientConfigOptions<ReadableStream>
config?: WebClickHouseClientConfigOptions
): WebClickHouseClient {
const keep_alive = {
enabled: config?.keep_alive?.enabled ?? true,
}
return new ClickHouseClient<ReadableStream>({
impl: {
make_connection: (params: ConnectionParams) => new WebConnection(params),
make_connection: (params: ConnectionParams) =>
new WebConnection({ ...params, keep_alive }),
make_result_set: (
stream: ReadableStream,
format: DataFormat,
Expand Down
10 changes: 8 additions & 2 deletions packages/client-web/src/connection/web_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ type WebInsertParams<T> = Omit<
values: string
}

export type WebConnectionParams = ConnectionParams & {
keep_alive: {
enabled: boolean
}
}

export class WebConnection implements Connection<ReadableStream> {
private readonly defaultHeaders: Record<string, string>
constructor(private readonly params: ConnectionParams) {
constructor(private readonly params: WebConnectionParams) {
this.defaultHeaders = {
Authorization: `Basic ${btoa(`${params.username}:${params.password}`)}`,
}
Expand Down Expand Up @@ -175,7 +181,7 @@ export class WebConnection implements Connection<ReadableStream> {
const response = await fetch(url, {
body: values,
headers,
keepalive: false,
keepalive: this.params.keep_alive.enabled,
method: method ?? 'POST',
signal: abortController.signal,
})
Expand Down
Loading