aboutsummaryrefslogtreecommitdiffstats
path: root/Gruntfile.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2014-08-06 22:40:56 -0700
committerAdam Soltys <[email protected]>2014-08-06 22:41:18 -0700
commite1464288b2f53fd4c200d64df9a085dbb0ca80b7 (patch)
tree3cb0f1de508c09ed9c6788fea16d7d8b0a6bc191 /Gruntfile.js
parenta6d9c76af2b4c7f3accf27fce5ecd83fbdcaf0c1 (diff)
downloadcoinos-server-e1464288b2f53fd4c200d64df9a085dbb0ca80b7.tar.gz
coinos-server-e1464288b2f53fd4c200d64df9a085dbb0ca80b7.zip
Display calculator after sign in
Diffstat (limited to 'Gruntfile.js')
0 files changed, 0 insertions, 0 deletions
a id='n117' href='#n117'>117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
import config from "$config";
import { fail, wait, sleep } from "$lib/utils";
import { createClient, defineScript } from "redis";
import { warn, l, err } from "$lib/logging";

let SCRIPT = `
local balanceKey = KEYS[1]
local creditKey = KEYS[2]
local amount = tonumber(ARGV[1])
local tip = tonumber(ARGV[2])
local fee = tonumber(ARGV[3])
local ourfee = tonumber(ARGV[4])

local balance = tonumber(redis.call('get', balanceKey) or '0')
local credit = tonumber(redis.call('get', creditKey) or '0')

local covered = math.min(credit, ourfee)
ourfee = ourfee - covered

if balance < amount + tip + fee + ourfee then
    return {err = 'Insufficient funds ⚡️' .. balance .. ' of ⚡️' .. amount + tip + fee + ourfee}
end

redis.call('decrby', creditKey, tostring(math.floor(covered)))
redis.call('decrby', balanceKey, tostring(math.floor(amount + tip + fee + ourfee)))

return ourfee
`;

let debit = defineScript({
  NUMBER_OF_KEYS: 2,
  SCRIPT,
  transformArguments: (...args) => args.map((a) => a.toString()),
});

export let db = createClient({
  url: config.db,
  scripts: { debit },
  socket: {
    reconnectStrategy: (retries) => Math.min(retries * 50, 5000),
  },
});

export let archive = createClient({
  url: config.archive,
  socket: {
    reconnectStrategy: (retries) => Math.min(retries * 50, 5000),
  },
});

export let arc2 = createClient({
  url: config.arc2,
  socket: {
    reconnectStrategy: (retries) => Math.min(retries * 50, 5000),
  },
});

async function dbReconnect() {
  try {
    await db.connect();
  } catch (err) {
    console.error("Failed to connect to Redis, retrying...", err);
    setTimeout(dbReconnect, 5000); // Retry after 5 seconds
  }
}

async function archiveReconnect() {
  try {
    await archive.connect();
  } catch (err) {
    console.error("Failed to connect to Redis, retrying...", err);
    setTimeout(archiveReconnect, 5000); // Retry after 5 seconds
  }
}

dbReconnect();
archiveReconnect();

db.on("error", (e) => {
  if (e.message.startsWith("getaddr")) return;
  err("Redis error", e.message);
});

db.on("end", () => {
  warn("Redis connection ended");
});

export default db;

export let g = async (k) => {
  let v = await db.get(k);
  try {
    return JSON.parse(v);
  } catch (e) {
    return v;
  }
};

export let s = (k, v) => {
  if (k === "user:null" || k === "user:undefined") fail("null user");
  db.set(k, JSON.stringify(v));
};

export let ga = async (k) => {
  let v = await archive.get(k);
  try {
    return JSON.parse(v);
  } catch (e) {
    return v;
  }
};

export let sa = (k, v) => {
  if (k === "user:null" || k === "user:undefined") {
    warn("###### NULL USER #######");
    console.trace();
  }
  archive.set(k, JSON.stringify(v));
};

let retries = {};
export let t = async (k, f) => {
  try {
    await db.watch(k);
    await f(await db.get(k), db);
  } catch (err) {
    if (!err.message.includes("watch")) throw err;

    let r = retries[k] || 0;
    retries[k] = r + 1;

    if (r < 10) {
      await sleep(100);
      await t(k, f);
    } else {
      delete retries[k];
      fail("unable to obtain lock");
    }
  }

  delete retries[k];
};