Skip to content

Commit d0cbcd9

Browse files
committed
replace cache with cache-manager
1 parent 7bb2313 commit d0cbcd9

14 files changed

Lines changed: 890 additions & 472 deletions

File tree

OpenFlow/src/Auth.ts

Lines changed: 145 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -24,150 +24,150 @@ export class Auth {
2424
}
2525
}
2626

27-
public static item_cache: BaseObserver = null;
27+
// public static item_cache: BaseObserver = null;
2828

29-
public static authorizationCache: HashTable<CachedUser> = {};
30-
private static cacheTimer: NodeJS.Timeout;
31-
public static shutdown() {
32-
if (!NoderedUtil.IsNullUndefinded(this.cacheTimer)) {
33-
clearInterval(this.cacheTimer);
34-
}
35-
}
36-
public static ensureotel() {
37-
if (!NoderedUtil.IsNullUndefinded(Logger.otel) && NoderedUtil.IsNullUndefinded(Auth.item_cache)) {
38-
Auth.item_cache = Logger.otel.meter.createValueObserver("openflow_item_cache_count", {
39-
description: 'Total number of cached items'
40-
}, res => {
41-
let keys: string[] = Object.keys(this.authorizationCache);
42-
let types = {};
43-
for (let i = keys.length - 1; i >= 0; i--) {
44-
if (!types[this.authorizationCache[keys[i]].type]) types[this.authorizationCache[keys[i]].type] = 0;
45-
types[this.authorizationCache[keys[i]].type]++;
46-
}
47-
keys = Object.keys(types);
48-
for (let i = keys.length - 1; i >= 0; i--) {
49-
res.observe(types[keys[i]], { ...Logger.otel.defaultlabels, type: keys[i] })
50-
}
51-
});
52-
}
53-
}
54-
public static getUser(key: string, type: string): User {
55-
if (NoderedUtil.IsNullUndefinded(this.cacheTimer)) this.cacheTimer = setInterval(this.cleanCache, 60000)
56-
var res: CachedUser = this.authorizationCache[key + type];
57-
if (res === null || res === undefined) {
58-
if (Config.log_cache) Logger.instanse.debug("[" + type + "][" + key + "] not found in cache");
59-
return null;
60-
}
61-
var begin: number = res.firstsignin.getTime();
62-
var end: number = new Date().getTime();
63-
var seconds = Math.round((end - begin) / 1000);
64-
let cache_seconds: number = Config.api_credential_cache_seconds;
65-
if (type == "grafana") cache_seconds = Config.grafana_credential_cache_seconds;
66-
if (type == "dashboard") cache_seconds = Config.dashboard_credential_cache_seconds;
67-
if (type == "cleanacl") cache_seconds = Config.cleanacl_credential_cache_seconds;
68-
if (type == "userroles") cache_seconds = Config.cleanacl_credential_cache_seconds;
69-
if (type == "mq") cache_seconds = Config.mq_credential_cache_seconds;
70-
if (type == "mqe") cache_seconds = Config.mq_credential_cache_seconds;
71-
if (type == "password") cache_seconds = Config.cleanacl_credential_cache_seconds;
72-
if (seconds < cache_seconds) {
73-
Logger.instanse.silly("Return user " + res.user.username + " from cache");
74-
return res.user as User;
75-
}
76-
this.RemoveUser(key, type);
77-
return null;
78-
}
79-
public static async clearCache(reason: string) {
80-
DBHelper.cached_roles = [];
81-
if (this.authorizationCache == null || this.authorizationCache == {}) {
82-
if (Config.log_cache) Logger.instanse.debug("clearCache called, but cache was empty, reason: " + reason);
83-
return;
84-
}
85-
Auth.ensureotel();
86-
let keys: string[] = Object.keys(this.authorizationCache);
87-
await Auth.semaphore.down();
88-
this.authorizationCache = {}
89-
Auth.semaphore.up();
90-
if (Config.log_cache) Logger.instanse.debug("clearCache called with " + keys.length + " keys in cache, reason: " + reason);
91-
}
92-
public static async cleanCache() {
93-
try {
94-
if (this.authorizationCache == null) return;
95-
const keys: string[] = Object.keys(this.authorizationCache);
96-
for (let i = keys.length - 1; i >= 0; i--) {
97-
let key: string = keys[i];
98-
var res: CachedUser = this.authorizationCache[key];
99-
if (res === null || res === undefined) continue;
100-
var begin: number = res.firstsignin.getTime();
101-
var end: number = new Date().getTime();
102-
var seconds = Math.round((end - begin) / 1000);
103-
let cache_seconds: number = Config.api_credential_cache_seconds;
104-
if (res.type == "grafana") cache_seconds = Config.grafana_credential_cache_seconds;
105-
if (res.type == "dashboard") cache_seconds = Config.dashboard_credential_cache_seconds;
106-
if (res.type == "cleanacl") cache_seconds = Config.cleanacl_credential_cache_seconds;
107-
if (res.type == "userroles") cache_seconds = Config.cleanacl_credential_cache_seconds;
108-
if (res.type == "mq") cache_seconds = Config.mq_credential_cache_seconds;
109-
if (res.type == "mqe") cache_seconds = Config.mq_credential_cache_seconds;
110-
if (res.type == "password") cache_seconds = Config.cleanacl_credential_cache_seconds;
111-
if (seconds >= cache_seconds) {
112-
this.RemoveUser(key, res.type);
113-
}
114-
}
115-
const keys2: string[] = Object.keys(this.authorizationCache);
116-
if (Config.log_cache) Logger.instanse.debug("cleanCache called with " + keys.length + " keys in cache, and " + keys2.length + " after enumeration");
117-
} catch (error) {
118-
Logger.instanse.error(error)
119-
}
120-
}
121-
public static async RemoveUser(key: string, type: string): Promise<void> {
122-
Auth.ensureotel();
123-
await Auth.semaphore.down();
124-
if (!NoderedUtil.IsNullUndefinded(this.authorizationCache[key + type])) {
125-
if (Config.log_cache) Logger.instanse.debug("Delete user with key " + key + " from cache");
126-
delete this.authorizationCache[key + type];
127-
} else {
128-
if (Config.log_cache) Logger.instanse.debug("RemoveUser user called with " + key + " but was not found in cache");
129-
}
130-
Auth.semaphore.up();
131-
}
132-
public static async AddUser(user: User | TokenUser, key: string, type: string): Promise<void> {
133-
Auth.ensureotel();
134-
await Auth.semaphore.down();
135-
if (NoderedUtil.IsNullUndefinded(this.authorizationCache[key + type])) {
136-
if (Config.log_cache) Logger.instanse.debug("Adding user " + user.name + " to cache with key " + key);
137-
} else {
138-
if (Config.log_cache) Logger.instanse.debug("Updating user " + user.name + " to cache with key " + key);
139-
}
140-
var cuser: CachedUser = new CachedUser(user, user._id, type);
141-
this.authorizationCache[key + type] = cuser;
142-
Auth.semaphore.up();
143-
}
144-
public static Semaphore = (n) => ({
145-
n,
146-
async down() {
147-
while (this.n <= 0) await this.wait();
148-
this.n--;
149-
},
150-
up() {
151-
this.n++;
152-
},
153-
async wait() {
154-
if (this.n <= 0) return new Promise((res, req) => {
155-
setImmediate(async () => res(await this.wait()))
156-
});
157-
},
158-
});
159-
public static semaphore = Auth.Semaphore(1);
160-
}
161-
export class CachedUser {
162-
public firstsignin: Date;
163-
constructor(
164-
public user: User | TokenUser,
165-
public _id: string,
166-
public type: string
167-
) {
168-
this.firstsignin = new Date();
169-
}
170-
}
171-
interface HashTable<T> {
172-
[key: string]: T;
29+
// public static authorizationCache: HashTable<CachedUser> = {};
30+
// private static cacheTimer: NodeJS.Timeout;
31+
// public static shutdown() {
32+
// if (!NoderedUtil.IsNullUndefinded(this.cacheTimer)) {
33+
// clearInterval(this.cacheTimer);
34+
// }
35+
// }
36+
// public static ensureotel() {
37+
// if (!NoderedUtil.IsNullUndefinded(Logger.otel) && NoderedUtil.IsNullUndefinded(Auth.item_cache)) {
38+
// Auth.item_cache = Logger.otel.meter.createValueObserver("openflow_item_cache_count", {
39+
// description: 'Total number of cached items'
40+
// }, res => {
41+
// let keys: string[] = Object.keys(this.authorizationCache);
42+
// let types = {};
43+
// for (let i = keys.length - 1; i >= 0; i--) {
44+
// if (!types[this.authorizationCache[keys[i]].type]) types[this.authorizationCache[keys[i]].type] = 0;
45+
// types[this.authorizationCache[keys[i]].type]++;
46+
// }
47+
// keys = Object.keys(types);
48+
// for (let i = keys.length - 1; i >= 0; i--) {
49+
// res.observe(types[keys[i]], { ...Logger.otel.defaultlabels, type: keys[i] })
50+
// }
51+
// });
52+
// }
53+
// }
54+
// public static getUser(key: string, type: string): User {
55+
// if (NoderedUtil.IsNullUndefinded(this.cacheTimer)) this.cacheTimer = setInterval(this.cleanCache, 60000)
56+
// var res: CachedUser = this.authorizationCache[key + type];
57+
// if (res === null || res === undefined) {
58+
// if (Config.log_cache) Logger.instanse.debug("[" + type + "][" + key + "] not found in cache");
59+
// return null;
60+
// }
61+
// var begin: number = res.firstsignin.getTime();
62+
// var end: number = new Date().getTime();
63+
// var seconds = Math.round((end - begin) / 1000);
64+
// let cache_seconds: number = Config.api_credential_cache_seconds;
65+
// if (type == "grafana") cache_seconds = Config.grafana_credential_cache_seconds;
66+
// if (type == "dashboard") cache_seconds = Config.dashboard_credential_cache_seconds;
67+
// if (type == "cleanacl") cache_seconds = Config.cleanacl_credential_cache_seconds;
68+
// if (type == "userroles") cache_seconds = Config.cleanacl_credential_cache_seconds;
69+
// if (type == "mq") cache_seconds = Config.mq_credential_cache_seconds;
70+
// if (type == "mqe") cache_seconds = Config.mq_credential_cache_seconds;
71+
// if (type == "password") cache_seconds = Config.cleanacl_credential_cache_seconds;
72+
// if (seconds < cache_seconds) {
73+
// Logger.instanse.silly("Return user " + res.user.username + " from cache");
74+
// return res.user as User;
75+
// }
76+
// this.RemoveUser(key, type);
77+
// return null;
78+
// }
79+
// public static async clearCache(reason: string) {
80+
// // DBHelper.cached_roles = [];
81+
// if (this.authorizationCache == null || this.authorizationCache == {}) {
82+
// if (Config.log_cache) Logger.instanse.debug("clearCache called, but cache was empty, reason: " + reason);
83+
// return;
84+
// }
85+
// Auth.ensureotel();
86+
// let keys: string[] = Object.keys(this.authorizationCache);
87+
// await Auth.semaphore.down();
88+
// this.authorizationCache = {}
89+
// Auth.semaphore.up();
90+
// if (Config.log_cache) Logger.instanse.debug("clearCache called with " + keys.length + " keys in cache, reason: " + reason);
91+
// }
92+
// public static async cleanCache() {
93+
// try {
94+
// if (this.authorizationCache == null) return;
95+
// const keys: string[] = Object.keys(this.authorizationCache);
96+
// for (let i = keys.length - 1; i >= 0; i--) {
97+
// let key: string = keys[i];
98+
// var res: CachedUser = this.authorizationCache[key];
99+
// if (res === null || res === undefined) continue;
100+
// var begin: number = res.firstsignin.getTime();
101+
// var end: number = new Date().getTime();
102+
// var seconds = Math.round((end - begin) / 1000);
103+
// let cache_seconds: number = Config.api_credential_cache_seconds;
104+
// if (res.type == "grafana") cache_seconds = Config.grafana_credential_cache_seconds;
105+
// if (res.type == "dashboard") cache_seconds = Config.dashboard_credential_cache_seconds;
106+
// if (res.type == "cleanacl") cache_seconds = Config.cleanacl_credential_cache_seconds;
107+
// if (res.type == "userroles") cache_seconds = Config.cleanacl_credential_cache_seconds;
108+
// if (res.type == "mq") cache_seconds = Config.mq_credential_cache_seconds;
109+
// if (res.type == "mqe") cache_seconds = Config.mq_credential_cache_seconds;
110+
// if (res.type == "password") cache_seconds = Config.cleanacl_credential_cache_seconds;
111+
// if (seconds >= cache_seconds) {
112+
// this.RemoveUser(key, res.type);
113+
// }
114+
// }
115+
// const keys2: string[] = Object.keys(this.authorizationCache);
116+
// if (Config.log_cache) Logger.instanse.debug("cleanCache called with " + keys.length + " keys in cache, and " + keys2.length + " after enumeration");
117+
// } catch (error) {
118+
// Logger.instanse.error(error)
119+
// }
120+
// }
121+
// public static async RemoveUser(key: string, type: string): Promise<void> {
122+
// Auth.ensureotel();
123+
// await Auth.semaphore.down();
124+
// if (!NoderedUtil.IsNullUndefinded(this.authorizationCache[key + type])) {
125+
// if (Config.log_cache) Logger.instanse.debug("Delete user with key " + key + " from cache");
126+
// delete this.authorizationCache[key + type];
127+
// } else {
128+
// if (Config.log_cache) Logger.instanse.debug("RemoveUser user called with " + key + " but was not found in cache");
129+
// }
130+
// Auth.semaphore.up();
131+
// }
132+
// public static async AddUser(user: User | TokenUser, key: string, type: string): Promise<void> {
133+
// Auth.ensureotel();
134+
// await Auth.semaphore.down();
135+
// if (NoderedUtil.IsNullUndefinded(this.authorizationCache[key + type])) {
136+
// if (Config.log_cache) Logger.instanse.debug("Adding user " + user.name + " to cache with key " + key);
137+
// } else {
138+
// if (Config.log_cache) Logger.instanse.debug("Updating user " + user.name + " to cache with key " + key);
139+
// }
140+
// var cuser: CachedUser = new CachedUser(user, user._id, type);
141+
// this.authorizationCache[key + type] = cuser;
142+
// Auth.semaphore.up();
143+
// }
144+
// public static Semaphore = (n) => ({
145+
// n,
146+
// async down() {
147+
// while (this.n <= 0) await this.wait();
148+
// this.n--;
149+
// },
150+
// up() {
151+
// this.n++;
152+
// },
153+
// async wait() {
154+
// if (this.n <= 0) return new Promise((res, req) => {
155+
// setImmediate(async () => res(await this.wait()))
156+
// });
157+
// },
158+
// });
159+
// public static semaphore = Auth.Semaphore(1);
173160
}
161+
// export class CachedUser {
162+
// public firstsignin: Date;
163+
// constructor(
164+
// public user: User | TokenUser,
165+
// public _id: string,
166+
// public type: string
167+
// ) {
168+
// this.firstsignin = new Date();
169+
// }
170+
// }
171+
// interface HashTable<T> {
172+
// [key: string]: T;
173+
// }

OpenFlow/src/Config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ export class Config {
9595
Config.tls_ca = Config.getEnv("tls_ca", "");
9696
Config.tls_passphrase = Config.getEnv("tls_passphrase", "");
9797

98+
Config.cache_store_type = Config.getEnv("cache_store_type", "memory");
99+
Config.cache_store_max = parseInt(Config.getEnv("cache_store_max", "1000"));
100+
Config.cache_store_ttl_seconds = parseInt(Config.getEnv("cache_store_ttl_seconds", "3600"));
101+
Config.cache_store_redis_host = Config.getEnv("cache_store_redis_host", "");
102+
Config.cache_store_redis_port = parseInt(Config.getEnv("cache_store_redis_port", "6379"));
103+
Config.cache_store_redis_password = Config.getEnv("cache_store_redis_password", "");
104+
98105
Config.api_credential_cache_seconds = parseInt(Config.getEnv("api_credential_cache_seconds", "900"));
99106
Config.dashboard_credential_cache_seconds = parseInt(Config.getEnv("dashboard_credential_cache_seconds", "900"));
100107
Config.grafana_credential_cache_seconds = parseInt(Config.getEnv("grafana_credential_cache_seconds", "900"));
@@ -274,6 +281,13 @@ export class Config {
274281
public static tls_ca: string = Config.getEnv("tls_ca", "");
275282
public static tls_passphrase: string = Config.getEnv("tls_passphrase", "");
276283

284+
public static cache_store_type: string = Config.getEnv("cache_store_type", "memory");
285+
public static cache_store_max: number = parseInt(Config.getEnv("cache_store_max", "1000"));
286+
public static cache_store_ttl_seconds: number = parseInt(Config.getEnv("cache_store_ttl_seconds", "3600"));
287+
public static cache_store_redis_host: string = Config.getEnv("cache_store_redis_host", "");
288+
public static cache_store_redis_port: number = parseInt(Config.getEnv("cache_store_redis_port", "6379"));
289+
public static cache_store_redis_password: string = Config.getEnv("cache_store_redis_password", "");
290+
277291
public static api_credential_cache_seconds: number = parseInt(Config.getEnv("api_credential_cache_seconds", "900"));
278292
public static dashboard_credential_cache_seconds: number = parseInt(Config.getEnv("dashboard_credential_cache_seconds", "900"));
279293
public static grafana_credential_cache_seconds: number = parseInt(Config.getEnv("grafana_credential_cache_seconds", "900"));
@@ -302,7 +316,7 @@ export class Config {
302316

303317
public static client_heartbeat_timeout: number = parseInt(Config.getEnv("client_heartbeat_timeout", "60"));
304318

305-
public static expected_max_roles: number = parseInt(Config.getEnv("expected_max_roles", "4000"));
319+
public static expected_max_roles: number = parseInt(Config.getEnv("expected_max_roles", "20000"));
306320
public static decorate_roles_fetching_all_roles = Config.parseBoolean(Config.getEnv("decorate_roles_fetching_all_roles", "true"));
307321
public static roles_cached_in_seconds: number = parseInt(Config.getEnv("roles_cached_in_seconds", "300"));
308322
public static max_recursive_group_depth: number = parseInt(Config.getEnv("max_recursive_group_depth", "2"));

0 commit comments

Comments
 (0)