@go-mailer/jarvis 10.9.2 → 10.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -9,20 +9,20 @@ const WalletThrottler = async ({ redisClient = null, RMQBroker, wallet_charge_qu
|
|
|
9
9
|
const lua_script = fs.readFileSync(path.join(__dirname, 'throttler.lua'), 'utf-8')
|
|
10
10
|
const deduct_sha = await redisClient.scriptLoad(lua_script)
|
|
11
11
|
|
|
12
|
-
const deductCredits = async ({ tenant_id,
|
|
12
|
+
const deductCredits = async ({ tenant_id, resource_id, resource_type, wallet_type, amount, min_allowed = 0 }) => {
|
|
13
13
|
const balance_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}`
|
|
14
|
-
const usage_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}:usage`
|
|
14
|
+
const usage_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}:${resource_type}:${resource_id}:usage`
|
|
15
15
|
|
|
16
16
|
let result
|
|
17
17
|
try {
|
|
18
18
|
result = await redisClient.evalSha(deduct_sha, {
|
|
19
19
|
keys: [balance_key, usage_key],
|
|
20
|
-
arguments: [amount.toString(), min_allowed.toString()
|
|
20
|
+
arguments: [amount.toString(), min_allowed.toString()]
|
|
21
21
|
})
|
|
22
22
|
} catch (err) {
|
|
23
23
|
result = await redisClient.eval(lua_script, {
|
|
24
24
|
keys: [balance_key, usage_key],
|
|
25
|
-
arguments: [amount.toString(), min_allowed.toString()
|
|
25
|
+
arguments: [amount.toString(), min_allowed.toString()]
|
|
26
26
|
})
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -47,16 +47,16 @@ const WalletThrottler = async ({ redisClient = null, RMQBroker, wallet_charge_qu
|
|
|
47
47
|
throw new Error('wallet_charge_queue is required for WalletThrottler')
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
const used_keys = await redisClient.keys('wallet:tenant:*:wallet
|
|
50
|
+
const used_keys = await redisClient.keys('wallet:tenant:*:wallet:*:*:*:usage')
|
|
51
51
|
for (const usage_key of used_keys) {
|
|
52
52
|
const usage = await redisClient.get(usage_key)
|
|
53
|
-
const [, , tenant_id, , wallet_type] = usage_key.split(':')
|
|
53
|
+
const [, , tenant_id, , wallet_type, resource_type, resource_id] = usage_key.split(':')
|
|
54
54
|
if (usage && parseInt(usage, 10) === 0) continue
|
|
55
55
|
|
|
56
56
|
await redisClient.multi().set(usage_key, 0).exec()
|
|
57
57
|
RMQBroker.publish(
|
|
58
58
|
wallet_charge_queue,
|
|
59
|
-
JSON.stringify({ tenant_id, resource_id
|
|
59
|
+
JSON.stringify({ tenant_id, resource_id, resource_type, wallet_type, cost: parseInt(usage, 10), timestamp: Date.now() })
|
|
60
60
|
)
|
|
61
61
|
|
|
62
62
|
console.log(`Synced ${usage} used credits for ${tenant_id}`)
|
|
@@ -65,31 +65,17 @@ const WalletThrottler = async ({ redisClient = null, RMQBroker, wallet_charge_qu
|
|
|
65
65
|
|
|
66
66
|
const loadCache = async ({ tenant_id, wallet_type, balance }) => {
|
|
67
67
|
const balance_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}`
|
|
68
|
-
const usage_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}:usage`
|
|
69
68
|
|
|
70
69
|
// Load balance from DB if missing
|
|
71
70
|
const redis_balance = await redisClient.get(balance_key)
|
|
72
71
|
if (redis_balance === null) {
|
|
73
72
|
await redisClient.set(balance_key, balance)
|
|
74
73
|
}
|
|
75
|
-
|
|
76
|
-
// Check & sync any dangling used immediately
|
|
77
|
-
const used = parseInt((await redisClient.get(usage_key)) || '0', 10)
|
|
78
|
-
if (used > 0) {
|
|
79
|
-
RMQBroker.publish(
|
|
80
|
-
wallet_charge_queue,
|
|
81
|
-
JSON.stringify({ tenant_id, resource_id: 0, resource_type: wallet_type, wallet_type, cost: used, timestamp: Date.now() })
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
await redisClient.set(usage_key, 0) // reset after sync
|
|
85
|
-
await redisClient.decrBy(balance_key, used) // update Redis balance too
|
|
86
|
-
console.log(`Synced dangling used ${used} for ${tenant_id} on startup`)
|
|
87
|
-
}
|
|
88
74
|
}
|
|
89
75
|
|
|
90
|
-
const refundCredits = async ({ tenant_id, wallet_type, amount, resource_id }) => {
|
|
76
|
+
const refundCredits = async ({ tenant_id, wallet_type, amount, resource_id, resource_type }) => {
|
|
91
77
|
const balance_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}`
|
|
92
|
-
const usage_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}:usage`
|
|
78
|
+
const usage_key = `wallet:tenant:${tenant_id}:wallet:${wallet_type}:${resource_type}:${resource_id}:usage`
|
|
93
79
|
|
|
94
80
|
// Atomic refund in Redis
|
|
95
81
|
await redisClient
|
|
@@ -106,6 +92,7 @@ const WalletThrottler = async ({ redisClient = null, RMQBroker, wallet_charge_qu
|
|
|
106
92
|
amount: 0,
|
|
107
93
|
units: amount,
|
|
108
94
|
resource_id,
|
|
95
|
+
resource_type,
|
|
109
96
|
type: wallet_type
|
|
110
97
|
})
|
|
111
98
|
)
|
|
@@ -8,7 +8,6 @@ local balanceKey = KEYS[1]
|
|
|
8
8
|
local usedKey = KEYS[2]
|
|
9
9
|
local amount = tonumber(ARGV[1])
|
|
10
10
|
local minAllowed = tonumber(ARGV[2]) or 0
|
|
11
|
-
local msgType = ARGV[3]
|
|
12
11
|
|
|
13
12
|
local balance = redis.call('GET', balanceKey)
|
|
14
13
|
if not balance then
|
|
@@ -25,8 +24,6 @@ end
|
|
|
25
24
|
redis.call('SET', balanceKey, balance - amount)
|
|
26
25
|
|
|
27
26
|
-- Track usage
|
|
28
|
-
|
|
29
|
-
redis.call('INCRBY', usedKey, amount)
|
|
30
|
-
end
|
|
27
|
+
redis.call('INCRBY', usedKey, amount)
|
|
31
28
|
|
|
32
29
|
return {1, "success", balance - amount}
|