Skip to content

Commit

Permalink
chore(release): goreleaser archive
Browse files Browse the repository at this point in the history
  • Loading branch information
xtlsoft committed Aug 31, 2020
1 parent 57c08a3 commit 8e0a755
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
11 changes: 11 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
builds:
- main: ./unikvd/
id: "unikvd"
binary: unikvd
goos:
- linux
- darwin
- windows
archives:
- files:
- README.md
- docs/*
- LICENSE
- unikv.yml
45 changes: 40 additions & 5 deletions drivers/redis/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -37,13 +46,29 @@ 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
}

// Unset unsets data
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
}

Expand All @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions unikv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespaces:
default:
buckets: {}
14 changes: 11 additions & 3 deletions unikvd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "/"), "/")
Expand Down

0 comments on commit 8e0a755

Please sign in to comment.