Skip to content

Instantly share code, notes, and snippets.

package main
//一致性哈希(Consistent Hashing)
//author: Xiong Chuan Liang
//date: 2015-2-20
import (
"fmt"
"hash/crc32"
"sort"
@yrong
yrong / golang-nuts.go
Created March 15, 2018 01:38 — forked from ryanfitz/golang-nuts.go
two ways to call a function every 2 seconds
package main
import (
"fmt"
"time"
)
// Suggestions from golang-nuts
// http://play.golang.org/p/Ctg3_AQisl
var str = "vfs.fs.size[C:,pfree]"
var matched = str.match(/^vfs.fs.size\[(.*?):,pfree\]$/)
//matched[1]="C"
@yrong
yrong / gist:bfc1503c248b0da80a7abd63ed93325b
Created March 15, 2018 06:09
consul service discovery
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/hashicorp/consul/api"
"log"
"math/rand"
"net/http"
session_id=$(curl -s -XPUT "${consul_address}/v1/session/create" -d "{\"Name\": \"${task_name}\"}" | jq -r '.ID')
acquire_lock() {
session_id=${1}
echo "Trying to acquire the lock..."
result=$(curl -s -XPUT "${consul_address}/v1/kv/locks/${task_name}/.lock?acquire=${session_id}")
[ "${result}" == "true" ] && echo "Lock acquired"
}
release_lock() {
session_id=${1}
echo "Releasing the lock..."
@yrong
yrong / crawler.go
Created May 30, 2018 11:11 — forked from avalanche123/crawler.go
A Tour of Go. Exercise: Web Crawler
package main
import (
"fmt"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
Fetch(url string) (body string, urls []string, err error)
@yrong
yrong / learn.lua
Created June 8, 2018 07:42 — forked from tylerneylon/learn.lua
Learn Lua quickly with this short yet comprehensive and friendly script. It's written as both an introduction and a quick reference. It's also a valid Lua script so you can verify that the code does what it says, and learn more by modifying and running this script in your Lua interpreter.
-- Two dashes start a one-line comment.
--[[
Adding two ['s and ]'s makes it a
multi-line comment.
--]]
----------------------------------------------------
-- 1. Variables and flow control.
----------------------------------------------------
@yrong
yrong / observer.go
Created July 6, 2018 04:16
observer.go
// Package main serves as an example application that makes use of the observer pattern.
package main
import (
"fmt"
"time"
)
type (
// Event defines an indication of a point-in-time occurrence.
@yrong
yrong / multiple_select.go
Created July 13, 2018 08:31
multiple select
func main(){
var n int = 10
var chs = make([]chan int, n)
var worker = func(n int, c chan int){
for i:=0;i<n;i++{
c<-i
}
close(c)
}
@yrong
yrong / postgresql.py
Created July 30, 2018 01:06 — forked from robertsosinski/postgresql.py
DataDog script for collecting PostgreSQL stats
# Create the datadog user with select only permissions:
# CREATE USER datadog WITH PASSWORD '<complex_password>';
#
# Grant select permissions on a table or view that you want to monitor:
# GRANT SELECT ON <schema>.<table> TO datadog;
#
# Grant permissions for a specific column on a table or view that you want to monitor:
# GRANT SELECT (id, name) ON <schema>.<table> TO datadog;
#
# Let non-superusers look at pg_stat_activity in a read-only fashon.