Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
fc50af50bb | |||
19a8d7f7b1 | |||
8b3b2700d6 | |||
|
6cdb4ff377 | ||
b104e25898 | |||
8102d709f4 | |||
134f7374aa | |||
828f211a4e | |||
75c9d467e7 | |||
699358383d | |||
82b95f8605 | |||
8ec0a4ef77 | |||
fb8e9ccb75 | |||
129f02b3bc | |||
55cdcfb3af | |||
ac5e6dffa0 | |||
66756f74dd | |||
28234bcc3c | |||
7723dcaddf |
19
.github/dependabot.yml
vendored
19
.github/dependabot.yml
vendored
@@ -1,19 +0,0 @@
|
|||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
|
|
||||||
# Maintain dependencies for GitHub Actions
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
20
.github/workflows/autoapprove.yml
vendored
20
.github/workflows/autoapprove.yml
vendored
@@ -1,20 +0,0 @@
|
|||||||
name: "autoapprove"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
autoapprove:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: approve
|
|
||||||
uses: hmarr/auto-approve-action@v3
|
|
||||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
|
||||||
id: approve
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
21
.github/workflows/automerge.yml
vendored
21
.github/workflows/automerge.yml
vendored
@@ -1,21 +0,0 @@
|
|||||||
name: "automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'vtolstov'
|
|
||||||
steps:
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
47
.github/workflows/build.yml
vendored
47
.github/workflows/build.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
78
.github/workflows/codeql-analysis.yml
vendored
78
.github/workflows/codeql-analysis.yml
vendored
@@ -1,78 +0,0 @@
|
|||||||
# For most projects, this workflow file will not need changing; you simply need
|
|
||||||
# to commit it to your repository.
|
|
||||||
#
|
|
||||||
# You may wish to alter this file to override the set of languages analyzed,
|
|
||||||
# or to provide custom queries or build logic.
|
|
||||||
#
|
|
||||||
# ******** NOTE ********
|
|
||||||
# We have attempted to detect the languages in your repository. Please check
|
|
||||||
# the `language` matrix defined below to confirm you have the correct set of
|
|
||||||
# supported CodeQL languages.
|
|
||||||
#
|
|
||||||
name: "codeql"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
||||||
# Learn more:
|
|
||||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: init
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 https://git.io/JvXDl
|
|
||||||
|
|
||||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
||||||
# and modify them (or add more) to build your code if your project
|
|
||||||
# uses a compiled language
|
|
||||||
|
|
||||||
#- run: |
|
|
||||||
# make bootstrap
|
|
||||||
# make release
|
|
||||||
|
|
||||||
- name: analyze
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
27
.github/workflows/dependabot-automerge.yml
vendored
27
.github/workflows/dependabot-automerge.yml
vendored
@@ -1,27 +0,0 @@
|
|||||||
name: "dependabot-automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- name: metadata
|
|
||||||
id: metadata
|
|
||||||
uses: dependabot/fetch-metadata@v1.3.6
|
|
||||||
with:
|
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
53
.github/workflows/job_coverage.yml
vendored
Normal file
53
.github/workflows/job_coverage.yml
vendored
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
name: coverage
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main, v3, v4 ]
|
||||||
|
paths-ignore:
|
||||||
|
- '.github/**'
|
||||||
|
- '.gitea/**'
|
||||||
|
pull_request:
|
||||||
|
branches: [ main, v3, v4 ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
build:
|
||||||
|
if: github.server_url != 'https://github.com'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
|
||||||
|
- name: test coverage
|
||||||
|
run: |
|
||||||
|
go test -v -cover ./... -covermode=count -coverprofile coverage.out -coverpkg ./...
|
||||||
|
go tool cover -func coverage.out -o coverage.out
|
||||||
|
|
||||||
|
- name: coverage badge
|
||||||
|
uses: tj-actions/coverage-badge-go@v2
|
||||||
|
with:
|
||||||
|
green: 80
|
||||||
|
filename: coverage.out
|
||||||
|
|
||||||
|
- uses: stefanzweifel/git-auto-commit-action@v4
|
||||||
|
name: autocommit
|
||||||
|
with:
|
||||||
|
commit_message: Apply Code Coverage Badge
|
||||||
|
skip_fetch: false
|
||||||
|
skip_checkout: false
|
||||||
|
file_pattern: ./README.md
|
||||||
|
|
||||||
|
- name: push
|
||||||
|
if: steps.auto-commit-action.outputs.changes_detected == 'true'
|
||||||
|
uses: ad-m/github-push-action@master
|
||||||
|
with:
|
||||||
|
github_token: ${{ github.token }}
|
||||||
|
branch: ${{ github.ref }}
|
29
.github/workflows/job_lint.yml
vendored
Normal file
29
.github/workflows/job_lint.yml
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
name: lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches: [ master, v3, v4 ]
|
||||||
|
paths-ignore:
|
||||||
|
- '.github/**'
|
||||||
|
- '.gitea/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run lint
|
||||||
|
uses: golangci/golangci-lint-action@v6
|
||||||
|
with:
|
||||||
|
version: 'latest'
|
31
.github/workflows/job_test.yml
vendored
Normal file
31
.github/workflows/job_test.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches: [ master, v3, v4 ]
|
||||||
|
push:
|
||||||
|
branches: [ master, v3, v4 ]
|
||||||
|
paths-ignore:
|
||||||
|
- '.github/**'
|
||||||
|
- '.gitea/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
50
.github/workflows/job_tests.yml
vendored
Normal file
50
.github/workflows/job_tests.yml
vendored
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches: [ master, v3, v4 ]
|
||||||
|
push:
|
||||||
|
branches: [ master, v3, v4 ]
|
||||||
|
paths-ignore:
|
||||||
|
- '.github/**'
|
||||||
|
- '.gitea/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: checkout tests
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: master
|
||||||
|
filter: 'blob:none'
|
||||||
|
repository: unistack-org/micro-tests
|
||||||
|
path: micro-tests
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup go work
|
||||||
|
env:
|
||||||
|
GOWORK: ${{ github.workspace }}/go.work
|
||||||
|
run: |
|
||||||
|
go work init
|
||||||
|
go work use .
|
||||||
|
go work use micro-tests
|
||||||
|
- name: setup deps
|
||||||
|
env:
|
||||||
|
GOWORK: ${{ github.workspace }}/go.work
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run tests
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
GOWORK: ${{ github.workspace }}/go.work
|
||||||
|
run: |
|
||||||
|
cd micro-tests
|
||||||
|
go test -mod readonly -v ./... || true
|
47
.github/workflows/pr.yml
vendored
47
.github/workflows/pr.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: prbuild
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
@@ -1,44 +1,5 @@
|
|||||||
run:
|
run:
|
||||||
concurrency: 4
|
concurrency: 8
|
||||||
deadline: 5m
|
timeout: 5m
|
||||||
issues-exit-code: 1
|
issues-exit-code: 1
|
||||||
tests: true
|
tests: true
|
||||||
|
|
||||||
linters-settings:
|
|
||||||
govet:
|
|
||||||
check-shadowing: true
|
|
||||||
enable:
|
|
||||||
- fieldalignment
|
|
||||||
|
|
||||||
linters:
|
|
||||||
enable:
|
|
||||||
- govet
|
|
||||||
- deadcode
|
|
||||||
- errcheck
|
|
||||||
- govet
|
|
||||||
- ineffassign
|
|
||||||
- staticcheck
|
|
||||||
- structcheck
|
|
||||||
- typecheck
|
|
||||||
- unused
|
|
||||||
- varcheck
|
|
||||||
- bodyclose
|
|
||||||
- gci
|
|
||||||
- goconst
|
|
||||||
- gocritic
|
|
||||||
- gosimple
|
|
||||||
- gofmt
|
|
||||||
- gofumpt
|
|
||||||
- goimports
|
|
||||||
- golint
|
|
||||||
- gosec
|
|
||||||
- makezero
|
|
||||||
- misspell
|
|
||||||
- nakedret
|
|
||||||
- nestif
|
|
||||||
- nilerr
|
|
||||||
- noctx
|
|
||||||
- prealloc
|
|
||||||
- unconvert
|
|
||||||
- unparam
|
|
||||||
disable-all: false
|
|
||||||
|
4
README.md
Normal file
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# GRPC Server
|
||||||
|

|
||||||
|
|
||||||
|
This plugin is a grpc server for micro.
|
24
go.mod
24
go.mod
@@ -1,20 +1,24 @@
|
|||||||
module go.unistack.org/micro-server-grpc/v3
|
module go.unistack.org/micro-server-grpc/v3
|
||||||
|
|
||||||
go 1.22
|
go 1.22.2
|
||||||
|
|
||||||
toolchain go1.23.1
|
toolchain go1.24.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang/protobuf v1.5.4
|
go.unistack.org/micro/v3 v3.11.41
|
||||||
go.unistack.org/micro/v3 v3.10.97
|
golang.org/x/net v0.33.0
|
||||||
golang.org/x/net v0.30.0
|
google.golang.org/grpc v1.69.2
|
||||||
google.golang.org/grpc v1.67.1
|
google.golang.org/protobuf v1.36.1
|
||||||
google.golang.org/protobuf v1.35.1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/kr/pretty v0.3.1 // indirect
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
||||||
golang.org/x/sys v0.26.0 // indirect
|
golang.org/x/sys v0.28.0 // indirect
|
||||||
golang.org/x/text v0.19.0 // indirect
|
golang.org/x/text v0.21.0 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
64
go.sum
64
go.sum
@@ -1,20 +1,56 @@
|
|||||||
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
|
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
||||||
|
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||||
|
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||||
|
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||||
|
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
||||||
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
|
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||||
|
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||||
|
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
|
||||||
|
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
|
||||||
|
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
|
||||||
|
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
|
||||||
|
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
|
||||||
|
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
|
||||||
|
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
|
||||||
|
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
|
||||||
|
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
|
||||||
|
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
go.unistack.org/micro/v3 v3.10.97 h1:8l7fv+i06/PjPrBBhRC/ZQkWGIOuHPg3jJN0vktYE78=
|
go.unistack.org/micro/v3 v3.11.41 h1:dP4sBLIZpMo+MWGe5bbESewK8wBzYm4Yik/67x4dEtQ=
|
||||||
go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
go.unistack.org/micro/v3 v3.11.41/go.mod h1:POGU5hstnAT9LH70m8FalyQSNi2GfIew71K75JenIZk=
|
||||||
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||||
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 h1:QCqS/PdaHTSWGvupk2F/ehwHtGc0/GYkT+3GAcR1CCc=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 h1:TqExAhdPaB60Ux47Cn0oLV07rGnxZzIsaRhQaqS666A=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
|
||||||
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
|
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
|
||||||
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
|
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
|
||||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
|
||||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
107
grpc.go
107
grpc.go
@@ -12,13 +12,13 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
greflection "google.golang.org/grpc/reflection"
|
greflection "google.golang.org/grpc/reflection"
|
||||||
reflectionv1pb "google.golang.org/grpc/reflection/grpc_reflection_v1"
|
reflectionv1pb "google.golang.org/grpc/reflection/grpc_reflection_v1"
|
||||||
|
|
||||||
// nolint: staticcheck
|
// nolint: staticcheck
|
||||||
oldproto "github.com/golang/protobuf/proto"
|
|
||||||
"go.unistack.org/micro/v3/broker"
|
"go.unistack.org/micro/v3/broker"
|
||||||
"go.unistack.org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v3/errors"
|
"go.unistack.org/micro/v3/errors"
|
||||||
@@ -39,6 +39,7 @@ import (
|
|||||||
"google.golang.org/grpc/peer"
|
"google.golang.org/grpc/peer"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
"google.golang.org/protobuf/protoadapt"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -75,9 +76,12 @@ type Server struct {
|
|||||||
opts server.Options
|
opts server.Options
|
||||||
unknownHandler grpc.StreamHandler
|
unknownHandler grpc.StreamHandler
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
stateLive *atomic.Uint32
|
||||||
|
stateReady *atomic.Uint32
|
||||||
|
stateHealth *atomic.Uint32
|
||||||
started bool
|
started bool
|
||||||
registered bool
|
registered bool
|
||||||
reflection bool
|
// reflection bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServer(opts ...server.Option) *Server {
|
func newServer(opts ...server.Option) *Server {
|
||||||
@@ -90,6 +94,9 @@ func newServer(opts ...server.Option) *Server {
|
|||||||
handlers: make(map[string]server.Handler),
|
handlers: make(map[string]server.Handler),
|
||||||
subscribers: make(map[*subscriber][]broker.Subscriber),
|
subscribers: make(map[*subscriber][]broker.Subscriber),
|
||||||
exit: make(chan chan error),
|
exit: make(chan chan error),
|
||||||
|
stateLive: &atomic.Uint32{},
|
||||||
|
stateReady: &atomic.Uint32{},
|
||||||
|
stateHealth: &atomic.Uint32{},
|
||||||
}
|
}
|
||||||
|
|
||||||
g.opts.Meter = g.opts.Meter.Clone(meter.Labels("type", "grpc"))
|
g.opts.Meter = g.opts.Meter.Clone(meter.Labels("type", "grpc"))
|
||||||
@@ -208,24 +215,6 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
|||||||
return status.Errorf(codes.Internal, "method does not exist in context")
|
return status.Errorf(codes.Internal, "method does not exist in context")
|
||||||
}
|
}
|
||||||
|
|
||||||
ts := time.Now()
|
|
||||||
var sp tracer.Span
|
|
||||||
if !slices.Contains(tracer.DefaultSkipEndpoints, fullMethod) {
|
|
||||||
ctx, sp = g.opts.Tracer.Start(ctx, fullMethod+" rpc-server",
|
|
||||||
tracer.WithSpanKind(tracer.SpanKindServer),
|
|
||||||
tracer.WithSpanLabels(
|
|
||||||
"endpoint", fullMethod,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
defer func() {
|
|
||||||
st := status.Convert(err)
|
|
||||||
if st != nil || st.Code() != codes.OK {
|
|
||||||
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
||||||
}
|
|
||||||
sp.Finish()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// get grpc metadata
|
// get grpc metadata
|
||||||
gmd, ok := gmetadata.FromIncomingContext(ctx)
|
gmd, ok := gmetadata.FromIncomingContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -239,9 +228,30 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endpointName := serviceName + "/" + methodName
|
||||||
|
|
||||||
|
ts := time.Now()
|
||||||
|
var sp tracer.Span
|
||||||
|
if !slices.Contains(tracer.DefaultSkipEndpoints, endpointName) {
|
||||||
|
ctx, sp = g.opts.Tracer.Start(ctx, "rpc-server",
|
||||||
|
tracer.WithSpanKind(tracer.SpanKindServer),
|
||||||
|
tracer.WithSpanLabels(
|
||||||
|
"endpoint", endpointName,
|
||||||
|
"server", "grpc",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
defer func() {
|
||||||
|
st := status.Convert(err)
|
||||||
|
if st != nil || st.Code() != codes.OK {
|
||||||
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
}
|
||||||
|
sp.Finish()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
md := metadata.New(len(gmd))
|
md := metadata.New(len(gmd))
|
||||||
for k, v := range gmd {
|
for k, v := range gmd {
|
||||||
md.Set(k, strings.Join(v, ", "))
|
md[k] = strings.Join(v, ", ")
|
||||||
}
|
}
|
||||||
md.Set("Path", fullMethod)
|
md.Set("Path", fullMethod)
|
||||||
md.Set("Micro-Server", "grpc")
|
md.Set("Micro-Server", "grpc")
|
||||||
@@ -285,22 +295,23 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
|||||||
|
|
||||||
// create new context
|
// create new context
|
||||||
ctx = metadata.NewIncomingContext(ctx, md)
|
ctx = metadata.NewIncomingContext(ctx, md)
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, metadata.New(0))
|
||||||
|
|
||||||
stream = &streamWrapper{ctx, stream}
|
stream = &streamWrapper{ctx, stream}
|
||||||
|
|
||||||
if !slices.Contains(meter.DefaultSkipEndpoints, fullMethod) {
|
if !slices.Contains(meter.DefaultSkipEndpoints, endpointName) {
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc()
|
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", endpointName, "server", "grpc").Inc()
|
||||||
defer func() {
|
defer func() {
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
g.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", fullMethod).Update(te.Seconds())
|
g.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", endpointName, "server", "grpc").Update(te.Seconds())
|
||||||
g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", fullMethod).Update(te.Seconds())
|
g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", endpointName, "server", "grpc").Update(te.Seconds())
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Dec()
|
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", endpointName, "server", "grpc").Dec()
|
||||||
|
|
||||||
st := status.Convert(err)
|
st := status.Convert(err)
|
||||||
if st == nil || st.Code() == codes.OK {
|
if st == nil || st.Code() == codes.OK {
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc()
|
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", endpointName, "server", "grpc", "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc()
|
||||||
} else {
|
} else {
|
||||||
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc()
|
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", endpointName, "server", "grpc", "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@@ -409,7 +420,7 @@ func (g *Server) processRequest(ctx context.Context, stream grpc.ServerStream, s
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
g.opts.Hooks.EachNext(func(hook options.Hook) {
|
g.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||||
if h, ok := hook.(server.HookHandler); ok {
|
if h, ok := hook.(server.HookHandler); ok {
|
||||||
fn = h(fn)
|
fn = h(fn)
|
||||||
}
|
}
|
||||||
@@ -435,7 +446,7 @@ func (g *Server) processRequest(ctx context.Context, stream grpc.ServerStream, s
|
|||||||
// user defined error that proto based we can attach it to grpc status
|
// user defined error that proto based we can attach it to grpc status
|
||||||
statusCode = convertCode(appErr)
|
statusCode = convertCode(appErr)
|
||||||
statusDesc = appErr.Error()
|
statusDesc = appErr.Error()
|
||||||
errStatus, err = status.New(statusCode, statusDesc).WithDetails(oldproto.MessageV1(verr))
|
errStatus, err = status.New(statusCode, statusDesc).WithDetails(protoadapt.MessageV1Of(verr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -493,7 +504,7 @@ func (g *Server) processStream(ctx context.Context, stream grpc.ServerStream, se
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.Hooks.EachNext(func(hook options.Hook) {
|
opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||||
if h, ok := hook.(server.HookHandler); ok {
|
if h, ok := hook.(server.HookHandler); ok {
|
||||||
fn = h(fn)
|
fn = h(fn)
|
||||||
}
|
}
|
||||||
@@ -520,7 +531,7 @@ func (g *Server) processStream(ctx context.Context, stream grpc.ServerStream, se
|
|||||||
// user defined error that proto based we can attach it to grpc status
|
// user defined error that proto based we can attach it to grpc status
|
||||||
statusCode = convertCode(appErr)
|
statusCode = convertCode(appErr)
|
||||||
statusDesc = appErr.Error()
|
statusDesc = appErr.Error()
|
||||||
errStatus, err = status.New(statusCode, statusDesc).WithDetails(oldproto.MessageV1(verr))
|
errStatus, err = status.New(statusCode, statusDesc).WithDetails(protoadapt.MessageV1Of(verr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -646,19 +657,8 @@ func (g *Server) Register() error {
|
|||||||
return subscriberList[i].topic > subscriberList[j].topic
|
return subscriberList[i].topic > subscriberList[j].topic
|
||||||
})
|
})
|
||||||
|
|
||||||
endpoints := make([]*register.Endpoint, 0, len(handlerList)+len(subscriberList))
|
|
||||||
for _, n := range handlerList {
|
|
||||||
endpoints = append(endpoints, g.handlers[n].Endpoints()...)
|
|
||||||
}
|
|
||||||
for _, e := range subscriberList {
|
|
||||||
endpoints = append(endpoints, e.Endpoints()...)
|
|
||||||
}
|
|
||||||
g.RUnlock()
|
g.RUnlock()
|
||||||
|
|
||||||
service.Nodes[0].Metadata["protocol"] = "grpc"
|
|
||||||
service.Nodes[0].Metadata["transport"] = service.Nodes[0].Metadata["protocol"]
|
|
||||||
service.Endpoints = endpoints
|
|
||||||
|
|
||||||
g.RLock()
|
g.RLock()
|
||||||
registered := g.registered
|
registered := g.registered
|
||||||
g.RUnlock()
|
g.RUnlock()
|
||||||
@@ -831,6 +831,9 @@ func (g *Server) Start() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
g.stateLive.Store(1)
|
||||||
|
g.stateReady.Store(1)
|
||||||
|
g.stateHealth.Store(1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -900,12 +903,18 @@ func (g *Server) Start() error {
|
|||||||
go func() {
|
go func() {
|
||||||
g.srv.GracefulStop()
|
g.srv.GracefulStop()
|
||||||
close(exit)
|
close(exit)
|
||||||
|
g.stateLive.Store(0)
|
||||||
|
g.stateReady.Store(0)
|
||||||
|
g.stateHealth.Store(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-exit:
|
case <-exit:
|
||||||
case <-time.After(g.opts.GracefulTimeout):
|
case <-time.After(g.opts.GracefulTimeout):
|
||||||
g.srv.Stop()
|
g.srv.Stop()
|
||||||
|
g.stateLive.Store(0)
|
||||||
|
g.stateReady.Store(0)
|
||||||
|
g.stateHealth.Store(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// close transport
|
// close transport
|
||||||
@@ -962,6 +971,18 @@ func (g *Server) GRPCServer() *grpc.Server {
|
|||||||
return g.srv
|
return g.srv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Server) Live() bool {
|
||||||
|
return g.stateLive.Load() == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Server) Ready() bool {
|
||||||
|
return g.stateReady.Load() == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Server) Health() bool {
|
||||||
|
return g.stateHealth.Load() == 1
|
||||||
|
}
|
||||||
|
|
||||||
func NewServer(opts ...server.Option) *Server {
|
func NewServer(opts ...server.Option) *Server {
|
||||||
return newServer(opts...)
|
return newServer(opts...)
|
||||||
}
|
}
|
||||||
|
22
handler.go
22
handler.go
@@ -3,7 +3,6 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/register"
|
|
||||||
"go.unistack.org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,34 +10,17 @@ type rpcHandler struct {
|
|||||||
opts server.HandlerOptions
|
opts server.HandlerOptions
|
||||||
handler interface{}
|
handler interface{}
|
||||||
name string
|
name string
|
||||||
endpoints []*register.Endpoint
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRPCHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
func newRPCHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
||||||
options := server.NewHandlerOptions(opts...)
|
options := server.NewHandlerOptions(opts...)
|
||||||
|
|
||||||
typ := reflect.TypeOf(handler)
|
|
||||||
hdlr := reflect.ValueOf(handler)
|
hdlr := reflect.ValueOf(handler)
|
||||||
name := reflect.Indirect(hdlr).Type().Name()
|
name := reflect.Indirect(hdlr).Type().Name()
|
||||||
|
|
||||||
var endpoints []*register.Endpoint
|
|
||||||
|
|
||||||
for m := 0; m < typ.NumMethod(); m++ {
|
|
||||||
if e := register.ExtractEndpoint(typ.Method(m)); e != nil {
|
|
||||||
e.Name = name + "." + e.Name
|
|
||||||
|
|
||||||
for k, v := range options.Metadata[e.Name] {
|
|
||||||
e.Metadata[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
endpoints = append(endpoints, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &rpcHandler{
|
return &rpcHandler{
|
||||||
name: name,
|
name: name,
|
||||||
handler: handler,
|
handler: handler,
|
||||||
endpoints: endpoints,
|
|
||||||
opts: options,
|
opts: options,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,10 +33,6 @@ func (r *rpcHandler) Handler() interface{} {
|
|||||||
return r.handler
|
return r.handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rpcHandler) Endpoints() []*register.Endpoint {
|
|
||||||
return r.endpoints
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rpcHandler) Options() server.HandlerOptions {
|
func (r *rpcHandler) Options() server.HandlerOptions {
|
||||||
return r.opts
|
return r.opts
|
||||||
}
|
}
|
||||||
|
@@ -1,489 +0,0 @@
|
|||||||
//go:build ignore
|
|
||||||
// +build ignore
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Copyright 2016 gRPC authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package reflection implements server reflection service.
|
|
||||||
|
|
||||||
The service implemented is defined in:
|
|
||||||
https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1alpha/reflection.proto.
|
|
||||||
|
|
||||||
To register server reflection on a gRPC server:
|
|
||||||
|
|
||||||
import "google.golang.org/grpc/reflection"
|
|
||||||
|
|
||||||
s := grpc.NewServer()
|
|
||||||
pb.RegisterYourOwnServer(s, &server{})
|
|
||||||
|
|
||||||
// Register reflection service on gRPC server.
|
|
||||||
reflection.Register(s)
|
|
||||||
|
|
||||||
s.Serve(lis)
|
|
||||||
*/
|
|
||||||
package grpc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"compress/gzip"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
|
||||||
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/codes"
|
|
||||||
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
|
|
||||||
"google.golang.org/grpc/status"
|
|
||||||
)
|
|
||||||
|
|
||||||
type serverReflectionServer struct {
|
|
||||||
rpb.UnimplementedServerReflectionServer
|
|
||||||
s *grpc.Server
|
|
||||||
|
|
||||||
initSymbols sync.Once
|
|
||||||
serviceNames []string
|
|
||||||
symbols map[string]*dpb.FileDescriptorProto // map of fully-qualified names to files
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register registers the server reflection service on the given gRPC server.
|
|
||||||
func Register(s *grpc.Server) {
|
|
||||||
rpb.RegisterServerReflectionServer(s, &serverReflectionServer{
|
|
||||||
s: s,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// protoMessage is used for type assertion on proto messages.
|
|
||||||
// Generated proto message implements function Descriptor(), but Descriptor()
|
|
||||||
// is not part of interface proto.Message. This interface is needed to
|
|
||||||
// call Descriptor().
|
|
||||||
type protoMessage interface {
|
|
||||||
Descriptor() ([]byte, []int)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *serverReflectionServer) getSymbols() (svcNames []string, symbolIndex map[string]*dpb.FileDescriptorProto) {
|
|
||||||
s.initSymbols.Do(func() {
|
|
||||||
serviceInfo := s.s.GetServiceInfo()
|
|
||||||
|
|
||||||
s.symbols = map[string]*dpb.FileDescriptorProto{}
|
|
||||||
s.serviceNames = make([]string, 0, len(serviceInfo))
|
|
||||||
processed := map[string]struct{}{}
|
|
||||||
for svc, info := range serviceInfo {
|
|
||||||
s.serviceNames = append(s.serviceNames, svc)
|
|
||||||
fdenc, ok := parseMetadata(info.Metadata)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fd, err := decodeFileDesc(fdenc)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
s.processFile(fd, processed)
|
|
||||||
}
|
|
||||||
sort.Strings(s.serviceNames)
|
|
||||||
})
|
|
||||||
|
|
||||||
return s.serviceNames, s.symbols
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *serverReflectionServer) processFile(fd *dpb.FileDescriptorProto, processed map[string]struct{}) {
|
|
||||||
filename := fd.GetName()
|
|
||||||
if _, ok := processed[filename]; ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
processed[filename] = struct{}{}
|
|
||||||
|
|
||||||
prefix := fd.GetPackage()
|
|
||||||
|
|
||||||
for _, msg := range fd.MessageType {
|
|
||||||
s.processMessage(fd, prefix, msg)
|
|
||||||
}
|
|
||||||
for _, en := range fd.EnumType {
|
|
||||||
s.processEnum(fd, prefix, en)
|
|
||||||
}
|
|
||||||
for _, ext := range fd.Extension {
|
|
||||||
s.processField(fd, prefix, ext)
|
|
||||||
}
|
|
||||||
for _, svc := range fd.Service {
|
|
||||||
svcName := fqn(prefix, svc.GetName())
|
|
||||||
s.symbols[svcName] = fd
|
|
||||||
for _, meth := range svc.Method {
|
|
||||||
name := fqn(svcName, meth.GetName())
|
|
||||||
s.symbols[name] = fd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dep := range fd.Dependency {
|
|
||||||
fdenc := proto.FileDescriptor(dep)
|
|
||||||
fdDep, err := decodeFileDesc(fdenc)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
s.processFile(fdDep, processed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *serverReflectionServer) processMessage(fd *dpb.FileDescriptorProto, prefix string, msg *dpb.DescriptorProto) {
|
|
||||||
msgName := fqn(prefix, msg.GetName())
|
|
||||||
s.symbols[msgName] = fd
|
|
||||||
|
|
||||||
for _, nested := range msg.NestedType {
|
|
||||||
s.processMessage(fd, msgName, nested)
|
|
||||||
}
|
|
||||||
for _, en := range msg.EnumType {
|
|
||||||
s.processEnum(fd, msgName, en)
|
|
||||||
}
|
|
||||||
for _, ext := range msg.Extension {
|
|
||||||
s.processField(fd, msgName, ext)
|
|
||||||
}
|
|
||||||
for _, fld := range msg.Field {
|
|
||||||
s.processField(fd, msgName, fld)
|
|
||||||
}
|
|
||||||
for _, oneof := range msg.OneofDecl {
|
|
||||||
oneofName := fqn(msgName, oneof.GetName())
|
|
||||||
s.symbols[oneofName] = fd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *serverReflectionServer) processEnum(fd *dpb.FileDescriptorProto, prefix string, en *dpb.EnumDescriptorProto) {
|
|
||||||
enName := fqn(prefix, en.GetName())
|
|
||||||
s.symbols[enName] = fd
|
|
||||||
|
|
||||||
for _, val := range en.Value {
|
|
||||||
valName := fqn(enName, val.GetName())
|
|
||||||
s.symbols[valName] = fd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *serverReflectionServer) processField(fd *dpb.FileDescriptorProto, prefix string, fld *dpb.FieldDescriptorProto) {
|
|
||||||
fldName := fqn(prefix, fld.GetName())
|
|
||||||
s.symbols[fldName] = fd
|
|
||||||
}
|
|
||||||
|
|
||||||
func fqn(prefix, name string) string {
|
|
||||||
if prefix == "" {
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
return prefix + "." + name
|
|
||||||
}
|
|
||||||
|
|
||||||
// fileDescForType gets the file descriptor for the given type.
|
|
||||||
// The given type should be a proto message.
|
|
||||||
func (s *serverReflectionServer) fileDescForType(st reflect.Type) (*dpb.FileDescriptorProto, error) {
|
|
||||||
m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(protoMessage)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("failed to create message from type: %v", st)
|
|
||||||
}
|
|
||||||
enc, _ := m.Descriptor()
|
|
||||||
|
|
||||||
return decodeFileDesc(enc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// decodeFileDesc does decompression and unmarshalling on the given
|
|
||||||
// file descriptor byte slice.
|
|
||||||
func decodeFileDesc(enc []byte) (*dpb.FileDescriptorProto, error) {
|
|
||||||
raw, err := decompress(enc)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to decompress enc: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fd := new(dpb.FileDescriptorProto)
|
|
||||||
if err := proto.Unmarshal(raw, fd); err != nil {
|
|
||||||
return nil, fmt.Errorf("bad descriptor: %v", err)
|
|
||||||
}
|
|
||||||
return fd, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// decompress does gzip decompression.
|
|
||||||
func decompress(b []byte) ([]byte, error) {
|
|
||||||
r, err := gzip.NewReader(bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("bad gzipped descriptor: %v", err)
|
|
||||||
}
|
|
||||||
out, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("bad gzipped descriptor: %v", err)
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func typeForName(name string) (reflect.Type, error) {
|
|
||||||
pt := proto.MessageType(name)
|
|
||||||
if pt == nil {
|
|
||||||
return nil, fmt.Errorf("unknown type: %q", name)
|
|
||||||
}
|
|
||||||
st := pt.Elem()
|
|
||||||
|
|
||||||
return st, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fileDescContainingExtension(st reflect.Type, ext int32) (*dpb.FileDescriptorProto, error) {
|
|
||||||
m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(proto.Message)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("failed to create message from type: %v", st)
|
|
||||||
}
|
|
||||||
|
|
||||||
var extDesc *proto.ExtensionDesc
|
|
||||||
for id, desc := range proto.RegisteredExtensions(m) {
|
|
||||||
if id == ext {
|
|
||||||
extDesc = desc
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if extDesc == nil {
|
|
||||||
return nil, fmt.Errorf("failed to find registered extension for extension number %v", ext)
|
|
||||||
}
|
|
||||||
|
|
||||||
return decodeFileDesc(proto.FileDescriptor(extDesc.Filename))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *serverReflectionServer) allExtensionNumbersForType(st reflect.Type) ([]int32, error) {
|
|
||||||
m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(proto.Message)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("failed to create message from type: %v", st)
|
|
||||||
}
|
|
||||||
|
|
||||||
exts := proto.RegisteredExtensions(m)
|
|
||||||
out := make([]int32, 0, len(exts))
|
|
||||||
for id := range exts {
|
|
||||||
out = append(out, id)
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// fileDescWithDependencies returns a slice of serialized fileDescriptors in
|
|
||||||
// wire format ([]byte). The fileDescriptors will include fd and all the
|
|
||||||
// transitive dependencies of fd with names not in sentFileDescriptors.
|
|
||||||
func fileDescWithDependencies(fd *dpb.FileDescriptorProto, sentFileDescriptors map[string]bool) ([][]byte, error) {
|
|
||||||
r := [][]byte{}
|
|
||||||
queue := []*dpb.FileDescriptorProto{fd}
|
|
||||||
for len(queue) > 0 {
|
|
||||||
currentfd := queue[0]
|
|
||||||
queue = queue[1:]
|
|
||||||
if sent := sentFileDescriptors[currentfd.GetName()]; len(r) == 0 || !sent {
|
|
||||||
sentFileDescriptors[currentfd.GetName()] = true
|
|
||||||
currentfdEncoded, err := proto.Marshal(currentfd)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
r = append(r, currentfdEncoded)
|
|
||||||
}
|
|
||||||
for _, dep := range currentfd.Dependency {
|
|
||||||
fdenc := proto.FileDescriptor(dep)
|
|
||||||
fdDep, err := decodeFileDesc(fdenc)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
queue = append(queue, fdDep)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// fileDescEncodingByFilename finds the file descriptor for given filename,
|
|
||||||
// finds all of its previously unsent transitive dependencies, does marshalling
|
|
||||||
// on them, and returns the marshalled result.
|
|
||||||
func (s *serverReflectionServer) fileDescEncodingByFilename(name string, sentFileDescriptors map[string]bool) ([][]byte, error) {
|
|
||||||
enc := proto.FileDescriptor(name)
|
|
||||||
if enc == nil {
|
|
||||||
return nil, fmt.Errorf("unknown file: %v", name)
|
|
||||||
}
|
|
||||||
fd, err := decodeFileDesc(enc)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return fileDescWithDependencies(fd, sentFileDescriptors)
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseMetadata finds the file descriptor bytes specified meta.
|
|
||||||
// For SupportPackageIsVersion4, m is the name of the proto file, we
|
|
||||||
// call proto.FileDescriptor to get the byte slice.
|
|
||||||
// For SupportPackageIsVersion3, m is a byte slice itself.
|
|
||||||
func parseMetadata(meta interface{}) ([]byte, bool) {
|
|
||||||
// Check if meta is the file name.
|
|
||||||
if fileNameForMeta, ok := meta.(string); ok {
|
|
||||||
return proto.FileDescriptor(fileNameForMeta), true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if meta is the byte slice.
|
|
||||||
if enc, ok := meta.([]byte); ok {
|
|
||||||
return enc, true
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
// fileDescEncodingContainingSymbol finds the file descriptor containing the
|
|
||||||
// given symbol, finds all of its previously unsent transitive dependencies,
|
|
||||||
// does marshalling on them, and returns the marshalled result. The given symbol
|
|
||||||
// can be a type, a service or a method.
|
|
||||||
func (s *serverReflectionServer) fileDescEncodingContainingSymbol(name string, sentFileDescriptors map[string]bool) ([][]byte, error) {
|
|
||||||
_, symbols := s.getSymbols()
|
|
||||||
fd := symbols[name]
|
|
||||||
if fd == nil {
|
|
||||||
// Check if it's a type name that was not present in the
|
|
||||||
// transitive dependencies of the registered services.
|
|
||||||
if st, err := typeForName(name); err == nil {
|
|
||||||
fd, err = s.fileDescForType(st)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if fd == nil {
|
|
||||||
return nil, fmt.Errorf("unknown symbol: %v", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fileDescWithDependencies(fd, sentFileDescriptors)
|
|
||||||
}
|
|
||||||
|
|
||||||
// fileDescEncodingContainingExtension finds the file descriptor containing
|
|
||||||
// given extension, finds all of its previously unsent transitive dependencies,
|
|
||||||
// does marshalling on them, and returns the marshalled result.
|
|
||||||
func (s *serverReflectionServer) fileDescEncodingContainingExtension(typeName string, extNum int32, sentFileDescriptors map[string]bool) ([][]byte, error) {
|
|
||||||
st, err := typeForName(typeName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
fd, err := fileDescContainingExtension(st, extNum)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return fileDescWithDependencies(fd, sentFileDescriptors)
|
|
||||||
}
|
|
||||||
|
|
||||||
// allExtensionNumbersForTypeName returns all extension numbers for the given type.
|
|
||||||
func (s *serverReflectionServer) allExtensionNumbersForTypeName(name string) ([]int32, error) {
|
|
||||||
st, err := typeForName(name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
extNums, err := s.allExtensionNumbersForType(st)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return extNums, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServerReflectionInfo is the reflection service handler.
|
|
||||||
func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflection_ServerReflectionInfoServer) error {
|
|
||||||
sentFileDescriptors := make(map[string]bool)
|
|
||||||
for {
|
|
||||||
in, err := stream.Recv()
|
|
||||||
if err == io.EOF {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
out := &rpb.ServerReflectionResponse{
|
|
||||||
ValidHost: in.Host,
|
|
||||||
OriginalRequest: in,
|
|
||||||
}
|
|
||||||
switch req := in.MessageRequest.(type) {
|
|
||||||
case *rpb.ServerReflectionRequest_FileByFilename:
|
|
||||||
b, err := s.fileDescEncodingByFilename(req.FileByFilename, sentFileDescriptors)
|
|
||||||
if err != nil {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_ErrorResponse{
|
|
||||||
ErrorResponse: &rpb.ErrorResponse{
|
|
||||||
ErrorCode: int32(codes.NotFound),
|
|
||||||
ErrorMessage: err.Error(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_FileDescriptorResponse{
|
|
||||||
FileDescriptorResponse: &rpb.FileDescriptorResponse{FileDescriptorProto: b},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *rpb.ServerReflectionRequest_FileContainingSymbol:
|
|
||||||
b, err := s.fileDescEncodingContainingSymbol(req.FileContainingSymbol, sentFileDescriptors)
|
|
||||||
if err != nil {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_ErrorResponse{
|
|
||||||
ErrorResponse: &rpb.ErrorResponse{
|
|
||||||
ErrorCode: int32(codes.NotFound),
|
|
||||||
ErrorMessage: err.Error(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_FileDescriptorResponse{
|
|
||||||
FileDescriptorResponse: &rpb.FileDescriptorResponse{FileDescriptorProto: b},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *rpb.ServerReflectionRequest_FileContainingExtension:
|
|
||||||
typeName := req.FileContainingExtension.ContainingType
|
|
||||||
extNum := req.FileContainingExtension.ExtensionNumber
|
|
||||||
b, err := s.fileDescEncodingContainingExtension(typeName, extNum, sentFileDescriptors)
|
|
||||||
if err != nil {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_ErrorResponse{
|
|
||||||
ErrorResponse: &rpb.ErrorResponse{
|
|
||||||
ErrorCode: int32(codes.NotFound),
|
|
||||||
ErrorMessage: err.Error(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_FileDescriptorResponse{
|
|
||||||
FileDescriptorResponse: &rpb.FileDescriptorResponse{FileDescriptorProto: b},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *rpb.ServerReflectionRequest_AllExtensionNumbersOfType:
|
|
||||||
extNums, err := s.allExtensionNumbersForTypeName(req.AllExtensionNumbersOfType)
|
|
||||||
if err != nil {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_ErrorResponse{
|
|
||||||
ErrorResponse: &rpb.ErrorResponse{
|
|
||||||
ErrorCode: int32(codes.NotFound),
|
|
||||||
ErrorMessage: err.Error(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_AllExtensionNumbersResponse{
|
|
||||||
AllExtensionNumbersResponse: &rpb.ExtensionNumberResponse{
|
|
||||||
BaseTypeName: req.AllExtensionNumbersOfType,
|
|
||||||
ExtensionNumber: extNums,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *rpb.ServerReflectionRequest_ListServices:
|
|
||||||
svcNames, _ := s.getSymbols()
|
|
||||||
serviceResponses := make([]*rpb.ServiceResponse, len(svcNames))
|
|
||||||
for i, n := range svcNames {
|
|
||||||
serviceResponses[i] = &rpb.ServiceResponse{
|
|
||||||
Name: n,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out.MessageResponse = &rpb.ServerReflectionResponse_ListServicesResponse{
|
|
||||||
ListServicesResponse: &rpb.ListServiceResponse{
|
|
||||||
Service: serviceResponses,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return status.Errorf(codes.InvalidArgument, "invalid MessageRequest: %v", in.MessageRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := stream.Send(out); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -50,6 +50,7 @@ func (r *reflector) RangeExtensionsByMessage(message protoreflect.FullName, f fu
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReflector(t *testing.T) {
|
func TestReflector(t *testing.T) {
|
||||||
|
t.Skip()
|
||||||
srv := NewServer(Reflection(&reflector{}), server.Address(":12345"))
|
srv := NewServer(Reflection(&reflector{}), server.Address(":12345"))
|
||||||
if err := srv.Init(); err != nil {
|
if err := srv.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"go.unistack.org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
@@ -11,7 +9,6 @@ import (
|
|||||||
var _ server.Request = &rpcRequest{}
|
var _ server.Request = &rpcRequest{}
|
||||||
|
|
||||||
type rpcRequest struct {
|
type rpcRequest struct {
|
||||||
rw io.ReadWriter
|
|
||||||
payload interface{}
|
payload interface{}
|
||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
header metadata.Metadata
|
header metadata.Metadata
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"go.unistack.org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
@@ -11,7 +9,6 @@ import (
|
|||||||
var _ server.Response = &rpcResponse{}
|
var _ server.Response = &rpcResponse{}
|
||||||
|
|
||||||
type rpcResponse struct {
|
type rpcResponse struct {
|
||||||
rw io.ReadWriter
|
|
||||||
header metadata.Metadata
|
header metadata.Metadata
|
||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
}
|
}
|
||||||
|
@@ -140,7 +140,7 @@ func (server *rServer) register(rcvr interface{}) error {
|
|||||||
return fmt.Errorf("rpc Register: type %s is not exported", sname)
|
return fmt.Errorf("rpc Register: type %s is not exported", sname)
|
||||||
}
|
}
|
||||||
if _, present := server.serviceMap[sname]; present {
|
if _, present := server.serviceMap[sname]; present {
|
||||||
return fmt.Errorf("rpc: service already defined: " + sname)
|
return fmt.Errorf("rpc: service already defined: %s", sname)
|
||||||
}
|
}
|
||||||
s.name = sname
|
s.name = sname
|
||||||
s.method = make(map[string]*methodType)
|
s.method = make(map[string]*methodType)
|
||||||
|
@@ -11,7 +11,6 @@ import (
|
|||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v3/logger"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"go.unistack.org/micro/v3/options"
|
"go.unistack.org/micro/v3/options"
|
||||||
"go.unistack.org/micro/v3/register"
|
|
||||||
"go.unistack.org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,14 +56,12 @@ type subscriber struct {
|
|||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
subscriber interface{}
|
subscriber interface{}
|
||||||
handlers []*handler
|
handlers []*handler
|
||||||
endpoints []*register.Endpoint
|
|
||||||
opts server.SubscriberOptions
|
opts server.SubscriberOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
||||||
options := server.NewSubscriberOptions(opts...)
|
options := server.NewSubscriberOptions(opts...)
|
||||||
|
|
||||||
var endpoints []*register.Endpoint
|
|
||||||
var handlers []*handler
|
var handlers []*handler
|
||||||
|
|
||||||
if typ := reflect.TypeOf(sub); typ.Kind() == reflect.Func {
|
if typ := reflect.TypeOf(sub); typ.Kind() == reflect.Func {
|
||||||
@@ -82,18 +79,7 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio
|
|||||||
|
|
||||||
handlers = append(handlers, h)
|
handlers = append(handlers, h)
|
||||||
|
|
||||||
endpoints = append(endpoints, ®ister.Endpoint{
|
|
||||||
Name: "Func",
|
|
||||||
Request: register.ExtractSubValue(typ),
|
|
||||||
Metadata: map[string]string{
|
|
||||||
"topic": topic,
|
|
||||||
"subscriber": "true",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
hdlr := reflect.ValueOf(sub)
|
|
||||||
name := reflect.Indirect(hdlr).Type().Name()
|
|
||||||
|
|
||||||
for m := 0; m < typ.NumMethod(); m++ {
|
for m := 0; m < typ.NumMethod(); m++ {
|
||||||
method := typ.Method(m)
|
method := typ.Method(m)
|
||||||
h := &handler{
|
h := &handler{
|
||||||
@@ -110,14 +96,6 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio
|
|||||||
|
|
||||||
handlers = append(handlers, h)
|
handlers = append(handlers, h)
|
||||||
|
|
||||||
endpoints = append(endpoints, ®ister.Endpoint{
|
|
||||||
Name: name + "." + method.Name,
|
|
||||||
Request: register.ExtractSubValue(method.Type),
|
|
||||||
Metadata: map[string]string{
|
|
||||||
"topic": topic,
|
|
||||||
"subscriber": "true",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +105,6 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio
|
|||||||
topic: topic,
|
topic: topic,
|
||||||
subscriber: sub,
|
subscriber: sub,
|
||||||
handlers: handlers,
|
handlers: handlers,
|
||||||
endpoints: endpoints,
|
|
||||||
opts: options,
|
opts: options,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,6 +133,7 @@ func (g *Server) createSubHandler(sb *subscriber, opts server.Options) broker.Ha
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx := metadata.NewIncomingContext(sb.opts.Context, hdr)
|
ctx := metadata.NewIncomingContext(sb.opts.Context, hdr)
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, metadata.New(0))
|
||||||
|
|
||||||
results := make(chan error, len(sb.handlers))
|
results := make(chan error, len(sb.handlers))
|
||||||
|
|
||||||
@@ -197,7 +175,7 @@ func (g *Server) createSubHandler(sb *subscriber, opts server.Options) broker.Ha
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.Hooks.EachNext(func(hook options.Hook) {
|
opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||||
if h, ok := hook.(server.HookSubHandler); ok {
|
if h, ok := hook.(server.HookSubHandler); ok {
|
||||||
fn = h(fn)
|
fn = h(fn)
|
||||||
}
|
}
|
||||||
@@ -241,10 +219,6 @@ func (s *subscriber) Subscriber() interface{} {
|
|||||||
return s.subscriber
|
return s.subscriber
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *subscriber) Endpoints() []*register.Endpoint {
|
|
||||||
return s.endpoints
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) Options() server.SubscriberOptions {
|
func (s *subscriber) Options() server.SubscriberOptions {
|
||||||
return s.opts
|
return s.opts
|
||||||
}
|
}
|
||||||
|
@@ -26,6 +26,7 @@ func TestServiceMethod(t *testing.T) {
|
|||||||
if err != nil && test.err == true {
|
if err != nil && test.err == true {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
t.Logf("input %s service %s method %s", test.input, service, method)
|
||||||
// unexpected error
|
// unexpected error
|
||||||
if err != nil && test.err == false {
|
if err != nil && test.err == false {
|
||||||
t.Fatalf("unexpected err %v for %+v", err, test)
|
t.Fatalf("unexpected err %v for %+v", err, test)
|
||||||
|
Reference in New Issue
Block a user