why is fasthttp like single process?

有些话、适合烂在心里 提交于 2019-12-14 03:37:23

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/45250120/why-is-fasthttp-like-single-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!