/
service.go
54 lines (45 loc) · 916 Bytes
/
service.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
50
51
52
53
54
package discovery
import (
"encoding/json"
"log"
"sync/atomic"
)
// Service to register
type Service struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Host string `json:"host"`
Port int `json:"port"`
lastIndex uint64
quit chan struct{}
}
// NewService returns a new Service
func NewService(name, host string, port int) *Service {
return &Service{
Name: name,
Host: host,
Port: port,
quit: make(chan struct{}, 1),
}
}
// Stop stops the heartbeat
func (srv *Service) Stop() {
var s struct{}
srv.quit <- s
}
//SetIndex sets lastIndex
func (srv *Service) SetIndex(index uint64) {
atomic.StoreUint64(&srv.lastIndex, index)
}
// GetIndex get lastIndex
func (srv *Service) GetIndex() uint64 {
return atomic.LoadUint64(&srv.lastIndex)
}
func (srv *Service) String() string {
b, err := json.Marshal(srv)
if err != nil {
log.Println(err)
return ""
}
return string(b)
}