duanlou2917 2017-07-22 02:40
浏览 33
已采纳

为什么fasthttp像单个进程?

requestHandler := func(ctx *fasthttp.RequestCtx) {

    time.Sleep(time.Second*time.Duration(10))
    fmt.Fprintf(ctx, "Hello, world! Requested path is %q", ctx.Path())
}


s := &fasthttp.Server{
Handler: requestHandler
}

if err := s.ListenAndServe("127.0.0.1:82"); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}

multiple request,and it cost time like X*10s. fasthttp is single process?

after two days... I am sorry for this question,i describe my question not well.My question is caused by the browser,the browser request the same url by synchronization, and it mislead me, it make think the fasthttp web server hanlde the request by synchronization.

  • 写回答

2条回答 默认 最新

  • duanjiagu0655 2017-07-22 04:00
    关注

    I think instead of fasthttp is single process?, you're asking whether fasthttp handles client requests concurrently or not?

    I'm pretty sure that any server (including fasthttp) package will handle client requests concurrently. You should write a test/benchmark instead of manually access the server through several browsers. The following is an example of such test code:

    package main_test
    
    import (
        "io/ioutil"
        "net/http"
        "sync"
        "testing"
        "time"
    )
    
    func doRequest(uri string) error {
        resp, err := http.Get(uri)
        if err != nil {
            return err
        }
        defer resp.Body.Close()
    
        _, err = ioutil.ReadAll(resp.Body)
        if err != nil {
            return err
        }
    
        return nil
    }
    
    func TestGet(t *testing.T) {
        N := 1000
        wg := sync.WaitGroup{}
        wg.Add(N)
    
        start := time.Now()
        for i := 0; i < N; i++ {
            go func() {
                if err := doRequest("http://127.0.0.1:82"); err != nil {
                    t.Error(err)
                }
                wg.Done()
            }()
        }
        wg.Wait()
    
        t.Logf("Total duration for %d concurrent request(s) is %v", N, time.Since(start))
    }
    

    And the result (in my computer) is

    fasthttp_test.go:42: Total duration for 1000 concurrent request(s) is 10.6066411s

    You can see that the answer to your question is No, it handles the request concurrently.

    UPDATE:

    In case the requested URL is the same, your browser may perform the request sequentially. See Multiple Ajax requests for same URL. This explains why the response times are X*10s.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)