Skip to content

Instantly share code, notes, and snippets.

View mingshun's full-sized avatar

mingshun

View GitHub Profile
@mingshun
mingshun / gist:9886473
Created March 31, 2014 06:33
Exercise 1 of Chapter 12, Programming Erlang 2nd Edition
start(AnAtom, Fun) when is_atom(AnAtom), is_function(Fun, 0) ->
Sender = self(),
Run = fun() ->
case catch register(AnAtom, self()) of
true ->
Sender ! {started, self()},
Fun();
_ ->
Sender ! {already_running, self()}
end
@mingshun
mingshun / gist:9910068
Created April 1, 2014 08:24
Exercise 5 of Chapter 7, Programming Erlang 2nd Eidition
reverse_bits_in_binary(<<X/binary>>) ->
reverse_bits(X, <<>>).
reverse_bits(<<B1:1, B2:1, B3:1, B4:1, B5:1, B6:1, B7:1, B8:1, R/binary>>, T) ->
reverse_bits(R, <<B8:1, B7:1, B6:1, B5:1, B4:1, B3:1, B2:1, B1:1, T/binary>>);
reverse_bits(<<>>, T) ->
T.
@mingshun
mingshun / gist:9910270
Created April 1, 2014 08:38
Exercise 1 of Chapter 7, Programming Erlang 2nd Edition
reverse_bytes_in_binary(<<X/binary>>) ->
reverse_bytes(X, <<>>).
reverse_bytes(<<Byte:8, R/binary>>, T) ->
reverse_bytes(R, <<Byte, T/binary>>);
reverse_bytes(<<>>, T) ->
T.
@mingshun
mingshun / gist:713fef40577654b70db7
Created June 21, 2014 07:25
upgrade nginx without downtime
kill -USR2 [old master process]
kill -WINCH [old master process]
# After all old workers quit, shut down the old master process if it is unused any more.
kill -TERM [old master process]
# Revert to the old nginx while the old master was not shutted down.
kill -HUP [old master process]
kill -QUIT [new master process]
kill -TERM [new master process]
@mingshun
mingshun / nginx.conf
Last active August 29, 2015 14:07
Secure ssl configure for Nginx
user nobody;
worker_processes 2;
#error_log /dev/null crit;
events {
worker_connections 1024;
}
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class LoopExecutorService {
private int nThreads;
private TaskFactory factory;
private LoopExecutor executor;
@mingshun
mingshun / source-map.ts
Last active March 2, 2023 23:51
SourceMap Parser in Typescript
class SourceMapConsumer {
private readonly base64Table: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private readonly mappingItems: MappingItem[];
constructor(raw: string) {
this.mappingItems = this.parseSourcemap(raw);
}
originalPositionFor(position: Position): MappingItem | null {
for (const item of this.mappingItems) {
@mingshun
mingshun / jailer.c
Created September 2, 2023 12:00 — forked from SBell6hf/jailer.c
A ptrace-based syscall jailer that runs on arm64, x86_64 and i386
// SPDX-License-Identifier: CC0-1.0+
#include <sys/syscall.h>
#include <sys/user.h>
#include <errno.h>
#include <sys/procfs.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <string.h>