Skip to content

Instantly share code, notes, and snippets.

@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 / 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)
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 / 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"
var str = "vfs.fs.size[C:,pfree]"
var matched = str.match(/^vfs.fs.size\[(.*?):,pfree\]$/)
//matched[1]="C"
@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
package main
//一致性哈希(Consistent Hashing)
//author: Xiong Chuan Liang
//date: 2015-2-20
import (
"fmt"
"hash/crc32"
"sort"
@yrong
yrong / epoll.go
Created March 8, 2018 10:04 — forked from tevino/epoll.go
Golang example for using epoll
package main
import (
"fmt"
"net"
"os"
"syscall"
)
const (
var msgSend;
var counter = 0
setInterval(function(){
msgSend = new Buffer(counter.toString());
ipfs.pubsub.publish(topic, msgSend, (err) => {
if (err) {
throw err
}
// msg was broadcasted
})
@yrong
yrong / btree.hpp
Created February 24, 2018 07:41 — forked from zhenghaoz/btree.hpp
B Tree
#include <memory>
#include <vector>
#include <iostream>
template <unsigned N, typename Key, typename Value>
class BTree
{
template <typename T> using vector = std::vector<T>;
template <typename T> using shared_ptr = std::shared_ptr<T>;