Cannot use *grpcpool.ClientConn as type *grpc.ClientConn
wakenmeng opened this issue · 4 comments
> go build
./main.go:31:24: cannot use conn (type *grpcpool.ClientConn) as type *grpc.ClientConn in argument to testproto.NewFooClient
Above incompatible types error occured when build. Here's the code:
go version go1.11.5 linux/amd64
package main
import (
"context"
"github.com/processout/grpc-go-pool"
"google.golang.org/grpc"
"log"
pb "/local/path/proto"
"time"
)
func newConnection() (*grpc.ClientConn, error) {
conn, err := grpc.Dial("127.0.0.1:10000", grpc.WithInsecure())
if err != nil {
log.Fatal("Connect to service failed. ", err)
return nil, err
}
log.Println("Connected to service")
return conn, nil
}
var connPool *grpcpool.Pool
func InitPool() (err error) {
connPool, err = grpcpool.New(newConnection, 5, 5, time.Second)
return
}
func main() {
InitPool()
conn, _ := connPool.Get(context.Background())
cli := pb.NewFooClient(conn)
log.Println("done")
}
Hi @wakenmeng
I'm not sure to understand your issue- could you please give me more context in what you're trying to achieve?
The NewFooClient
function seems to only accept *grpc.ClientConn
(not an interface either), so to me this is expected behavior that the compiler complains about the type of the pool being *grpcpool.ClientConn
. Perhaps you could change that function arguments to take the type you're looking for?
I read the test code and found out that I misuse the library. Sorry that I forgot to close this issue, I'm closing it. thanks for your reply @manuel-huez.
@wakenmeng I am having the same issue and was wondering if you could elaborate on how you resolved this. Thank you so much!
@magv96 sure. The grpcPoolConn
from pool.Get(ctx)
has a member *grpc.ClientConn
, which is type grpc.ClientConn
. Here's the defination
So to use it with grpc:
pool := InitPool()
conn, _ := pool.Get(context.Background())
grpcClient := pb.NewFooClient(conn.ClientConn) // Here, use conn.ClientConn, which is grpc.ClientConn type.
grpcClient.DoTheCall()
by the way, if there's an example file will be helpful.