zkwatcher

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2015 License: Apache-2.0 Imports: 6 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ZKWatcher

ZKWatcher provides an easy way to recursively watch a zookeeper path and get meaningful events out of it.

Dependencies

The only dependency is the samuel's zookeeper driver: github.com/samuel/go-zookeeper/zk. If you have godep, you can use it directly, otherwise, you might need to download the driver: go get -d.

Usage

Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/agrarianlabs/zkwatcher"
	"github.com/samuel/go-zookeeper/zk"
)

func main() {
	conn, _, err := zk.Connect([]string{"zookeeper host addr"}, 10*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	watcher := zkwatcher.NewWatcher(conn)
	defer func() { _ = watcher.Close() }()

	// Make sure the path exists before starting the watcher.
	if err := watcher.Watch("/path/to/watch"); err != nil {
		log.Println(err)
		return
	}

	for event := range watcher.C {
		if event.Error != nil {
			log.Println(err)
			return
		}
		fmt.Printf("%s on %s\n", event.Type, event.Path)
	}
}

Tests

The tests are only integration tests and relies on a running zookeeper. The tests will create random roots prefixed with zkwatchertest_. Even in case of failure, those should be removed.

With Docker

If you have Docker, make sure you have the DOCKER_IP variable defined and run:

make test

or with custom test options:

make test OPTS='-short -v -cover'
Without Docker

If you don't have Docker, you can spin up a Zookeeper on your own and then run:

go test -v -tags integration -zk-host $YOUR_ZK_TARGET .

or if you have godep:

godep go test -v -cover -tags integration -zk-host $YOUR_ZK_TARGET .

Documentation

Overview

Package zkwatcher provides an easy way to recursively watch a zookeeper path with meaningful events.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/agrarianlabs/zkwatcher"
	"github.com/samuel/go-zookeeper/zk"
)

func main() {
	conn, _, err := zk.Connect([]string{"zookeeper host addr"}, 10*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	watcher := zkwatcher.NewWatcher(conn)
	defer func() { _ = watcher.Close() }()

	// Make sure the path exists before starting the watcher.
	if err := watcher.Watch("/path/to/watch"); err != nil {
		log.Println(err)
		return
	}

	for event := range watcher.C {
		if event.Error != nil {
			log.Println(err)
			return
		}
		fmt.Printf("%s on %s\n", event.Type, event.Path)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Path  string
	Type  EventType
	Error error
}

Event represent a change in the watched tree.

type EventType

type EventType int

EventType enum type.

const (
	Create EventType
	Delete
	Update
)

EventType enum values.

func (EventType) String

func (e EventType) String() string

type Stats

type Stats struct {
	Depth           int      `json:"depth"`
	Cap             int      `json:"cap"`
	Goroutines      int      `json:"goroutines"`
	Running         bool     `json:"running"`
	WatchedNodes    []string `json:"watched_nodes"`
	WatchedChildren []string `json:"watched_children"`
}

Stats contains all runtime data from the watcher.

type Watcher

type Watcher struct {
	C <-chan Event // Exposed read channel
	// contains filtered or unexported fields
}

Watcher provides a read-only chan `C` where all the watched events are sent to.

func NewWatcher

func NewWatcher(conn *zk.Conn) *Watcher

NewWatcher initializes a watcher. It is the caller responsibility to Close it.

func (*Watcher) Close

func (wa *Watcher) Close() error

Close terminates the watcher. Blocks until every goroutines are gone.

func (*Watcher) Stats

func (wa *Watcher) Stats() Stats

Stats .

func (*Watcher) Watch

func (wa *Watcher) Watch(zkPath string) error

Watch creates a recursive watcher on the given zookeeper path. NOTE: require an existing `zkPath`. NOTE: there is no limit on the depth of the watcher, so the amount of gorountines

can be big.

func (*Watcher) WatchLimit

func (wa *Watcher) WatchLimit(zkPath string, limit int) error

WatchLimit creates a recursive watcher on the given zookeeper path. - `limit` indicates the maximum depth to go to. -1 for no limit. NOTE: require an existing `zkPath`.

Directories

Path Synopsis
Godeps
_workspace/src/github.com/samuel/go-zookeeper/zk
Package zk is a native Go client library for the ZooKeeper orchestration service.
Package zk is a native Go client library for the ZooKeeper orchestration service.

Jump to

Keyboard shortcuts

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