Skip to content

Instantly share code, notes, and snippets.

View pcdinh's full-sized avatar

Pham Cong Dinh pcdinh

View GitHub Profile
@pcdinh
pcdinh / NioScalabilityTest.java
Created January 7, 2012 15:59 — forked from jbrisbin/NioScalabilityTest.java
Pure Java NIO scalability test
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
@pcdinh
pcdinh / sendmail.py
Created March 19, 2012 08:13 — forked from danigm/sendmail.py
Send mail with attachment in python
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
import time
import random
@pcdinh
pcdinh / graceful_http_server.py
Created December 22, 2011 19:35 — forked from mikelikespie/graceful_http_server.py
Graceful tornado http server shutdown
class MyHTTPServer(tornado.httpserver.HTTPServer):
_stopped = False
def _quit_if_ioloop_is_empty(self):
ioloop = tornado.ioloop.IOLoop.instance()
if len(ioloop._handlers) <= 1:
logger.info("Graceful shutdown complete. Exiting!")
exit(0)
else:
logger.info("Waiting for ioloop to be empty. has %d handlers left" % len(ioloop._handlers))
@pcdinh
pcdinh / install.sh
Created August 10, 2018 10:08 — forked from sergeyklay/install.sh
Install Nginx with Nchan
#!/usr/bin/env bash
# Works fine on Ubuntu 14.0.4 LTS
NGINX_VERSION="1.9.9"
NCHAN_VERSION="0.97"
HEADERS_MORE_VERSION="0.29"
DEV_KIT_VERSION="0.2.19"
ECHO_VERSION="0.58"
FANCY_INDEX_VERSION="0.3.5"
@pcdinh
pcdinh / about.txt
Created July 5, 2018 07:13 — forked from jessejlt/about.txt
nginx, flask, and file downloads
Okay so here's the setup:
[-] The primary server API is exposed via Flask (Python) and all static files, including all html, css, js is served by nginx.
[-] Python is exposing an API at url http://domain.com/api/download/<file_id>, where file_id is a database id for the file that we're interested in downloading.
1. User wants to download a file, so we spawn a new window with the url '/api/download/<file_id>'
2. Nginx intercepts the request, sees that it starts with /api/, and then forwards the request to Flask, which is being served on port 5000.
3. Flask routes the request to its download method, retrieves the pertinent data from the file_id, and constructs additional header settings to make nginx happy and to force the browser to see the file stream as a download request instead of the browser just trying to open the file in a new window. Flask then returns the modified header stream to nginx
4. Nginx is finally ready to do some work. While parsing the headers for the incoming request, it encounters "X

O(1) Block Propagation

The problem

Bitcoin miners want their newly-found blocks to propagate across the network as quickly as possible, because every millisecond of delay increases the chances that another block, found at about the same time, wins the "block race."

@pcdinh
pcdinh / openresty-luarocks.sh
Created July 28, 2017 11:19 — forked from yagop/openresty-luarocks.sh
OpenResty + LuaRocks
wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz \
-O ngx_openresty-1.7.10.1.tar.gz
tar xzvf ngx_openresty-1.7.10.1.tar.gz
cd ngx_openresty-1.7.10.1
./configure
make
sudo make install
wget https://codeload.github.com/keplerproject/luarocks/tar.gz/v2.0.13 \
@pcdinh
pcdinh / nginx.conf
Created July 13, 2017 07:59 — forked from deltheil/nginx.conf
Hide sensitive GET parameters within nginx access logs thanks to the Lua module
http {
log_format filt '$remote_addr - $remote_user [$time_local] "$_request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
location /login {
# `set` is provided by the Rewrite module
set $filter "password|secret";
@pcdinh
pcdinh / 100_base.conf
Created June 28, 2017 09:31 — forked from danackerson/100_base.conf
using nginx + lua + redis for redirects and rewrites
# using such a setup requires `apt-get install lua-nginx-redis` under Ubuntu Trusty
# more info @ http://wiki.nginx.org/HttpLuaModule#access_by_lua
http {
lua_package_path "/etc/nginx/include.d/?.lua;;";
lua_socket_pool_size 100;
lua_socket_connect_timeout 10ms;
lua_socket_read_timeout 10ms;
server {
@pcdinh
pcdinh / semiprivate.md
Created June 27, 2017 10:25 — forked from tarcieri/semiprivate.md
Ed25519-based semi-private keys

Semiprivate Keys

Semi-private keys are an expansion of the traditional idea of asymmetric keys, which have a public/private keypair, to N keys which can each represent a different capability level. In the degenerate case, a semi-private key system has 3 different types of keys. These are, to use the Tahoe terminology:

  • writecap: can publish new ciphertexts
  • readcap: can read/authenticate ciphertexts