瀏覽代碼

improve http proxy effeciency

v2ray 10 年之前
父節點
當前提交
e3a00d1400
共有 2 個文件被更改,包括 16 次插入14 次删除
  1. 2 2
      proxy/http/chan_reader.go
  2. 14 12
      proxy/http/http.go

+ 2 - 2
proxy/http/chan_reader.go

@@ -21,9 +21,9 @@ func NewChanReader(stream <-chan *alloc.Buffer) *ChanReader {
 }
 
 func (this *ChanReader) fill() {
-	b, ok := <-this.stream
+	b, open := <-this.stream
 	this.current = b
-	if !ok {
+	if !open {
 		this.eof = true
 		this.current = nil
 	}

+ 14 - 12
proxy/http/http.go

@@ -108,18 +108,19 @@ func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
 			log.Info("Request to remote: %s", string(buffer.Value))
 			packet := v2net.NewPacket(v2net.NewTCPDestination(address), buffer, true)
 			ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
-			defer close(ray.InboundInput())
-
-			responseReader := bufio.NewReader(NewChanReader(ray.InboundOutput()))
-			response, err := http.ReadResponse(responseReader, request)
-			if err != nil {
-				return
-			}
-
-			responseBuffer := alloc.NewBuffer().Clear()
-			response.Write(responseBuffer)
-			conn.Write(responseBuffer.Value)
-			responseBuffer.Release()
+			go func() {
+				defer close(ray.InboundInput())
+				responseReader := bufio.NewReader(NewChanReader(ray.InboundOutput()))
+				response, err := http.ReadResponse(responseReader, request)
+				if err != nil {
+					return
+				}
+
+				responseBuffer := alloc.NewBuffer().Clear()
+				defer responseBuffer.Release()
+				response.Write(responseBuffer)
+				conn.Write(responseBuffer.Value)
+			}()
 		} else {
 			response := &http.Response{
 				Status:        "400 Bad Request",
@@ -157,6 +158,7 @@ func (this *HttpProxyServer) handleConnect(request *http.Request, address v2net.
 	buffer := alloc.NewSmallBuffer().Clear()
 	response.Write(buffer)
 	writer.Write(buffer.Value)
+	buffer.Release()
 
 	packet := v2net.NewPacket(v2net.NewTCPDestination(address), nil, true)
 	ray := this.space.PacketDispatcher().DispatchToOutbound(packet)