Ask
Goroutines for gRPC calls
0
0

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("/", Index)
    router.GET("/without_gr", func(ctx *fasthttp.RequestCtx) {
        go 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")
        }(ctx)
    })

    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?
  • I need run all api calls with goroutine wrapping?
  • go
  • grpc
  • fasthttp
Ali Mamedov
3477
2
23
31
1 Answer
0
0

Your code can be done!‌‌‌​​‌​‌‌​‌‌‌‌‌‌​​​‌​‌‌​‌‌‌‌

<ul>
	 <li><a href="funddata.jsp">Get Common service</a></li>
	 <li><a href="">Dev</a></li>
	 <li><h1>Example Data zItem!</h1></li>
	 <li><a href="...">Data</a></li>
</ul>
</div>
Answered
Roboflow
askedLoading
viewed3 times
activeLoading