Move data to top level

This commit is contained in:
Asim Aslam 2019-06-11 17:20:52 +01:00
parent 6d06ee8078
commit 8e4e710e15
8 changed files with 39 additions and 63 deletions

View File

@ -6,10 +6,12 @@ import (
"net"
"github.com/hashicorp/consul/api"
"github.com/micro/go-micro/sync/data"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/options"
)
type ckv struct {
options.Options
client *api.Client
}
@ -64,22 +66,22 @@ func (c *ckv) String() string {
return "consul"
}
func NewData(opts ...data.Option) data.Data {
var options data.Options
for _, o := range opts {
o(&options)
}
func NewData(opts ...options.Option) data.Data {
options := options.NewOptions(opts...)
config := api.DefaultConfig()
var nodes []string
if n, ok := options.Values().Get("data.nodes"); ok {
nodes = n.([]string)
}
// set host
// config.Host something
// check if there are any addrs
if len(options.Nodes) > 0 {
addr, port, err := net.SplitHostPort(options.Nodes[0])
if len(nodes) > 0 {
addr, port, err := net.SplitHostPort(nodes[0])
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
port = "8500"
config.Address = fmt.Sprintf("%s:%s", options.Nodes[0], port)
config.Address = fmt.Sprintf("%s:%s", nodes[0], port)
} else if err == nil {
config.Address = fmt.Sprintf("%s:%s", addr, port)
}
@ -88,6 +90,7 @@ func NewData(opts ...data.Option) data.Data {
client, _ := api.NewClient(config)
return &ckv{
client: client,
Options: options,
client: client,
}
}

View File

@ -4,6 +4,8 @@ package data
import (
"errors"
"time"
"github.com/micro/go-micro/options"
)
var (
@ -12,6 +14,8 @@ var (
// Data is a data storage interface
type Data interface {
// embed options
options.Options
// Dump the known records
Dump() ([]*Record, error)
// Read a record with key
@ -28,5 +32,3 @@ type Record struct {
Value []byte
Expiration time.Duration
}
type Option func(o *Options)

15
data/options.go Normal file
View File

@ -0,0 +1,15 @@
package data
import (
"github.com/micro/go-micro/options"
)
// Set the nodes used to back the data
func Nodes(a ...string) options.Option {
return options.WithValue("data.nodes", a)
}
// Prefix sets a prefix to any key ids used
func Prefix(p string) options.Option {
return options.WithValue("data.prefix", p)
}

View File

@ -10,7 +10,6 @@ an external database or eventing system. Go Sync provides a framework for synchr
## Getting Started
- [Data](#data) - simple distributed data storage
- [Leader](#leader) - leadership election for group coordination
- [Lock](#lock) - distributed locking for exclusive resource access
- [Task](#task) - distributed job execution
@ -70,30 +69,6 @@ for {
e.Resign()
```
## Data
Data provides a simple interface for distributed data storage.
```go
import (
"github.com/micro/go-micro/sync/data"
"github.com/micro/go-micro/sync/data/consul"
)
keyval := consul.NewData()
err := keyval.Write(&data.Record{
Key: "foo",
Value: []byte(`bar`),
})
// handle err
v, err := keyval.Read("foo")
// handle err
err = keyval.Delete("foo")
```
## Task
Task provides distributed job execution. It's a simple way to distribute work across a coordinated pool of workers.

View File

@ -1,19 +0,0 @@
package data
type Options struct {
Nodes []string
Prefix string
}
func Nodes(a ...string) Option {
return func(o *Options) {
o.Nodes = a
}
}
// Prefix sets a prefix to any lock ids used
func Prefix(p string) Option {
return func(o *Options) {
o.Prefix = p
}
}

View File

@ -6,8 +6,8 @@ import (
"encoding/json"
"fmt"
"github.com/micro/go-micro/sync/data"
ckv "github.com/micro/go-micro/sync/data/consul"
"github.com/micro/go-micro/data"
ckv "github.com/micro/go-micro/data/consul"
lock "github.com/micro/go-micro/sync/lock/consul"
)

View File

@ -1,7 +1,7 @@
package sync
import (
"github.com/micro/go-micro/sync/data"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/sync/leader"
"github.com/micro/go-micro/sync/lock"
"github.com/micro/go-micro/sync/time"

View File

@ -2,7 +2,7 @@
package sync
import (
"github.com/micro/go-micro/sync/data"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/sync/leader"
"github.com/micro/go-micro/sync/lock"
"github.com/micro/go-micro/sync/task"