From 8e0a755df533bc526eab12d0ffb37366e48a48f0 Mon Sep 17 00:00:00 2001 From: Tianle Xu Date: Mon, 31 Aug 2020 13:11:31 +0800 Subject: [PATCH] chore(release): goreleaser archive --- .goreleaser.yml | 11 ++++++++++ drivers/redis/driver.go | 45 ++++++++++++++++++++++++++++++++++++----- unikv.yml | 3 +++ unikvd/main.go | 14 ++++++++++--- 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 unikv.yml diff --git a/.goreleaser.yml b/.goreleaser.yml index 83da7b0..802f9ce 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,3 +1,14 @@ builds: - main: ./unikvd/ id: "unikvd" + binary: unikvd + goos: + - linux + - darwin + - windows +archives: + - files: + - README.md + - docs/* + - LICENSE + - unikv.yml diff --git a/drivers/redis/driver.go b/drivers/redis/driver.go index 2c89441..22998f2 100644 --- a/drivers/redis/driver.go +++ b/drivers/redis/driver.go @@ -9,8 +9,10 @@ import ( // Driver is the memorydriver type Driver struct { - conn redis.Conn - prefix string + conn redis.Conn + prefix string + ctx *DriverContext + reconnected bool } // Get gets data @@ -21,14 +23,21 @@ func (d *Driver) Get(key string) (string, error) { if err == redis.ErrNil { return "", unikv.ErrNotFound } + if !d.reconnected { + d.connect() + d.reconnected = true + return d.Get(key) + } return "", err } + if d.reconnected { + d.reconnected = false + } if _, ok := data.([]byte); !ok { if data == nil { return "", unikv.ErrNotFound - } else { - return "", fmt.Errorf("Unknown result %v", data) } + return "", fmt.Errorf("Unknown result %v", data) } return string(data.([]byte)), nil } @@ -37,6 +46,14 @@ func (d *Driver) Get(key string) (string, error) { func (d *Driver) Put(key string, value string) error { key = unikv.ConcatPrefix(d.prefix, key) _, err := d.conn.Do("SET", key, value) + if !d.reconnected { + d.connect() + d.reconnected = true + return d.Put(key, value) + } + if d.reconnected { + d.reconnected = false + } return err } @@ -44,6 +61,14 @@ func (d *Driver) Put(key string, value string) error { func (d *Driver) Unset(key string) error { key = unikv.ConcatPrefix(d.prefix, key) _, err := d.conn.Do("SET", key, "EX", "1") + if !d.reconnected { + d.connect() + d.reconnected = true + return d.Unset(key) + } + if d.reconnected { + d.reconnected = false + } return err } @@ -52,13 +77,23 @@ func (d *Driver) Close() error { return d.conn.Close() } +func (d *Driver) connect() error { + if d.conn != nil { + d.conn.Close() + } + var err error + d.conn, err = redis.DialURL(d.ctx.Server, d.ctx.Options...) + return err +} + // NewDriver creates a driver func NewDriver(prefix string, ctx *DriverContext) (*Driver, error) { drv := &Driver{ prefix: prefix, + ctx: ctx, } var err error - drv.conn, err = redis.DialURL(ctx.Server, ctx.Options...) + err = drv.connect() if err != nil { return nil, err } diff --git a/unikv.yml b/unikv.yml new file mode 100644 index 0000000..f8e1c5b --- /dev/null +++ b/unikv.yml @@ -0,0 +1,3 @@ +namespaces: + default: + buckets: {} diff --git a/unikvd/main.go b/unikvd/main.go index 99bb035..917dcf1 100644 --- a/unikvd/main.go +++ b/unikvd/main.go @@ -16,15 +16,21 @@ import ( ) var namespaces map[string]*unikv.Namespace +var enableLog = true func main() { - if len(os.Args) != 2 { - fmt.Println(`USAGE: unikvd [LISTEN_ADDR]`) + if len(os.Args) <= 2 { + fmt.Println(`USAGE: unikvd [LISTEN_ADDR] [--disable-log]`) os.Exit(0) } reinit() defer cleanup() http.HandleFunc("/v1/", handle) + if len(os.Args) == 3 { + if os.Args[2] == "--disable-log" { + enableLog = false + } + } fmt.Println("Listening on: ", os.Args[1]) err := http.ListenAndServe(os.Args[1], nil) if err != nil { @@ -91,7 +97,9 @@ var stats struct { func handle(rw http.ResponseWriter, r *http.Request) { stats.totalRequests++ - log.Printf("Request %s %s\r\n", r.Method, r.RequestURI) + if enableLog { + log.Printf("Request %s %s\r\n", r.Method, r.RequestURI) + } rw.Header().Add("Server", "UniKVd") uri := strings.Split(r.URL.RequestURI(), "?")[0] sp := strings.Split(strings.Trim(uri, "/"), "/")