Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable keepalive tests #87

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 71 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,88 @@ on:

jobs:
test:
env:
CI: true
timeout-minutes: 10
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [18.x]
kernel: [6.5]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Uninstall all linux-headers packages
run: |
sudo apt-get remove --purge -y 'linux-headers-*'

- name: install redis-cli
run: sudo apt-get install redis-tools
- name: Reinstall specific linux-headers package
run: |
sudo apt-get update
sudo apt-get install -y linux-headers-$(uname -r)

- name: Install dependencies
run: npm install
- name: Install build dependencies
run: |
sudo apt-get update && sudo apt-get install -y build-essential linux-headers-$(uname -r) wget unzip libelf-dev linux-source-6.5.0
tar jxvf /usr/src/linux-source-6.5.0.tar.bz2
cd linux-source-6.5.0

- name: Setup Standalone Tests
run: make test-standalone-setup
- name: Configure kernel sources
run: |
wget https://github.com/veithen/knetstat/archive/refs/heads/master.zip
unzip master.zip
cd knetstat-master
# wget https://cl.archive.ubuntu.com/ubuntu/pool/main/l/linux-azure-6.5/linux-azure-6.5_6.5.0.orig.tar.gz && tar xvzf linux-azure-6.5_6.5.0.orig.tar.gz
mv ../linux-source-6.5.0 linux-6.5
cp /boot/config-$(uname -r) linux-${{ matrix.kernel }}/.config
make -C linux-${{ matrix.kernel }} clean
yes "" | make -C linux-${{ matrix.kernel }} oldconfig
make -C linux-${{ matrix.kernel }} modules_prepare
sed -i '/Module.symvers/d' ./Makefile
make KBUILD_MODPOST_WARN=1 KSRC=linux-${{ matrix.kernel }}
sudo insmod knetstat.ko
sudo lsmod | grep knetstat
- name: dmesg
if: always()
run: |
sudo dmesg|tail -n 10
ls -la /lib/modules


- name: Run Standalone tests
run: make test-standalone
- name: run knetstat
run: cat /proc/net/tcpstat

- name: Teardown Standalone Tests
run: make test-standalone-teardown

- name: Setup Clustered Tests
run: make test-cluster-setup

- name: Check Redis Cluster
run: timeout 60 bash <<< "until redis-cli -c -p 16371 cluster info | grep 'cluster_state:ok'; do sleep 1; done"

- name: Run Clustered tests
run: make test-cluster

- name: Teardown Clustered Tests
run: make test-cluster-teardown
# - name: Install node
# uses: actions/setup-node@v4
# with:
# node-version: ${{ matrix.node-version }}
#
# - name: install redis-cli
# run: sudo apt-get install redis-tools
#
#
# - name: Install dependencies
# run: npm install
#
# - name: Setup Standalone Tests
# run: make test-standalone-setup
#
# - name: Run Standalone tests
# run: make test-standalone
#
# - name: Teardown Standalone Tests
# run: make test-standalone-teardown
#
# - name: Setup Clustered Tests
# run: make test-cluster-setup
#
# - name: Check Redis Cluster
# run: timeout 60 bash <<< "until redis-cli -c -p 16371 cluster info | grep 'cluster_state:ok'; do sleep 1; done"
#
# - name: Run Clustered tests
# run: make test-cluster
#
# - name: Teardown Clustered Tests
# run: make test-cluster-teardown
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ It's a fork from [LimitDB](https://github.com/limitd/limitdb).
- [Configure](#configure)
- [Options available](#options-available)
- [Buckets](#buckets)
- [Ping](#ping)
- [Overrides](#overrides)
- [ERL (Elevated Rate Limits)](#erl-elevated-rate-limits)
- [Prerequisites](#prerequisites)
Expand Down Expand Up @@ -55,11 +54,6 @@ const limitd = new Limitd({
}
},
prefix: 'test:',
ping: {
interval: 1000,
maxFailedAttempts: 5,
reconnectIfFailed: true
},
username: 'username',
password: 'password'
});
Expand All @@ -71,9 +65,9 @@ const limitd = new Limitd({
- `nodes` (array): [Redis Cluster Configuration](https://github.com/luin/ioredis#cluster).
- `buckets` (object): Setup your bucket types.
- `prefix` (string): Prefix keys in Redis.
- `ping` (object): Configure ping to Redis DB.
- `username` (string): Redis username. This is ignored if not in Cluster mode. Needs Redis >= v6.
- `password` (string): Redis password.
- `keepAlive` (number): TCP KeepAlive on the socket expressed in milliseconds. Set to a non-number value to disable keepAlive

### Buckets:

Expand All @@ -91,14 +85,6 @@ If you omit `size`, limitdb assumes that `size` is the value of `per_interval`.

If you don't specify a filling rate with `per_interval` or any other `per_x`, the bucket is fixed and you have to manually reset it using `PUT`.

### Ping:

- `interval` (number): represents the time between two consecutive pings. Default: 3000.
- `maxFailedAttempts` (number): is the allowed number of failed pings before declaring the connection as dead. Default: 5.
- `reconnectIfFailed` (boolean): indicates whether we should try to reconnect is the connection is declared dead. Default: true.



## Overrides
You can also define `overrides` inside your type definitions as follows:

Expand Down
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LimitdRedis extends EventEmitter {

this.db = new LimitDBRedis(_.pick(params, [
'uri', 'nodes', 'buckets', 'prefix', 'slotsRefreshTimeout', 'slotsRefreshInterval',
'username', 'password', 'tls', 'dnsLookup', 'globalTTL', 'cacheSize', 'ping']));
'username', 'password', 'tls', 'dnsLookup', 'globalTTL', 'cacheSize']));

this.db.on('error', (err) => {
this.emit('error', err);
Expand Down
39 changes: 2 additions & 37 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,17 @@ const LRU = require('lru-cache');
const utils = require('./utils');
const Redis = require('ioredis');
const { validateParams, validateERLParams } = require('./validation');
const DBPing = require("./db_ping");
const { calculateQuotaExpiration, resolveElevatedParams, isFixedWindowEnabled, removeHashtag } = require('./utils');
const EventEmitter = require('events').EventEmitter;

const TAKE_LUA = fs.readFileSync(`${__dirname}/take.lua`, "utf8");
const TAKE_ELEVATED_LUA = fs.readFileSync(`${__dirname}/take_elevated.lua`, "utf8");
const PUT_LUA = fs.readFileSync(`${__dirname}/put.lua`, "utf8");

const PING_SUCCESS = "successful";
const PING_ERROR = "error";
const PING_RECONNECT = "reconnect";
const PING_RECONNECT_DRY_RUN = "reconnect-dry-run";

const DEFAULT_COMMAND_TIMEOUT = 125; // Milliseconds
const DEFAULT_KEEPALIVE = 10000; // Milliseconds

class LimitDBRedis extends EventEmitter {
static get PING_SUCCESS() {
return PING_SUCCESS;
}
static get PING_ERROR() {
return PING_ERROR;
}
static get PING_RECONNECT() {
return PING_RECONNECT;
}
static get PING_RECONNECT_DRY_RUN() {
return PING_RECONNECT_DRY_RUN;
}

/**
* Creates an instance of LimitDB client for Redis.
* @param {params} params - The configuration for the database and client.
Expand Down Expand Up @@ -63,6 +45,7 @@ class LimitDBRedis extends EventEmitter {
keyPrefix: config.prefix,
password: config.password,
tls: config.tls,
keepAlive: config.keepAlive || DEFAULT_KEEPALIVE,
reconnectOnError: (err) => {
// will force a reconnect when error starts with `READONLY`
// this code is only triggered when auto-failover is disabled
Expand All @@ -88,7 +71,6 @@ class LimitDBRedis extends EventEmitter {
this.redis = new Redis.Cluster(config.nodes, clusterOptions);
} else {
this.redis = new Redis(config.uri, redisOptions);
this.#setupPing(config);
}

this.redis.defineCommand('take', {
Expand Down Expand Up @@ -120,24 +102,7 @@ class LimitDBRedis extends EventEmitter {

}

#setupPing(config) {
this.redis.on("ready", () => this.#startPing(config));
this.redis.on("close", () => this.#stopPing());
}

#startPing(config) {
this.#stopPing();
this.ping = new DBPing(config.ping, this.redis);
this.ping.on("ping", (data) => this.emit("ping", data));
}

#stopPing() {
this.ping?.stop();
this.ping?.removeAllListeners();
}

close(callback) {
this.#stopPing();
this.redis.quit(callback);
}

Expand Down
106 changes: 0 additions & 106 deletions lib/db_ping.js

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "limitd-redis",
"version": "8.3.1",
"version": "8.4.0",
"description": "A database client for limits on top of redis",
"main": "index.js",
"repository": {
Expand Down Expand Up @@ -28,8 +28,10 @@
"eslint": "^6.1.0",
"mocha": "^5.2.0",
"mockdate": "^3.0.5",
"net-keepalive": "^4.0.10",
"nyc": "^14.1.1",
"sinon": "^19.0.2",
"sockopt": "^2.0.1",
"toxiproxy-node-client": "^2.0.6"
}
}
4 changes: 2 additions & 2 deletions test/db.clustermode.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('when using LimitDB', () => {
return new LimitDB({ nodes: clusterNodes, buckets: {}, prefix: 'tests:', ..._.omit(params, ['uri']) });
};

dbTests(clientCreator);
dbTests(clientCreator, { clusterNodes });

describe('when using the clustered #constructor', () => {
it('should allow setting username and password', (done) => {
Expand All @@ -28,4 +28,4 @@ describe('when using LimitDB', () => {
});
});
})
});
});
Loading
Loading