/
service.go
181 lines (162 loc) · 4.35 KB
/
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Package discovery is the service discovery module implemented by ETCD.
//
// Copyright 2018 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package discovery
import (
"context"
"time"
tp "github.com/henrylee2cn/teleport"
heartbeat "github.com/henrylee2cn/tp-ext/plugin-heartbeat"
"github.com/xiaoenai/tp-micro/model/etcd"
)
const (
// minimum lease TTL is 5-second
minLeaseTTL = 5
)
// Service automatically registered api info to etcd
type Service struct {
hostport string
serviceKey string
allApis []string
excludeApis []string
serviceInfo *ServiceInfo
client *etcd.Client
leaseid etcd.LeaseID
}
var (
_ tp.PostRegPlugin = new(Service)
_ tp.PostListenPlugin = new(Service)
)
// ServicePlugin creates a teleport plugin which automatically registered api info to etcd.
// Note:
// excludeApis must not be registered to etcd.
// If etcdConfig.DialTimeout<0, it means unlimit;
// If etcdConfig.DialTimeout=0, use the default value(15s).
func ServicePlugin(hostport string, etcdConfig etcd.EasyConfig, excludeApis ...string) *Service {
s := ServicePluginFromEtcd(hostport, nil, excludeApis...)
var err error
s.client, err = etcd.EasyNew(etcdConfig)
if err != nil {
tp.Fatalf("%v: %v", err, s.Name())
return s
}
return s
}
// ServicePluginFromEtcd creates a teleport plugin which automatically registered api info to etcd.
// Note:
// excludeApis must not be registered to etcd.
func ServicePluginFromEtcd(hostport string, etcdClient *etcd.Client, excludeApis ...string) *Service {
s := &Service{
hostport: hostport,
serviceKey: createServiceKey(hostport),
client: etcdClient,
serviceInfo: new(ServiceInfo),
}
s.ExcludeApi(heartbeat.HeartbeatUri)
s.ExcludeApi(excludeApis...)
return s
}
// Etcd returns the etcd client.
func (s *Service) Etcd() *etcd.Client {
return s.client
}
// ExcludeApi appends apis that must not be registered to etcd.
func (s *Service) ExcludeApi(excludeApi ...string) {
s.excludeApis = append(s.excludeApis, excludeApi...)
}
// Name returns name.
func (s *Service) Name() string {
return "ETCD(" + s.serviceKey + ")"
}
// PostReg registers URI path.
func (s *Service) PostReg(handler *tp.Handler) error {
s.allApis = append(s.allApis, handler.Name())
return nil
}
// PostListen adds serviceInfo, and starts etcd keep alive.
func (s *Service) PostListen() error {
L:
for _, api := range s.allApis {
for _, a := range s.excludeApis {
if a == api {
continue L
}
}
s.serviceInfo.Append(api)
}
ch, err := s.keepAlive()
if err != nil {
return err
}
go func() {
name := s.Name()
for {
select {
case <-s.client.Ctx().Done():
tp.Warnf("%s: etcd server closed", name)
s.revoke()
tp.Warnf("%s: stop\n", name)
return
case ka, ok := <-ch:
if !ok {
tp.Debugf("%s: etcd keep alive channel closed, and restart it", name)
s.revoke()
ch = s.anywayKeepAlive()
} else {
tp.Tracef("%s: recv etcd ttl:%d", name, ka.TTL)
}
}
}
}()
return nil
}
func (s *Service) anywayKeepAlive() <-chan *etcd.LeaseKeepAliveResponse {
ch, err := s.keepAlive()
for err != nil {
time.Sleep(minLeaseTTL)
ch, err = s.keepAlive()
}
return ch
}
func (s *Service) keepAlive() (<-chan *etcd.LeaseKeepAliveResponse, error) {
resp, err := s.client.Grant(context.TODO(), minLeaseTTL)
if err != nil {
return nil, err
}
info := s.serviceInfo.String()
_, err = s.client.Put(
context.TODO(),
s.serviceKey,
info,
etcd.WithLease(resp.ID),
)
if err != nil {
return nil, err
}
s.leaseid = resp.ID
ch, err := s.client.KeepAlive(context.TODO(), resp.ID)
if err == nil {
tp.Infof("%s: PUT %q : %q", s.Name(), s.serviceKey, info)
}
return ch, err
}
func (s *Service) revoke() {
_, err := s.client.Revoke(context.TODO(), s.leaseid)
if err != nil {
tp.Errorf("%s: revoke service error: %s", s.Name(), err.Error())
return
}
}