Skip to content

Instantly share code, notes, and snippets.

View Petelin's full-sized avatar

lin Petelin

View GitHub Profile
@Petelin
Petelin / update_if_bigger.lua
Last active June 22, 2018 09:41
this lua script is used to in redis. it will set to the key if the value is bigger than the `updateat:{timestamp}`, if no udpateat founded update as normal
local a = redis.call('get',KEYS[1]);
if a and a:find("^updateat:") ~= nil then
-- this key already update by anther thread. so we test
if tonumber(string.sub(a, 10, string.len(a))) <= tonumber(KEYS[2]) then
return redis.call('set',KEYS[1], KEYS[3]);
end
return;
end
return redis.call('set',KEYS[1], KEYS[3]);
@Petelin
Petelin / gkill.sh
Created July 13, 2018 05:51
two greate tool to start proc, or kill proc by name
33 # search and kill
32 function gkill(){
31 filter=$1
30 exp=[${filter:0:1}]${filter:1}
29 echo '>ps aux | grep' $exp
28 echo $( ps aux | grep $exp --color=always | xargs -I % echo '\n' %)
27 echo "Kill?"
26 read Keypress
25 case "$Keypress" in
24 * ) kill $2 $args $(ps aux | grep $exp --color=always | awk '{print $2}');
@Petelin
Petelin / runInSubprocss.go
Created July 25, 2018 08:08
run real job in subprocess
package main
import (
"fmt"
"os"
"os/exec"
"time"
)
func main() {
@Petelin
Petelin / random_plan9.cpp
Created November 4, 2018 16:49
random plan9
#include <u.h>
#include <libc.h>
/*
* algorithm by
* D. P. Mitchell & J. A. Reeds
*/
#define LEN 607
#define TAP 273
@Petelin
Petelin / random_MT.cpp
Created November 15, 2018 02:34
random MT, with seed
#include <stdio.h>
#include <time.h>
#ifndef MERSENNE_TWISTER_H
#define MERSENNE_TWISTER_H
#define __STDC_LIMIT_MACROS
# define uint32_t unsigned int
#ifdef __cplusplus
extern "C" {
@Petelin
Petelin / alloc.go
Created December 5, 2018 08:01
go堆外分配分区
# from https://www.zhihu.com/people/tou-gou-bao-da
package main
import (
"fmt"
"math"
"math/rand"
"reflect"
"runtime"
"sort"
@Petelin
Petelin / patchVariable.go
Created January 23, 2019 10:19
go通过反射替换任意变量的函数
func TestH(t *testing.T) {
say := func() {
fmt.Println("say")
}
rec := patchFunction(&say, func() { fmt.Println("replace") })
say()
rec()
say()
}
@Petelin
Petelin / condition.go
Last active March 21, 2019 10:41
三个线程顺序打印ABC
package main
import (
"fmt"
"sync"
"time"
)
var l = sync.Mutex{}
var cond = sync.NewCond(&l)
package main
import (
"fmt"
"sync"
"time"
)
func main() {
s := []string{"A", "B", "C"}
// Copyright (c) 2012-2019 Grabtaxi Holdings PTE LTD (GRAB), All Rights Reserved. NOTICE: All information contained herein
// is, and remains the property of GRAB. The intellectual and technical concepts contained herein are confidential, proprietary
// and controlled by GRAB and may be covered by patents, patents in process, and are protected by trade secret or copyright law.
//
// You are strictly forbidden to copy, download, store (in any medium), transmit, disseminate, adapt or change this material
// in any way unless prior written permission is obtained from GRAB. Access to the source code contained herein is hereby
// forbidden to anyone except current GRAB employees or contractors with binding Confidentiality and Non-disclosure agreements
// explicitly covering such access.
//
// The copyright notice above does not evidence any actual or intended publication or disclosure of this source code,