File: client2.go

package info (click to toggle)
golang-github-mesos-mesos-go 0.0.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,964 kB
  • ctags: 6,435
  • sloc: makefile: 18
file content (88 lines) | stat: -rw-r--r-- 1,636 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package zoo

import (
	"sync"
	"time"

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

const (
	defaultSessionTimeout = 60 * time.Second
	currentPath           = "."
)

var zkSessionTimeout = defaultSessionTimeout

type client2 struct {
	*zk.Conn
	path     string
	done     chan struct{} // signal chan, closes when the underlying connection terminates
	stopOnce sync.Once
}

func connect2(hosts []string, path string) (*client2, error) {
	c, ev, err := zk.Connect(hosts, zkSessionTimeout)
	if err != nil {
		return nil, err
	}
	done := make(chan struct{})
	go func() {
		// close the 'done' chan when the zk event chan closes (signals termination of zk connection)
		defer close(done)
		for {
			if _, ok := <-ev; !ok {
				return
			}
		}
	}()
	return &client2{
		Conn: c,
		path: path,
		done: done,
	}, nil
}

func (c *client2) stopped() <-chan struct{} {
	return c.done
}

func (c *client2) stop() {
	c.stopOnce.Do(c.Close)
}

func (c *client2) data(path string) (data []byte, err error) {
	data, _, err = c.Get(path)
	return
}

func (c *client2) watchChildren(path string) (string, <-chan []string, <-chan error) {
	errCh := make(chan error, 1)
	snap := make(chan []string)

	watchPath := c.path
	if path != "" && path != currentPath {
		watchPath = watchPath + path
	}
	go func() {
		defer close(errCh)
		for {
			children, _, ev, err := c.ChildrenW(watchPath)
			if err != nil {
				errCh <- err
				return
			}
			select {
			case snap <- children:
			case <-c.done:
				return
			}
			e := <-ev // wait for the next watch-related event
			if e.Err != nil {
				errCh <- e.Err
				return
			}
		}
	}()
	return watchPath, snap, errCh
}