-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
80 lines (69 loc) · 1.53 KB
/
options.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
package xshare
import (
"fmt"
"github.com/hwcer/cosgo/utils"
"strings"
"time"
)
const (
SelectorTypeLocal = "local" //本地程序内访问
SelectorTypeProcess = "process" //进程内访问
SelectorTypeDiscovery = "discovery" //服务发现
)
type Rpcx struct {
Redis string //服务发现
Timeout int32
Network string
Address string //仅仅启动服务器时需要
BasePath string
}
// Options Etcd Redis 二选一
var Options = &Rpcx{
Timeout: 2,
Network: "tcp",
Address: ":8100",
}
var Service = map[string]string{}
var rpcServerAddress *utils.Address
func Address() *utils.Address {
if rpcServerAddress != nil {
return rpcServerAddress
}
rpcServerAddress = utils.NewAddress(Options.Address)
if rpcServerAddress.Retry == 0 {
rpcServerAddress.Retry = 100
}
if rpcServerAddress.Host == "" {
rpcServerAddress.Host, _ = LocalIpv4()
}
rpcServerAddress.Scheme = Options.Network
return rpcServerAddress
}
func Timeout() time.Duration {
return time.Second * time.Duration(Options.Timeout)
}
func AddressPrefix() string {
return Options.Network + "@"
}
func AddressFormat(address string) string {
prefix := AddressPrefix()
if strings.HasPrefix(address, prefix) {
return address
}
b := strings.Builder{}
b.WriteString(prefix)
b.WriteString(address)
return b.String()
}
func LocalIpv4() (ip string, err error) {
var ipv4 []string
if ipv4, err = utils.LocalIPv4s(); err != nil {
return
}
if len(ipv4) == 0 {
err = fmt.Errorf("无法获取服务器的内网IP")
} else {
ip = ipv4[0]
}
return
}