Skip to content

Commit

Permalink
perf: DEBUG添加请求耗时记录
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Mar 20, 2024
1 parent eb5bbd1 commit cebdf0b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/jms-sdk-go/httplib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ type AuthSign interface {
Sign(req *http.Request) error
}

var debugDev = false

func init() {
debugEnv := os.Getenv("DEBUG_DEV")
if strings.EqualFold(debugEnv, "true") || strings.EqualFold(debugEnv, "1") {
debugDev = true
}
}

const miniTimeout = time.Second * 30

func NewClient(baseUrl string, timeout time.Duration) (*Client, error) {
Expand Down Expand Up @@ -156,11 +165,18 @@ func (c *Client) Do(method, reqUrl string, data, res interface{}, params ...map[
if err != nil {
return
}
start := time.Now()
resp, err = c.http.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if debugDev {
now := time.Now()
date := now.Format("2006-01-02 15:04:05")
fmt.Printf("%s [DEBUG_DEV] Request %s %s cost: %v\n", date, req.Method, req.URL, now.Sub(start))
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return resp, err
Expand Down

0 comments on commit cebdf0b

Please sign in to comment.