fix: data race

This commit is contained in:
debugtalk
2022-01-18 20:30:24 +08:00
parent 7ef04674dc
commit c10bf8574d

View File

@@ -1,6 +1,7 @@
package hrp package hrp
import ( import (
"sync"
"time" "time"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
@@ -13,16 +14,18 @@ import (
func NewBoomer(spawnCount int, spawnRate float64) *HRPBoomer { func NewBoomer(spawnCount int, spawnRate float64) *HRPBoomer {
b := &HRPBoomer{ b := &HRPBoomer{
Boomer: boomer.NewStandaloneBoomer(spawnCount, spawnRate), Boomer: boomer.NewStandaloneBoomer(spawnCount, spawnRate),
debug: false, pluginsMutex: new(sync.RWMutex),
debug: false,
} }
return b return b
} }
type HRPBoomer struct { type HRPBoomer struct {
*boomer.Boomer *boomer.Boomer
plugins []common.Plugin // each task has its own plugin process plugins []common.Plugin // each task has its own plugin process
debug bool pluginsMutex *sync.RWMutex // avoid data race
debug bool
} }
// SetDebug configures whether to log HTTP request and response content. // SetDebug configures whether to log HTTP request and response content.
@@ -60,7 +63,10 @@ func (b *HRPBoomer) Run(testcases ...ITestCase) {
} }
func (b *HRPBoomer) Quit() { func (b *HRPBoomer) Quit() {
for _, plugin := range b.plugins { b.pluginsMutex.Lock()
plugins := b.plugins
b.pluginsMutex.Unlock()
for _, plugin := range plugins {
plugin.Quit() plugin.Quit()
} }
b.Boomer.Quit() b.Boomer.Quit()
@@ -73,7 +79,9 @@ func (b *HRPBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
// each testcase has its own plugin process // each testcase has its own plugin process
plugin, _ := initPlugin(config.Path) plugin, _ := initPlugin(config.Path)
if plugin != nil { if plugin != nil {
b.pluginsMutex.Lock()
b.plugins = append(b.plugins, plugin) b.plugins = append(b.plugins, plugin)
b.pluginsMutex.Unlock()
} }
return &boomer.Task{ return &boomer.Task{