etcdv3

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2019 License: Apache-2.0 Imports: 9 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JsonBindHandle = func(bind *EtcdBind) error {
	response := bind.Response
	logger.Debugf("JsonBindHandle: bind: %v ", bind)
	for _, e := range response.Events {
		logger.Debugf("e: %v ", e)

		eventType := e.Type
		switch eventType {
		case clientv3.EventTypeDelete:
			i := bind.KV.Result
			delete(i, string(e.Kv.Key))
			err := fmt.Errorf("Etcd is deleted: %s \n", bind.Key)
			logger.Errorf(err.Error())
			return err
		default:
			kv := e.Kv

			if kv == nil {
				logger.Errorf("kv is nil!!")
				return errors.New("kv is nil!!")
			}
			bindKV := bind.KV
			key := string(kv.Key)
			v := kv.Value
			target := bindKV.BindTargetFunc()
			json.Unmarshal(v, target)
			bind.KV.Result[key] = target
			logger.Debugf("Bind key: %s value: %s to target: %v \n", key, string(v), target)
		}
	}
	return nil
}

Support json value binder

View Source
var StringBindFunc = func(bind *EtcdBind) error {
	response := bind.Response
	for _, e := range response.Events {
		eventType := e.Type
		switch eventType {
		case clientv3.EventTypeDelete:
			err := fmt.Errorf("Etcd is deleted: %s \n", bind.Key)
			logger.Errorf(err.Error())
			return err
		default:
			kv := e.Kv
			key := string(kv.Key)
			ptr := bind.KV.BindTargetFunc()
			logger.Debugf("bind key: %s to target: %v \n", key, ptr)
			strPtr := ptr.(*string)
			*strPtr = string(kv.Value)
			logger.Debugf("binded key: %s target: %v \n", key, *strPtr)
			bind.KV.Result[key] = strPtr
		}
	}
	return nil
}

Support string value binder

Functions

This section is empty.

Types

type BindKV

type BindKV struct {
	Result         map[string]interface{}
	BindTargetFunc BindTargetFunc
}

Bind to key may be with opOpt.Range(or opOpt.Prefix), this should returns multiple instance.

type BindTargetFunc

type BindTargetFunc func() interface{}

type BinderFunc

type BinderFunc interface {
	NewTarget() interface{}
}

type EtcdBind

type EtcdBind struct {
	Mutex    *sync.Mutex
	KV       *BindKV
	Response EtcdResponse
	Key      string
	Process  []EtcdBindHandle
	Opts     []clientv3.OpOption
}

func (*EtcdBind) GetValue

func (bind *EtcdBind) GetValue(kvs []*mvccpb.KeyValue)

func (*EtcdBind) ParseFromKv

func (bind *EtcdBind) ParseFromKv(kv *mvccpb.KeyValue) error

func (*EtcdBind) ProcessAll

func (bind *EtcdBind) ProcessAll(resp *clientv3.WatchResponse) error

type EtcdBindHandle

type EtcdBindHandle func(*EtcdBind) error //bind , cur path ,cur value

type EtcdClient

type EtcdClient struct {
	BindMap    map[string]EtcdBind
	Config     clientv3.Config
	EtcdConfig EtcdConfig
	Client     clientv3.Client
}

func Init

func Init(etcdConfig *EtcdConfig) EtcdClient

func (*EtcdClient) Bind

func (client *EtcdClient) Bind(target interface{}, path string, opts []clientv3.OpOption, handle ...EtcdBindHandle)

func (*EtcdClient) BindMux

func (e *EtcdClient) BindMux(target interface{}, mu *sync.Mutex, path string, opts []clientv3.OpOption, process ...EtcdBindHandle)

func (*EtcdClient) BindMuxMultiTargetWithKey

func (e *EtcdClient) BindMuxMultiTargetWithKey(kv *BindKV, mu *sync.Mutex, path string, opts []clientv3.OpOption, process ...EtcdBindHandle)

func (*EtcdClient) BindWithMultiResult

func (client *EtcdClient) BindWithMultiResult(f BindTargetFunc, path string, opts []clientv3.OpOption, handle ...EtcdBindHandle) map[string]interface{}

func (*EtcdClient) Close

func (client *EtcdClient) Close()

func (*EtcdClient) GetKey

func (e *EtcdClient) GetKey(subkey string) string

func (*EtcdClient) Process

func (e *EtcdClient) Process(bind *EtcdBind) error

type EtcdConfig

type EtcdConfig struct {
	Cache          string   `yaml:"Cache"`
	Endpoints      []string `yaml:"Endpoints"`
	Root           string   `yaml:"Root"`
	TimeoutSeconds int64    `yaml:"TimeoutSeconds"`
}

type EtcdResponse

type EtcdResponse *clientv3.WatchResponse

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL