This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathproviders.js
293 lines (261 loc) · 7.21 KB
/
providers.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict'
const cache = require('hashlru')
const varint = require('varint')
const PeerId = require('peer-id')
const { Key } = require('interface-datastore')
const { default: Queue } = require('p-queue')
const { Buffer } = require('buffer')
const c = require('./constants')
const utils = require('./utils')
/**
* This class manages known providers.
* A provider is a peer that we know to have the content for a given CID.
*
* Every `cleanupInterval` providers are checked if they
* are still valid, i.e. younger than the `provideValidity`.
* If they are not, they are deleted.
*
* To ensure the list survives restarts of the daemon,
* providers are stored in the datastore, but to ensure
* access is fast there is an LRU cache in front of that.
*/
class Providers {
/**
* @param {Object} datastore
* @param {PeerId} [self]
* @param {number} [cacheSize=256]
*/
constructor (datastore, self, cacheSize) {
this.datastore = datastore
this._log = utils.logger(self, 'providers')
/**
* How often invalid records are cleaned. (in seconds)
*
* @type {number}
*/
this.cleanupInterval = c.PROVIDERS_CLEANUP_INTERVAL
/**
* How long is a provider valid for. (in seconds)
*
* @type {number}
*/
this.provideValidity = c.PROVIDERS_VALIDITY
/**
* LRU cache size
*
* @type {number}
*/
this.lruCacheSize = cacheSize || c.PROVIDERS_LRU_CACHE_SIZE
this.providers = cache(this.lruCacheSize)
this.syncQueue = new Queue({ concurrency: 1 })
}
/**
* Release any resources.
*
* @returns {void}
*/
stop () {
if (this._cleaner) {
clearInterval(this._cleaner)
this._cleaner = null
}
}
/**
* Check all providers if they are still valid, and if not delete them.
*
* @returns {Promise<void>}
* @private
*/
_cleanup () {
return this.syncQueue.add(async () => {
this._log('start cleanup')
const start = Date.now()
let count = 0
let deleteCount = 0
const deleted = new Map()
const batch = this.datastore.batch()
// Get all provider entries from the datastore
const query = this.datastore.query({ prefix: c.PROVIDERS_KEY_PREFIX })
for await (const entry of query) {
try {
// Add a delete to the batch for each expired entry
const { cid, peerId } = parseProviderKey(entry.key)
const time = readTime(entry.value)
const now = Date.now()
const delta = now - time
const expired = delta > this.provideValidity
this._log('comparing: %d - %d = %d > %d %s',
now, time, delta, this.provideValidity, expired ? '(expired)' : '')
if (expired) {
deleteCount++
batch.delete(entry.key)
const peers = deleted.get(cid) || new Set()
peers.add(peerId)
deleted.set(cid, peers)
}
count++
} catch (err) {
this._log.error(err.message)
}
}
this._log('deleting %d / %d entries', deleteCount, count)
// Commit the deletes to the datastore
if (deleted.size) {
await batch.commit()
}
// Clear expired entries from the cache
for (const [cid, peers] of deleted) {
const key = makeProviderKey(cid)
const provs = this.providers.get(key)
if (provs) {
for (const peerId of peers) {
provs.delete(peerId)
}
if (provs.size === 0) {
this.providers.remove(key)
} else {
this.providers.set(key, provs)
}
}
}
this._log('Cleanup successful (%dms)', Date.now() - start)
})
}
/**
* Get the currently known provider peer ids for a given CID.
*
* @param {CID} cid
* @returns {Promise<Map<String, Date>>}
*
* @private
*/
async _getProvidersMap (cid) {
const cacheKey = makeProviderKey(cid)
let provs = this.providers.get(cacheKey)
if (!provs) {
provs = await loadProviders(this.datastore, cid)
this.providers.set(cacheKey, provs)
}
return provs
}
get cleanupInterval () {
return this._cleanupInterval
}
set cleanupInterval (val) {
this._cleanupInterval = val
if (this._cleaner) {
clearInterval(this._cleaner)
}
this._cleaner = setInterval(
() => this._cleanup(),
this.cleanupInterval
)
}
/**
* Add a new provider for the given CID.
*
* @param {CID} cid
* @param {PeerId} provider
* @returns {Promise<void>}
*/
async addProvider (cid, provider) { // eslint-disable-line require-await
return this.syncQueue.add(async () => {
this._log('addProvider %s', cid.toBaseEncodedString())
const provs = await this._getProvidersMap(cid)
this._log('loaded %s provs', provs.size)
const now = Date.now()
provs.set(utils.encodeBase32(provider.id), now)
const dsKey = makeProviderKey(cid)
this.providers.set(dsKey, provs)
return writeProviderEntry(this.datastore, cid, provider, now)
})
}
/**
* Get a list of providers for the given CID.
*
* @param {CID} cid
* @returns {Promise<Array<PeerId>>}
*/
async getProviders (cid) { // eslint-disable-line require-await
return this.syncQueue.add(async () => {
this._log('getProviders %s', cid.toBaseEncodedString())
const provs = await this._getProvidersMap(cid)
return [...provs.keys()].map((base32PeerId) => {
return new PeerId(utils.decodeBase32(base32PeerId))
})
})
}
}
/**
* Encode the given key its matching datastore key.
*
* @param {CID|string} cid - cid or base32 encoded string
* @returns {string}
*
* @private
*/
function makeProviderKey (cid) {
cid = typeof cid === 'string' ? cid : utils.encodeBase32(cid.buffer)
return c.PROVIDERS_KEY_PREFIX + cid
}
/**
* Write a provider into the given store.
*
* @param {Datastore} store
* @param {CID} cid
* @param {PeerId} peer
* @param {number} time
* @returns {Promise<void>}
*
* @private
*/
async function writeProviderEntry (store, cid, peer, time) { // eslint-disable-line require-await
const dsKey = [
makeProviderKey(cid),
'/',
utils.encodeBase32(peer.id)
].join('')
const key = new Key(dsKey)
const buffer = Buffer.from(varint.encode(time))
return store.put(key, buffer)
}
/**
* Parse the CID and provider peer id from the key
*
* @param {DKey} key
* @returns {Object} object with peer id and cid
*
* @private
*/
function parseProviderKey (key) {
const parts = key.toString().split('/')
if (parts.length !== 4) {
throw new Error('incorrectly formatted provider entry key in datastore: ' + key)
}
return {
cid: parts[2],
peerId: parts[3]
}
}
/**
* Load providers for the given CID from the store.
*
* @param {Datastore} store
* @param {CID} cid
* @returns {Promise<Map<PeerId, Date>>}
*
* @private
*/
async function loadProviders (store, cid) {
const providers = new Map()
const query = store.query({ prefix: makeProviderKey(cid) })
for await (const entry of query) {
const { peerId } = parseProviderKey(entry.key)
providers.set(peerId, readTime(entry.value))
}
return providers
}
function readTime (buf) {
return varint.decode(buf)
}
module.exports = Providers