Skip to content

Commit

Permalink
Make keep_alive configurable in the Web version (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
slvrtrn authored Jan 4, 2024
1 parent df4839d commit eaa0713
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
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

0 comments on commit eaa0713

Please sign in to comment.