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

View File

@ -4,6 +4,8 @@ package data
import ( import (
"errors" "errors"
"time" "time"
"github.com/micro/go-micro/options"
) )
var ( var (
@ -12,6 +14,8 @@ var (
// Data is a data storage interface // Data is a data storage interface
type Data interface { type Data interface {
// embed options
options.Options
// Dump the known records // Dump the known records
Dump() ([]*Record, error) Dump() ([]*Record, error)
// Read a record with key // Read a record with key
@ -28,5 +32,3 @@ type Record struct {
Value []byte Value []byte
Expiration time.Duration 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 ## Getting Started
- [Data](#data) - simple distributed data storage
- [Leader](#leader) - leadership election for group coordination - [Leader](#leader) - leadership election for group coordination
- [Lock](#lock) - distributed locking for exclusive resource access - [Lock](#lock) - distributed locking for exclusive resource access
- [Task](#task) - distributed job execution - [Task](#task) - distributed job execution
@ -70,30 +69,6 @@ for {
e.Resign() 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
Task provides distributed job execution. It's a simple way to distribute work across a coordinated pool of workers. 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" "encoding/json"
"fmt" "fmt"
"github.com/micro/go-micro/sync/data" "github.com/micro/go-micro/data"
ckv "github.com/micro/go-micro/sync/data/consul" ckv "github.com/micro/go-micro/data/consul"
lock "github.com/micro/go-micro/sync/lock/consul" lock "github.com/micro/go-micro/sync/lock/consul"
) )

View File

@ -1,7 +1,7 @@
package sync package sync
import ( 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/leader"
"github.com/micro/go-micro/sync/lock" "github.com/micro/go-micro/sync/lock"
"github.com/micro/go-micro/sync/time" "github.com/micro/go-micro/sync/time"

View File

@ -2,7 +2,7 @@
package sync package sync
import ( 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/leader"
"github.com/micro/go-micro/sync/lock" "github.com/micro/go-micro/sync/lock"
"github.com/micro/go-micro/sync/task" "github.com/micro/go-micro/sync/task"