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
- func IsZKInvalidErr(err error) bool
- func IsZKRecoverableErr(err error) bool
- func ParentNode(path string) string
- func PathJoin(nodes ...string) string
- func StateAlive(state zk.State) bool
- type AlarmTrigger
- type ChildListener
- type Client
- func (cli *Client) AppendDeadWatcher(watcher *Watcher)
- func (cli *Client) Close()
- func (cli *Client) Conn() *zk.Conn
- func (cli *Client) ConnAlive() bool
- func (cli *Client) Connecting() bool
- func (cli *Client) CreateTempRawValue(path string, bytes []byte) error
- func (cli *Client) Delete(path string) error
- func (cli *Client) EnsurePath(path string) error
- func (cli *Client) Exists(path string) (bool, error)
- func (cli *Client) Get(path string, codec Codec) (interface{}, error)
- func (cli *Client) GetChildren(path string) ([]string, error)
- func (cli *Client) GetJSON(path string, typ reflect.Type) (interface{}, error)
- func (cli *Client) GetString(path string) (string, error)
- func (cli *Client) NewWatcher(handler EventHandler) (*Watcher, error)
- func (cli *Client) ParseJSON(path string, target interface{}) error
- func (cli *Client) Reconnect() error
- func (cli *Client) SetJSON(path string, obj interface{}) error
- func (cli *Client) SetMapJSONValue(path, key string, obj interface{}) error
- func (cli *Client) SetMapStringValue(path, key, s string) error
- func (cli *Client) SetMapValue(path, key string, obj interface{}, codec Codec) error
- func (cli *Client) SetRawValue(path string, bytes []byte) error
- func (cli *Client) SetString(path, s string) error
- func (cli *Client) SetTempJSON(path string, obj interface{}) error
- func (cli *Client) SetTempRawValue(path string, bytes []byte) error
- func (cli *Client) SetTempString(path, s string) error
- func (cli *Client) SetTempValue(path string, obj interface{}, codec Codec) error
- func (cli *Client) SetValue(path string, obj interface{}, codec Codec) error
- func (cli *Client) Sync(path string, obj interface{}, codec Codec) (*Watcher, error)
- func (cli *Client) SyncMap(path string, m interface{}, valueCodec Codec, syncChild bool) (*Watcher, error)
- func (cli *Client) SyncWatch(path string, obj interface{}, codec Codec, listener ValueListener) (*Watcher, error)
- func (cli *Client) SyncWatchJSON(path string, obj interface{}, listener ValueListener) (*Watcher, error)
- func (cli *Client) SyncWatchJSONMap(path string, m interface{}, syncChild bool, listener ChildListener) (*Watcher, error)
- func (cli *Client) SyncWatchMap(path string, m interface{}, valueCodec Codec, syncChild bool, ...) (*Watcher, error)
- func (cli *Client) SyncWatchString(path string, s *string, listener ValueListener) (*Watcher, error)
- func (cli *Client) SyncWatchStringMap(path string, m map[string]string, syncChild bool, listener ChildListener) (*Watcher, error)
- func (cli *Client) Watch(path string, obj interface{}, codec Codec, listener ValueListener) (*Watcher, error)
- func (cli *Client) WatchJSON(path string, obj interface{}, listener ValueListener) (*Watcher, error)
- func (cli *Client) WatchJSONMap(path string, m interface{}, syncChild bool, listener ChildListener) (*Watcher, error)
- func (cli *Client) WatchMap(path string, m interface{}, valueCodec Codec, syncChild bool, ...) (*Watcher, error)
- func (cli *Client) WatchString(path string, s *string, listener ValueListener) (*Watcher, error)
- func (cli *Client) WatchStringMap(path string, m map[string]string, syncChild bool, listener ChildListener) (*Watcher, error)
- type ClientOption
- type ClientOptions
- type Codec
- type EventHandler
- type JSONCodec
- type StringCodec
- type ValueListener
- type Watcher
Constants ¶
const (
PathSplit = "/"
)
Variables ¶
This section is empty.
Functions ¶
func IsZKInvalidErr ¶ added in v0.9.1
func IsZKRecoverableErr ¶ added in v0.9.1
func ParentNode ¶ added in v0.9.11
Types ¶
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 ¶
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) CreateTempRawValue ¶ added in v0.9.6
CreateTempRawValue create temp raw value in zookeeper
func (*Client) EnsurePath ¶
EnsurePath check or create target path
func (*Client) GetChildren ¶ added in v0.9.6
GetChildren get child nodes
func (*Client) NewWatcher ¶
func (cli *Client) NewWatcher(handler EventHandler) (*Watcher, error)
NewWatcher create new watcher
func (*Client) ParseJSON ¶ added in v0.9.1
ParseJSON parse json value from zookeeper into target object
func (*Client) SetMapJSONValue ¶ added in v0.9.1
SetMapJSONValue in zookeeper
func (*Client) SetMapStringValue ¶ added in v0.9.1
SetMapStringValue in zookeeper
func (*Client) SetMapValue ¶
SetMapValue set map value in zookeeper
func (*Client) SetRawValue ¶
SetRawValue set raw value in zookeeper
func (*Client) SetTempJSON ¶ added in v0.9.3
SetTempJSON in zookeeper
func (*Client) SetTempRawValue ¶ added in v0.9.3
SetTempRawValue set temp raw value in zookeeper
func (*Client) SetTempString ¶ added in v0.9.3
SetTempString in zookeeper
func (*Client) SetTempValue ¶ added in v0.9.3
SetTempValue set temp value in zookeeper
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
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 ¶
EventHandler zookeeper event listener
type JSONCodec ¶
type JSONCodec struct {
// contains filtered or unexported fields
}
JSONCodec struct
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