Skip to content

Commit

Permalink
chore: update lru-cache dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Nov 19, 2023
1 parent c8521c9 commit f6e1043
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
27 changes: 14 additions & 13 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CoapServerOptions, requestListener, CoapPacket, Block, MiddlewareParame
import BlockCache from './cache'
import OutgoingMessage from './outgoing_message'
import { Socket, createSocket, SocketOptions } from 'dgram'
import LRUCache from 'lru-cache'
import { LRUCache } from 'lru-cache'
import os from 'os'
import IncomingMessage from './incoming_message'
import ObserveStream from './observe_write_stream'
Expand Down Expand Up @@ -77,8 +77,8 @@ function allAddresses (type): string[] {
return addresses
}

class CoapLRUCache<K, V> extends LRUCache<K, V> {
pruneTimer: NodeJS.Timer
class CoapLRUCache<K extends {}, V extends {}> extends LRUCache<K, V> {
pruneTimer: NodeJS.Timeout
}

class CoAPServer extends EventEmitter {
Expand Down Expand Up @@ -148,19 +148,19 @@ class CoAPServer extends EventEmitter {
// 32 MB / 1280 = 26214
// The max lifetime is roughly 200s per packet.
// Which gave us 131 packets/second guarantee
let max = 32768 * 1024
let maxSize = 32768 * 1024

if (typeof this._options.cacheSize === 'number' && this._options.cacheSize >= 0) {
max = this._options.cacheSize
maxSize = this._options.cacheSize
}

this._lru = new CoapLRUCache({
max,
length: (n, key) => {
maxSize,
sizeCalculation: (n, key) => {
return n.buffer.byteLength
},
maxAge: parameters.exchangeLifetime * 1000,
dispose: (key, value) => {
ttl: parameters.exchangeLifetime * 1000,
dispose: (value, key) => {
if (value.sender != null) {
value.sender.reset()
}
Expand Down Expand Up @@ -345,7 +345,7 @@ class CoAPServer extends EventEmitter {
if (parameters.pruneTimerPeriod != null) {
// Start LRU pruning timer
this._lru.pruneTimer = setInterval(() => {
this._lru.prune()
this._lru.purgeStale()
}, parameters.pruneTimerPeriod * 1000)
if (this._lru.pruneTimer.unref != null) {
this._lru.pruneTimer.unref()
Expand All @@ -368,11 +368,11 @@ class CoAPServer extends EventEmitter {
if (this._internal_socket && this._sock instanceof Socket) {
this._sock.close()
}
this._lru.reset()
this._lru.clear()
this._sock = null
this.emit('close')
} else {
this._lru.reset()
this._lru.clear()
}

this._block2Cache.reset()
Expand Down Expand Up @@ -409,7 +409,8 @@ class CoAPServer extends EventEmitter {
if (cached.response != null && (packet.reset ?? false)) {
cached.response.end()
}
return lru.del(this._toKey(request, packet, false))
lru.delete(this._toKey(request, packet, false))
return
} else if (packet.ack ?? packet.reset ?? false) {
return // nothing to do, ignoring silently
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@
"typescript": "^4.9.3"
},
"dependencies": {
"@types/lru-cache": "^5.1.1",
"@types/lru-cache": "^7.10.10",
"bl": "^6.0.0",
"@types/readable-stream": "^2.3.15",
"capitalize": "^2.0.4",
"coap-packet": "^1.1.1",
"debug": "^4.3.4",
"fastseries": "^2.0.0",
"lru-cache": "^6.0.0",
"lru-cache": "^10.0.3",
"readable-stream": "^4.2.0"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,14 +1222,14 @@ describe('server LRU', function () {
server.on('request', (req, res) => {
res.end()

expect(server._lru.itemCount).to.be.equal(1)
expect(server._lru.size).to.be.equal(1)

clock.tick(parameters.exchangeLifetime * 500)

expect(server._lru.itemCount).to.be.equal(1)
expect(server._lru.size).to.be.equal(1)

clock.tick(parameters.exchangeLifetime * 1000)
expect(server._lru.itemCount).to.be.equal(0)
expect(server._lru.size).to.be.equal(0)
done()
})
})
Expand Down

0 comments on commit f6e1043

Please sign in to comment.