-
Notifications
You must be signed in to change notification settings - Fork 798
/
host.go
152 lines (129 loc) · 3.89 KB
/
host.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 cluster
import (
"context"
"net"
"sync"
v2 "mosn.io/mosn/pkg/api/v2"
"mosn.io/mosn/pkg/log"
"mosn.io/mosn/pkg/network"
"mosn.io/mosn/pkg/types"
)
// simpleHost is an implement of types.Host and types.HostInfo
type simpleHost struct {
hostname string
addressString string
clusterInfo types.ClusterInfo
stats types.HostStats
metaData v2.Metadata
tlsDisable bool
weight uint32
healthFlags uint64
}
func NewSimpleHost(config v2.Host, clusterInfo types.ClusterInfo) types.Host {
// clusterInfo should not be nil
// pre resolve address
GetOrCreateAddr(config.Address)
return &simpleHost{
hostname: config.Hostname,
addressString: config.Address,
clusterInfo: clusterInfo,
stats: newHostStats(clusterInfo.Name(), config.Address),
metaData: config.MetaData,
tlsDisable: config.TLSDisable,
weight: config.Weight,
}
}
// types.HostInfo Implement
func (sh *simpleHost) Hostname() string {
return sh.hostname
}
func (sh *simpleHost) Metadata() v2.Metadata {
return sh.metaData
}
func (sh *simpleHost) ClusterInfo() types.ClusterInfo {
return sh.clusterInfo
}
func (sh *simpleHost) Address() net.Addr {
return GetOrCreateAddr(sh.addressString)
}
func (sh *simpleHost) AddressString() string {
return sh.addressString
}
func (sh *simpleHost) HostStats() types.HostStats {
return sh.stats
}
func (sh *simpleHost) Weight() uint32 {
return sh.weight
}
func (sh *simpleHost) Config() v2.Host {
return v2.Host{
HostConfig: v2.HostConfig{
Address: sh.addressString,
Hostname: sh.hostname,
TLSDisable: sh.tlsDisable,
Weight: sh.weight,
},
MetaData: sh.metaData,
}
}
func (sh *simpleHost) SupportTLS() bool {
return !sh.tlsDisable && sh.clusterInfo.TLSMng().Enabled()
}
// types.Host Implement
func (sh *simpleHost) CreateConnection(context context.Context) types.CreateConnectionData {
var tlsMng types.TLSContextManager
if !sh.tlsDisable {
tlsMng = sh.clusterInfo.TLSMng()
}
clientConn := network.NewClientConnection(nil, sh.clusterInfo.ConnectTimeout(), tlsMng, sh.Address(), nil)
clientConn.SetBufferLimit(sh.clusterInfo.ConnBufferLimitBytes())
return types.CreateConnectionData{
Connection: clientConn,
HostInfo: sh,
}
}
func (sh *simpleHost) ClearHealthFlag(flag types.HealthFlag) {
sh.healthFlags &= ^uint64(flag)
}
func (sh *simpleHost) ContainHealthFlag(flag types.HealthFlag) bool {
return sh.healthFlags&uint64(flag) > 0
}
func (sh *simpleHost) SetHealthFlag(flag types.HealthFlag) {
sh.healthFlags |= uint64(flag)
}
func (sh *simpleHost) HealthFlag() types.HealthFlag {
return types.HealthFlag(sh.healthFlags)
}
func (sh *simpleHost) Health() bool {
return sh.healthFlags == 0
}
// net.Addr reuse for same address, valid in simple type
var AddrStore *sync.Map = &sync.Map{}
func GetOrCreateAddr(addrstr string) net.Addr {
if addr, ok := AddrStore.Load(addrstr); ok {
return addr.(net.Addr)
}
addr, err := net.ResolveTCPAddr("tcp", addrstr)
if err != nil {
log.DefaultLogger.Errorf("[upstream] resolve addr %s failed: %v", addrstr, err)
return nil
}
AddrStore.Store(addrstr, addr)
return addr
}