summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-08-21 02:34:43 +0700
committerShulhan <ms@kilabit.info>2022-08-21 02:34:43 +0700
commitb8e4a6d776707bf857430ddc3666aae794a87edf (patch)
tree601ffeaac1b0a34d88f8be903ff81bd62e0fdf03
parent6e27a84dba52e68fe7b75df50190934ab96c1946 (diff)
downloadgorankusu-b8e4a6d776707bf857430ddc3666aae794a87edf.tar.xz
all: fix all linter warnings
Some linter, govet, warns about possible copied Mutex on HttpRequest. To fix this we implement method clone and Stringer on HttpRequest.
-rw-r--r--Makefile1
-rw-r--r--example/example.go19
-rw-r--r--http_target.go36
-rw-r--r--key_form_input.go6
-rw-r--r--run_request.go7
-rw-r--r--websocket_server.go11
6 files changed, 63 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index aad6cd3..c92682e 100644
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,7 @@
all:
go run ./internal/cmd/trunks build
CGO_ENABLED=1 go test -v -race ./...
+ -golangci-lint run ./...
dev:
go run ./internal/cmd/trunks
diff --git a/example/example.go b/example/example.go
index 25c1532..04c94fa 100644
--- a/example/example.go
+++ b/example/example.go
@@ -374,8 +374,11 @@ func (ex *Example) pathExampleErrorGet(epr *libhttp.EndpointRequest) ([]byte, er
return nil, liberrors.Internal(fmt.Errorf("server error"))
}
-func (ex *Example) pathExamplePost(epr *libhttp.EndpointRequest) ([]byte, error) {
- epr.HttpRequest.ParseMultipartForm(0)
+func (ex *Example) pathExamplePost(epr *libhttp.EndpointRequest) (resb []byte, err error) {
+ err = epr.HttpRequest.ParseMultipartForm(0)
+ if err != nil {
+ return nil, err
+ }
data := &requestResponse{
Method: epr.HttpRequest.Method,
@@ -469,18 +472,18 @@ func (ex *Example) preattackExampleGet(rr *trunks.RunRequest) {
func (ex *Example) attackExampleErrorGet(rr *trunks.RunRequest) vegeta.Targeter {
return func(tgt *vegeta.Target) error {
- rr.HttpTarget.AttackLocker.Lock()
+ rr.HttpTarget.Lock()
*tgt = ex.targetExampleErrorGet
- rr.HttpTarget.AttackLocker.Unlock()
+ rr.HttpTarget.Unlock()
return nil
}
}
func (ex *Example) attackExampleGet(rr *trunks.RunRequest) vegeta.Targeter {
return func(tgt *vegeta.Target) error {
- rr.HttpTarget.AttackLocker.Lock()
+ rr.HttpTarget.Lock()
*tgt = ex.targetExampleGet
- rr.HttpTarget.AttackLocker.Unlock()
+ rr.HttpTarget.Unlock()
return nil
}
}
@@ -545,9 +548,9 @@ func (ex *Example) preattackExamplePostForm(rr *trunks.RunRequest) {
func (ex *Example) attackExamplePostForm(rr *trunks.RunRequest) vegeta.Targeter {
return func(tgt *vegeta.Target) error {
- rr.HttpTarget.AttackLocker.Lock()
+ rr.HttpTarget.Lock()
*tgt = ex.targetExamplePostForm
- rr.HttpTarget.AttackLocker.Unlock()
+ rr.HttpTarget.Unlock()
return nil
}
}
diff --git a/http_target.go b/http_target.go
index b209c0d..94a5609 100644
--- a/http_target.go
+++ b/http_target.go
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"sort"
+ "strings"
"sync"
libhttp "github.com/shuLhan/share/lib/http"
@@ -53,7 +54,7 @@ type HttpTarget struct {
RequestType libhttp.RequestType
Method libhttp.RequestMethod
- AttackLocker sync.Mutex `json:"-"` // Use this inside the Attack to lock resource.
+ sync.Mutex `json:"-"` // Use this inside the Attack to lock resource.
// AllowAttack if its true the "Attack" button will be showed on user
// interface and client will be allowed to run load testing on this
@@ -65,6 +66,26 @@ type HttpTarget struct {
IsCustomizable bool
}
+// clone the source HttpTarget.
+// This method is provided to prevent the sync.Mutex being copied.
+func (ht *HttpTarget) clone(src *HttpTarget) {
+ ht.Params = src.Params
+ ht.ConvertParams = src.ConvertParams
+ ht.Headers = src.Headers
+ ht.Run = src.Run
+ ht.PreAttack = src.PreAttack
+ ht.Attack = src.Attack
+ ht.ID = src.ID
+ ht.Name = src.Name
+ ht.Hint = src.Hint
+ ht.Path = src.Path
+ ht.Results = src.Results
+ ht.RequestType = src.RequestType
+ ht.Method = src.Method
+ ht.AllowAttack = src.AllowAttack
+ ht.IsCustomizable = src.IsCustomizable
+}
+
func (ht *HttpTarget) init() (err error) {
if len(ht.Name) == 0 {
return fmt.Errorf("HttpTarget.Name is empty")
@@ -132,3 +153,16 @@ func (ht *HttpTarget) sortResults() {
return ht.Results[x].Name > ht.Results[y].Name
})
}
+
+func (ht *HttpTarget) String() string {
+ var sb strings.Builder
+
+ fmt.Fprintf(&sb, `ID:%s Name:%s Hint:%s Path:%s `+
+ `Params:%v ConvertParams:%v Headers:%v `+
+ `AllowAttack:%t IsCustomizable:%t`,
+ ht.ID, ht.Name, ht.Hint, ht.Path,
+ ht.Params, ht.ConvertParams, ht.Headers,
+ ht.AllowAttack, ht.IsCustomizable)
+
+ return sb.String()
+}
diff --git a/key_form_input.go b/key_form_input.go
index 2a8789a..4922517 100644
--- a/key_form_input.go
+++ b/key_form_input.go
@@ -17,7 +17,7 @@ type KeyFormInput map[string]FormInput
// ToHttpHeader convert the KeyFormInputs to the standard http.Header.
func (kfi KeyFormInput) ToHttpHeader() (headers http.Header) {
headers = http.Header{}
- if kfi == nil || len(kfi) == 0 {
+ if len(kfi) == 0 {
return headers
}
for k, fi := range kfi {
@@ -44,7 +44,7 @@ func (kfi KeyFormInput) ToJsonObject() (data map[string]interface{}) {
// bytes.
func (kfi KeyFormInput) ToMultipartFormData() (data map[string][]byte) {
data = make(map[string][]byte, len(kfi))
- if kfi == nil || len(kfi) == 0 {
+ if len(kfi) == 0 {
return data
}
for k, fi := range kfi {
@@ -56,7 +56,7 @@ func (kfi KeyFormInput) ToMultipartFormData() (data map[string][]byte) {
// ToUrlValues convert the KeyFormInput to the standard url.Values.
func (kfi KeyFormInput) ToUrlValues() (vals url.Values) {
vals = url.Values{}
- if kfi == nil || len(kfi) == 0 {
+ if len(kfi) == 0 {
return vals
}
for k, fi := range kfi {
diff --git a/run_request.go b/run_request.go
index ac875df..968bfdc 100644
--- a/run_request.go
+++ b/run_request.go
@@ -50,10 +50,11 @@ func generateRunRequest(
}
outrr = &RunRequest{
- Target: *origTarget,
- HttpTarget: *origHttpTarget,
+ Target: *origTarget,
}
+ outrr.HttpTarget.clone(origHttpTarget)
+
outrr.Target.Vars = req.Target.Vars
outrr.HttpTarget.Headers = req.HttpTarget.Headers
outrr.HttpTarget.Params = req.HttpTarget.Params
@@ -95,5 +96,5 @@ func generateWebSocketTarget(
}
func (rr *RunRequest) String() string {
- return fmt.Sprintf("Target:%v HttpTarget:%v\n", rr.Target, rr.HttpTarget)
+ return fmt.Sprintf("Target:%v HttpTarget:%s\n", rr.Target, rr.HttpTarget.String())
}
diff --git a/websocket_server.go b/websocket_server.go
index 0c1d965..5928c73 100644
--- a/websocket_server.go
+++ b/websocket_server.go
@@ -53,16 +53,23 @@ func (trunks *Trunks) initWebSocketServer() (err error) {
trunks.Wsd = websocket.NewServer(opts)
- trunks.Wsd.RegisterTextHandler(
+ err = trunks.Wsd.RegisterTextHandler(
"POST",
apiAttackHttp,
trunks.handleWsAttackHttp,
)
- trunks.Wsd.RegisterTextHandler(
+ if err != nil {
+ return err
+ }
+
+ err = trunks.Wsd.RegisterTextHandler(
"DELETE",
apiAttackHttp,
trunks.handleWsAttackHttpCancel,
)
+ if err != nil {
+ return err
+ }
return nil
}