-
Notifications
You must be signed in to change notification settings - Fork 4
/
AsyncLane.js
168 lines (140 loc) · 5.84 KB
/
AsyncLane.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { getSample } from 'source/common/math/sample'
import { createAsyncFuncQueue } from './AsyncFuncQueue'
// ## AsyncLane ##
// list of AsyncQueue (here AsyncQueue = lane)
// no specific lane select logic provided
// only provide summary status, not query for each value
// for load-balancing when using JS to trigger heavier external code, or implement a specific queue strategy
const createAsyncLane = ({
laneSize, // the size can't be changed for now
createAsyncQueue = createAsyncFuncQueue // allow swap AsyncQueue
}) => {
// sanity check
laneSize = Number(laneSize)
if (!(laneSize >= 1)) throw new Error(`invalid laneSize: ${laneSize}`)
// init data to [ { index, asyncQueue } ]
const laneList = getSample((index) => ({ index, asyncQueue: createAsyncQueue() }), laneSize)
const getStatus = (isVerbose) => laneList.map(({ index, asyncQueue }) => isVerbose
? { index, length: asyncQueue.getLength() }
: asyncQueue.getLength()
)
const reset = () => laneList.forEach(({ asyncQueue }) => { asyncQueue.reset() })
const push = (value, laneIndex) => {
const lane = laneList[ laneIndex ]
if (!lane) throw new Error(`invalid laneIndex: ${laneIndex}`)
return lane.asyncQueue.push(value)
}
return {
laneList, // for extend func, not recommend direct access
getStatus, reset, push
}
}
// ## sample extend ##
// below is some idea to extend asyncLane
// note this code is more for sample, and be compose-able
// for max performance better single all-in-one enhance with targeted API
// ## AutoSelectLane extend ##
// sample extend func for asyncLane
// add auto lane select
// note the select pattern can change like: `(laneList, value, ...args) => lane`
const extendAutoSelectLane = (
asyncLane,
selectLane = selectMinLoadLane // (laneList, value) => lane
) => {
const { laneList } = asyncLane
return {
...asyncLane,
pushAuto: (value) => asyncLane.push(value, selectLane(laneList, value).index) // new
}
}
const selectMinLoadLane = (laneList, value /* , ...args */) => laneList.reduce(
(o, lane) => o.asyncQueue.getLength() > lane.asyncQueue.getLength() ? lane : o,
laneList[ 0 ] // default to first lane
)
// ## ValueList extend ##
// sample extend func for asyncLane
// maintain the value in an extra list
// ## About lazy drop ##
// since the valueList and asyncQueue are pushed at the same time
// and valueList keep a newer first order
// so to drop stale value from valueList, just clip the queue length to match asyncQueue
// and since we expect more func will be pushed
// the lazy clip can happen only after a new func is pushed
const extendLaneValueList = (asyncLane) => {
const { laneList } = asyncLane
// patch data to [ { index, asyncQueue, valueList } ]
laneList.forEach((lane) => { lane.valueList = [] }) // newer first, length may be longer due to lazy trim, when some long running func get pushed at the same time
const reset = () => {
asyncLane.reset()
laneList.forEach(({ valueList }) => { valueList.length = 0 })
}
const getStatus = (isVerbose) => {
const status = asyncLane.getStatus()
isVerbose && status.forEach((laneStatus) => { laneStatus.valueList = laneList[ laneStatus.index ].valueList })
return status
}
const push = (value, laneIndex) => {
const valuePromise = asyncLane.push(value, laneIndex)
const { asyncQueue, valueList } = laneList[ laneIndex ]
valueList.unshift(value)
valueList.length = asyncQueue.getLength() // lazy stale value drop, happen on new value push
return valuePromise
}
const trimValueList = () => laneList.forEach(({ asyncQueue, valueList }) => { // combine with setInterval for more frequent stale value trim, most time lazy drop should be enough
valueList.length = asyncQueue.getLength()
})
return {
...asyncLane,
getStatus, reset, push, // change
trimValueList // new
}
}
// ## ValueMap extend ##
// sample extend func for asyncLane
// maintain the value in an extra map
// require the value to have id
// require manual trim of valueMap
const extendLaneValueMap = (asyncLane) => {
const { laneList } = asyncLane
// patch data to [ { index, asyncQueue, valueMap } ]
laneList.forEach((lane) => { lane.valueMap = new Map() }) // newer first, length may be longer due to lazy trim, when some long running func get pushed at the same time
const reset = () => {
asyncLane.reset()
laneList.forEach(({ valueMap }) => { valueMap.clear() })
}
const getStatus = (isVerbose) => {
const status = asyncLane.getStatus()
isVerbose && status.forEach((laneStatus) => { laneStatus.valueList = [ ...laneList[ laneStatus.index ].valueMap.values() ] })
return status
}
const push = (value, laneIndex) => {
if (!value.id) throw new Error('expect value.id')
const valuePromise = asyncLane.push(value, laneIndex)
laneList[ laneIndex ].valueMap.set(value.id, value)
// if (laneList[ laneIndex ].valueMap.has(value.id)) throw new Error(`duplicate value.id: ${value.id}`) // the check is too late, and can't prevent id duplicate cross lane
return valuePromise
}
const trimValueMap = ( // combine with setInterval for more frequent stale value trim
filterFunc // return true to keep
) => laneList.forEach(({ valueMap }) => valueMap.forEach((value, id) => filterFunc(value, id) || valueMap.delete(id)))
const findValue = (id, laneIndex) => {
const lane = laneList[ laneIndex ]
return lane && lane.valueMap.get(id)
}
const dropValue = (id, laneIndex) => {
const lane = laneList[ laneIndex ]
return lane && lane.valueMap.delete(id)
}
return {
...asyncLane,
getStatus, reset, push, // change
trimValueMap, findValue, dropValue // new
}
}
export {
createAsyncLane,
// sample extend func, also compose-able, not for top performance
extendAutoSelectLane, selectMinLoadLane,
extendLaneValueList,
extendLaneValueMap
}