forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservanthandle.go
executable file
·88 lines (79 loc) · 2.69 KB
/
servanthandle.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
package tars
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/TarsCloud/TarsGo/tars/transport"
)
//AddServant add dispatch and interface for object.
func AddServant(v dispatch, f interface{}, obj string) {
addServantCommon(v, f, obj, false)
}
//AddServantWithContext add dispatch and interface for object, which have ctx,context
func AddServantWithContext(v dispatch, f interface{}, obj string) {
addServantCommon(v, f, obj, true)
}
func addServantCommon(v dispatch, f interface{}, obj string, withContext bool) {
objRunList = append(objRunList, obj)
cfg, ok := tarsConfig[obj]
if !ok {
ReportNotifyInfo(NOTIFY_ERROR, "servant obj name not found:"+obj)
TLOG.Debug("servant obj name not found:", obj)
panic(ok)
}
if v, ok := f.(destroyableImp); ok {
TLOG.Debugf("add destroyable obj %s", obj)
destroyableObjs = append(destroyableObjs, v)
}
TLOG.Debug("add:", cfg)
jp := NewTarsProtocol(v, f, withContext)
s := transport.NewTarsServer(jp, cfg)
goSvrs[obj] = s
}
// AddHttpServant add http servant handler with default exceptionStatusChecker for obj.
func AddHttpServant(mux *TarsHttpMux, obj string) {
AddHttpServantWithExceptionStatusChecker(mux, obj, DefaultExceptionStatusChecker)
}
// AddHttpServantWithExceptionStatusChecker add http servant handler with exceptionStatusChecker for obj.
func AddHttpServantWithExceptionStatusChecker(mux *TarsHttpMux, obj string, exceptionStatusChecker func(int) bool) {
cfg, ok := tarsConfig[obj]
if !ok {
ReportNotifyInfo(NOTIFY_ERROR, "servant obj name not found:"+obj)
TLOG.Debug("servant obj name not found:", obj)
panic(ok)
}
TLOG.Debug("add http server:", cfg)
objRunList = append(objRunList, obj)
appConf := GetServerConfig()
addrInfo := strings.SplitN(cfg.Address, ":", 2)
var port int
if len(addrInfo) == 2 {
port, _ = strconv.Atoi(addrInfo[1])
}
httpConf := &TarsHttpConf{
Container: appConf.Container,
AppName: fmt.Sprintf("%s.%s", appConf.App, appConf.Server),
Version: appConf.Version,
IP: addrInfo[0],
Port: int32(port),
SetId: appConf.Setdivision,
ExceptionStatusChecker: exceptionStatusChecker,
}
mux.SetConfig(httpConf)
s := &http.Server{Addr: cfg.Address, Handler: mux}
httpSvrs[obj] = s
}
// AddServantWithProtocol adds a servant with protocol and obj
func AddServantWithProtocol(proto transport.ServerProtocol, obj string) {
objRunList = append(objRunList, obj)
cfg, ok := tarsConfig[obj]
if !ok {
ReportNotifyInfo(NOTIFY_ERROR, "servant obj name not found:"+obj)
TLOG.Debug("servant obj name not found ", obj)
panic(ok)
}
TLOG.Debug("add:", cfg)
s := transport.NewTarsServer(proto, cfg)
goSvrs[obj] = s
}