From 74cfadfa23da18580f25b09446b8cf7f32b5c55f Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Tue, 1 Jul 2025 09:26:48 +0200 Subject: [PATCH 1/3] Identify powersync in MongoDB connections. --- libs/lib-mongodb/src/db/mongo.ts | 7 ++++++- .../src/storage/implementation/util.ts | 2 +- modules/module-mongodb/src/replication/MongoManager.ts | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libs/lib-mongodb/src/db/mongo.ts b/libs/lib-mongodb/src/db/mongo.ts index 5062043b0..ddeaf39dc 100644 --- a/libs/lib-mongodb/src/db/mongo.ts +++ b/libs/lib-mongodb/src/db/mongo.ts @@ -1,7 +1,6 @@ import * as mongo from 'mongodb'; import * as timers from 'timers/promises'; import { BaseMongoConfigDecoded, normalizeMongoConfig } from '../types/types.js'; -import { validateIpHostname } from '@powersync/lib-services-framework'; /** * Time for new connection to timeout. @@ -33,6 +32,9 @@ export interface MongoConnectionOptions { maxPoolSize: number; } +/** + * Create a MongoClient for the storage database. + */ export function createMongoClient(config: BaseMongoConfigDecoded, options?: MongoConnectionOptions) { const normalized = normalizeMongoConfig(config); return new mongo.MongoClient(normalized.uri, { @@ -47,6 +49,9 @@ export function createMongoClient(config: BaseMongoConfigDecoded, options?: Mong // How long to wait for new primary selection serverSelectionTimeoutMS: 30_000, + // Identify the client + appName: 'powersync-storage', + lookup: normalized.lookup, // Avoid too many connections: diff --git a/modules/module-mongodb-storage/src/storage/implementation/util.ts b/modules/module-mongodb-storage/src/storage/implementation/util.ts index ea6b587f3..2c9a05d53 100644 --- a/modules/module-mongodb-storage/src/storage/implementation/util.ts +++ b/modules/module-mongodb-storage/src/storage/implementation/util.ts @@ -107,7 +107,7 @@ export function replicaIdToSubkey(table: bson.ObjectId, id: storage.ReplicaId): /** * Helper function for creating a MongoDB client from consumers of this package */ -export const createMongoClient = (url: string, options?: mongo.MongoClientOptions) => { +const createMongoClient = (url: string, options?: mongo.MongoClientOptions) => { return new mongo.MongoClient(url, options); }; diff --git a/modules/module-mongodb/src/replication/MongoManager.ts b/modules/module-mongodb/src/replication/MongoManager.ts index d2cc2cee2..3742406ba 100644 --- a/modules/module-mongodb/src/replication/MongoManager.ts +++ b/modules/module-mongodb/src/replication/MongoManager.ts @@ -3,6 +3,9 @@ import { mongo } from '@powersync/lib-service-mongodb'; import { NormalizedMongoConnectionConfig } from '../types/types.js'; import { BSON_DESERIALIZE_DATA_OPTIONS } from '@powersync/service-core'; +/** + * Manage a MongoDB source database connection. + */ export class MongoManager { public readonly client: mongo.MongoClient; public readonly db: mongo.Db; @@ -26,6 +29,9 @@ export class MongoManager { // How long to wait for new primary selection serverSelectionTimeoutMS: 30_000, + // Identify the client + appName: 'powersync', + // Avoid too many connections: // 1. It can overwhelm the source database. // 2. Processing too many queries in parallel can cause the process to run out of memory. From e62270e744cf35cc7c58fa22879d5cab61e1e045 Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Tue, 1 Jul 2025 09:47:53 +0200 Subject: [PATCH 2/3] Add `program_name` for MySQL. --- .../src/replication/BinLogReplicationJob.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/module-mysql/src/replication/BinLogReplicationJob.ts b/modules/module-mysql/src/replication/BinLogReplicationJob.ts index 49280c9e3..24a07a49a 100644 --- a/modules/module-mysql/src/replication/BinLogReplicationJob.ts +++ b/modules/module-mysql/src/replication/BinLogReplicationJob.ts @@ -55,7 +55,16 @@ export class BinLogReplicationJob extends replication.AbstractReplicationJob { // such as caused by cached PG schemas. const connectionManager = this.connectionFactory.create({ // Pool connections are only used intermittently. - idleTimeout: 30_000 + idleTimeout: 30_000, + + connectAttributes: { + // https://dev.mysql.com/doc/refman/8.0/en/performance-schema-connection-attribute-tables.html + // These do not appear to be supported by Zongji yet, so we only specify it here. + // Query using `select * from performance_schema.session_connect_attrs`. + program_name: 'powersync' + + // _client_name and _client_version is specified by the driver + } }); try { await this.rateLimiter?.waitUntilAllowed({ signal: this.abortController.signal }); From 57e11056756452467030e55063deffef31091e60 Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Tue, 1 Jul 2025 09:49:39 +0200 Subject: [PATCH 3/3] Add changeset. --- .changeset/slow-hounds-walk.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .changeset/slow-hounds-walk.md diff --git a/.changeset/slow-hounds-walk.md b/.changeset/slow-hounds-walk.md new file mode 100644 index 000000000..1745e4389 --- /dev/null +++ b/.changeset/slow-hounds-walk.md @@ -0,0 +1,10 @@ +--- +'@powersync/service-module-mongodb-storage': patch +'@powersync/service-module-mongodb': patch +'@powersync/service-module-mysql': patch +'@powersync/lib-service-mongodb': patch +'@powersync/service-core': patch +'@powersync/service-image': patch +--- + +Add 'powersync' as the app name for MongoDB and MySQL connections.