dosin84644 2016-08-31 12:45
浏览 54
已采纳

在后台使用goroutine进行HTTP并发延迟

Why is there such a delay in processing of incoming requests by the main server goroutine and how can this delay be avoided ?

Simple code with NO DELAYS

package main
import (
    "log"
    "net/http"
)
func main() {
    http.HandleFunc("/", root)
    http.ListenAndServe(":8090", nil)
}
//---------------------------------------------------------------------------
//  http handlers
//---------------------------------------------------------------------------
func root(w http.ResponseWriter, r *http.Request) {
    log.Printf("[root] `%v`
", r.URL.Path)
    w.Write([]byte("What the hell"))
}

Result testing the loading

    ╰─➤  wrk -d20s -t5 -c100 http://localhost:8090
Running 20s test @ http://localhost:8090
  5 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    17.07ms   46.63ms 368.77ms   91.73%
    Req/Sec    10.99k     4.87k   24.48k    62.49%
  1038912 requests in 20.10s, 128.80MB read
Requests/sec:  51684.63
Transfer/sec:      6.41MB

Adding goroutines

package main
import (
    "log"
    "net/http"
)
func main() {
    _ = NewTesterGo(100)

    http.HandleFunc("/", root)
    http.ListenAndServe(":8090", nil)
}
//---------------------------------------------------------------------------
//  http handlers
//---------------------------------------------------------------------------
func root(w http.ResponseWriter, r *http.Request) {
    log.Printf("[root] `%v`
", r.URL.Path)
    w.Write([]byte("What the fuck"))
}

//---------------------------------------------------------------------------
//  tester segment
//---------------------------------------------------------------------------
type (
    TesterGo struct {
        Work chan string
    }
)

func NewTesterGo(count int) *TesterGo {
    t:=&TesterGo{
        Work:make(chan string,100),
    }
    for ; count > 0 ; count -- {
        go t.Worker()
    }
    return t

}
func (t *TesterGo) Worker() {
    log.Printf("[testergo][worker][work] стартовал....
")
    for {
        select {
        case work := <-t.Work:
            log.Printf("[testerGo][Worker] %v
", work)
        default:
        }
    }
}

Result with loading

╰─➤  wrk -d20s -t5 -c100 http://localhost:8090
Running 20s test @ http://localhost:8090
  5 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   464.71ms  305.44ms   1.90s    77.90%
    Req/Sec    54.62     43.74   200.00     67.50%
  3672 requests in 20.05s, 466.17KB read
  Socket errors: connect 0, read 0, write 0, **timeout 97**
Requests/sec:    **183.11**
Transfer/sec:     23.25KB
  • 写回答

1条回答 默认 最新

  • dpoh61610 2016-08-31 12:59
    关注

    your goroutines use default, causing them to spin immediately if there is nothing in the channel (and there is nothing in your example). This probably makes Go's scheduler do way more context switching than needed, and probably consume a lot of CPU for nothing.

    Is there a reason to default in a loop? If not try one of the following:

    Either no default, the goroutines would simply "sleep" until there's work.

     for {
            select {
            case work := <-t.Work:
                log.Printf("[testerGo][Worker] %v
    ", work)
            }
        }
    

    This BTW makes the select completely redundant, so just get rid of it:

     for { //you can also use a range on the channel
         work := <- t.Work 
         log.Printf("[testerGo][Worker] %v
    ", work)
     }
    

    Second option - a timeout that will make them wait before continuing in the loop:

     for {
            select {
            case work := <-t.Work:
                log.Printf("[testerGo][Worker] %v
    ", work)
    
            case <- time.After(100*time.Millisecond): //or whatever you prefer
    
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?