Я сделал сервис, используюзий ClickHouse
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { createClient } from '@clickhouse/client';
import { createId } from "@paralleldrive/cuid2";
import { Request } from 'express';
@Injectable()
export class ClickHouseService implements OnModuleDestroy {
private readonly clickHouse: any;
constructor() {
let id = createId();
console.log('session=', id)
this.clickHouse = createClient({
host: process.env.CLICKHOUSE_HOST ?? 'http://localhost:8123',
username: process.env.CLICKHOUSE_USER ?? 'default',
password: process.env.CLICKHOUSE_PASSWORD ?? '',
session_id: id,
});
}
async query(query: string, settings: any = {}) {
settings.wait_end_of_query = 1;
try {
const ret = await this.clickHouse.query({
query: query,
clickhouse_settings: settings,
});
const response = await ret.json();
return response.data;
} catch (e) {
console.error(e.message, query);
return [];
}
}
async onModuleDestroy() {
await this.clickHouse.close();
console.log('destroyed')
}
}
}
В контроллере сервис подключается стандартным способом
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly prismaService: PrismaService,
private readonly clickHouseService: ClickHouseService,
) {}
При каждом вызове создается уникальный id сессии.
Тем не менее, на разных урлах (я сделал middleware которое печатает урл) используется одни и та же сессия и она частенько оказывается заблокирована. Почему так и что делать?
session= lo6ronfwgg0bhy6ummmr1esd
URL Requested: /source_clicks_dohod?startDate=2023-09-07%2000:00:00&endDate=2023-09-14%2000:00:00
URL Requested: /source_clicks_dohod?startDate=2023-09-07%2000:00:00&endDate=2023-09-14%2000:00:00
URL Requested: /sum_transactions/?startDate=2023-09-07%2000:00:00&endDate=2023-09-14%2000:00:00
Session lo6ronfwgg0bhy6ummmr1esd is locked by a concurrent client. SELECT ...
Session lo6ronfwgg0bhy6ummmr1esd is locked by a concurrent client.
WITH interval_data AS ( ....
URL Requested: /sum_convertibility_loans/?startDate=2023-09-07%2000:00:00&endDate=2023-09-14%2000:00:00
Session lo6ronfwgg0bhy6ummmr1esd is locked by a concurrent client. SELECT ...
Session lo6ronfwgg0bhy6ummmr1esd is locked by a concurrent client. SELECT ...
URL Requested: /sum_postback_logs/?startDate=2023-09-07%2000:00:00&endDate=2023-09-14%2000:00:00
app.modules.ts
@Module({
imports: [AuthModule, RolesModule, SettingsModule],
controllers: [AppController, SettingsController],
providers: [AppService, PrismaService, ClickHouseService, LoggerMiddleware],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(ClickHouseMiddleware).forRoutes('*');
consumer.apply(LoggerMiddleware).forRoutes('*');
}
}