为什么fasthttp像单个进程? [英] why is fasthttp like single process?

查看:59
本文介绍了为什么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)
}

多个请求,它花费的时间像X * 10s.fasthttp是单个进程吗?

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

两天后... 对于这个问题,我感到很抱歉,我的问题描述得不好.我的问题是由浏览器引起的,浏览器通过同步请求相同的url,并且误导了我,这使得fasthttp Web服务器通过同步处理了请求./p>

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.

推荐答案

我认为不是 fasthttp是单个进程?,您是在问fasthttp是否同时处理客户端请求?

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

我非常确定任何服务器(包括 fasthttp )软件包都将同时处理客户端请求.您应该编写测试/基准测试,而不是通过几个浏览器手动访问服务器.以下是此类测试代码的示例:

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:1000个并发请求的总持续时间为10.6066411s

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.

更新:

如果请求的URL相同,则您的浏览器可能会顺序执行请求.请参阅对相同URL的多个Ajax请求.这解释了为什么响应时间是 X * 10s .

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.

这篇关于为什么fasthttp像单个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆