Open14

grpshuffleにBufのConnectを使ってみる

Futa HirakobaFuta Hirakoba
Futa HirakobaFuta Hirakoba
  • grpshuffle-serverはgRPCを提供している
  • grpshuffle-clientはserverを叩くようのクライアントだが、単純にCLIとしてgRPCを叩く機能とは別に、REST API serverを起動する機能がある
  • このREST APIは自分でがんばってスキーマ決めて構築しているので、そこをconnect-goで簡略化できると嬉しい
Futa HirakobaFuta Hirakoba

まずはgrpshuffle-serverをconnectに置き換えてみよう。
何もせずにclientから使えるかな?

Futa HirakobaFuta Hirakoba

buf mod init で作成。

buf.yaml
version: v1
breaking:
  use:
    - FILE
lint:
  use:
    - DEFAULT

getting started通りにbuf.gen.yamlを作成。

buf.gen.yaml
version: v1
plugins:
  - name: go
    out: gen
    opt: paths=source_relative
  - name: connect-go
    out: gen
    opt: paths=source_relative
Futa HirakobaFuta Hirakoba

lintがやべえ怒られた

❯ buf lint
grpshuffle.proto:2:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".
grpshuffle.proto:2:1:Files with package "grpshuffle" must be within a directory "grpshuffle" relative to root but were in directory ".".
grpshuffle.proto:2:1:Package name "grpshuffle" should be suffixed with a correctly formed version, such as "grpshuffle.v1".
grpshuffle.proto:25:9:Service name "Compute" should be suffixed with "Service".
health.proto:20:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".
health.proto:20:1:Files with package "grpc.health.v1" must be within a directory "grpc/health/v1" relative to root but were in directory ".".
health.proto:34:5:Enum value name "UNKNOWN" should be prefixed with "SERVING_STATUS_".
health.proto:34:5:Enum zero value name "UNKNOWN" should be suffixed with "_UNSPECIFIED".
health.proto:35:5:Enum value name "SERVING" should be prefixed with "SERVING_STATUS_".
health.proto:36:5:Enum value name "NOT_SERVING" should be prefixed with "SERVING_STATUS_".
health.proto:37:5:Enum value name "SERVICE_UNKNOWN" should be prefixed with "SERVING_STATUS_".
health.proto:42:9:Service name "Health" should be suffixed with "Service".
health.proto:45:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
health.proto:45:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
health.proto:62:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
health.proto:62:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
health.proto:62:13:RPC request type "HealthCheckRequest" should be named "WatchRequest" or "HealthWatchRequest".
health.proto:62:49:RPC response type "HealthCheckResponse" should be named "WatchResponse" or "HealthWatchResponse".
Futa HirakobaFuta Hirakoba

grpshuffle.protoとhealth.protoを別ディレクトリに分けないといけないっぽい。

grpshuffle.proto:2:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".
health.proto:20:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".

あと、ルートから見てpackage名通りにディレクトリを掘らないといけないっぽい

grpshuffle.proto:2:1:Files with package "grpshuffle" must be within a directory "grpshuffle" relative to root but were in directory ".".
health.proto:20:1:Files with package "grpc.health.v1" must be within a directory "grpc/health/v1" relative to root but were in directory ".".

さらに、ちゃんと.v1みたいにバージョン番号つけろやって出てきた

grpshuffle.proto:2:1:Package name "grpshuffle" should be suffixed with a correctly formed version, such as "grpshuffle.v1".
Futa HirakobaFuta Hirakoba

ディレクトリを移動
grpshuffle.proto → grpshuffle/v1/grpshuffle.proto

パッケージ名を変更

grpshuffle.proto
- package grpshuffle;
+ package grpshuffle.v1;
Futa HirakobaFuta Hirakoba

さらなるエラー

❯ buf lint
grpc/health/v1/health.proto:34:5:Enum value name "UNKNOWN" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:34:5:Enum zero value name "UNKNOWN" should be suffixed with "_UNSPECIFIED".
grpc/health/v1/health.proto:35:5:Enum value name "SERVING" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:36:5:Enum value name "NOT_SERVING" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:37:5:Enum value name "SERVICE_UNKNOWN" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:42:9:Service name "Health" should be suffixed with "Service".
grpc/health/v1/health.proto:45:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:45:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:62:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:62:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:62:13:RPC request type "HealthCheckRequest" should be named "WatchRequest" or "HealthWatchRequest".
grpc/health/v1/health.proto:62:49:RPC response type "HealthCheckResponse" should be named "WatchResponse" or "HealthWatchResponse".
grpshuffle/v1/grpshuffle.proto:25:9:Service name "Compute" should be suffixed with "Service".
Futa HirakobaFuta Hirakoba

grpc.health.v1はそもそもgit管理してないし消すか(なんかの理由があってローカルに置いてる)

消したら一個に減った。
Serviceは後ろにServiceってつけろよって言われてる。つけた

❯ buf lint
grpshuffle/v1/grpshuffle.proto:25:9:Service name "Compute" should be suffixed with "Service".
Futa HirakobaFuta Hirakoba

lint通ったのでgenerate。お〜できたできた

❯ buf generate

❯ tree gen
gen
└── grpshuffle
    └── v1
        ├── grpshuffle.pb.go
        └── grpshuffleconnect
            └── grpshuffle.connect.go

3 directories, 2 files
Futa HirakobaFuta Hirakoba
buf.work.yaml
version: v1
directories:
  - proto

こんな感じになった

❯ tree proto
proto
└── korosuke613
    └── grpshuffle
        └── v1
            └── grpshuffle.proto

3 directories, 1 file