修复闭包的线程安全bug

This commit is contained in:
M09Ic 2022-12-02 18:05:33 +08:00
parent 38e654913d
commit a780f002bf
2 changed files with 15 additions and 24 deletions

View File

@ -81,13 +81,13 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
return return
} }
var bl *pkg.Baseline
resp, reqerr := pool.client.Do(pctx, req) resp, reqerr := pool.client.Do(pctx, req)
if pool.ClientType == ihttp.FAST { if pool.ClientType == ihttp.FAST {
defer fasthttp.ReleaseResponse(resp.FastResponse) defer fasthttp.ReleaseResponse(resp.FastResponse)
defer fasthttp.ReleaseRequest(req.FastRequest) defer fasthttp.ReleaseRequest(req.FastRequest)
} }
var bl *pkg.Baseline
if reqerr != nil && reqerr != fasthttp.ErrBodyTooLarge { if reqerr != nil && reqerr != fasthttp.ErrBodyTooLarge {
pool.failedCount++ pool.failedCount++
pool.Statistor.FailedNumber++ pool.Statistor.FailedNumber++
@ -226,20 +226,23 @@ type Pool struct {
} }
func (p *Pool) Init() error { func (p *Pool) Init() error {
p.initwg.Add(2) p.initwg.Add(1)
p.pool.Invoke(newUnit("/", InitIndexSource)) p.pool.Invoke(newUnit("/", InitIndexSource))
p.pool.Invoke(newUnit(pkg.RandPath(), InitRandomSource))
p.initwg.Wait() p.initwg.Wait()
// todo 分析baseline
// 检测基本访问能力
if p.base.ErrString != "" {
return fmt.Errorf(p.base.String())
}
if p.index.ErrString != "" { if p.index.ErrString != "" {
return fmt.Errorf(p.index.String()) return fmt.Errorf(p.index.String())
} }
p.index.Collect()
logs.Log.Important("[baseline.index] " + p.index.String())
p.initwg.Add(1)
p.pool.Invoke(newUnit(pkg.RandPath(), InitRandomSource))
p.initwg.Wait()
// 检测基本访问能力
if p.base.ErrString != "" {
return fmt.Errorf(p.base.String())
}
p.base.Collect()
logs.Log.Important("[baseline.random] " + p.base.String())
if p.base.RedirectURL != "" { if p.base.RedirectURL != "" {
// 自定协议升级 // 自定协议升级
@ -250,11 +253,6 @@ func (p *Pool) Init() error {
p.BaseURL = strings.Replace(p.BaseURL, "http", "https", 1) p.BaseURL = strings.Replace(p.BaseURL, "http", "https", 1)
} }
} }
p.base.Collect()
p.index.Collect()
logs.Log.Important("[baseline.random] " + p.base.String())
logs.Log.Important("[baseline.index] " + p.index.String())
if p.base.RedirectURL != "" { if p.base.RedirectURL != "" {
CheckRedirect = func(redirectURL string) bool { CheckRedirect = func(redirectURL string) bool {

View File

@ -23,7 +23,6 @@ func NewBaseline(u, host string, resp *ihttp.Response) *Baseline {
if resp.ClientType == ihttp.STANDARD { if resp.ClientType == ihttp.STANDARD {
bl.Host = host bl.Host = host
} }
bl.Body = resp.Body() bl.Body = resp.Body()
bl.BodyLength = len(bl.Body) bl.BodyLength = len(bl.Body)
bl.Header = resp.Header() bl.Header = resp.Header()
@ -177,9 +176,9 @@ func (bl *Baseline) Get(key string) string {
func (bl *Baseline) Additional(key string) string { func (bl *Baseline) Additional(key string) string {
if v := bl.Get(key); v != "" { if v := bl.Get(key); v != "" {
return " [" + v + "] " return " [" + v + "]"
} else { } else {
return " " return ""
} }
} }
@ -259,9 +258,3 @@ func (bl *Baseline) Jsonify() string {
} }
return string(bs) return string(bs)
} }
func (bl *Baseline) ToMap() map[string]interface{} {
return map[string]interface{}{
"status": bl.Status,
}
}