Skip to content

Instantly share code, notes, and snippets.

View shasharoman's full-sized avatar

DSha shasharoman

View GitHub Profile
@shasharoman
shasharoman / balancer.lua
Created August 22, 2021 06:18
openresty-dynamic-upstream
local balancer = require 'ngx.balancer'
local upstream = require 'ngx.upstream'
local dyupcl = require 'dyupcl'
balancer.set_timeouts(1, 1, 60)
local name = balancer.get_last_failure()
if not name then
local ok, err = balancer.set_more_tries(1)
if not ok then
ngx.log(ngx.ERR, err)
@shasharoman
shasharoman / access.lua
Last active August 22, 2021 06:10
openresty-rate-limit
local limit = require 'limit'
local ignoreIps = {}
local ignoreKeys = {}
local headers = ngx.req.get_headers()
local key = headers['api-key'] or ''
local ip = string.match(headers['x-forwarded-for'] or ngx.var.remote_addr, '%d+.%d+.%d+.%d+')
for i, v in ipairs(ignoreKeys) do
if v == key then
@shasharoman
shasharoman / ws-tunnel-core.js
Created December 4, 2018 12:53
websocket tunnel core code
const C = require('../constant');
const nUrl = require('url');
const uuid = require('uuid');
const ws = require('ws');
const lib = require(process.env.lib);
const logger = lib.logger.createLogger('tunnel-service-ws');
const fw = require(process.env.fw);
let id2Socket = {}; // 信道id与socket映射,用于sendMessage
let id2Notify = {}; // 信道id的回调信息,暂时使用内部的http rpc方式
@shasharoman
shasharoman / queue-core.js
Created June 25, 2018 10:04
core redis-queue logic
const Promise = require('bluebird');
const _ = require('lodash');
const uuid = require('uuid');
const C = require('../constant');
const proxy = require('../proxy');
const lib = require(process.env.lib);
const redis = lib.redis.createClient(C.QUEUE.REDIS);
const logger = lib.logger.createLogger('common-service-queue');
exports.pushTask = pushTask;