Skip to content

Commit

Permalink
use ttl cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu committed Jul 24, 2024
1 parent 484bd67 commit 7564e93
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
16 changes: 4 additions & 12 deletions @xen-orchestra/xapi/host.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LRU from 'lru-cache'
import TTLCache from '@isaacs/ttlcache'
import { asyncEach } from '@vates/async-each'
import { asyncMap } from '@xen-orchestra/async-map'
import { decorateClass } from '@vates/decorate-with'
Expand Down Expand Up @@ -44,9 +44,9 @@ const IPMI_SENSOR_REGEX_BY_PRODUCT_NAME = Object.keys(IPMI_SENSOR_REGEX_BY_DATA_
{}
)

const IPMI_CACHE = new LRU({
max: MAX_HOSTS_PER_POOL,
const IPMI_CACHE = new TTLCache({
ttl: IPMI_CACHE_TTL,
max: MAX_HOSTS_PER_POOL,
})

const waitAgentRestart = (xapi, hostRef, prevAgentStartTime) =>
Expand Down Expand Up @@ -182,17 +182,11 @@ class Host {
}

async getIpmiSensors(ref) {
const cache = IPMI_CACHE.get(ref)
if (cache !== undefined) {
return cache
}

const productName = (await this.getField('host', ref, 'bios_strings'))['system-product-name']?.toLowerCase()
if (IPMI_SENSOR_REGEX_BY_DATA_TYPE_BY_SUPPORTED_PRODUCT_NAME[productName] === undefined) {
IPMI_CACHE.set(ref, {})
return {}
}
const callSensorPlugin = fn => this.callAsync('host.call_plugin', ref, '2crsi-sensors.py', fn, {})
const callSensorPlugin = fn => this.call(IPMI_CACHE, 'host.call_plugin', ref, '2crsi-sensors.py', fn, {})
// https://github.com/AtaxyaNetwork/xcp-ng-xapi-plugins/tree/ipmi-sensors?tab=readme-ov-file#ipmi-sensors-parser
const [stringifiedIpmiSensors, ip] = await Promise.all([callSensorPlugin('get_info'), callSensorPlugin('get_ip')])
const ipmiSensors = JSON.parse(stringifiedIpmiSensors)
Expand All @@ -217,8 +211,6 @@ class Host {

ipmiSensorsByDataType[IPMI_SENSOR_DATA_TYPE.generalInfo] = { ip }

IPMI_CACHE.set(ref, ipmiSensorsByDataType)

return ipmiSensorsByDataType
}
}
Expand Down
2 changes: 1 addition & 1 deletion @xen-orchestra/xapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"test": "node--test"
},
"dependencies": {
"@isaacs/ttlcache": "^1.4.1",
"@vates/async-each": "^1.0.0",
"@vates/decorate-with": "^2.1.0",
"@vates/nbd-client": "^3.0.2",
Expand All @@ -34,7 +35,6 @@
"http-request-plus": "^1.0.0",
"json-rpc-protocol": "^0.13.2",
"lodash": "^4.17.15",
"lru-cache": "^7.0.4",
"promise-toolbox": "^0.21.0",
"vhd-lib": "^4.11.0",
"xo-common": "^0.8.0"
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,11 @@
make-error "^1.1.1"
v8-compile-cache-lib "^3.0.1"

"@isaacs/ttlcache@^1.4.1":
version "1.4.1"
resolved "https://registry.yarnpkg.com/@isaacs/ttlcache/-/ttlcache-1.4.1.tgz#21fb23db34e9b6220c6ba023a0118a2dd3461ea2"
integrity sha512-RQgQ4uQ+pLbqXfOmieB91ejmLwvSgv9nLx6sT6sD83s7umBypgg+OIBOBbEUiJXrfpnp9j0mRhYYdzp9uqq3lA==

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
Expand Down

0 comments on commit 7564e93

Please sign in to comment.