本文整理匯總了Golang中github.com/docker/libkv.NewStore函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewStore函數的具體用法?Golang NewStore怎麽用?Golang NewStore使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewStore函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestMultiplePersistConnection
// TestMultiplePersistConnection tests the second connection to a
// BoltDB fails when one is already open with PersistConnection flag
func TestMultiplePersistConnection(t *testing.T) {
kv, err := libkv.NewStore(
store.BOLTDB,
[]string{"/tmp/not_exist_dir/__boltdbtest"},
&store.Config{
Bucket: "boltDBTest",
ConnectionTimeout: 1 * time.Second,
PersistConnection: true},
)
assert.NoError(t, err)
assert.NotNil(t, kv)
if _, ok := kv.(*BoltDB); !ok {
t.Fatal("Error registering and initializing boltDB")
}
// Must fail if multiple boltdb requests are made with a valid timeout
kv, err = libkv.NewStore(
store.BOLTDB,
[]string{"/tmp/not_exist_dir/__boltdbtest"},
&store.Config{
Bucket: "boltDBTest",
ConnectionTimeout: 1 * time.Second,
PersistConnection: true},
)
assert.Error(t, err)
_ = os.Remove("/tmp/not_exist_dir/__boltdbtest")
}
示例2: TestConcurrentConnection
// TestConcurrentConnection tests simultaenous get/put using
// two handles.
func TestConcurrentConnection(t *testing.T) {
var err error
kv1, err1 := libkv.NewStore(
store.BOLTDB,
[]string{"/tmp/__boltdbtest"},
&store.Config{
Bucket: "boltDBTest",
ConnectionTimeout: 1 * time.Second},
)
assert.NoError(t, err1)
assert.NotNil(t, kv1)
kv2, err2 := libkv.NewStore(
store.BOLTDB,
[]string{"/tmp/__boltdbtest"},
&store.Config{Bucket: "boltDBTest",
ConnectionTimeout: 1 * time.Second},
)
assert.NoError(t, err2)
assert.NotNil(t, kv2)
key1 := "TestKV1"
value1 := []byte("TestVal1")
err = kv1.Put(key1, value1, nil)
assert.NoError(t, err)
key2 := "TestKV2"
value2 := []byte("TestVal2")
err = kv2.Put(key2, value2, nil)
assert.NoError(t, err)
pair1, err1 := kv1.Get(key1)
assert.NoError(t, err)
if assert.NotNil(t, pair1) {
assert.NotNil(t, pair1.Value)
}
assert.Equal(t, pair1.Value, value1)
pair2, err2 := kv2.Get(key2)
assert.NoError(t, err)
if assert.NotNil(t, pair2) {
assert.NotNil(t, pair2.Value)
}
assert.Equal(t, pair2.Value, value2)
// AtomicPut using kv1 and kv2 should succeed
_, _, err = kv1.AtomicPut(key1, []byte("TestnewVal1"), pair1, nil)
assert.NoError(t, err)
_, _, err = kv2.AtomicPut(key2, []byte("TestnewVal2"), pair2, nil)
assert.NoError(t, err)
testutils.RunTestCommon(t, kv1)
testutils.RunTestCommon(t, kv2)
kv1.Close()
kv2.Close()
_ = os.Remove("/tmp/__boltdbtest")
}
示例3: NewStore
func NewStore(machines []string, cmd *ServerCommand, keyspace string) *Storage {
etcd.Register()
clt, err := libkv.NewStore(store.ETCD, cmd.Config.BackendMachines, &store.Config{})
if err != nil {
panic(err)
}
_, err = clt.List(keyspace)
if err != store.ErrKeyNotFound && err != nil {
log.WithError(err).Fatal("store: Store backend not reachable")
}
cfg := client.Config{
Endpoints: machines,
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
kapi := client.NewKeysAPI(c)
return &Storage{Client: clt, Kapi: kapi, command: cmd, keyspace: keyspace}
}
示例4: SetUpTest
func (s *EtcdSuite) SetUpTest(c *check.C) {
s.createComposeProject(c, "etcd")
s.composeProject.Start(c)
etcd.Register()
url := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress + ":2379"
kv, err := libkv.NewStore(
store.ETCD,
[]string{url},
&store.Config{
ConnectionTimeout: 10 * time.Second,
},
)
if err != nil {
c.Fatal("Cannot create store etcd")
}
s.kv = kv
// wait for etcd
err = utils.Try(60*time.Second, func() error {
_, err := kv.Exists("test")
if err != nil {
return fmt.Errorf("Etcd connection error to %s: %v", url, err)
}
return nil
})
c.Assert(err, checker.IsNil)
}
示例5: NewStore
func NewStore(backend string, machines []string, a *AgentCommand, keyspace string) *Store {
store, err := libkv.NewStore(store.Backend(backend), machines, nil)
if err != nil {
log.Fatal(err)
}
return &Store{Client: store, agent: a, keyspace: keyspace}
}
示例6: getKVStore
func getKVStore(addr string, options *kvstore.Config) (kvstore.Store, error) {
u, err := url.Parse(addr)
if err != nil {
return nil, err
}
kvType := strings.ToLower(u.Scheme)
kvHost := u.Host
var backend kvstore.Backend
switch kvType {
case "consul":
backend = kvstore.CONSUL
case "etcd":
backend = kvstore.ETCD
}
kv, err := libkv.NewStore(
backend,
[]string{kvHost},
options,
)
if err != nil {
return nil, err
}
return kv, nil
}
示例7: Initialize
// Initialize is exported
func (s *Discovery) Initialize(uris string, heartbeat time.Duration, ttl time.Duration) error {
var (
parts = strings.SplitN(uris, "/", 2)
addrs = strings.Split(parts[0], ",")
prefix = ""
err error
)
// A custom prefix to the path can be optionally used.
if len(parts) == 2 {
prefix = parts[1]
}
s.heartbeat = heartbeat
s.ttl = ttl
s.path = path.Join(prefix, discoveryPath)
// Creates a new store, will ignore options given
// if not supported by the chosen store
s.store, err = libkv.NewStore(
s.backend,
addrs,
&store.Config{
EphemeralTTL: s.ttl,
},
)
return err
}
示例8: setupConsul
func (s *ConsulSuite) setupConsul(c *check.C) {
s.createComposeProject(c, "consul")
s.composeProject.Start(c)
consul.Register()
kv, err := libkv.NewStore(
store.CONSUL,
[]string{s.composeProject.Container(c, "consul").NetworkSettings.IPAddress + ":8500"},
&store.Config{
ConnectionTimeout: 10 * time.Second,
},
)
if err != nil {
c.Fatal("Cannot create store consul")
}
s.kv = kv
// wait for consul
err = utils.Try(60*time.Second, func() error {
_, err := kv.Exists("test")
if err != nil {
return err
}
return nil
})
c.Assert(err, checker.IsNil)
}
示例9: NewBgpRouteManager
func NewBgpRouteManager(masterIface string, server net.IP, as string) *BgpRouteManager {
b := &BgpRouteManager{
ethIface: masterIface,
server: server,
neighborlist: map[string]string{},
asnum: as,
}
consul.Register()
client := "localhost:8500"
kv, err := libkv.NewStore(
store.CONSUL, // or "consul"
[]string{client},
&store.Config{
ConnectionTimeout: 10 * time.Second,
},
)
b.kv = kv
if err != nil {
log.Fatal("Cannot create store consul")
}
b.neighborkey = "bgpneighbor"
if exist, _ := kv.Exists(b.neighborkey); exist == false {
err := b.kv.Put(b.neighborkey, []byte("bgpneighbor"), &store.WriteOptions{IsDir: true})
if err != nil {
fmt.Errorf("Something went wrong when initializing key %v", b.neighborkey)
}
}
err = b.kv.Put(b.neighborkey+"/"+server.String(), []byte(as), nil)
if err != nil {
log.Errorf("Error trying to put value at key: %v", b.neighborkey)
}
return b
}
示例10: NewStore
func NewStore(backend Backend, addrsStr string) (kvstore.Store, error) {
var kvbackend kvstore.Backend
switch backend {
case CONSUL:
kvbackend = kvstore.CONSUL
case ETCD:
kvbackend = kvstore.ETCD
default:
return nil, fmt.Errorf("Unknown store backend: %q", backend)
}
if addrsStr == "" {
switch backend {
case CONSUL:
addrsStr = DefaultConsulEndpoints
case ETCD:
addrsStr = DefaultEtcdEndpoints
}
}
addrs := strings.Split(addrsStr, ",")
store, err := libkv.NewStore(kvbackend, addrs, &kvstore.Config{ConnectionTimeout: 10 * time.Second})
if err != nil {
return nil, err
}
return store, nil
}
示例11: newClient
// newClient used to connect to KV Store
func newClient(kv string, addrs string) (DataStore, error) {
store, err := libkv.NewStore(store.Backend(kv), []string{addrs}, &store.Config{})
if err != nil {
return nil, err
}
ds := &datastore{store: store}
return ds, nil
}
示例12: NewStore
func NewStore(storeUrl string) (store.Store, error) {
kv, addrs := parseStoreUrl(storeUrl)
config := &store.Config{}
st, err := libkv.NewStore(store.Backend(kv), addrs, config)
if err != nil {
return nil, err
}
return st, nil
}
示例13: provide
func (provider *Kv) provide(configurationChan chan<- types.ConfigMessage) error {
storeConfig := &store.Config{
ConnectionTimeout: 30 * time.Second,
Bucket: "traefik",
}
if provider.TLS != nil {
caPool := x509.NewCertPool()
if provider.TLS.CA != "" {
ca, err := ioutil.ReadFile(provider.TLS.CA)
if err != nil {
return fmt.Errorf("Failed to read CA. %s", err)
}
caPool.AppendCertsFromPEM(ca)
}
cert, err := tls.LoadX509KeyPair(provider.TLS.Cert, provider.TLS.Key)
if err != nil {
return fmt.Errorf("Failed to load keypair. %s", err)
}
storeConfig.TLS = &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caPool,
InsecureSkipVerify: provider.TLS.InsecureSkipVerify,
}
}
kv, err := libkv.NewStore(
provider.storeType,
strings.Split(provider.Endpoint, ","),
storeConfig,
)
if err != nil {
return err
}
if _, err := kv.List(""); err != nil {
return err
}
provider.kvclient = kv
if provider.Watch {
go provider.watchKv(configurationChan, provider.Prefix)
}
configuration := provider.loadConfig()
configurationChan <- types.ConfigMessage{
ProviderName: string(provider.storeType),
Configuration: configuration,
}
return nil
}
示例14: TestRegister
func TestRegister(t *testing.T) {
Register()
kv, err := libkv.NewStore(store.ETCD, []string{client}, nil)
assert.NoError(t, err)
assert.NotNil(t, kv)
if _, ok := kv.(*Etcd); !ok {
t.Fatal("Error registering and initializing etcd")
}
}
示例15: TestRegister
func TestRegister(t *testing.T) {
Register()
kv, err := libkv.NewStore(store.CONSUL, []string{client}, nil)
assert.NoError(t, err)
assert.NotNil(t, kv)
if _, ok := kv.(*Consul); !ok {
t.Fatal("Error registering and initializing consul")
}
}