Skip to content

Instantly share code, notes, and snippets.

View shenfeng's full-sized avatar

Feng Shen shenfeng

View GitHub Profile
@shenfeng
shenfeng / LockFreeQueue.java
Created February 26, 2022 10:38
lock free concurrent queue java
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
public class LockFreeQueue<E> {
private static final VarHandle NEXT;
private static final VarHandle HEAD;
private static final VarHandle TAIL;
static {
try {
def fork_and_wait(args, s):
p = subprocess.Popen(args, stdout=subprocess.PIPE)
def wait():
time.sleep(s)
if p.returncode is None:
logging.info("kill process %s", p.pid)
p.kill()
sys.exit(1)
@shenfeng
shenfeng / tw.py
Last active August 29, 2015 14:00
ThoughtWorks的代码题 https://www.jinshuju.net/f/EGQL3D
def transform(specials, mapping, n):
"""
https://www.jinshuju.net/f/EGQL3D
"""
# rule 5
if str(specials[0]) in str(n):
return mapping[specials[0]]
return ''.join(mapping[i] for i in specials if n % i == 0) or str(n)
@shenfeng
shenfeng / nginx_access_log_parser.py
Created December 31, 2013 01:43
parse nginx access log in python
def seg_access_log(line):
delimiters = {'[': ']', '"': '"'}
idx, start, count, delimiter, results = 0, 0, len(line), ' ', []
while 1:
idx = line.find(delimiter, start)
delimiter = ' ' # reset
if idx < 0:
break
@shenfeng
shenfeng / inverse-index.py
Created July 9, 2013 23:09
教老婆学习python:了解倒排索引是怎么回事。
__author__ = 'feng'
articles = [
'Familiarize Yourself with IntelliJ IDEA editor',
'While keeping the Ctrl key pressed, rotate the mouse wheel. As you rotate the mouse wheel forward',
'font size grows larger; as you rotate the mouse wheel backwards, font size decreases',
'In the popup frame, type Reset font size, and click Enter.',
'These operations apply to the active editor only. In the other editor tabs, font size is not affected.',
'There is no default keyboard shortcut associated with Reset font size action. However, you can create your',
'Place the caret in the editor.'
@shenfeng
shenfeng / gist:5589978
Created May 16, 2013 07:19
wildcard_match
__author__ = 'feng'
def is_match(patten, str):
match_index = 0
for idx, c in enumerate(patten):
if c == '?':
match_index += 1
elif c == '*':
if idx == len(patten) - 1:
(ns example
(:use [org.httpkit.server
compojure.core]))
(defn- handle-client-data [data]
(let [data (read-json data)]
;; routing can implented here. like socket.io's event routing
))
;;; when streaming/polling, client post data to server,
@shenfeng
shenfeng / http-kit-hello-world.clj
Created February 4, 2013 01:28
clojure web "hello world"
;;; http://http-kit.org
(ns main
(:use org.httpkit.server))
(defn handler [req]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "hello world"})
(run-server handler {:port 8080})
@shenfeng
shenfeng / gist:4700178
Last active January 23, 2024 19:23
High performance HTTP proxy with http-kit
(ns org.httpkit.proxy-test
(:use [org.httpkit.server :only [run-server async-response]]
[org.httpkit.client :only [request]]))
(defn- proxy-opts [req]
{:url (str "http://192.168.1.101:9090" (:uri req)
(if-let [q (:query-string req)]
(str "?" q)
""))
:timeout 30000 ;ms
@shenfeng
shenfeng / gist:4016355
Created November 5, 2012 09:45
serialize/deserialize vector<string>
#include <iostream>
#include <vector>
using namespace std;
char* serialize(vector<string> &v, unsigned int *count) {
unsigned int total_count = 0;
for(int i = 0; i < v.size(); i++ ) {