fix http connector

This commit is contained in:
ginuerzh 2022-01-28 16:13:43 +08:00
parent 1b22a5db9f
commit 0e9a975a26
5 changed files with 20 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/url"
"os"
"strings"
"time"
@ -31,6 +32,13 @@ func (l *stringList) Set(value string) error {
func buildConfigFromCmd(services, nodes stringList) (*config.Config, error) {
cfg := &config.Config{}
if v := os.Getenv("GOST_PROFILING"); v != "" {
cfg.Profiling = &config.ProfilingConfig{
Addr: v,
Enabled: true,
}
}
var chain *config.ChainConfig
if len(nodes) > 0 {
chain = &config.ChainConfig{

View File

@ -89,7 +89,7 @@ func main() {
if addr == "" {
addr = ":6060"
}
log.Info("profiling serve on: ", addr)
log.Info("profiling serve on ", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}()
}

View File

@ -108,7 +108,10 @@ func (c *httpConnector) Connect(ctx context.Context, conn net.Conn, network, add
if err != nil {
return nil, err
}
defer resp.Body.Close()
// NOTE: the server may return `Transfer-Encoding: chunked` header,
// then the Content-Length of response will be unknown (-1),
// in this case, close body will be blocked, so we leave it untouched.
// defer resp.Body.Close()
if log.IsLevelEnabled(logger.DebugLevel) {
dump, _ := httputil.DumpResponse(resp, false)

View File

@ -148,8 +148,6 @@ func (h *dnsHandler) exchange(ctx context.Context, msg []byte, log logger.Logger
if log.IsLevelEnabled(logger.DebugLevel) {
log.Debug(mq.String())
} else {
log.Info(h.dumpMsgHeader(&mq))
}
var mr *dns.Msg
@ -198,7 +196,7 @@ func (h *dnsHandler) exchange(ctx context.Context, msg []byte, log logger.Logger
var reply []byte
for _, ex := range h.exchangers {
log.Infof("exchange message %d via %s: %s", mq.Id, ex.String(), mq.Question[0].String())
log.Debugf("exchange message %d via %s: %s", mq.Id, ex.String(), mq.Question[0].String())
reply, err = ex.Exchange(ctx, query)
if err == nil {
break
@ -215,12 +213,6 @@ func (h *dnsHandler) exchange(ctx context.Context, msg []byte, log logger.Logger
return nil, err
}
if log.IsLevelEnabled(logger.DebugLevel) {
log.Debug(mr.String())
} else {
log.Info(h.dumpMsgHeader(mr))
}
return reply, nil
}

View File

@ -1,7 +1,6 @@
package http2
import (
"context"
"errors"
"net"
"net/http"
@ -70,23 +69,21 @@ type ServerConn struct {
w http.ResponseWriter
localAddr net.Addr
remoteAddr net.Addr
cancel context.CancelFunc
closed chan struct{}
}
func NewServerConn(w http.ResponseWriter, r *http.Request, localAddr, remoteAddr net.Addr) *ServerConn {
ctx, cancel := context.WithCancel(r.Context())
return &ServerConn{
r: r.Clone(ctx),
r: r,
w: w,
localAddr: localAddr,
remoteAddr: remoteAddr,
cancel: cancel,
closed: make(chan struct{}),
}
}
func (c *ServerConn) Done() <-chan struct{} {
return c.r.Context().Done()
return c.closed
}
func (c *ServerConn) Request() *http.Request {
@ -106,11 +103,10 @@ func (c *ServerConn) Write(b []byte) (n int, err error) {
}
func (c *ServerConn) Close() error {
c.cancel()
select {
case <-c.r.Context().Done():
case <-c.closed:
default:
close(c.closed)
}
return nil
}