Skip to content

Commit b11724e

Browse files
authored
fix(OL2): Deferment cleanup has to be a timer to not be concurrent (#4896)
* Deferment cleanup has to be a timer to not be concurrent * Remove obsolete snapshot
1 parent 11948b4 commit b11724e

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

packages/objectloader2/src/helpers/defermentManager.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DefermentManagerOptions } from '../operations/options.js'
44

55
export class DefermentManager {
66
private deferments: Map<string, DeferredBase> = new Map()
7+
private timer?: ReturnType<typeof setTimeout>
78
private logger: CustomLogger
89
private currentSize = 0
910
private disposed = false
@@ -12,6 +13,7 @@ export class DefermentManager {
1213
private totalDefermentRequests: Map<string, number> = new Map()
1314

1415
constructor(private options: DefermentManagerOptions) {
16+
this.resetGlobalTimer()
1517
this.logger = options.logger || ((): void => {})
1618
}
1719

@@ -59,14 +61,6 @@ export class DefermentManager {
5961
if (this.disposed) throw new Error('DefermentManager is disposed')
6062
const now = this.now()
6163
this.currentSize += item.size || 0
62-
if (this.currentSize > this.options.maxSizeInMb * 1024 * 1024) {
63-
this.logger(
64-
'deferments size exceeded, cleaning up',
65-
this.currentSize,
66-
this.options.maxSizeInMb
67-
)
68-
this.cleanDeferments()
69-
}
7064
//order matters here with found before undefer
7165
const deferredBase = this.deferments.get(item.baseId)
7266
if (deferredBase) {
@@ -79,9 +73,21 @@ export class DefermentManager {
7973
}
8074
}
8175

76+
private resetGlobalTimer(): void {
77+
const run = (): void => {
78+
this.cleanDeferments()
79+
this.timer = setTimeout(run, this.options.ttlms)
80+
}
81+
this.timer = setTimeout(run, this.options.ttlms)
82+
}
83+
8284
dispose(): void {
8385
if (this.disposed) return
8486
this.disposed = true
87+
if (this.timer) {
88+
clearTimeout(this.timer)
89+
this.timer = undefined
90+
}
8591
this.clearDeferments()
8692
}
8793

@@ -99,6 +105,15 @@ export class DefermentManager {
99105
}
100106

101107
private cleanDeferments(): void {
108+
const maxSizeBytes = this.options.maxSizeInMb * 1024 * 1024
109+
if (this.currentSize < maxSizeBytes) {
110+
this.logger(
111+
'deferments size is ok, no need to clean',
112+
this.currentSize,
113+
maxSizeBytes
114+
)
115+
return
116+
}
102117
const now = this.now()
103118
let cleaned = 0
104119
const start = performance.now()
@@ -115,6 +130,9 @@ export class DefermentManager {
115130
this.currentSize -= deferredBase.getItem()?.size || 0
116131
this.deferments.delete(deferredBase.getId())
117132
cleaned++
133+
if (this.currentSize < maxSizeBytes) {
134+
break
135+
}
118136
}
119137
}
120138
this.logger(

packages/objectloader2/src/operations/databases/__snapshots__/indexedDatabase.spec.ts.snap

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`IndexedDatabase > should add and get an item 1`] = `
4-
[
5-
{
6-
"baseId": "id1",
7-
"item": {
8-
"foo": "bar",
9-
},
10-
},
11-
]
12-
`;
13-
143
exports[`IndexedDatabase > should add and get multiple items 1`] = `
154
[
165
{

0 commit comments

Comments
 (0)