顯示具有 Golang 標籤的文章。 顯示所有文章
顯示具有 Golang 標籤的文章。 顯示所有文章

2015年9月18日 星期五

SSDB Sharding Proxy




在工作上使用ssdb 作為產品的storage DB,但是因為目前沒有支援sharding 分片式處理
所以用Go開發了一個sharding proxy,並且避開使用index db處理,相對的處理時間會有可能比index db 模式來得長


設計思維:
   將每個ssdb instance 當作一個區塊(node)看待


實作功能:
  1. 完全遵守ssdb protocol 資料協定,所以在使用上不需修改client code
  2. 新增node 只需要將node資訊寫入設定檔,不需重新啟動proxy service
  3. 目前確認已測試完成的指令請參考 github readme



資料處理:
  1. 寫入資料:寫入資料會優先寫入到第一個node,這樣在熱查詢的時候會加快找到資料的速度
       
  1. 取得單一資料(get,hget):會優先讀取第一個node,如果找不到則遍尋所有的node,直到找到資料,如果都找不到則回應not_found
                                      
  1. 取得列表資料(keys,scan,hgetall...etc):
    會掃描全部的node後在合併全部資料,合併資料時,資料優先順序為先查詢到則後面有查詢到重複資料也不會覆蓋目前資料,確保資料的最新狀態不會被舊資料給取代
            
              

2015年9月10日 星期四

使用Go 做反向代理伺服器

使用Go作反向代理,可以使用go 自帶的reverse proxy
但是如果背後的http server是走https的話,你又是用自簽憑證那你就會遇到inseurce verify的問題

解法就是取代原本reverse proxy自帶的client transport設定

package main import (
"net/http"
"net/http/httputil"
"net/url"
"time"
"net"
"log"
"fmt"
"crypto/tls"
)
func main() {
go ReverseHttpsProxy(445,"https://127.0.0.1:443/","my.crt","my.key")
ReverseHttpProxy(8081,"http://127.0.0.1:8080/")
}
func ReverseHttpsProxy(port int,dst string,crt string,key string) {
u, e := url.Parse(dst)
if e != nil {
log.Fatal("Bad destination.")
}
h := httputil.NewSingleHostReverseProxy(u)
//if your certificate signed by yourself,you need use this bypass secure verify
var InsecureTransport http.RoundTripper = &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: 10 * time.Second,
}
h.Transport = InsecureTransport
err := http.ListenAndServeTLS(fmt.Sprintf(":%d",port),crt, key ,h)
if err != nil {
log.Println("Error:",err)
}
}
func ReverseHttpProxy(port int,dst string) {
u, e := url.Parse(dst)
if e != nil {
log.Fatal("Bad http destination.")
}
h := httputil.NewSingleHostReverseProxy(u)
err := http.ListenAndServe(fmt.Sprintf(":%d",port),h)
if err != nil {
log.Println("Error:",err)
}
}


gist:https://gist.github.com/matishsiao/8270e18923d8f78f56c2

2015年9月1日 星期二

Go的多字串比對

使用Go的精簡算法而完成的多字串比對

================================================

package main

import "fmt"

func main() {
fmt.Println("Multi string comparison")
a := "hello"
        b := "gopher"
c := "world"
d := "hello"

        // Go 可以支援精簡算法

        ok := a == b || a == c

fmt.Printf("check a == b || a == c:%v var a:%s b:%s c:%s d:%s\n",ok,a,b,c,d)

ok = a == d

fmt.Printf("check a == d:%v var a:%s b:%s c:%s d:%s\n",ok,a,b,c,d)
}

範例程式:
https://play.golang.org/p/e7Jz5CJ5Xm

2015年8月24日 星期一

如何用Go組出Y-m-d H:i:s 格式時間字串

最近的再處理有關Go 時間格式的問題,覺得沒有一隻小function 可以幫忙處理時間字串很麻煩
所以寫了一隻小function來用
參數說明:
Y:2009 //Year
y:09   //Year
M:01   //Month
m:1    //Month
D:02   //Date
D:2    //Date
H:15   //Hour
I:04   //Minute
i:4    //Minute
s:03   //Second

code 位置:
https://gist.github.com/matishsiao/bf3bfa9f13c9eaff2750

2015年8月21日 星期五

產生Go API Document

因為公司的關係接觸了Go lang,現在目標每天分享一篇關於Go的資訊
第一篇就是
https://engineroom.teamwork.com/generate-api-from-annotations-in-go/?utm_source=golangweekly&utm_medium=email

這主要是可以產生API document,方便文件管理,不過我自己還沒時間測試

我也會分享在Facebook Group :Golang Gopher Taiwan