Skip to content

Commit

Permalink
feat: add support for protobuf service config
Browse files Browse the repository at this point in the history
When sharing a schema via Buf, service config JSON files are not
included.

This commit adds an alternate way of defining service config: in the
protobuf files, via a custom annotation.

The service config protobuf schema is already defined in:

https://buf.build/grpc/grpc/docs/main:grpc.service_config
  • Loading branch information
odsod committed Jul 6, 2022
1 parent ef2a7ee commit eaf8a86
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 36 deletions.
45 changes: 43 additions & 2 deletions .sage/sagefile.go → .sage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"context"
"os"

"go.einride.tech/sage/sg"
"go.einride.tech/sage/tools/sgbuf"
"go.einride.tech/sage/tools/sgconvco"
"go.einride.tech/sage/tools/sggit"
"go.einride.tech/sage/tools/sggo"
Expand All @@ -23,11 +25,22 @@ func main() {
}

func All(ctx context.Context) error {
sg.Deps(ctx, ConvcoCheck, GolangciLint, GoReview, GoTest, FormatMarkdown, FormatYAML)
sg.Deps(ctx, ConvcoCheck, GoLint, GoReview, GoTest, BufLint, FormatMarkdown, FormatYAML)
sg.Deps(ctx, BufGenerateExample)
sg.SerialDeps(ctx, GoModTidy, GitVerifyNoDiff)
return nil
}

func BufLint(ctx context.Context) error {
sg.Logger(ctx).Println("linting Buf module..")
return sgbuf.Command(ctx, "lint").Run()
}

func BufPush(ctx context.Context) error {
sg.Logger(ctx).Println("pushing Buf module..")
return sgbuf.Command(ctx, "push").Run()
}

func FormatYAML(ctx context.Context) error {
sg.Logger(ctx).Println("formatting YAML files...")
return sgyamlfmt.Command(ctx, "-d", sg.FromGitRoot(), "-r").Run()
Expand All @@ -48,7 +61,7 @@ func GoReview(ctx context.Context) error {
return sggoreview.Command(ctx, "-c", "1", "./...").Run()
}

func GolangciLint(ctx context.Context) error {
func GoLint(ctx context.Context) error {
sg.Logger(ctx).Println("linting Go files...")
return sggolangcilint.Run(ctx)
}
Expand All @@ -67,3 +80,31 @@ func GitVerifyNoDiff(ctx context.Context) error {
sg.Logger(ctx).Println("verifying that git has no diff...")
return sggit.VerifyNoDiff(ctx)
}

func BufGenerateExample(ctx context.Context) error {
sg.Deps(ctx, ProtocGenGoGrpcServiceConfig)
sg.Logger(ctx).Println("generating example...")
if err := os.RemoveAll(sg.FromGitRoot("internal", "gen", "proto")); err != nil {
return err
}
return sgbuf.Command(
ctx,
"generate",
"--template",
"buf.gen.example.yaml",
"--path",
"einride/serviceconfig/example",
).Run()
}

func ProtocGenGoGrpcServiceConfig(ctx context.Context) error {
sg.Logger(ctx).Println("building binary...")
return sg.Command(
ctx,
"go",
"build",
"-o",
sg.FromBinDir("protoc-gen-go-grpc-service-config"),
sg.FromGitRoot(),
).Run()
}
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ clean-sage:
all: $(sagefile)
@$(sagefile) All

.PHONY: buf-generate-example
buf-generate-example: $(sagefile)
@$(sagefile) BufGenerateExample

.PHONY: buf-lint
buf-lint: $(sagefile)
@$(sagefile) BufLint

.PHONY: buf-push
buf-push: $(sagefile)
@$(sagefile) BufPush

.PHONY: convco-check
convco-check: $(sagefile)
@$(sagefile) ConvcoCheck
Expand All @@ -41,6 +53,10 @@ format-yaml: $(sagefile)
git-verify-no-diff: $(sagefile)
@$(sagefile) GitVerifyNoDiff

.PHONY: go-lint
go-lint: $(sagefile)
@$(sagefile) GoLint

.PHONY: go-mod-tidy
go-mod-tidy: $(sagefile)
@$(sagefile) GoModTidy
Expand All @@ -53,6 +69,6 @@ go-review: $(sagefile)
go-test: $(sagefile)
@$(sagefile) GoTest

.PHONY: golangci-lint
golangci-lint: $(sagefile)
@$(sagefile) GolangciLint
.PHONY: protoc-gen-go-grpc-service-config
protoc-gen-go-grpc-service-config: $(sagefile)
@$(sagefile) ProtocGenGoGrpcServiceConfig
13 changes: 13 additions & 0 deletions buf.gen.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: v1

managed:
enabled: true
go_package_prefix:
default: go.einride.tech/protoc-gen-go-grpc-service-config
except:
- buf.build/googleapis/googleapis

plugins:
- name: go-grpc-service-config
out: internal/gen/proto
opt: module=go.einride.tech/protoc-gen-go-grpc-service-config
35 changes: 35 additions & 0 deletions buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: cncf
repository: xds
commit: 0492166643854ea08814f4349286f9cb
- remote: buf.build
owner: envoyproxy
repository: envoy
commit: 14eefb0da6dc45b784fc05e55e66326d
- remote: buf.build
owner: envoyproxy
repository: protoc-gen-validate
commit: dc09a417d27241f7b069feae2cd74a0e
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 86d30bdfc34044fb9339e1bd9673839b
- remote: buf.build
owner: grpc
repository: grpc
commit: 40b550c256324a8f85ba933e92479f95
- remote: buf.build
owner: opencensus
repository: opencensus
commit: d1d7ccd1281e4f42ab7ce9a7d2b29e33
- remote: buf.build
owner: opentelemetry
repository: opentelemetry
commit: 509848662be44e4d8aaf9189f6e6ab01
- remote: buf.build
owner: prometheus
repository: client-model
commit: 1d56a02d481a412a83b3c4984eb90c2e
22 changes: 22 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: v1

name: buf.build/einride/grpc-service-config

deps:
- buf.build/grpc/grpc

lint:
use:
- DEFAULT
except:
- RPC_RESPONSE_STANDARD_NAME
- RPC_REQUEST_RESPONSE_UNIQUE
- ENUM_VALUE_PREFIX
ignore:
- google

breaking:
use:
- PACKAGE
ignore:
- google
26 changes: 26 additions & 0 deletions einride/serviceconfig/example/v1/default_service_config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";

package einride.serviceconfig.example.v1;

import "einride/serviceconfig/v1/annotations.proto";

option (einride.serviceconfig.v1.default_service_config) = {
method_config: {
name: {}
timeout: {
seconds: 10
}
retry_policy: {
initial_backoff: {
nanos: 200000000 // 0.2s
}
max_backoff: {
seconds: 60
}
max_attempts: 5
backoff_multiplier: 2
retryable_status_codes: UNAVAILABLE
retryable_status_codes: UNKNOWN
}
}
};
15 changes: 15 additions & 0 deletions einride/serviceconfig/v1/annotations.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package einride.serviceconfig.v1;

import "google/protobuf/descriptor.proto";
import "grpc/service_config/service_config.proto";

extend google.protobuf.FileOptions {
// The `default_service_config` annotation provides default gRPC service
// configuration for the services in the package.
//
// Magic number is the 28 most significant bits in the sha256sum of
// `einride.serviceconfig.v1.default_service_config`.
grpc.service_config.ServiceConfig default_service_config = 262421647;
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module go.einride.tech/protoc-gen-go-grpc-service-config
go 1.17

require (
go.buf.build/protocolbuffers/go/einride/grpc-service-config v1.2.1
go.buf.build/protocolbuffers/go/grpc/grpc v1.2.54
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
)
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.buf.build/protocolbuffers/go/cncf/xds v1.2.14/go.mod h1:hq9vhcZd4ZTK2iCNk4F8oyZAxq1iz/LJUQUiNjF8gZQ=
go.buf.build/protocolbuffers/go/einride/grpc-service-config v1.2.1 h1:QqsSxCQvwCHPWIUCyNtpI+A4tD4+V00BgdcnOhoXNbw=
go.buf.build/protocolbuffers/go/einride/grpc-service-config v1.2.1/go.mod h1:kgQcZlW4PmcEACPrzYB5NGsdzqvNd4J9bn5fMjv//wo=
go.buf.build/protocolbuffers/go/envoyproxy/envoy v1.2.161/go.mod h1:yZ3Q0ckzdHykCgNkMsFgLqBUINk15M+fG2HB8rKPwn0=
go.buf.build/protocolbuffers/go/envoyproxy/protoc-gen-validate v1.2.6/go.mod h1:rFeh5GJ10STGBM5uR3XCCgIPQAoOHxKgoAdeLRWtvHU=
go.buf.build/protocolbuffers/go/grpc/grpc v1.2.54 h1:hHkd95hdd3rLsFdKAat+l157/1v9fcFAIvgL62RfY78=
go.buf.build/protocolbuffers/go/grpc/grpc v1.2.54/go.mod h1:llaEv0/K+Zy3jdYfneJw3pJJsgvYyw7v9bjyso2VdT8=
go.buf.build/protocolbuffers/go/opencensus/opencensus v1.2.4/go.mod h1:fZwrLuqDEQM75QwCWh2NESbiB/aE/RyYREab/5rQEOs=
go.buf.build/protocolbuffers/go/opentelemetry/opentelemetry v1.2.39/go.mod h1:kchE+AygbBgTRXUNtLGxZnbMBv7iFN1z3L2jZaMeqZo=
go.buf.build/protocolbuffers/go/prometheus/client-model v1.2.1/go.mod h1:34sL1yXFVbViH1alMgoGlmAJT5Io7SuUNCJvhh5wklU=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eaf8a86

Please sign in to comment.