Skip to content

Commit

Permalink
add gRPC client
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Oct 24, 2023
1 parent 46f7b10 commit 7be2674
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
// DefaultClientTimeout is default timeout for katsubushi client
var DefaultClientTimeout = 5 * time.Second

type ClientInterface interface {
Fetch(ctx context.Context) (uint64, error)
FetchMulti(ctx context.Context, n int) ([]uint64, error)
}

// Client is katsubushi client
type Client struct {
memcacheClients []*memcacheClient
Expand Down
35 changes: 35 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
gogrpc "google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -119,3 +120,37 @@ func (sv *gRPCStats) Get(ctx context.Context, req *grpc.StatsRequest) (*grpc.Sta
GetMisses: st.GetMisses,
}, nil
}

type GRPCClient struct {
client grpc.GeneratorClient
}

func NewGRPCClient(addr string) (*GRPCClient, error) {
conn, err := gogrpc.Dial(
addr,
gogrpc.WithTransportCredentials(insecure.NewCredentials()),
gogrpc.WithBlock(),
)
if err != nil {
return nil, err
}
return &GRPCClient{
client: grpc.NewGeneratorClient(conn),
}, nil
}

func (g *GRPCClient) Fetch(ctx context.Context) (uint64, error) {
res, err := g.client.Fetch(ctx, &grpc.FetchRequest{})
if err != nil {
return 0, err
}
return res.Id, nil
}

func (g *GRPCClient) FetchMulti(ctx context.Context, n int) ([]uint64, error) {
res, err := g.client.FetchMulti(ctx, &grpc.FetchMultiRequest{N: uint32(n)})
if err != nil {
return nil, err
}
return res.Ids, nil
}
43 changes: 41 additions & 2 deletions grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestGRPCSingle(t *testing.T) {
if res.Id == 0 {
t.Fatal("id should not be 0")
}
t.Logf("HTTP fetched single ID: %d", res.Id)
t.Logf("gRPC fetched single ID: %d", res.Id)
}
}

Expand All @@ -83,7 +83,46 @@ func TestGRPCMulti(t *testing.T) {
t.Fatal("id should not be 0")
}
}
t.Logf("HTTP fetched IDs: %v", res.Ids)
t.Logf("gRPC fetched IDs: %v", res.Ids)
}
}

func TestGRPCClientSingle(t *testing.T) {
client, err := katsubushi.NewGRPCClient(fmt.Sprintf("localhost:%d", grpcPort))
if err != nil {
t.Fatal(err)
}
for i := 0; i < 10; i++ {
id, err := client.Fetch(context.Background())
if err != nil {
t.Fatal(err)
}
if id == 0 {
t.Fatal("id should not be 0")
}
t.Logf("gRPC fetched single ID: %d", id)
}
}

func TestGRPCClientMulti(t *testing.T) {
client, err := katsubushi.NewGRPCClient(fmt.Sprintf("localhost:%d", grpcPort))
if err != nil {
t.Fatal(err)
}
for i := 0; i < 10; i++ {
ids, err := client.FetchMulti(context.Background(), 10)
if err != nil {
t.Fatal(err)
}
if len(ids) != 10 {
t.Fatalf("ids should contain 10 elements %v", ids)
}
for _, id := range ids {
if id == 0 {
t.Fatal("id should not be 0")
}
}
t.Logf("gRPC fetched IDs: %v", ids)
}
}

Expand Down

0 comments on commit 7be2674

Please sign in to comment.