From 96809a4e1a67bed7a7cd854b689241b25853c279 Mon Sep 17 00:00:00 2001 From: fenos Date: Wed, 16 Oct 2024 14:39:06 +0200 Subject: [PATCH] fix: tus locker release --- src/internal/monitoring/otel.ts | 6 ++++++ src/storage/protocols/tus/postgres-locker.ts | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/internal/monitoring/otel.ts b/src/internal/monitoring/otel.ts index 5741c737..e801808a 100644 --- a/src/internal/monitoring/otel.ts +++ b/src/internal/monitoring/otel.ts @@ -33,6 +33,7 @@ import { TenantConnection } from '@internal/database' import { S3Store } from '@tus/s3-store' import { Upload } from '@aws-sdk/lib-storage' import { StreamSplitter } from '@tus/server' +import { PgLock } from '@storage/protocols/tus' const tracingEnabled = process.env.TRACING_ENABLED === 'true' const headersEnv = process.env.OTEL_EXPORTER_OTLP_TRACES_HEADERS || '' @@ -259,6 +260,11 @@ const sdk = new NodeSDK({ }, }, }), + new ClassInstrumentation({ + targetClass: PgLock, + enabled: true, + methodsToInstrument: ['lock', 'unlock', 'acquireLock'], + }), new ClassInstrumentation({ targetClass: S3Client, enabled: true, diff --git a/src/storage/protocols/tus/postgres-locker.ts b/src/storage/protocols/tus/postgres-locker.ts index cf4fc025..36f67b37 100644 --- a/src/storage/protocols/tus/postgres-locker.ts +++ b/src/storage/protocols/tus/postgres-locker.ts @@ -1,10 +1,10 @@ import { Lock, Locker, RequestRelease } from '@tus/server' -import { clearTimeout } from 'timers' -import EventEmitter from 'events' -import { Database, DBError } from '../../database' +import { clearTimeout } from 'node:timers' +import EventEmitter from 'node:events' +import { Database } from '../../database' import { PubSubAdapter } from '@internal/pubsub' import { UploadId } from './upload-id' -import { ERRORS } from '@internal/errors' +import { ErrorCode, ERRORS, StorageBackendError } from '@internal/errors' const REQUEST_LOCK_RELEASE_MESSAGE = 'REQUEST_LOCK_RELEASE' @@ -94,7 +94,7 @@ export class PgLock implements Lock { await db.mustLockObject(uploadId.bucket, uploadId.objectName, uploadId.version) return true } catch (e) { - if (e instanceof DBError && e.message === 'resource_locked') { + if (e instanceof StorageBackendError && e.code === ErrorCode.ResourceLocked) { await this.notifier.release(id) await new Promise((resolve) => { setTimeout(resolve, 500)