/
client.go
175 lines (148 loc) · 4.09 KB
/
client.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
/**
* Tencent is pleased to support the open source community by making polaris-go available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 client
// A client implementation.
import (
"sync/atomic"
"time"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/log"
"github.com/polarismesh/polaris-go/pkg/model"
namingpb "github.com/polarismesh/polaris-go/pkg/model/pb/v1"
"github.com/polarismesh/polaris-go/pkg/plugin"
connector "github.com/polarismesh/polaris-go/plugin/serverconnector/common"
"github.com/polarismesh/polaris-go/plugin/serverconnector/sidecar/dns"
)
const (
dnsTimeout = 2 * time.Second
UDPSize uint16 = 65535
protocolSidecar = "sidecar"
)
// GetNewConnFunc returns a new connection function.
type GetNewConnFunc func() Conn
// Connector SideCar connector
type Connector struct {
*plugin.PluginBase
Timeout time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
Conn *AsyncConn // 用于异步发送
discoverConnector *connector.DiscoverConnector
// DnsID dns id
DnsID uint32
SideCarIp string
SideCarPort int
GetSyncConnFunc GetNewConnFunc
}
func getSyncConn() Conn {
conn := new(ConnBase)
return conn
}
// RspData 返回结果记录结构
type RspData struct {
RRArr []dns.RR
DetailErrInfo *dns.DetailErrInfoRR
OpCode int
RCode int
}
// Name 返回Name
func (c *Connector) Name() string {
return protocolSidecar
}
// Init 初始化
func (c *Connector) Init(ctx *plugin.InitContext) {
c.PluginBase = plugin.NewPluginBase(ctx)
cfgValue := ctx.Config.GetGlobal().GetServerConnector().GetPluginConfig(c.Name())
_ = cfgValue
c.DnsID = 0
c.discoverConnector = &connector.DiscoverConnector{}
c.discoverConnector.Init(ctx, nil)
c.discoverConnector.ServiceConnector = c.PluginBase
c.GetSyncConnFunc = getSyncConn
}
// IsEnable enable
func (c *Connector) IsEnable(cfg config.Configuration) bool {
return cfg.GetGlobal().GetSystem().GetMode() == model.ModeWithAgent
}
// 原子增加DnsID
func (c *Connector) getDnsMsgId() uint16 {
// need lock or use atomic
t := atomic.AddUint32(&c.DnsID, 1)
dnsId := t >> 16
return uint16(dnsId)
}
// Send 异步发送
func (c *Connector) Send(request *namingpb.DiscoverRequest) error {
dnsMsg, err := convertDiscoverRequestToDnsMsg(request, c.getDnsMsgId())
if err != nil {
log.GetBaseLogger().Errorf("%s", err)
return err
}
err = c.Conn.pushToWriteChanWrapper(dnsMsg)
return err
}
// Recv 异步接收
func (c *Connector) Recv() (*namingpb.DiscoverResponse, error) {
select {
case rspData := <-c.Conn.ReadChan:
if rspData.OpCode != dns.OpCodePolarisGetResource {
return nil, nil
}
rsp, err := convertRspDataToDiscoverResponse(rspData)
if err != nil {
return nil, err
}
return rsp, nil
default:
return nil, nil
}
}
// dial timeout
func (c *Connector) dialTimeout() time.Duration {
if c.Timeout != 0 {
return c.Timeout
}
if c.DialTimeout != 0 {
return c.DialTimeout
}
return dnsTimeout
}
// read timeout
func (c *Connector) readTimeout() time.Duration {
if c.ReadTimeout != 0 {
return c.ReadTimeout
}
return dnsTimeout
}
// write timeout
func (c *Connector) writeTimeout() time.Duration {
if c.WriteTimeout != 0 {
return c.WriteTimeout
}
return dnsTimeout
}
// getTimeoutForRequest
func (c *Connector) getTimeoutForRequest(timeout time.Duration) time.Duration {
var requestTimeout time.Duration
if c.Timeout != 0 {
requestTimeout = c.Timeout
} else {
requestTimeout = timeout
}
return requestTimeout
}