-
Notifications
You must be signed in to change notification settings - Fork 10
feature: support custom interceptors #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbe1903
feature: support custom interceptors
rustatian 432d56e
fix: always add metrics interceptor
rustatian d4cd2a6
chore: minor fixes
rustatian 563f827
chore: update the order of interceptors
rustatian 0c73e3f
chore: minor fixes
rustatian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| name: grpc-interceptors | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| - stable | ||
| pull_request: | ||
| branches: | ||
| - master | ||
| - stable | ||
|
|
||
| jobs: | ||
| grpc_interceptors_test: | ||
| name: gRPC interceptors (Go ${{ matrix.go }}, PHP ${{ matrix.php }}, OS ${{matrix.os}}) | ||
| runs-on: ${{ matrix.os }} | ||
| timeout-minutes: 60 | ||
| strategy: | ||
| matrix: | ||
| php: ["8.5"] | ||
| go: [stable] | ||
| os: ["ubuntu-latest"] | ||
|
|
||
| steps: | ||
| - name: Set up Go ${{ matrix.go }} | ||
| uses: actions/setup-go@v5 # action page: <https://github.com/actions/setup-go> | ||
| with: | ||
| go-version: ${{ matrix.go }} | ||
|
|
||
| - name: Set up PHP ${{ matrix.php }} | ||
| uses: shivammathur/setup-php@v2 # action page: <https://github.com/shivammathur/setup-php> | ||
| with: | ||
| php-version: ${{ matrix.php }} | ||
| extensions: sockets | ||
|
|
||
| - name: Check out code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Get Composer Cache Directory | ||
| id: composer-cache | ||
| run: | | ||
| cd tests/php_test_files | ||
| echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Init Composer Cache # Docs: <https://git.io/JfAKn#php---composer> | ||
| uses: actions/cache@v4 | ||
| with: | ||
| path: ${{ steps.composer-cache.outputs.dir }} | ||
| key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ hashFiles('**/composer.json') }} | ||
| restore-keys: ${{ runner.os }}-composer- | ||
|
|
||
| - name: Install Composer dependencies | ||
| run: cd tests/php_test_files && composer update --prefer-dist --no-progress --ansi | ||
|
|
||
| - name: Init Go modules Cache # Docs: <https://git.io/JfAKn#go---modules> | ||
| uses: actions/cache@v4 | ||
| with: | ||
| path: ~/go/pkg/mod | ||
| key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
| restore-keys: ${{ runner.os }}-go- | ||
|
|
||
| - name: Install Go dependencies | ||
| run: go mod download | ||
|
|
||
| - name: Run interceptors e2e test | ||
| run: cd tests && go test -timeout 20m -v -race -tags=debug grpc_interceptors_test.go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,39 @@ | ||
| FROM --platform=${TARGETPLATFORM:-linux/amd64} golang:1.22-alpine as builder | ||
| FROM golang:1.26-alpine AS builder | ||
|
|
||
| ARG TARGETPLATFORM | ||
|
|
||
| ENV CGO_ENABLED=0 | ||
|
|
||
| WORKDIR /src | ||
| COPY protoc_plugins/ . | ||
| # https://buf.build/docs/bsr/remote-plugins/custom-plugins/#build-a-docker-image | ||
| RUN test "${TARGETPLATFORM}" = "linux/amd64" || (echo "buf plugin image must be built for linux/amd64" && exit 1) | ||
|
|
||
| COPY protoc_plugins/go.mod protoc_plugins/go.sum ./ | ||
| RUN go mod download | ||
| RUN go mod tidy | ||
|
|
||
| RUN go build -trimpath -ldflags "-s" -o protoc-gen-php-grpc-plugin protoc-gen-php-grpc/main.go | ||
| COPY protoc_plugins/ . | ||
|
|
||
| RUN go build -trimpath -ldflags "-s -w" -o /out/protoc-gen-php-grpc-plugin ./protoc-gen-php-grpc | ||
|
|
||
| FROM scratch | ||
|
|
||
| ARG APP_VERSION="" | ||
| # Supply real metadata at build time, for example: | ||
| # docker build --platform linux/amd64 --build-arg BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)" --build-arg APP_VERSION="vX.Y.Z" -f protoc_plugins/Dockerfile . | ||
| ARG APP_VERSION="unknown" | ||
| ARG BUILD_TIME="unknown" | ||
|
|
||
| # Runtime dependencies | ||
| LABEL org.opencontainers.image.title="protoc-gen-php-grpc" | ||
| LABEL org.opencontainers.image.description="protoc plugin for generating PHP gRPC service stubs" | ||
| LABEL org.opencontainers.image.url="https://roadrunner.dev" | ||
| LABEL org.opencontainers.image.source="https://github.com/roadrunner-server/grpc" | ||
| LABEL org.opencontainers.image.vendor="SpiralScout" | ||
| LABEL org.opencontainers.image.version="$APP_VERSION" | ||
| LABEL org.opencontainers.image.created="$BUILD_TIME" | ||
| LABEL org.opencontainers.image.version="${APP_VERSION}" | ||
| LABEL org.opencontainers.image.created="${BUILD_TIME}" | ||
| LABEL org.opencontainers.image.licenses="MIT" | ||
rustatian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| COPY --from=builder /src/protoc-gen-php-grpc-plugin / | ||
| COPY --from=builder --chown=65532:65532 /out/protoc-gen-php-grpc-plugin /protoc-gen-php-grpc-plugin | ||
|
|
||
| USER 65532:65532 | ||
|
|
||
| ENTRYPOINT ["/protoc-gen-php-grpc-plugin"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| version: '3' | ||
| rpc: | ||
| listen: "tcp://127.0.0.1:6011" | ||
| server: | ||
| command: "php php_test_files/worker-grpc.php" | ||
| relay: "pipes" | ||
| relay_timeout: "20s" | ||
| logs: | ||
| mode: development | ||
| level: error | ||
| grpc: | ||
| listen: "tcp://127.0.0.1:9011" | ||
| proto: | ||
| - "proto/service/service.proto" | ||
| interceptors: | ||
| - "interceptor1" | ||
rustatian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - "interceptor2" | ||
| max_send_msg_size: 50 | ||
| max_recv_msg_size: 50 | ||
| max_connection_idle: 0s | ||
| max_connection_age: 0s | ||
| max_connection_age_grace: 0s | ||
| max_concurrent_streams: 10 | ||
| ping_time: 1s | ||
| timeout: 200s | ||
| pool: | ||
| num_workers: 2 | ||
| max_jobs: 0 | ||
| allocate_timeout: 60s | ||
| destroy_timeout: 60s | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // Package grpc_test contains end-to-end tests for the gRPC plugin. | ||
| package grpc_test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package grpc_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "os" | ||
| "os/signal" | ||
| "strings" | ||
| "sync" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "tests/interceptor1" | ||
| "tests/interceptor2" | ||
| mocklogger "tests/mock" | ||
| "tests/proto/service" | ||
|
|
||
| "github.com/roadrunner-server/config/v5" | ||
| "github.com/roadrunner-server/endure/v2" | ||
| grpcPlugin "github.com/roadrunner-server/grpc/v5" | ||
| rpcPlugin "github.com/roadrunner-server/rpc/v5" | ||
| "github.com/roadrunner-server/server/v5" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| ) | ||
|
|
||
| func TestGrpcInterceptors(t *testing.T) { | ||
| cont := endure.New(slog.LevelDebug) | ||
|
|
||
| cfg := &config.Plugin{ | ||
| Version: "2023.3.0", | ||
| Path: "configs/.rr-grpc-rq-interceptors.yaml", | ||
| } | ||
|
|
||
| l, observed := mocklogger.ZapTestLogger(zap.DebugLevel) | ||
| err := cont.RegisterAll( | ||
| cfg, | ||
| &interceptor1.Plugin{}, | ||
| &interceptor2.Plugin{}, | ||
| &grpcPlugin.Plugin{}, | ||
| &rpcPlugin.Plugin{}, | ||
| &server.Plugin{}, | ||
| l, | ||
| ) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = cont.Init() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| ch, err := cont.Serve() | ||
| assert.NoError(t, err) | ||
|
|
||
| sig := make(chan os.Signal, 1) | ||
| signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
|
|
||
| wg := &sync.WaitGroup{} | ||
| wg.Add(1) | ||
|
|
||
| stopCh := make(chan struct{}, 1) | ||
|
|
||
| go func() { | ||
| defer wg.Done() | ||
| for { | ||
| select { | ||
| case e := <-ch: | ||
| assert.Fail(t, "error", e.Error.Error()) | ||
| err = cont.Stop() | ||
| if err != nil { | ||
| assert.FailNow(t, "error", err.Error()) | ||
| } | ||
| case <-sig: | ||
| err = cont.Stop() | ||
| if err != nil { | ||
| assert.FailNow(t, "error", err.Error()) | ||
| } | ||
| return | ||
| case <-stopCh: | ||
| err = cont.Stop() | ||
| if err != nil { | ||
| assert.FailNow(t, "error", err.Error()) | ||
| } | ||
| return | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| time.Sleep(time.Second) | ||
|
|
||
| conn, err := grpc.NewClient("127.0.0.1:9011", grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, conn) | ||
|
|
||
| client := service.NewEchoClient(conn) | ||
| resp, err := client.Ping(context.Background(), &service.Message{Msg: "hello"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "HELLO", resp.Msg) | ||
| _ = conn.Close() | ||
|
|
||
| stopCh <- struct{}{} | ||
| wg.Wait() | ||
|
|
||
| interceptor1Log := observed.FilterMessageSnippet("interceptor1 created marker") | ||
| require.Equal(t, 1, interceptor1Log.Len()) | ||
|
|
||
| interceptor2Log := observed.FilterMessageSnippet("interceptor2 received marker") | ||
| require.Equal(t, 1, interceptor2Log.Len()) | ||
|
|
||
| marker, ok := interceptor2Log.All()[0].ContextMap()["marker"].(string) | ||
| require.True(t, ok) | ||
| require.True(t, strings.HasPrefix(marker, interceptor1.MarkerPrefix)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // Package interceptor1 provides a simple test interceptor used in e2e tests. | ||
| package interceptor1 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.