From eb140ee8fabaeb825f5c26858ac37029b6daa37f Mon Sep 17 00:00:00 2001 From: Sxci Date: Wed, 28 Oct 2020 17:58:30 +0800 Subject: [PATCH 1/3] qiniu api v7.6.0; add debug info gor http.DefaultClient --- cmd/root.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 45effb61..50c46d15 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,9 @@ package cmd import ( "fmt" + "net/http" + "net/http/httptrace" + "net/http/httputil" "os" "path/filepath" "runtime" @@ -103,8 +106,8 @@ func initConfig() { if DebugFlag { logs.SetLevel(logs.LevelDebug) client.TurnOnDebug() - // master 已合并, v7.5.0 分支没包含次参数,等待 v7.5.1 - // client.DeepDebugInfo = DeepDebugInfo + client.DeepDebugInfo = DeepDebugInfo + initHttpDefaultClient() } else { logs.SetLevel(logs.LevelInformational) } @@ -159,3 +162,45 @@ func initConfig() { } os.Rename(jsonConfigFile, cfgFile) } + +type MyTransport struct { + Transport http.RoundTripper +} + +func (t MyTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if DebugFlag { + trace := &httptrace.ClientTrace{ + GotConn: func(connInfo httptrace.GotConnInfo) { + remoteAddr := connInfo.Conn.RemoteAddr() + logs.Debug(fmt.Sprintf("Network: %s, Remote ip:%s, URL: %s", remoteAddr.Network(), remoteAddr.String(), req.URL)) + }, + } + req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) + bs, bErr := httputil.DumpRequest(req, DeepDebugInfo) + if bErr == nil { + logs.Debug(string(bs)) + } + } + + resp, err := t.Transport.RoundTrip(req) + + if DebugFlag { + bs, dErr := httputil.DumpResponse(resp, DeepDebugInfo) + if dErr == nil { + logs.Debug(string(bs)) + } + } + return resp, err +} + +func initHttpDefaultClient() { + t0 := http.DefaultClient.Transport + if t0 == nil { + t0 = http.DefaultTransport + } + if t0 != nil { + http.DefaultClient.Transport = MyTransport{ + Transport: t0, + } + } +} diff --git a/go.mod b/go.mod index 67a742bd..a012ff96 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.3.2 // indirect github.com/pelletier/go-toml v1.8.0 // indirect - github.com/qiniu/api.v7/v7 v7.5.0 + github.com/qiniu/api.v7/v7 v7.6.0 github.com/satori/go.uuid v1.2.0 // indirect github.com/spf13/afero v1.3.1 // indirect github.com/spf13/cast v1.3.1 // indirect diff --git a/go.sum b/go.sum index c7912b86..8de2bea7 100644 --- a/go.sum +++ b/go.sum @@ -237,8 +237,8 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= -github.com/qiniu/api.v7/v7 v7.5.0 h1:DY6NrIp6FZ1GP4Roc9hRnO2m+OLzASYNnvz5Mbgw1rk= -github.com/qiniu/api.v7/v7 v7.5.0/go.mod h1:VE5oC5rkE1xul0u1S2N0b2Uxq9/6hZzhyqjgK25XDcM= +github.com/qiniu/api.v7/v7 v7.6.0 h1:396UGG+AWLh80pIhpPNCgEzb04t4S8CGKxqvLkiQeZI= +github.com/qiniu/api.v7/v7 v7.6.0/go.mod h1:zg3DaqU8mVnoQSQmtC/Mr2wXTJIE7fvcup+7sMt58Q0= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= From 2b8129700507a8f76ecd55389fb7f65303878ec9 Mon Sep 17 00:00:00 2001 From: Sxci Date: Wed, 28 Oct 2020 22:37:33 +0800 Subject: [PATCH 2/3] update init seq --- cmd/root.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 50c46d15..931f19ba 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -194,13 +194,17 @@ func (t MyTransport) RoundTrip(req *http.Request) (*http.Response, error) { } func initHttpDefaultClient() { - t0 := http.DefaultClient.Transport - if t0 == nil { - t0 = http.DefaultTransport - } + t0 := http.DefaultTransport if t0 != nil { - http.DefaultClient.Transport = MyTransport{ + http.DefaultTransport = MyTransport{ Transport: t0, } } + + t1 := http.DefaultClient.Transport + if t1 != nil { + http.DefaultClient.Transport = MyTransport{ + Transport: t1, + } + } } From ad1211379074d2039eb5ca6768647028ce53d512 Mon Sep 17 00:00:00 2001 From: Sxci Date: Tue, 17 Nov 2020 10:44:42 +0800 Subject: [PATCH 3/3] do not dump resp when resp is nil --- cmd/root.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 931f19ba..878e56df 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,6 +9,7 @@ import ( "path/filepath" "runtime" "strings" + "time" "github.com/astaxie/beego/logs" homedir "github.com/mitchellh/go-homedir" @@ -170,24 +171,34 @@ type MyTransport struct { func (t MyTransport) RoundTrip(req *http.Request) (*http.Response, error) { if DebugFlag { trace := &httptrace.ClientTrace{ + // 如果 ConnectStart Network 都没有显示,很可能是 DNS 失败 + ConnectStart: func(network, addr string) { + logs.Debug(fmt.Sprintf("ConnectStart Network: %s, Remote ip:%s, URL: %s", network, addr, req.URL)) + }, GotConn: func(connInfo httptrace.GotConnInfo) { remoteAddr := connInfo.Conn.RemoteAddr() - logs.Debug(fmt.Sprintf("Network: %s, Remote ip:%s, URL: %s", remoteAddr.Network(), remoteAddr.String(), req.URL)) + logs.Debug(fmt.Sprintf("GotConn Network: %s, Remote ip:%s, URL: %s", remoteAddr.Network(), remoteAddr.String(), req.URL)) }, } req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) bs, bErr := httputil.DumpRequest(req, DeepDebugInfo) if bErr == nil { logs.Debug(string(bs)) + } else { + logs.Debug(bErr) } } resp, err := t.Transport.RoundTrip(req) if DebugFlag { - bs, dErr := httputil.DumpResponse(resp, DeepDebugInfo) - if dErr == nil { - logs.Debug(string(bs)) + if err != nil { + logs.Debug(err) + } else { + bs, dErr := httputil.DumpResponse(resp, DeepDebugInfo) + if dErr == nil { + logs.Debug(string(bs)) + } } } return resp, err @@ -207,4 +218,7 @@ func initHttpDefaultClient() { Transport: t1, } } + if http.DefaultClient.Timeout == 0 { + http.DefaultClient.Timeout = 180 * time.Second + } }