当我的goroutine数量很大时,代码会出现错误

时间:2019-04-30 08:09:25

标签: go

我的代码可以在少量goroutine上正常工作,但是内存和指针可能会发生大量错误。我猜这是我的锁使用不当。您能帮我看看我编写的工具吗?我已经调试了很长一段时间,但是没有办法开始。

我曾考虑过向时钟函数添加一个读锁,但仍然会报告错误,我也尝试使用sync.Map。但这并没有解决问题


package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "encoding/json"
    "strings"
    "sync"
    "time"
    "strconv"
)

type UrlArray struct {
    Url string `json:"url"`
    Method string `json:"method"`
    Params string `json:"params"`
}
type MsgRequest struct {
    Command string `json:"command"`
    Concurrent int `json:"concurrent"`
    IncrementalRatio float64 `json:"incrementalRatio"`
    InitialRatio float64 `json:"initialRatio"`
    Intervals int `json:"intervals"`
    UrlArray []UrlArray `json:"urls"`
}
type GroupData struct {
    TotalCount int `json:"totalCount"`
    FailCount int `json:"failCount"`
    SuccessRate float64 `json:"successRate"`
    CostTime float64 `json:"costTime"`
    AvergeTime float64 `json:"avergeTime"`
}
type MsgResponse struct {
    TimeObject map[string]GroupData `json:"timeObject"`
    TotalCount int `json:"totalCount"`
    FailCount int `json:"failCount"`
    SuccessRate float64 `json:"successRate"`
    CostTime float64 `json:"costTime"`
    AvergeTime float64 `json:"avergeTime"`
}
type LevelData struct {
    result map[string]GroupData
    sync.RWMutex
}
type ResultStatic struct {
    result map[string]MsgResponse 
}

var p = fmt.Println
var levelOutput LevelData
var output ResultStatic
var closeAllChan chan int
var isHandle = false


//write to map
func (r *LevelData) recordStatic(url string, status bool, useTimeSec float64) {
    r.Lock()
    defer r.Unlock()
    val, ok := r.result[url]
    if ok {
        val.TotalCount += 1
        val.CostTime += useTimeSec
        if status == false {
            val.FailCount += 1
        }
        val.SuccessRate = 1.00 - (float64(val.FailCount) * 1.00) / (float64(val.TotalCount) * 1.00) 
        val.AvergeTime = val.CostTime / float64(val.TotalCount)
        levelOutput.result[url] = val
        } else {
            failcnt := 0
            if status == false {
                failcnt = 1
            }
            successRate := float64(1 - failcnt)
            val := GroupData{TotalCount: 1, FailCount: failcnt, SuccessRate: successRate, CostTime: useTimeSec, AvergeTime: useTimeSec}
            levelOutput.result[url] = val
        }
}

//get request
func get(url string, params string) {
    start := time.Now()
    data := url + "?" + params
    response, err:= http.Get(data)
    end := time.Now()
    costTime := float64((end.Sub(start)).Seconds())

    if err != nil {
        levelOutput.recordStatic(url, false, costTime)
    } else {
        levelOutput.recordStatic(url, true, costTime)
    }

    defer response.Body.Close()
}   

// post request
func post(url string, params string) {
    start := time.Now()
    response, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(params))
    end := time.Now()
    costTime := float64((end.Sub(start)).Seconds())

    if err != nil {
        levelOutput.recordStatic(url, false, costTime)
    } else {
        levelOutput.recordStatic(url, true, costTime)
    }

    response.Body.Close()
}

//clock ticker
func (r *LevelData) clock(urlArrays []UrlArray, concurrent int, initialRatio float64, incrementalRatio float64, intervals int) {
    start := int(float64(concurrent) * initialRatio)
    increment := int(float64(concurrent) * incrementalRatio)
    ticker := time.NewTicker(time.Duration(intervals) * time.Second)
    endtotal := increment + concurrent
    objectKey := 0

    for range ticker.C {
        select {
        case <-closeAllChan:
            p("死循环退出")
            return 
        default:
            if objectKey != 0 {
                for _, v := range(urlArrays) {
                    val1, ok1 := output.result[v.Url]
                    p(val1, ok1)
                    if !ok1 {
                        val1 = MsgResponse{}
                        val1.TimeObject = make(map[string]GroupData)
                    }
                    val1.TimeObject[strconv.Itoa(objectKey)] = levelOutput.result[v.Url]

                    val1.TotalCount += (val1.TimeObject[strconv.Itoa(objectKey)]).TotalCount
                    val1.FailCount += (val1.TimeObject[strconv.Itoa(objectKey)]).FailCount
                    val1.CostTime += (val1.TimeObject[strconv.Itoa(objectKey)]).CostTime
                    val1.SuccessRate = 1.00 - (float64(val1.FailCount) * 1.00) / (float64(val1.TotalCount) * 1.00) 
                    val1.AvergeTime = val1.CostTime / float64(val1.TotalCount)
                    output.result[v.Url] = val1
                }
            } 

            levelOutput = LevelData{result: map[string]GroupData{}}

            if start >= endtotal {
                p("start == concurrent")
                close(closeAllChan)
                time.Sleep(time.Second)
                return
            }

            if objectKey == 0 {
                for i := 0; i < start; i++ {
                    go work(urlArrays)
                }
            } else {
                for i := 0; i < increment; i++ {
                    go work(urlArrays)
                }
            }

            objectKey += 1
            start += increment
        }
    }
}

//work
func work(urlArrays []UrlArray) {
    i := 0
    for {
        select {
        case <-closeAllChan:
            return 
        default:
            index := i % len(urlArrays)
            i++

            url := urlArrays[index].Url
            method := urlArrays[index].Method
            params := urlArrays[index].Params

            if method == "Get" {
                get(url, params)
            } else {
                post(url, params)
            } 
        }
    }
} 

func setupResponse(w *http.ResponseWriter, req *http.Request) {
    (*w).Header().Set("Access-Control-Allow-Origin", "*")
    (*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
    (*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}


func myfunc(w http.ResponseWriter, r *http.Request) {
    setupResponse(&w, r)
    if (r).Method == "OPTIONS" {
        return
    }

    b, err := ioutil.ReadAll(r.Body)
    defer r.Body.Close()
    if err != nil {
        http.Error(w, err.Error(), 501)
        return 
    }
    //json decode
    var msgs MsgRequest
    err = json.Unmarshal(b, &msgs)
    if err != nil {
        p(err.Error())
        http.Error(w, err.Error(), 502)
        return
    }

    var command = msgs.Command

    if command == "stop" {
        p("stop start")
        if isHandle {
            close(closeAllChan)
            isHandle = false 
        } else {
            p("no handle")
            http.Error(w, "未有数据在执行", 504)
            return
        }
    } else if command == "get" {
        p("get start")
        if isHandle {
            p(output.result)
            js, err := json.Marshal(output.result)
            if err != nil {
                http.Error(w, err.Error(), 503)
                return
            }
            w.Header().Set("Content-Type", "application/json")
            w.Write(js)
        } else {
            p("no handle")
            http.Error(w, "未有数据在执行", 504)
            return
        }
    } else {
        isHandle = true
        closeAllChan = make(chan int)
        output = ResultStatic{result: map[string]MsgResponse{}}
        urlArrays := msgs.UrlArray
        concurrent := msgs.Concurrent
        initialRatio := msgs.InitialRatio
        incrementalRatio := msgs.IncrementalRatio
        intervals := msgs.Intervals
        levelOutput.clock(urlArrays, concurrent, initialRatio, incrementalRatio, intervals)
        isHandle = false
        js, err := json.Marshal(output.result)
        if err != nil {
            http.Error(w, err.Error(), 503)
            return 
        }
        p(output.result)

        w.Header().Set("Content-Type", "application/json")
        w.Write(js) 
    }
    return
}

func main() {
    http.HandleFunc("/handle", myfunc)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        panic(err)
    }
}


// json example
// {
//  "command": "handle/get/stop",
//  "concurrent": 100,
//  "initialRatio": 0.60,
//  "incrementalRatio": 0.20,
//  "intervals": 10,
//  "urls": [
//      {
//          "url": "http://google.com",
//          "method": "GET",
//          "params": ""    
//      }
//  ]
// }

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x6782c4]

goroutine 64655 [running]:
main.post(0xc00001e0c0, 0x3c, 0xc000188510, 0x2f)
    /root/toolv2.go:114 +0x1b4
main.work(0xc000391380, 0x1, 0x4)
    /root/toolv2.go:193 +0xcf
created by main.(*LevelData).clock
    /root/toolv2.go:161 +0x657
exit status 2

1 个答案:

答案 0 :(得分:4)

“无效的内存地址或nil指针取消引用”不表示数据争用。您正在取消引用nil的指针:

func post(url string, params string) {
    start := time.Now()
    response, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(params))
    end := time.Now()
    costTime := float64((end.Sub(start)).Seconds())

    if err != nil {
        levelOutput.recordStatic(url, false, costTime)
    } else {
        levelOutput.recordStatic(url, true, costTime)
    }

    response.Body.Close()
}

在发生错误后,您一定不能取消引用响应,因为这样响应就为零。