@@ -10,6 +10,7 @@ import (
10
10
motan "github.com/weibocom/motan-go/core"
11
11
"github.com/weibocom/motan-go/log"
12
12
mpro "github.com/weibocom/motan-go/protocol"
13
+ "time"
13
14
)
14
15
15
16
type MotanServer struct {
@@ -81,9 +82,8 @@ func (m *MotanServer) run() {
81
82
}
82
83
83
84
func (m * MotanServer ) handleConn (conn net.Conn ) {
84
- defer motan .HandlePanic (func () {
85
- conn .Close ()
86
- })
85
+ defer conn .Close ()
86
+ defer motan .HandlePanic (nil )
87
87
buf := bufio .NewReader (conn )
88
88
for {
89
89
request , err := mpro .Decode (buf )
@@ -100,6 +100,12 @@ func (m *MotanServer) handleConn(conn net.Conn) {
100
100
func (m * MotanServer ) processReq (request * mpro.Message , conn net.Conn ) {
101
101
defer motan .HandlePanic (nil )
102
102
request .Header .SetProxy (m .proxy )
103
+ var ip string
104
+ if ta , ok := conn .RemoteAddr ().(* net.TCPAddr ); ok {
105
+ ip = ta .IP .String ()
106
+ } else {
107
+ ip = getRemoteIP (conn .RemoteAddr ().String ())
108
+ }
103
109
// TODO request , response reuse
104
110
var res * mpro.Message
105
111
if request .Header .IsHeartbeat () {
@@ -112,11 +118,7 @@ func (m *MotanServer) processReq(request *mpro.Message, conn net.Conn) {
112
118
vlog .Errorf ("motan server convert to motan request fail. rid :%d, service: %s, method:%s,err:%s\n " , request .Header .RequestID , request .Metadata .LoadOrEmpty (mpro .MPath ), request .Metadata .LoadOrEmpty (mpro .MMethod ), err .Error ())
113
119
res = mpro .BuildExceptionResponse (request .Header .RequestID , mpro .ExceptionToJSON (& motan.Exception {ErrCode : 500 , ErrMsg : "deserialize fail. err:" + err .Error () + " method:" + request .Metadata .LoadOrEmpty (mpro .MMethod ), ErrType : motan .ServiceException }))
114
120
} else {
115
- if ta , ok := conn .RemoteAddr ().(* net.TCPAddr ); ok {
116
- req .SetAttachment (motan .HostKey , ta .IP .String ())
117
- } else {
118
- req .SetAttachment (motan .HostKey , getRemoteIP (conn .RemoteAddr ().String ()))
119
- }
121
+ req .SetAttachment (motan .HostKey , ip )
120
122
req .GetRPCContext (true ).ExtFactory = m .extFactory
121
123
mres = m .handler .Call (req )
122
124
//TODO oneway
@@ -133,7 +135,12 @@ func (m *MotanServer) processReq(request *mpro.Message, conn net.Conn) {
133
135
}
134
136
}
135
137
resbuf := res .Encode ()
136
- conn .Write (resbuf .Bytes ())
138
+ conn .SetWriteDeadline (time .Now ().Add (motan .DefaultWriteTimeout ))
139
+ _ , err := conn .Write (resbuf .Bytes ())
140
+ if err != nil {
141
+ vlog .Errorf ("connection will close. ip: %s, cause:%s\n " , ip , err .Error ())
142
+ conn .Close ()
143
+ }
137
144
}
138
145
139
146
func getRemoteIP (address string ) string {
0 commit comments