Skip to content

Commit 7452b65

Browse files
committed
chore: additional reviewer feedback
Signed-off-by: Nathan Klick <[email protected]>
1 parent f34f3aa commit 7452b65

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/core/lease.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,43 @@ import { type LeaseRenewalService } from './lease_renewal.ts'
2424
import { sleep } from './helpers.ts'
2525

2626
export class Lease {
27+
/** The default duration in seconds for which the lease is to be held before being considered expired. */
2728
public static readonly DEFAULT_LEASE_DURATION = 20
2829

29-
private readonly _client: K8
30-
private readonly _renewalService: LeaseRenewalService
30+
/** The holder of the lease. */
3131
private readonly _leaseHolder: LeaseHolder
32+
33+
/** The namespace which contains the lease. */
3234
private readonly _namespace: string
3335

36+
/** The name of the lease. */
3437
private readonly _leaseName: string
38+
39+
/** The duration in seconds for which the lease is to be held. */
3540
private readonly _durationSeconds: number
3641

42+
/** The identifier of the scheduled lease renewal. */
3743
private _scheduleId: number | null = null
3844

39-
public constructor (client: K8,
40-
renewalService: LeaseRenewalService,
45+
/**
46+
* @param client - Injected kubernetes client need by the methods to create, renew, and delete leases.
47+
* @param renewalService - Injected lease renewal service need to support automatic (background) lease renewals.
48+
* @param leaseHolder - The holder of the lease.
49+
* @param namespace - The namespace in which the lease is to be acquired.
50+
* @param leaseName - The name of the lease to be acquired.
51+
* @param durationSeconds - The duration in seconds for which the lease is to be held.
52+
*/
53+
public constructor (private readonly client: K8,
54+
private readonly renewalService: LeaseRenewalService,
4155
leaseHolder: LeaseHolder,
4256
namespace: string,
4357
leaseName: string | null = null,
4458
durationSeconds: number | null = null) {
45-
if (!client) throw new MissingArgumentError('_client is required')
46-
if (!renewalService) throw new MissingArgumentError('_renewalService is required')
59+
if (!client) throw new MissingArgumentError('client is required')
60+
if (!renewalService) throw new MissingArgumentError('renewalService is required')
4761
if (!leaseHolder) throw new MissingArgumentError('_leaseHolder is required')
4862
if (!namespace) throw new MissingArgumentError('_namespace is required')
4963

50-
this._client = client
51-
this._renewalService = renewalService
5264
this._leaseHolder = leaseHolder
5365
this._namespace = namespace
5466

@@ -140,10 +152,10 @@ export class Lease {
140152
const lease = await this.retrieveLease()
141153

142154
if (this.scheduleId) {
143-
await this._renewalService.cancel(this.scheduleId)
155+
await this.renewalService.cancel(this.scheduleId)
144156
// Needed to ensure any pending renewals are truly cancelled before proceeding to delete the Lease.
145157
// This is required because clearInterval() is not guaranteed to abort any pending interval.
146-
await sleep(this._renewalService.calculateRenewalDelay(this))
158+
await sleep(this.renewalService.calculateRenewalDelay(this))
147159
}
148160

149161
this.scheduleId = null
@@ -184,7 +196,7 @@ export class Lease {
184196

185197
private async retrieveLease (): Promise<V1Lease> {
186198
try {
187-
return await this._client.readNamespacedLease(this.leaseName, this.namespace)
199+
return await this.client.readNamespacedLease(this.leaseName, this.namespace)
188200
} catch (e: any) {
189201
if (!(e instanceof SoloError)) {
190202
throw new LeaseAcquisitionError(`failed to read the lease named '${this.leaseName}' in the ` +
@@ -203,13 +215,13 @@ export class Lease {
203215
private async createOrRenewLease (lease: V1Lease): Promise<void> {
204216
try {
205217
if (!lease) {
206-
await this._client.createNamespacedLease(this.leaseName, this.namespace, this.leaseHolder.toJson(), this.durationSeconds)
218+
await this.client.createNamespacedLease(this.leaseName, this.namespace, this.leaseHolder.toJson(), this.durationSeconds)
207219
} else {
208-
await this._client.renewNamespaceLease(this.leaseName, this.namespace, lease)
220+
await this.client.renewNamespaceLease(this.leaseName, this.namespace, lease)
209221
}
210222

211223
if (!this.scheduleId) {
212-
this.scheduleId = await this._renewalService.schedule(this)
224+
this.scheduleId = await this.renewalService.schedule(this)
213225
}
214226
} catch (e: any) {
215227
throw new LeaseAcquisitionError(`failed to create or renew the lease named '${this.leaseName}' in the ` +
@@ -219,10 +231,10 @@ export class Lease {
219231

220232
private async transferLease (lease: V1Lease): Promise<void> {
221233
try {
222-
await this._client.transferNamespaceLease(lease, this.leaseHolder.toJson())
234+
await this.client.transferNamespaceLease(lease, this.leaseHolder.toJson())
223235

224-
if (!this._scheduleId) {
225-
this._scheduleId = await this._renewalService.schedule(this)
236+
if (!this.scheduleId) {
237+
this.scheduleId = await this.renewalService.schedule(this)
226238
}
227239
} catch (e: any) {
228240
throw new LeaseAcquisitionError(`failed to transfer the lease named '${this.leaseName}' in the ` +
@@ -232,7 +244,7 @@ export class Lease {
232244

233245
private async deleteLease (): Promise<void> {
234246
try {
235-
await this._client.deleteNamespacedLease(this.leaseName, this.namespace)
247+
await this.client.deleteNamespacedLease(this.leaseName, this.namespace)
236248
} catch (e: any) {
237249
throw new LeaseRelinquishmentError(`failed to delete the lease named '${this.leaseName}' in the ` +
238250
`'${this.namespace}' namespace`, e)

0 commit comments

Comments
 (0)