zkclient

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

zkclient - A golang zookeeper client

GoDoc

zkclient is a encapsulation utility of zookeeper based on go-zookeeper, supports the following features:

  • auto reconnect/re-watch
  • set/get/delete value
  • support string/json codec, and you can implement your own, see codec.go
  • real-time synchronize data from zookeeper to memory, see demo

Documentation

Overview

`zkclient` is a encapsulation utility of zookeeper based on [go-zookeeper](github.com/samuel/go-zookeeper), supports the following features:

## Features: - auto reconnect - set/get/delete value - support string/json codec, and you can implement your own - real-time synchronize data from zookeeper to memory

## Modules

- `Codec`: value encode/decode - `Watcher`: loop watch control - `Handler`: include `valueHandler` and `mapHandler`, set/get/delete value, handle event, synchronize value, trigger listener - `Listener`: include `ValueListener` and `ChildListener`, listen value updated/deleted

## API

- `Sync*`: synchronize data into memory - `SyncWatch*`: synchronize data into memory, and listen value change - `Decode*`: set value into zookeeper - `Encode*`: get value from zookeeper

Index

Constants

View Source
const (
	PathSplit = "/"
)

Variables

This section is empty.

Functions

func IsZKInvalidErr added in v0.9.1

func IsZKInvalidErr(err error) bool

func IsZKRecoverableErr added in v0.9.1

func IsZKRecoverableErr(err error) bool

func ParentNode added in v0.9.11

func ParentNode(path string) string

func PathJoin added in v1.0.0

func PathJoin(nodes ...string) string

func StateAlive

func StateAlive(state zk.State) bool

StateAlive whether zk state alive

Types

type AlarmTrigger

type AlarmTrigger func(err error)

AlarmTrigger alarm function

type ChildListener added in v0.9.1

type ChildListener interface {
	Update(path, child string, stat *zk.Stat, obj interface{})
	Delete(path, child string)
}

ChildListener child watch listener

type Client

type Client struct {
	sync.Mutex
	ClientOptions
	// contains filtered or unexported fields
}

Client for zookeeper

func NewClient

func NewClient(servers []string, options ...ClientOption) *Client

NewClient zookeeper client

func (*Client) AppendDeadWatcher

func (cli *Client) AppendDeadWatcher(watcher *Watcher)

AppendDeadWatcher add dead watcher, wait to watch again

func (*Client) Close

func (cli *Client) Close()

Close client, NOT use Client which already calling Close()

func (*Client) Conn

func (cli *Client) Conn() *zk.Conn

Conn for zookeeper

func (*Client) ConnAlive

func (cli *Client) ConnAlive() bool

ConnAlive check

func (*Client) Connecting

func (cli *Client) Connecting() bool

Connecting check

func (*Client) CreateTempRawValue added in v0.9.6

func (cli *Client) CreateTempRawValue(path string, bytes []byte) error

CreateTempRawValue create temp raw value in zookeeper

func (*Client) Delete

func (cli *Client) Delete(path string) error

Delete path

func (*Client) EnsurePath

func (cli *Client) EnsurePath(path string) error

EnsurePath check or create target path

func (*Client) Exists added in v0.9.6

func (cli *Client) Exists(path string) (bool, error)

Exists check node exists

func (*Client) Get

func (cli *Client) Get(path string, codec Codec) (interface{}, error)

Encode value from zookeeper, the raw value will be decoded by codec

func (*Client) GetChildren added in v0.9.6

func (cli *Client) GetChildren(path string) ([]string, error)

GetChildren get child nodes

func (*Client) GetJSON added in v0.9.1

func (cli *Client) GetJSON(path string, typ reflect.Type) (interface{}, error)

GetJSON get json value from zookeeper

func (*Client) GetString added in v0.9.1

func (cli *Client) GetString(path string) (string, error)

GetString get string value from zookeeper

func (*Client) NewWatcher

func (cli *Client) NewWatcher(handler EventHandler) (*Watcher, error)

NewWatcher create new watcher

func (*Client) ParseJSON added in v0.9.1

func (cli *Client) ParseJSON(path string, target interface{}) error

ParseJSON parse json value from zookeeper into target object

func (*Client) Reconnect

func (cli *Client) Reconnect() error

Reconnect connection

func (*Client) SetJSON

func (cli *Client) SetJSON(path string, obj interface{}) error

SetJSON in zookeeper

func (*Client) SetMapJSONValue added in v0.9.1

func (cli *Client) SetMapJSONValue(path, key string, obj interface{}) error

SetMapJSONValue in zookeeper

func (*Client) SetMapStringValue added in v0.9.1

func (cli *Client) SetMapStringValue(path, key, s string) error

SetMapStringValue in zookeeper

func (*Client) SetMapValue

func (cli *Client) SetMapValue(path, key string, obj interface{}, codec Codec) error

SetMapValue set map value in zookeeper

func (*Client) SetRawValue

func (cli *Client) SetRawValue(path string, bytes []byte) error

SetRawValue set raw value in zookeeper

func (*Client) SetString

func (cli *Client) SetString(path, s string) error

SetString in zookeeper

func (*Client) SetTempJSON added in v0.9.3

func (cli *Client) SetTempJSON(path string, obj interface{}) error

SetTempJSON in zookeeper

func (*Client) SetTempRawValue added in v0.9.3

func (cli *Client) SetTempRawValue(path string, bytes []byte) error

SetTempRawValue set temp raw value in zookeeper

func (*Client) SetTempString added in v0.9.3

func (cli *Client) SetTempString(path, s string) error

SetTempString in zookeeper

func (*Client) SetTempValue added in v0.9.3

func (cli *Client) SetTempValue(path string, obj interface{}, codec Codec) error

SetTempValue set temp value in zookeeper

func (*Client) SetValue

func (cli *Client) SetValue(path string, obj interface{}, codec Codec) error

SetValue set value in zookeeper

func (*Client) Sync

func (cli *Client) Sync(path string, obj interface{}, codec Codec) (*Watcher, error)

Sync synchronize value of the path to obj

func (*Client) SyncMap

func (cli *Client) SyncMap(path string, m interface{}, valueCodec Codec, syncChild bool) (*Watcher, error)

SyncMap synchronize sub-path value into a map

func (*Client) SyncWatch added in v0.9.1

func (cli *Client) SyncWatch(path string, obj interface{}, codec Codec, listener ValueListener) (*Watcher, error)

SyncWatch synchronize value of the path to obj, and trigger listener when value change

func (*Client) SyncWatchJSON added in v0.9.1

func (cli *Client) SyncWatchJSON(path string, obj interface{}, listener ValueListener) (*Watcher, error)

SyncWatchJSON synchronize json value of the path to obj, and trigger listener when value change

func (*Client) SyncWatchJSONMap added in v0.9.1

func (cli *Client) SyncWatchJSONMap(path string, m interface{}, syncChild bool, listener ChildListener) (*Watcher, error)

SyncWatchJSONMap synchronize sub-path json value into a map, and trigger listener when child value change

func (*Client) SyncWatchMap added in v0.9.1

func (cli *Client) SyncWatchMap(path string, m interface{}, valueCodec Codec, syncChild bool, listener ChildListener) (*Watcher, error)

SyncWatchMap synchronize sub-path value into a map, and trigger listener when child value change

func (*Client) SyncWatchString added in v0.9.1

func (cli *Client) SyncWatchString(path string, s *string, listener ValueListener) (*Watcher, error)

SyncWatchJSON synchronize string value of the path to obj, and trigger listener when value change

func (*Client) SyncWatchStringMap added in v0.9.1

func (cli *Client) SyncWatchStringMap(path string, m map[string]string, syncChild bool, listener ChildListener) (*Watcher, error)

SyncWatchStringMap synchronize sub-path string value into a map, and trigger listener when child value change

func (*Client) Watch added in v0.9.5

func (cli *Client) Watch(path string, obj interface{}, codec Codec, listener ValueListener) (*Watcher, error)

Watch synchronize value of the path to obj, and trigger listener when value change

func (*Client) WatchJSON added in v0.9.5

func (cli *Client) WatchJSON(path string, obj interface{}, listener ValueListener) (*Watcher, error)

WatchJSON watch json value of the path to obj, and trigger listener when value change

func (*Client) WatchJSONMap added in v0.9.5

func (cli *Client) WatchJSONMap(path string, m interface{}, syncChild bool, listener ChildListener) (*Watcher, error)

WatchJSONMap watch sub-path json value into a map, and trigger listener when child value change

func (*Client) WatchMap added in v0.9.5

func (cli *Client) WatchMap(path string, m interface{}, valueCodec Codec, syncChild bool, listener ChildListener) (*Watcher, error)

SyncWatchMap synchronize sub-path value into a map, and trigger listener when child value change

func (*Client) WatchString added in v0.9.5

func (cli *Client) WatchString(path string, s *string, listener ValueListener) (*Watcher, error)

WatchJSON watch string value of the path to obj, and trigger listener when value change

func (*Client) WatchStringMap added in v0.9.5

func (cli *Client) WatchStringMap(path string, m map[string]string, syncChild bool, listener ChildListener) (*Watcher, error)

WatchStringMap watch sub-path string value into a map, and trigger listener when child value change

type ClientOption added in v0.9.6

type ClientOption func(*ClientOptions)

func WithAlarmTrigger added in v0.9.6

func WithAlarmTrigger(trigger AlarmTrigger) ClientOption

func WithListenAsync added in v0.9.6

func WithListenAsync(async bool) ClientOption

func WithTimeout added in v0.9.6

func WithTimeout(timeout time.Duration) ClientOption

type ClientOptions added in v0.9.6

type ClientOptions struct {
	// contains filtered or unexported fields
}

type Codec

type Codec interface {
	// Encode object to byte, which must be a pointer
	Encode(obj interface{}) ([]byte, error)

	// Decode byte data to object, which must be a pointer
	Decode(data []byte) (interface{}, error)
}

Codec for data

type EventHandler

type EventHandler interface {
	Path() string
	Handle(*Watcher, *zk.Event) (<-chan zk.Event, error)
}

EventHandler zookeeper event listener

type JSONCodec

type JSONCodec struct {
	// contains filtered or unexported fields
}

JSONCodec struct

func (*JSONCodec) Decode

func (c *JSONCodec) Decode(data []byte) (interface{}, error)

Decode zk json decode

func (*JSONCodec) Encode

func (c *JSONCodec) Encode(obj interface{}) ([]byte, error)

Encode zk json encode

type StringCodec

type StringCodec struct {
}

func (*StringCodec) Decode

func (c *StringCodec) Decode(data []byte) (interface{}, error)

func (*StringCodec) Encode

func (c *StringCodec) Encode(obj interface{}) ([]byte, error)

type ValueListener added in v0.9.1

type ValueListener interface {
	Update(path string, stat *zk.Stat, obj interface{})
	Delete(path string)
}

ValueListener node watch listener

type Watcher

type Watcher struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Watcher zookeeper watcher

func (*Watcher) Alive added in v0.9.9

func (w *Watcher) Alive() bool

Alive whether watcher is watching

func (*Watcher) Close added in v0.9.3

func (w *Watcher) Close()

Close close watch event

func (*Watcher) Done added in v0.9.9

func (w *Watcher) Done() <-chan struct{}

Done chan

func (*Watcher) Watch

func (w *Watcher) Watch()

Watch start watch event

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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