@salto-io/persistent-pool 0.3.40-main.5ef2e84db → 0.3.40-publishOnlyDist.57b77fb79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -8
- package/dist/test/lib/dynamodb/dynamodb_repo.e2e.test.d.ts +0 -2
- package/dist/test/lib/dynamodb/dynamodb_repo.e2e.test.d.ts.map +0 -1
- package/dist/test/lib/dynamodb/dynamodb_repo.e2e.test.js +0 -98
- package/dist/test/lib/dynamodb/dynamodb_repo.e2e.test.js.map +0 -1
- package/dist/test/lib/dynamodb/dynamodb_repo.test.d.ts +0 -2
- package/dist/test/lib/dynamodb/dynamodb_repo.test.d.ts.map +0 -1
- package/dist/test/lib/dynamodb/dynamodb_repo.test.js +0 -533
- package/dist/test/lib/dynamodb/dynamodb_repo.test.js.map +0 -1
- package/dist/test/lib/dynamodb/environment/index.d.ts +0 -9
- package/dist/test/lib/dynamodb/environment/index.d.ts.map +0 -1
- package/dist/test/lib/dynamodb/environment/index.js +0 -70
- package/dist/test/lib/dynamodb/environment/index.js.map +0 -1
- package/dist/test/lib/dynamodb/environment/types.d.ts +0 -7
- package/dist/test/lib/dynamodb/environment/types.d.ts.map +0 -1
- package/dist/test/lib/dynamodb/environment/types.js +0 -3
- package/dist/test/lib/dynamodb/environment/types.js.map +0 -1
- package/dist/test/lib/dynamodb/utils.d.ts +0 -12
- package/dist/test/lib/dynamodb/utils.d.ts.map +0 -1
- package/dist/test/lib/dynamodb/utils.js +0 -53
- package/dist/test/lib/dynamodb/utils.js.map +0 -1
- package/dist/test/lib/renewed_lease.test.d.ts +0 -2
- package/dist/test/lib/renewed_lease.test.d.ts.map +0 -1
- package/dist/test/lib/renewed_lease.test.js +0 -134
- package/dist/test/lib/renewed_lease.test.js.map +0 -1
- package/dist/test/mock_repo.d.ts +0 -8
- package/dist/test/mock_repo.d.ts.map +0 -1
- package/dist/test/mock_repo.js +0 -22
- package/dist/test/mock_repo.js.map +0 -1
- package/dist/test/types.d.ts +0 -7
- package/dist/test/types.d.ts.map +0 -1
- package/dist/test/types.js +0 -9
- package/dist/test/types.js.map +0 -1
- package/dist/test/utils/async_to_array.d.ts +0 -3
- package/dist/test/utils/async_to_array.d.ts.map +0 -1
- package/dist/test/utils/async_to_array.js +0 -32
- package/dist/test/utils/async_to_array.js.map +0 -1
- package/dist/test/utils/promise_series.d.ts +0 -3
- package/dist/test/utils/promise_series.d.ts.map +0 -1
- package/dist/test/utils/promise_series.js +0 -28
- package/dist/test/utils/promise_series.js.map +0 -1
- package/dist/test/utils/random_string.d.ts +0 -6
- package/dist/test/utils/random_string.d.ts.map +0 -1
- package/dist/test/utils/random_string.js +0 -22
- package/dist/test/utils/random_string.js.map +0 -1
- package/dist/test/utils/repeat.d.ts +0 -3
- package/dist/test/utils/repeat.d.ts.map +0 -1
- package/dist/test/utils/repeat.js +0 -20
- package/dist/test/utils/repeat.js.map +0 -1
- package/dist/test/utils/timing.d.ts +0 -10
- package/dist/test/utils/timing.d.ts.map +0 -1
- package/dist/test/utils/timing.js +0 -39
- package/dist/test/utils/timing.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -14427
- package/src/index.ts +0 -21
- package/src/lib/dynamodb/dynamodb_repo.ts +0 -487
- package/src/lib/dynamodb/utils.ts +0 -168
- package/src/lib/renewed_lease.ts +0 -83
- package/src/types.ts +0 -106
package/src/lib/renewed_lease.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2023 Salto Labs Ltd.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with
|
|
6
|
-
* the License. You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import { types } from '@salto-io/lowerdash'
|
|
17
|
-
import { logger } from '@salto-io/logging'
|
|
18
|
-
import { Pool, LeaseUpdateOpts, Lease, InstanceId, InstanceNotLeasedError } from '../types'
|
|
19
|
-
|
|
20
|
-
const log = logger(module)
|
|
21
|
-
|
|
22
|
-
const poolFuncs: (keyof Pool)[] = ['lease', 'return']
|
|
23
|
-
const isPool = (
|
|
24
|
-
o: Pool | (() => Promise<Pool>)
|
|
25
|
-
): o is Pool => poolFuncs.every((f: keyof Pool) => typeof (o as Pool)[f] === 'function')
|
|
26
|
-
|
|
27
|
-
export type RenewedLeaseOpts<T> = {
|
|
28
|
-
poolOrFactory: Pool | (() => Promise<Pool>)
|
|
29
|
-
lease: Lease<T>
|
|
30
|
-
timeout: number
|
|
31
|
-
renewMargin: number
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export default class RenewedLease<T> extends types.Bean<RenewedLeaseOpts<T>>
|
|
35
|
-
implements Lease<T> {
|
|
36
|
-
timeoutId: NodeJS.Timeout | undefined
|
|
37
|
-
|
|
38
|
-
constructor(opts: RenewedLeaseOpts<T>) {
|
|
39
|
-
super(opts)
|
|
40
|
-
this.timeoutId = this.renewTimeout()
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
private pool(): Promise<Pool> {
|
|
44
|
-
return isPool(this.poolOrFactory)
|
|
45
|
-
? Promise.resolve(this.poolOrFactory)
|
|
46
|
-
: this.poolOrFactory()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get value(): T {
|
|
50
|
-
return this.lease.value
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get id(): InstanceId {
|
|
54
|
-
return this.lease.id
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private renewTimeout(): NodeJS.Timeout {
|
|
58
|
-
return setTimeout(this.renew.bind(this), this.timeout - this.renewMargin)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
private async renew(): Promise<void> {
|
|
62
|
-
const pool = await this.pool()
|
|
63
|
-
try {
|
|
64
|
-
await pool.updateTimeout(this.lease.id, this.timeout)
|
|
65
|
-
this.timeoutId = this.renewTimeout()
|
|
66
|
-
} catch (e) {
|
|
67
|
-
if (!(e instanceof InstanceNotLeasedError)) {
|
|
68
|
-
throw e
|
|
69
|
-
}
|
|
70
|
-
log.warn('lease returned by unknown entity, stops renew interval')
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async return(opts?: LeaseUpdateOpts): Promise<void> {
|
|
75
|
-
if (this.timeoutId !== undefined) {
|
|
76
|
-
clearTimeout(this.timeoutId)
|
|
77
|
-
this.timeoutId = undefined
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const pool = await this.pool()
|
|
81
|
-
return pool.return(this.lease.id, opts)
|
|
82
|
-
}
|
|
83
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2023 Salto Labs Ltd.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with
|
|
6
|
-
* the License. You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import { retry } from '@salto-io/lowerdash'
|
|
17
|
-
|
|
18
|
-
export type InstanceId = string
|
|
19
|
-
|
|
20
|
-
export type RetryStrategy = retry.RetryStrategy
|
|
21
|
-
|
|
22
|
-
export type Lease<T = unknown> = {
|
|
23
|
-
id: InstanceId
|
|
24
|
-
value: T
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type UnavailableLease = {
|
|
28
|
-
leaseExpiresBy: Date
|
|
29
|
-
clientId: string
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type LeaseWithStatus<T> = Lease<T> & (
|
|
33
|
-
({ status: 'suspended'; suspensionReason: string } & UnavailableLease)
|
|
34
|
-
| ({ status: 'leased' } & UnavailableLease)
|
|
35
|
-
| { status: 'available' }
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
export type LeaseUpdateOpts = { validateClientId: boolean }
|
|
39
|
-
|
|
40
|
-
export const DEFAULT_LEASE_UPDATE_OPTS: LeaseUpdateOpts = Object.freeze({ validateClientId: true })
|
|
41
|
-
|
|
42
|
-
export type Pool<T = unknown> = AsyncIterable<LeaseWithStatus<T>> & {
|
|
43
|
-
register(value: T, id?: InstanceId): Promise<InstanceId>
|
|
44
|
-
unregister(id: InstanceId): Promise<void>
|
|
45
|
-
suspend(
|
|
46
|
-
id: InstanceId,
|
|
47
|
-
reason: string,
|
|
48
|
-
timeout: number,
|
|
49
|
-
opts?: Partial<LeaseUpdateOpts>,
|
|
50
|
-
): Promise<void>
|
|
51
|
-
lease(returnTimeout: number): Promise<Lease<T> | null>
|
|
52
|
-
waitForLease(returnTimeout: number, retryStrategy: () => RetryStrategy): Promise<Lease<T>>
|
|
53
|
-
updateTimeout(id: InstanceId, newTimeout: number, opts?: Partial<LeaseUpdateOpts>): Promise<void>
|
|
54
|
-
return(id: InstanceId, opts?: Partial<LeaseUpdateOpts>): Promise<void>
|
|
55
|
-
clear(): Promise<void>
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export type RepoOpts = {
|
|
59
|
-
clientId: string
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type Repo = {
|
|
63
|
-
pool<T extends {}>(typeName: string): Promise<Pool<T>>
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export type RepoMaker<TOpts extends RepoOpts> = (opts: TOpts) => Repo
|
|
67
|
-
|
|
68
|
-
export abstract class InstanceError extends Error {
|
|
69
|
-
readonly id: string
|
|
70
|
-
readonly typeName: string
|
|
71
|
-
|
|
72
|
-
constructor(
|
|
73
|
-
{ id, typeName, message }: { id: InstanceId; typeName: string; message: string }
|
|
74
|
-
) {
|
|
75
|
-
super(`Instance "${id}" of type "${typeName}": ${message}`)
|
|
76
|
-
this.id = id
|
|
77
|
-
this.typeName = typeName
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export class InstanceIdAlreadyRegistered extends InstanceError {
|
|
82
|
-
constructor(
|
|
83
|
-
{ id, typeName }: { id: InstanceId; typeName: string }
|
|
84
|
-
) {
|
|
85
|
-
super({ id, typeName, message: 'already exists' })
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export class InstanceNotFoundError extends InstanceError {
|
|
90
|
-
constructor(
|
|
91
|
-
{ id, typeName }: { id: InstanceId; typeName: string }
|
|
92
|
-
) {
|
|
93
|
-
super({ id, typeName, message: 'not found' })
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export class InstanceNotLeasedError extends InstanceError {
|
|
98
|
-
readonly clientId: string
|
|
99
|
-
|
|
100
|
-
constructor(
|
|
101
|
-
{ id, typeName, clientId }: { id: InstanceId; typeName: string; clientId: string }
|
|
102
|
-
) {
|
|
103
|
-
super({ id, typeName, message: `not leased by client "${clientId}"` })
|
|
104
|
-
this.clientId = clientId
|
|
105
|
-
}
|
|
106
|
-
}
|