fix: avoid data race for httpstat

This commit is contained in:
debugtalk
2022-10-16 22:36:49 +08:00
parent 28e65e50f7
commit a413197ae6
7 changed files with 18 additions and 3 deletions
+1 -1
View File
@@ -61,7 +61,7 @@ func convertRun(cmd *cobra.Command, args []string) error {
outputType = convert.OutputTypePyTest outputType = convert.OutputTypePyTest
packages := []string{ packages := []string{
fmt.Sprintf("httprunner==%s", version.VERSION), fmt.Sprintf("httprunner==%s", version.HttpRunnerMinimumVersion),
} }
_, err := myexec.EnsurePython3Venv(venv, packages...) _, err := myexec.EnsurePython3Venv(venv, packages...)
if err != nil { if err != nil {
+1 -1
View File
@@ -21,7 +21,7 @@ var pytestCmd = &cobra.Command{
DisableFlagParsing: true, // allow to pass any args to pytest DisableFlagParsing: true, // allow to pass any args to pytest
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
packages := []string{ packages := []string{
fmt.Sprintf("httprunner==%s", version.VERSION), fmt.Sprintf("httprunner==%s", version.HttpRunnerMinimumVersion),
} }
_, err := myexec.EnsurePython3Venv(venv, packages...) _, err := myexec.EnsurePython3Venv(venv, packages...)
if err != nil { if err != nil {
+1
View File
@@ -17,6 +17,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
) )
var ( var (
+3
View File
@@ -3,9 +3,12 @@
package myexec package myexec
import ( import (
"fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings"
"syscall" "syscall"
"github.com/pkg/errors" "github.com/pkg/errors"
+1 -1
View File
@@ -207,7 +207,7 @@ func createPythonPlugin(projectName, venv string) error {
packages := []string{ packages := []string{
fmt.Sprintf("funppy==%s", fungo.Version), fmt.Sprintf("funppy==%s", fungo.Version),
fmt.Sprintf("httprunner==%s", version.VERSION), fmt.Sprintf("httprunner==%s", version.HttpRunnerMinimumVersion),
} }
_, err = myexec.EnsurePython3Venv(venv, packages...) _, err = myexec.EnsurePython3Venv(venv, packages...)
if err != nil { if err != nil {
+3
View File
@@ -6,3 +6,6 @@ import (
//go:embed VERSION //go:embed VERSION
var VERSION string var VERSION string
// httprunner python version
const HttpRunnerMinimumVersion = "v4.2.0"
+8
View File
@@ -11,6 +11,7 @@ import (
"net/http/httptrace" "net/http/httptrace"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
@@ -100,6 +101,8 @@ type Stat struct {
// connected network info // connected network info
network, addr string network, addr string
mux *sync.RWMutex // avoid data race
} }
// Finish sets the time when reading response is done. // Finish sets the time when reading response is done.
@@ -176,6 +179,7 @@ func (s *Stat) Print() {
// WithHTTPStat is a wrapper of httptrace.WithClientTrace. // WithHTTPStat is a wrapper of httptrace.WithClientTrace.
// It records the time of each httptrace hooks. // It records the time of each httptrace hooks.
func WithHTTPStat(req *http.Request, s *Stat) context.Context { func WithHTTPStat(req *http.Request, s *Stat) context.Context {
s.mux = new(sync.RWMutex)
s.schema = req.URL.Scheme s.schema = req.URL.Scheme
return httptrace.WithClientTrace(req.Context(), &httptrace.ClientTrace{ return httptrace.WithClientTrace(req.Context(), &httptrace.ClientTrace{
DNSStart: func(i httptrace.DNSStartInfo) { DNSStart: func(i httptrace.DNSStartInfo) {
@@ -228,6 +232,8 @@ func WithHTTPStat(req *http.Request, s *Stat) context.Context {
}, },
WroteRequest: func(info httptrace.WroteRequestInfo) { WroteRequest: func(info httptrace.WroteRequestInfo) {
s.mux.Lock()
defer s.mux.Unlock()
now := time.Now() now := time.Now()
s.serverStart = now s.serverStart = now
@@ -259,6 +265,8 @@ func WithHTTPStat(req *http.Request, s *Stat) context.Context {
}, },
GotFirstResponseByte: func() { GotFirstResponseByte: func() {
s.mux.Lock()
defer s.mux.Unlock()
s.serverDone = time.Now() s.serverDone = time.Now()
s.ServerProcessing = s.serverDone.Sub(s.serverStart) s.ServerProcessing = s.serverDone.Sub(s.serverStart)
s.StartTransfer = s.serverDone.Sub(s.dnsStart) s.StartTransfer = s.serverDone.Sub(s.dnsStart)