简体   繁体   中英

my net/http post method give 100% cpu usage

I have a web server with net/http and julienschmidt/httprouter. I don't know why, but if I post a "big" data, like hundred of newline from <textarea> , my go server dies.

I already tried to use goroutine to make for _, value := range target exclude from main thread and try to increase ulimit, but it seems doesn't work code:

func CreateRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    r.ParseMultipartForm(0)

    Title, Caption, Target := r.Form["title"], r.Form["caption"], r.Form["target"]

    if len(Title) > 0 && len(Caption) > 0 && len(Target) > 0 {

        CampaignId := helper.Md5Gen(time.Now().String())

        target := strings.Split(Target[0], "\n")

        for _, value := range target {

            value = strings.Replace(value, "\r", "", -1)

            if len(value) > 6 {
                data := structs.CampaignTarget{
                    CampaignId: CampaignId,
                    PhoneNumber: value,
                    IsExecuted: false,
                    ExecutedBy: "",
                    IsSuccess: false,
                    SendAt: int64(0),
                    Username: LoggedUsername(w, r),
                }
                database, err := helper.DataDatabase()
                if err == nil {
                    database.C("xxx").Insert(&data)
                }
            }
        }

        SavedFileName := ""

        file, handler, err := r.FormFile("media")

        if err == nil {

            path := "/xxx/path/" + LoggedUsername(w, r)

            err := os.MkdirAll(path, os.ModePerm)
            if err != nil {
                http.Error(w, "Problem with folder creation", 500)
                return
            }

            defer file.Close()

            SavedFileName = fmt.Sprintf("%v-%v", helper.Md5Gen(time.Now().String()), handler.Filename)

            f, err := os.OpenFile(path+"/"+SavedFileName, os.O_WRONLY|os.O_CREATE, 0666)
            if err != nil {
                http.Error(w, "Failed to Write File", 500)
                return
            }
            defer f.Close()
            io.Copy(f, file)

        }

        data := structs.Campaign{
            ID: CampaignId,
            Title: Title[0],
            Caption: Caption[0],
            TotalTarget: len(target),
            Media: SavedFileName,
            Username: LoggedUsername(w, r),
            CreatedAt: time.Now().Unix(),
        }
        database, err := helper.DataDatabase()
        if err == nil {
            database.C("xx").Insert(&data)
        }
    }
}

It's possible that splitting on newlines, where all of the contents of the string to split on is newlines is an expensive operation.

target := strings.Split(Target[0], "\n")

If you want to split newlines, consider using a bufio.Scanner instead

scanner := bufio.NewScanner(strings.NewReader("foo\nbar\nbaz"))
for scanner.Scan() {
    fmt.Println(scanner.Text())
}

Runnable example

This is because scanner advances to the next token when you call scan, whereas strings.Split processes the whole payload at once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM