-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
155 lines (144 loc) · 3.85 KB
/
core.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
package one
import (
"context"
"flag"
"fmt"
"github.com/Barber0/one-rpc/registry"
"github.com/Barber0/one-rpc/transport"
"github.com/Barber0/one-rpc/util/logger"
"gopkg.in/yaml.v2"
"io/ioutil"
"net"
"strings"
"sync"
"time"
)
var (
ctx Context
ConfPath string
)
type (
Context *contextImp
)
type contextImp struct {
Logger logger.Logger
RpcSvr map[string]*transport.OneSvr
svrWg sync.WaitGroup
Conf OneGlobalConf
RegisterCenters map[string]registry.RegisterCenter
}
type OneGlobalConf struct {
LogPath string `yaml:"logpath"`
Server map[string]*transport.OneSvrConf `yaml:"server"`
Client transport.OneCltConf `yaml:"client"`
Registry *registry.RegistryConf `yaml:"registry"`
}
func init() {
flag.StringVar(&ConfPath,"config","config.yaml","config path")
ctx = &contextImp{
Logger: logger.GetOneLogger("global"),
RpcSvr: make(map[string]*transport.OneSvr),
RegisterCenters: make(map[string]registry.RegisterCenter),
}
Init()
}
func Init() {
var err error
cfgFile,err := ioutil.ReadFile(ConfPath)
if err != nil {
panic(fmt.Errorf("parse config err: %v, path: %v", err, ConfPath))
}
if err = yaml.Unmarshal(cfgFile, &ctx.Conf); err != nil {
panic(fmt.Errorf("parse config err: %v", err))
}
if etcdConf := ctx.Conf.Registry.Etcd; etcdConf != nil {
ctx.RegisterCenters[REGISTER_CENTER_ETCD], err = registry.NewEtcdRegistryCenter(context.Background(), etcdConf)
if err != nil {
panic(fmt.Errorf("init etcd registry center failed, err: %v", err))
}
}
for name, svr := range ctx.Conf.Server {
if svr.AcceptTimeout != 0 {
ctx.Conf.Server[name].AcceptTimeout = svr.AcceptTimeout * time.Millisecond
}else {
ctx.Conf.Server[name].AcceptTimeout = SvrAcceptTimeout
}
ctx.Conf.Server[name].ReadTimeout = svr.ReadTimeout * time.Millisecond
ctx.Conf.Server[name].WriteTimeout = svr.WriteTimeout * time.Millisecond
ctx.Conf.Server[name].HandleTimeout = svr.HandleTimeout * time.Millisecond
ctx.Conf.Server[name].IdleTimeout = svr.IdleTimeout * time.Millisecond
if svr.QueueCap == 0 {
ctx.Conf.Server[name].QueueCap = QueueCap
}
if svr.TCPReadBuf == 0{
ctx.Conf.Server[name].TCPReadBuf = TCPReadBuf
}
if svr.TCPWriteBuf == 0{
ctx.Conf.Server[name].TCPWriteBuf = TCPWriteBuf
}
}
if ctx.Conf.Client.Balance == "" {
ctx.Conf.Client.Balance = NORMAL_BALANCE
}
if ctx.Conf.Client.TransProtocol == "" {
ctx.Conf.Client.TransProtocol = PROTOCOL
}
if ctx.Conf.Client.DialTimeout != 0 {
ctx.Conf.Client.DialTimeout *= time.Millisecond
}else {
ctx.Conf.Client.DialTimeout = CltDialTimeout
}
if ctx.Conf.Client.ReadTimeout != 0 {
ctx.Conf.Client.ReadTimeout *= time.Millisecond
}else {
ctx.Conf.Client.ReadTimeout = CltReadTimeout
}
if ctx.Conf.Client.WriteTimeout != 0 {
ctx.Conf.Client.WriteTimeout *= time.Millisecond
}else {
ctx.Conf.Client.WriteTimeout = CltWriteTimeout
}
if ctx.Conf.Client.IdleTimeout != 0 {
ctx.Conf.Client.IdleTimeout *= time.Millisecond
}else {
ctx.Conf.Client.IdleTimeout = CltIdleTimeout
}
}
func GetContext() Context {
return ctx
}
func Run() {
for obj, svr := range ctx.RpcSvr {
ctx.svrWg.Add(1)
go func() {
defer ctx.svrWg.Done()
if err := svr.Serve(); err != nil {
ctx.Logger.Errorf("server %s err: %v",obj,err)
}
}()
}
ctx.svrWg.Wait()
}
func RegisterService(name string, serverConf *transport.OneSvrConf, metaInfo...string) error {
ip, err := registry.GetLocalIP()
if err != nil {
return err
}
addr, err := net.ResolveTCPAddr("tcp", serverConf.Address)
if err != nil {
return err
}
meta := ®istry.AppMeta{
IP: ip,
Port: addr.Port,
Weight: serverConf.ServiceWeight,
MetaData: strings.Join(metaInfo,"\r\n"),
}
for regName, center := range ctx.RegisterCenters {
if err := center.Register(name, meta); err != nil {
return err
}
ctx.Logger.Debugf("Register %s in %s, meta: %v", name, regName, meta)
}
return nil
}