@@ -165,36 +165,10 @@ export class PgIpLock implements AnyLock {
165165 */
166166 public async acquire ( ) : Promise < boolean > {
167167 try {
168- // it will not throw on successful insert
169- if ( this . uniqueKey ) {
170- // noinspection SqlResolve
171- await this . options . pgClient . query ( `
172- INSERT INTO ${ PgIpLock . schemaName } .lock (id, channel, app)
173- VALUES (
174- ${ literal ( this . uniqueKey ) } ,
175- ${ literal ( this . channel ) } ,
176- ${ literal ( this . options . pgClient . appName ) }
177- ) ON CONFLICT (id) DO
178- UPDATE SET app = ${ PgIpLock . schemaName } .deadlock_check(
179- ${ PgIpLock . schemaName } .lock.app,
180- ${ literal ( this . options . pgClient . appName ) }
181- )
182- ` ) ;
183- } else {
184- // noinspection SqlResolve
185- await this . options . pgClient . query ( `
186- INSERT INTO ${ PgIpLock . schemaName } .lock (channel, app)
187- VALUES (
188- ${ literal ( this . channel ) } ,
189- ${ literal ( this . options . pgClient . appName ) }
190- ) ON CONFLICT (channel) DO
191- UPDATE SET app = ${ PgIpLock . schemaName } .deadlock_check(
192- ${ PgIpLock . schemaName } .lock.app,
193- ${ literal ( this . options . pgClient . appName ) }
194- )
195- ` ) ;
196- }
197-
168+ this . uniqueKey
169+ ? await this . acquireUniqueLock ( )
170+ : await this . acquireChannelLock ( )
171+ ;
198172 this . acquired = true ;
199173 } catch ( err ) {
200174 // will throw, because insert duplicates existing lock
@@ -209,6 +183,47 @@ export class PgIpLock implements AnyLock {
209183 return this . acquired ;
210184 }
211185
186+ /**
187+ * Acquires a lock with ID
188+ *
189+ * @return {Promise<void> }
190+ */
191+ private async acquireUniqueLock ( ) : Promise < void > {
192+ // noinspection SqlResolve
193+ await this . options . pgClient . query ( `
194+ INSERT INTO ${ PgIpLock . schemaName } .lock (id, channel, app)
195+ VALUES (
196+ ${ literal ( this . uniqueKey ) } ,
197+ ${ literal ( this . channel ) } ,
198+ ${ literal ( this . options . pgClient . appName ) }
199+ ) ON CONFLICT (id) DO
200+ UPDATE SET app = ${ PgIpLock . schemaName } .deadlock_check(
201+ ${ PgIpLock . schemaName } .lock.app,
202+ ${ literal ( this . options . pgClient . appName ) }
203+ )
204+ ` ) ;
205+ }
206+
207+ /**
208+ * Acquires a lock by unique channel
209+ *
210+ * @return {Promise<void> }
211+ */
212+ private async acquireChannelLock ( ) : Promise < void > {
213+ // noinspection SqlResolve
214+ await this . options . pgClient . query ( `
215+ INSERT INTO ${ PgIpLock . schemaName } .lock (channel, app)
216+ VALUES (
217+ ${ literal ( this . channel ) } ,
218+ ${ literal ( this . options . pgClient . appName ) }
219+ ) ON CONFLICT (channel) DO
220+ UPDATE SET app = ${ PgIpLock . schemaName } .deadlock_check(
221+ ${ PgIpLock . schemaName } .lock.app,
222+ ${ literal ( this . options . pgClient . appName ) }
223+ )
224+ ` ) ;
225+ }
226+
212227 /**
213228 * Releases acquired lock on this channel. After lock is released, another
214229 * running process or host would be able to acquire the lock.
0 commit comments