Refactor caching prefix building to provide unique defaults

This commit is contained in:
FoxxMD
2021-08-24 14:36:13 -04:00
parent 0ef2b99bd6
commit 75ac5297df
3 changed files with 63 additions and 66 deletions

View File

@@ -1,6 +1,6 @@
import {Logger} from "winston";
import {
buildCacheOptionsFromProvider,
buildCacheOptionsFromProvider, buildCachePrefix,
createAjvFactory,
mergeArr,
normalizeName,
@@ -43,6 +43,7 @@ import {operatorConfig} from "./Utils/CommandConfig";
import merge from 'deepmerge';
import * as process from "process";
import {cacheOptDefaults, cacheTTLDefaults} from "./Common/defaults";
import objectHash from "object-hash";
export interface ConfigBuilderOptions {
logger: Logger,
@@ -528,7 +529,7 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
level = 'verbose',
path,
} = {},
caching,
caching: opCache,
web: {
port = 8085,
maxLogs = 200,
@@ -552,8 +553,44 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
bots = [],
} = data;
let cache: StrongCache;
let defaultProvider: CacheOptions;
if(opCache === undefined) {
defaultProvider = {
store: 'memory',
...cacheOptDefaults
};
cache = {
...cacheTTLDefaults,
provider: defaultProvider
};
} else {
const {provider, ...restConfig} = opCache;
if(typeof provider === 'string') {
defaultProvider = {
store: provider as CacheProvider,
...cacheOptDefaults
};
} else {
const {ttl = 60, max = 500, store = 'memory', ...rest} = provider || {};
defaultProvider = {
store,
...cacheOptDefaults,
...rest,
};
}
cache = {
...cacheTTLDefaults,
...restConfig,
provider: defaultProvider,
}
}
let hydratedBots: BotInstanceConfig[] = bots.map(x => {
const {
name: botName,
polling: {
sharedMod = false,
limit = 100,
@@ -583,10 +620,10 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
} = x;
let cache: StrongCache;
let botCache: StrongCache;
if(caching === undefined) {
cache = {
botCache = {
...cacheTTLDefaults,
provider: {
store: 'memory',
@@ -596,7 +633,7 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
} else {
const {provider, ...restConfig} = caching;
if (typeof provider === 'string') {
cache = {
botCache = {
...cacheTTLDefaults,
...restConfig,
provider: {
@@ -606,7 +643,7 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
}
} else {
const {ttl = 60, max = 500, store = 'memory', ...rest} = provider || {};
cache = {
botCache = {
...cacheTTLDefaults,
...restConfig,
provider: {
@@ -618,7 +655,18 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
}
}
const botCreds = {
clientId: (ci as string),
clientSecret: (cs as string),
...restCred,
};
if (botCache.provider.prefix === undefined || botCache.provider.prefix === defaultProvider.prefix) {
// need to provide unique prefix to bot
botCache.provider.prefix = buildCachePrefix([botCache.provider.prefix, 'bot', (botName || objectHash.sha1(botCreds))]);
}
return {
name: botName,
snoowrap,
subreddits: {
names,
@@ -627,12 +675,8 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
heartbeatInterval,
dryRun,
},
credentials: {
clientId: (ci as string),
clientSecret: (cs as string),
...restCred,
},
caching: cache,
credentials: botCreds,
caching: botCache,
polling: {
sharedMod,
limit,
@@ -649,41 +693,6 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
});
let cache: StrongCache;
let defaultProvider: CacheOptions;
if(caching === undefined) {
defaultProvider = {
store: 'memory',
...cacheOptDefaults
};
cache = {
...cacheTTLDefaults,
provider: defaultProvider
};
} else {
const {provider, ...restConfig} = caching;
if(typeof provider === 'string') {
defaultProvider = {
store: provider as CacheProvider,
...cacheOptDefaults
};
} else {
const {ttl = 60, max = 500, store = 'memory', ...rest} = provider || {};
defaultProvider = {
store,
...cacheOptDefaults,
...rest,
};
}
cache = {
...cacheTTLDefaults,
...restConfig,
provider: defaultProvider,
}
}
const defaultOperators = typeof name === 'string' ? [name] : name;
const config: OperatorConfig = {

View File

@@ -13,7 +13,7 @@ import winston, {Logger} from "winston";
import fetch from 'node-fetch';
import {
asSubmission,
buildCacheOptionsFromProvider,
buildCacheOptionsFromProvider, buildCachePrefix,
cacheStats, createCacheManager,
formatNumber, getActivityAuthorName,
mergeArr,
@@ -605,19 +605,6 @@ export class BotResourcesManager {
const options = provider;
this.cacheType = options.store;
if(this.cacheType === 'redis') {
// need to make sure there is a key prefix
if(options.prefix === undefined) {
// name is way more friendly but...
if(name !== undefined) {
options.prefix = `${name}:`;
} else {
// if no name given use credentials hash since that is most likely unique among all configured bots
// and if it isn't then operator is running the same bot twice in which case a shared namespace isn't too bad since its technically running the same subreddits anyway
options.prefix = `${objectHash.sha1(credentials)}:`
}
}
}
this.defaultCache = createCacheManager(options);
if (this.cacheType === 'memory') {
const min = Math.min(...([this.ttlDefaults.wikiTTL, this.ttlDefaults.authorTTL, this.ttlDefaults.userNotesTTL].filter(x => x !== 0)));
@@ -667,10 +654,7 @@ export class BotResourcesManager {
// only need to create private if there settings are actually different than the default
if(hash !== this.cacheHash) {
const {provider: trueProvider, ...trueRest} = cacheConfig;
if(trueProvider.store === 'redis') {
// add subreddit name to prefix
trueProvider.prefix = `${(trueProvider.prefix || '')}${subName}:`;
}
trueProvider.prefix = trueProvider.prefix === this.defaultCacheConfig.provider.prefix ? buildCachePrefix([trueProvider.prefix, subName]) : trueProvider.prefix;
opts = {
cache: createCacheManager(trueProvider),
cacheType: trueProvider.store,

View File

@@ -1033,5 +1033,9 @@ export const getActivityAuthorName = (author: any): string => {
}
export const buildCachePrefix = (parts: any[]): string => {
return parts.filter(x => typeof x === 'string' && x !== '').map(x => x.trim()).map(x => x.split(':')).flat().join(':')
const prefix = parts.filter(x => typeof x === 'string' && x !== '').map(x => x.trim()).map(x => x.split(':')).flat().filter(x => x !== '').join(':')
if(prefix !== '') {
return `${prefix}:`;
}
return prefix;
}