douguanyan9928 2019-05-02 19:07
浏览 62

gRPC调用的Goroutines

I am trying to do a small prototype for a gRPC proxy service (based on github.com/buaazp/fasthttprouter ) which make calls to a gRPC backend

My prototype is based on gRPC helloworld example:

package main

import (
    pb "../proto/helloworld"
    "context"
    "fmt"
    "github.com/buaazp/fasthttprouter"
    grpcpool "github.com/processout/grpc-go-pool"
    "github.com/valyala/fasthttp"
    "google.golang.org/grpc"
    "log"
    "os"
    "time"
)

const (
    address     = "localhost:50051"
)

func main() {
    // grpc client pool

    var factory grpcpool.Factory

    factory = func() (*grpc.ClientConn, error) {
        conn, err := grpc.Dial(address, grpc.WithInsecure())
        if err != nil {
            log.Fatalf("Failed to start gRPC connection: %v", err)
        }
        return conn, err
    }

    pool, err := grpcpool.New(factory, 5, 5, time.Second)

    if err != nil {
        log.Fatalf("Failed to create gRPC pool: %v", err)
    }

    // routers and handlers

    router := fasthttprouter.New()
    router.GET("/without_gr", func(ctx *fasthttp.RequestCtx) {

        // ab -c 100 -n 1000 http://127.0.0.1:1111/without_gr
        // 8500 rps without goroutine

        conn, _ := pool.Get(ctx)
        defer conn.Close()

        if err != nil {
            _, _ = fmt.Fprintf(ctx, "Get pool failed")
            return
        }

        client := pb.NewGreeterClient(conn.ClientConn)

        _, _ = client.SayHello(ctx, &pb.HelloRequest{Name: "Pool ON"})

        _, _ = fmt.Fprintf(ctx, "Welcome to my website! Pool ON")
    })

    router.GET("/with_gr", func(ctx *fasthttp.RequestCtx) {
        go func(ctx *fasthttp.RequestCtx) {

            // ab -c 100 -n 1000 http://127.0.0.1:1111/with_gr
            // 14000 rps with goroutine

            conn, _ := pool.Get(ctx)
            defer conn.Close()

            if err != nil {
                _, _ = fmt.Fprintf(ctx, "Get pool failed")
                return
            }

            client := pb.NewGreeterClient(conn.ClientConn)

            _, _ = client.SayHello(ctx, &pb.HelloRequest{Name: "Pool ON"})

            _, _ = fmt.Fprintf(ctx, "Welcome to my website! Pool ON")
        }(ctx)
    })

    _ = fasthttp.ListenAndServe(":1111", router.Handler)
}

The results are:

  • http call with goroutine gives me 14K rps
  • http call without goroutine only 8K rps

My questions:

  • Is that correct sample?
  • Do i need run all api calls with goroutine wrapping?
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊