Skip to content

Instantly share code, notes, and snippets.

View xuanyu-h's full-sized avatar
🎯
Focusing

xuanyu xuanyu-h

🎯
Focusing
  • Shanghai
View GitHub Profile
# Ruby Thread Pool
# ================
# A thread pool is useful when you wish to do some work in a thread, but do
# not know how much work you will be doing in advance. Spawning one thread
# for each task is potentially expensive, as threads are not free.
#
# In this case, it might be more beneficial to start a predefined set of
# threads and then hand off work to them as it becomes available. This is
# the pure essence of what a thread pool is: an array of threads, all just
# waiting to do some work for you!
@xuanyu-h
xuanyu-h / synchronize_access_object.rb
Created November 1, 2018 03:30
synchronizing access to an object
# encoding: utf-8
# frozen_string_literal: true
require 'thwait'
class Object
def synchronize
mutex.synchronize { yield self }
end
class ArrayBlockingQueue
attr_reader :capacity, :queue
def initialize(capacity = 5)
@capacity = capacity
@lock = Mutex.new
@empty = ConditionVariable.new
@full = ConditionVariable.new
@queue = []
end
@xuanyu-h
xuanyu-h / SnowFlake.java
Created December 29, 2017 03:12
Twitter 雪花算法
/**
* @author Spirit
* @date 2017/12/14
*/
/**
* Twitter 雪花算法
*
* 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000
* 1 位标识
@xuanyu-h
xuanyu-h / vpnsetup-centos.sh
Last active July 4, 2018 15:17
vpnsetup-centos
#!/bin/sh
#
# Script for automatic setup of an IPsec VPN server on CentOS/RHEL 6 and 7.
# Works on any dedicated server or virtual private server (VPS) except OpenVZ.
#
# DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC!
#
# The latest version of this script is available at:
# https://github.com/hwdsl2/setup-ipsec-vpn
#
@xuanyu-h
xuanyu-h / sshd_config.sh
Created July 4, 2018 14:16
sshd_config.sh
Port 2202
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
@xuanyu-h
xuanyu-h / haproxy.conf
Created April 8, 2018 08:24 — forked from thpham/haproxy.conf
test config haproxy for gRPC loadbalancing
global
tune.ssl.default-dh-param 1024
defaults
timeout connect 10000ms
timeout client 60000ms
timeout server 60000ms
frontend fe_http
mode http
@xuanyu-h
xuanyu-h / Vagrantfile
Created January 3, 2018 03:19
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
BOX_NAME = ENV['BOX_NAME'] || "centos/7"
VAGRANTFILE_API_VERSION = "2"
CONSUL_ENCRYPT = "eEZJ2MTe0keJc2EkvZ+l3Q=="
CONSUL_DATACENTER = "nyc"
CONSUL_DATA_DIR = "/var/consul"
CONSUL_NODE_IP = {
@xuanyu-h
xuanyu-h / config.py
Last active December 22, 2017 02:41
python context??
from functools import wraps
def print_method_name(f):
@wraps(f)
def decorated(*args, **kwargs):
print("enter function: {0}, {1}, {2}".format(f.__name__, args, kwargs))
return f(*args, **kwargs)
return decorated
@xuanyu-h
xuanyu-h / singleton.py
Created December 20, 2017 07:31
Python 单例模式
class AuthManager(object):
"""Authentication Manager."""
def __new__(cls):
singleton = cls.__dict__.get('__singleton__')
if singleton is not None:
return singleton
cls.__singleton__ = singleton = object.__new__(cls)