OSDN Git Service

reorganize error code (#1133)
[bytom/bytom.git] / blockchain / pseudohsm / watch.go
1 // Copyright 2016 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
3 //
4 // The go-ethereum library is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // The go-ethereum library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16
17 // +build darwin,!ios freebsd linux,!arm64 netbsd solaris windows
18
19 package pseudohsm
20
21 //"fmt"
22 //"github.com/rjeczalik/notify"
23 //"time"
24
25 type watcher struct {
26         kc       *keyCache
27         starting bool
28         running  bool
29         //ev       chan notify.EventInfo
30         quit chan struct{}
31 }
32
33 func newWatcher(kc *keyCache) *watcher {
34         return &watcher{
35                 kc: kc,
36                 //ev:   make(chan notify.EventInfo, 10),
37                 quit: make(chan struct{}),
38         }
39 }
40
41 // starts the watcher loop in the background.
42 // Start a watcher in the background if that's not already in progress.
43 // The caller must hold w.kc.mu.
44 func (w *watcher) start() {
45         if w.starting || w.running {
46                 return
47         }
48         w.starting = true
49         go w.loop()
50 }
51
52 func (w *watcher) close() {
53         close(w.quit)
54 }
55
56 func (w *watcher) loop() {
57         defer func() {
58                 w.kc.mu.Lock()
59                 w.running = false
60                 w.starting = false
61                 w.kc.mu.Unlock()
62         }()
63
64         /*
65                 err := notify.Watch(w.kc.keydir, w.ev, notify.All)
66                 if err != nil {
67                         fmt.Printf("can't watch %s: %v", w.kc.keydir, err)
68                         return
69                 }
70                 defer notify.Stop(w.ev)
71                 fmt.Printf("now watching %s", w.kc.keydir)
72                 defer fmt.Printf("no longer watching %s", w.kc.keydir)
73
74                 w.kc.mu.Lock()
75                 w.running = true
76                 w.kc.mu.Unlock()
77
78                 // Wait for file system events and reload.
79                 // When an event occurs, the reload call is delayed a bit so that
80                 // multiple events arriving quickly only cause a single reload.
81                 var (
82                         debounce          = time.NewTimer(0)
83                         debounceDuration  = 500 * time.Millisecond
84                         inCycle, hadEvent bool
85                 )
86                 defer debounce.Stop()
87                 for {
88                         select {
89                         case <-w.quit:
90                                 return
91                         case <-w.ev:
92                                 if !inCycle {
93                                         debounce.Reset(debounceDuration)
94                                         inCycle = true
95                                 } else {
96                                         hadEvent = true
97                                 }
98                         case <-debounce.C:
99                                 w.kc.mu.Lock()
100                                 w.kc.reload()
101                                 w.kc.mu.Unlock()
102                                 if hadEvent {
103                                         debounce.Reset(debounceDuration)
104                                         inCycle, hadEvent = true, false
105                                 } else {
106                                         inCycle, hadEvent = false, false
107                                 }
108                         }
109                 }
110         */
111 }