Skip to content

Commit

Permalink
解决客户端内存泄漏问题
Browse files Browse the repository at this point in the history
  • Loading branch information
刘河 committed Nov 5, 2018
1 parent 2c9906e commit d432e63
Showing 1 changed file with 61 additions and 61 deletions.
122 changes: 61 additions & 61 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,67 +76,7 @@ func (c *TRPClient) process(conn net.Conn) error {
case "vkey":
log.Fatal("vkey不正确,请检查配置文件")
case "sign":
_, err := conn.Read(val)
nlen := binary.LittleEndian.Uint32(val)
log.Println("收到服务端数据,长度:", nlen)
if nlen <= 0 {
log.Println("数据长度错误。")
c.werror(conn)
continue
}
raw := make([]byte, nlen)
n, err := conn.Read(raw)
if err != nil {
return err
}
if n != int(nlen) {
log.Printf("读取服务端数据长度错误,已经读取%dbyte,总长度%d字节\n", n, nlen)
c.werror(conn)
continue
}
req, err := DecodeRequest(raw)
if err != nil {
log.Println("DecodeRequest错误:", err)
c.werror(conn)
continue
}
rawQuery := ""
if req.URL.RawQuery != "" {
rawQuery = "?" + req.URL.RawQuery
}
log.Println(req.URL.Path + rawQuery)
client := new(http.Client)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return disabledRedirect
}
resp, err := client.Do(req)
disRedirect := err != nil && strings.Contains(err.Error(), disabledRedirect.Error())
if err != nil && !disRedirect {
log.Println("请求本地客户端错误:", err)
c.werror(conn)
continue
}
if !disRedirect {
defer resp.Body.Close()
} else {
resp.Body = nil
resp.ContentLength = 0
}
respBytes, err := EncodeResponse(resp)
if err != nil {
log.Println("EncodeResponse错误:", err)
c.werror(conn)
continue
}
n, err = conn.Write(respBytes)
if err != nil {
log.Println("发送数据错误,错误:", err)
}
if n != len(respBytes) {
log.Printf("发送数据长度错误,已经发送:%dbyte,总字节长:%dbyte\n", n, len(respBytes))
} else {
log.Printf("本次请求成功完成,共发送:%dbyte\n", n)
}
c.deal(conn)
case "msg0":
log.Println("服务端返回错误。")
default:
Expand All @@ -145,3 +85,63 @@ func (c *TRPClient) process(conn net.Conn) error {
}
return nil
}
func (c *TRPClient) deal(conn net.Conn) error {
val := make([]byte, 4)
_, err := conn.Read(val)
nlen := binary.LittleEndian.Uint32(val)
log.Println("收到服务端数据,长度:", nlen)
if nlen <= 0 {
log.Println("数据长度错误。")
c.werror(conn)
}
raw := make([]byte, nlen)
n, err := conn.Read(raw)
if err != nil {
return err
}
if n != int(nlen) {
log.Printf("读取服务端数据长度错误,已经读取%dbyte,总长度%d字节\n", n, nlen)
c.werror(conn)
}
req, err := DecodeRequest(raw)
if err != nil {
log.Println("DecodeRequest错误:", err)
c.werror(conn)
}
rawQuery := ""
if req.URL.RawQuery != "" {
rawQuery = "?" + req.URL.RawQuery
}
log.Println(req.URL.Path + rawQuery)
client := new(http.Client)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return disabledRedirect
}
resp, err := client.Do(req)
disRedirect := err != nil && strings.Contains(err.Error(), disabledRedirect.Error())
if err != nil && !disRedirect {
log.Println("请求本地客户端错误:", err)
c.werror(conn)
}
if !disRedirect {
defer resp.Body.Close()
} else {
resp.Body = nil
resp.ContentLength = 0
}
respBytes, err := EncodeResponse(resp)
if err != nil {
log.Println("EncodeResponse错误:", err)
c.werror(conn)
}
n, err = conn.Write(respBytes)
if err != nil {
log.Println("发送数据错误,错误:", err)
}
if n != len(respBytes) {
log.Printf("发送数据长度错误,已经发送:%dbyte,总字节长:%dbyte\n", n, len(respBytes))
} else {
log.Printf("本次请求成功完成,共发送:%dbyte\n", n)
}
return nil
}

0 comments on commit d432e63

Please sign in to comment.