/
channel.go
49 lines (43 loc) · 852 Bytes
/
channel.go
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
package zerorpc
import (
"time"
"github.com/golang/glog"
"github.com/satori/go.uuid"
)
type channel struct {
codec *serverCodec
identity string
ticker *time.Ticker
counter int // count pending req
closed chan bool
}
func (c *channel) run() {
head := &EventHeader{Id: uuid.NewV4().String(), Version: PROTOCAL_VERSION}
req := &ServerResponse{head, "_zpc_hb", make([]interface{}, 0)}
o, err := req.MarshalMsg(nil)
if err != nil {
glog.Error(err)
return
}
for {
select {
case <-c.ticker.C:
c.codec.mutex.Lock()
glog.Error(o)
_, err = c.codec.zsock.SendMessageDontwait(c.identity, o)
if err != nil {
c.ticker.Stop()
c.codec = nil
c.counter = 0
glog.Error(err)
}
c.codec.mutex.Unlock()
return
case <-c.closed:
c.ticker.Stop()
c.codec = nil
c.ticker = nil
return
}
}
}