Skip to content

Instantly share code, notes, and snippets.

View limboinf's full-sized avatar
🎯
Focusing

limbo limboinf

🎯
Focusing
View GitHub Profile
@limboinf
limboinf / access.lua
Created September 30, 2017 03:31 — forked from mariocesar/access.lua
Nginx Lua script redis based for Basic user authentication
function password_encode(password)
local bcrypt = require 'bcrypt'
return bcrypt.digest(password, 12)
end
function check_password(password, encoded_password)
local bcrypt = require 'bcrypt'
return bcrypt.verify(password, encoded_password)
end

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@limboinf
limboinf / tmux-cheatsheet.markdown
Created March 27, 2017 08:00 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@limboinf
limboinf / jsonhandler.py
Created August 18, 2016 04:40 — forked from mminer/jsonhandler.py
A JSON request handler for Tornado.
import json
import tornado.web
class JsonHandler(BaseHandler):
"""Request handler where requests and responses speak JSON."""
def prepare(self):
# Incorporate request JSON into arguments dictionary.
if self.request.body:
try:

本文基本上这为两篇文章的翻译和整合 -- Scalable networking And Why are event-driven server so great

OPPC模型瓶颈

传统服务器模型如Apache为每一个请求生成一个子进程。当用户连接到服务器的一个子进程就产生,并处理连接。每个连接获得一个单独的线程和子进程。当用户请求数据返回时,子进程开始等待数据库操作返回。如果此时另一个用户也请求返回数据,这时就产生了阻塞。

这种模式在非常小的工作负荷是表现良好,当请求的数量变得太大是服务器会压力过于巨大。 当Apache达到进程的最大数量,所有进程都变得缓慢。每个请求都有自己的线程,如果服务代码使用PHP编写时,每个进程所需要的内存量是相当大的[1]。

fork()操作延时

@limboinf
limboinf / .gitconfig
Last active May 20, 2016 02:26 — forked from samsalisbury/.gitconfig
Git diff and merge with p4merge (OSX)
[merge]
keepBackup = false
tool = p4merge
[mergetool "p4merge"]
cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge "\"$PWD/$BASE\"" "\"$PWD/$REMOTE\"" "\"$PWD/$LOCAL\"" "\"$PWD/$MERGED\""
keepTemporaries = false
trustExitCode = false
keepBackup = false
[diff]
tool = p4merge
@limboinf
limboinf / torndb.ex.py
Last active January 19, 2016 07:11 — forked from damozhang/torndb.ex.py
Examples of that how to use Torndb.
import torndb
mysql_settings = {
'host': 'localhost:3306',
'user': 'user',
'password': 'password',
'database': 'database',
'time_zone': '-8:00',
'charset': 'utf8'