Compare commits
170 Commits
Author | SHA1 | Date | |
---|---|---|---|
e6e64ff070 | |||
5ec59f0989 | |||
d4a2dd918f | |||
8c42fbb18b | |||
f860254f7b | |||
0c060a5868 | |||
c24f1f26f8 | |||
|
566036802b | ||
f33595f72a | |||
f94c265c7a | |||
3036359547 | |||
9e0a58405f | |||
ee3f978683 | |||
46891c397f | |||
6856038abe | |||
786bbb7185 | |||
95207c9617 | |||
d646deb468 | |||
468819f0a0 | |||
832f1034a8 | |||
f0b6370ee1 | |||
3d522b094b | |||
92dcd1acd7 | |||
dc8a736e13 | |||
|
4219919c9e | ||
1f447ea747 | |||
|
30c0e01397 | ||
244f3def4d | |||
|
55cbc89e11 | ||
df00f718cf | |||
|
bc3369f3a6 | ||
8d4c661ce5 | |||
|
7b97212e26 | ||
|
9d5a2c1168 | ||
|
483c6bb801 | ||
|
7ba5fd5fee | ||
080705a5df | |||
|
79df512e5e | ||
|
3e893b78c8 | ||
8e9c64d78b | |||
|
d66aa424d2 | ||
|
f8d3695962 | ||
ae158ce5fc | |||
8125c9003c | |||
a6f6df257b | |||
6b19cb2fb7 | |||
|
db6fee9760 | ||
309f100532 | |||
|
22ae55f739 | ||
70700a3f86 | |||
7dd327086c | |||
a67efa39ae | |||
|
8ee91422cc | ||
f8ae500c5f | |||
|
7fcc042fbf | ||
3a22f3a900 | |||
|
452a124aee | ||
|
26d3adfe95 | ||
|
18d6584c8f | ||
f26dde5d63 | |||
|
66d3feb263 | ||
5c8effa23f | |||
|
c1e318d0b3 | ||
617764706c | |||
0f3e56f697 | |||
|
b87462c465 | ||
|
e877a92718 | ||
|
c60f0ccb26 | ||
7cf4a8d293 | |||
84b1b862a7 | |||
|
eb17921feb | ||
|
ddeb0a23c3 | ||
|
830d8d8fda | ||
|
ceaff6bf88 | ||
|
01848b8ec7 | ||
|
8ddfa39811 | ||
110a8a8a9c | |||
|
f2587f0876 | ||
eccdad9752 | |||
|
c05996ee6e | ||
734d6fa7af | |||
4c0ca3664a | |||
|
d34ce4f314 | ||
|
d9932033ee | ||
847887de84 | |||
|
10ea1928f4 | ||
|
52d37c6579 | ||
|
0af18ab84b | ||
|
c46d11a2d4 | ||
|
117f48aac5 | ||
b4a2fbdeeb | |||
e36db68d4d | |||
b6b101b140 | |||
cbafc74e41 | |||
1e19359ab1 | |||
8e88abbbca | |||
adf9a0d82d | |||
a1a5980534 | |||
25a74b6935 | |||
|
5ed755f5bb | ||
8dd303472d | |||
|
1f6b2b34ec | ||
ce3da2c9fa | |||
b248593e35 | |||
|
dda5d46e47 | ||
0f50e30f9c | |||
|
f983ccc6d9 | ||
|
ef9ee6d837 | ||
0103c05275 | |||
d1c11644e8 | |||
dc01d20561 | |||
cdee03a96e | |||
48bce4ed2f | |||
|
f00e34380b | ||
86ebd173f7 | |||
9923c98469 | |||
|
51b93c413c | ||
|
9617533481 | ||
|
71f7ea49e4 | ||
|
8e89b4b32f | ||
9a2965b4d4 | |||
|
d90ff34a67 | ||
d2ef32f628 | |||
|
d14209e31e | ||
d82f7c49c7 | |||
|
98c02c72b0 | ||
35ad59444a | |||
|
9e63562468 | ||
978f500ce2 | |||
|
8c8073ca01 | ||
|
8d283ce2b2 | ||
d98376446a | |||
|
e95d956ff7 | ||
|
ad2dc43aed | ||
54562aa346 | |||
|
6f202b3a97 | ||
aa73377fb5 | |||
|
6b1e5195ba | ||
cbbcb2c10a | |||
|
7d7fc39779 | ||
|
483eda5adc | ||
25d47a5b01 | |||
0e1531f6a3 | |||
ce9b97f24f | |||
82d14b4ea7 | |||
5beb4bc7d9 | |||
|
c42770b800 | ||
30c852913b | |||
97ee4eaa33 | |||
|
d77c36d479 | ||
|
4696079471 | ||
|
bf3c302813 | ||
a36801b7ee | |||
3b81209099 | |||
25abc921d5 | |||
d3ef82346d | |||
|
df72327b91 | ||
|
68e3240e8e | ||
|
f1671c0150 | ||
|
15cd1aaaa5 | ||
|
d9bae52513 | ||
|
a39577fb6a | ||
|
f5a44c602c | ||
|
676efbae2f | ||
|
39b8b610d6 | ||
|
4667c94f11 | ||
|
3d34b06e4f | ||
|
0e859116bc | ||
1206fecf93 | |||
e091caef5d |
19
.github/dependabot.yml
vendored
Normal file
19
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# 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/renovate.json
vendored
20
.github/renovate.json
vendored
@@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": [
|
|
||||||
"config:base"
|
|
||||||
],
|
|
||||||
"postUpdateOptions": ["gomodTidy"],
|
|
||||||
"packageRules": [
|
|
||||||
{
|
|
||||||
"matchUpdateTypes": ["minor", "patch", "pin", "digest"],
|
|
||||||
"automerge": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"groupName": "all deps",
|
|
||||||
"separateMajorMinor": true,
|
|
||||||
"groupSlug": "all",
|
|
||||||
"packagePatterns": [
|
|
||||||
"*"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
13
.github/stale.sh
vendored
13
.github/stale.sh
vendored
@@ -1,13 +0,0 @@
|
|||||||
#!/bin/bash -ex
|
|
||||||
|
|
||||||
export PATH=$PATH:$(pwd)/bin
|
|
||||||
export GO111MODULE=on
|
|
||||||
export GOBIN=$(pwd)/bin
|
|
||||||
|
|
||||||
#go get github.com/rvflash/goup@v0.4.1
|
|
||||||
|
|
||||||
#goup -v ./...
|
|
||||||
#go get github.com/psampaz/go-mod-outdated@v0.6.0
|
|
||||||
go list -u -m -mod=mod -json all | go-mod-outdated -update -direct -ci || true
|
|
||||||
|
|
||||||
#go list -u -m -json all | go-mod-outdated -update
|
|
20
.github/workflows/autoapprove.yml
vendored
Normal file
20
.github/workflows/autoapprove.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
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
Normal file
21
.github/workflows/automerge.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
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}}
|
13
.github/workflows/build.yml
vendored
13
.github/workflows/build.yml
vendored
@@ -3,19 +3,20 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
78
.github/workflows/codeql-analysis.yml
vendored
Normal file
78
.github/workflows/codeql-analysis.yml
vendored
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
# 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
Normal file
27
.github/workflows/dependabot-automerge.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
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}}
|
13
.github/workflows/pr.yml
vendored
13
.github/workflows/pr.yml
vendored
@@ -3,19 +3,20 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
bin
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
|
|
||||||
|
# General
|
||||||
|
.DS_Store
|
||||||
|
.idea
|
||||||
|
.vscode
|
19
codec.go
19
codec.go
@@ -3,30 +3,43 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ codec.Codec = &wrapGrpcCodec{}
|
||||||
|
_ encoding.Codec = &wrapMicroCodec{}
|
||||||
|
)
|
||||||
|
|
||||||
type wrapMicroCodec struct{ codec.Codec }
|
type wrapMicroCodec struct{ codec.Codec }
|
||||||
|
|
||||||
func (w *wrapMicroCodec) Name() string {
|
func (w *wrapMicroCodec) Name() string {
|
||||||
return w.Codec.String()
|
return w.Codec.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *wrapMicroCodec) Marshal(v interface{}) ([]byte, error) {
|
||||||
|
return w.Codec.Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *wrapMicroCodec) Unmarshal(d []byte, v interface{}) error {
|
||||||
|
return w.Codec.Unmarshal(d, v)
|
||||||
|
}
|
||||||
|
|
||||||
type wrapGrpcCodec struct{ encoding.Codec }
|
type wrapGrpcCodec struct{ encoding.Codec }
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) String() string {
|
func (w *wrapGrpcCodec) String() string {
|
||||||
return w.Codec.Name()
|
return w.Codec.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) Marshal(v interface{}) ([]byte, error) {
|
func (w *wrapGrpcCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||||
if m, ok := v.(*codec.Frame); ok {
|
if m, ok := v.(*codec.Frame); ok {
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
}
|
}
|
||||||
return w.Codec.Marshal(v)
|
return w.Codec.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) Unmarshal(d []byte, v interface{}) error {
|
func (w *wrapGrpcCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
||||||
if d == nil || v == nil {
|
if d == nil || v == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
16
context.go
16
context.go
@@ -1,16 +0,0 @@
|
|||||||
package grpc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/server"
|
|
||||||
)
|
|
||||||
|
|
||||||
func setServerOption(k, v interface{}) server.Option {
|
|
||||||
return func(o *server.Options) {
|
|
||||||
if o.Context == nil {
|
|
||||||
o.Context = context.Background()
|
|
||||||
}
|
|
||||||
o.Context = context.WithValue(o.Context, k, v)
|
|
||||||
}
|
|
||||||
}
|
|
2
error.go
2
error.go
@@ -6,7 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/errors"
|
"go.unistack.org/micro/v3/errors"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
4
generate.go
Normal file
4
generate.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package grpc
|
||||||
|
|
||||||
|
//go:generate go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
||||||
|
//go:generate sh -c "protoc -I./proto -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v3) -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto proto/test.proto"
|
21
go.mod
21
go.mod
@@ -1,12 +1,17 @@
|
|||||||
module github.com/unistack-org/micro-server-grpc/v3
|
module go.unistack.org/micro-server-grpc/v3
|
||||||
|
|
||||||
go 1.16
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.4
|
||||||
github.com/unistack-org/micro/v3 v3.3.16
|
go.unistack.org/micro/v3 v3.10.54
|
||||||
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6
|
golang.org/x/net v0.24.0
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
google.golang.org/grpc v1.63.0
|
||||||
google.golang.org/grpc v1.37.0
|
google.golang.org/protobuf v1.33.0
|
||||||
google.golang.org/protobuf v1.26.0
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
golang.org/x/sys v0.19.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
|
||||||
)
|
)
|
||||||
|
147
go.sum
147
go.sum
@@ -1,109 +1,48 @@
|
|||||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
|
||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
|
||||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
|
||||||
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
|
||||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
|
||||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
|
||||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
|
||||||
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
|
|
||||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
|
||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
|
||||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
|
||||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
|
||||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
|
||||||
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
|
||||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
|
go.unistack.org/micro/v3 v3.10.42 h1:A0nA6WT6wNq5fyQyzliX70Bj5/SGj5kadLSOySX4hro=
|
||||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
go.unistack.org/micro/v3 v3.10.42/go.mod h1:CSmEf5ddmft94MyKHnUSMM0W5dpmmTVbgImbgQWV5Ak=
|
||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
go.unistack.org/micro/v3 v3.10.52 h1:6LlAvLLlf+3JLCEQEVNQWi7DXCoI1ocuOqqoEPj5S+k=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
go.unistack.org/micro/v3 v3.10.52/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
go.unistack.org/micro/v3 v3.10.54 h1:3qbv7jg+wpcYG/nJXzE/GEIsM8i5UdpytL2cNE8i3y0=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
go.unistack.org/micro/v3 v3.10.54/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||||
github.com/unistack-org/micro/v3 v3.3.16 h1:v0h/oC0TO2n1djQJeOjD2jNEqKkiykwI6cpflEVTlQE=
|
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||||
github.com/unistack-org/micro/v3 v3.3.16/go.mod h1:ETGcQQUcjxGaD44LUMX+0fgo8Loh7ExldfIPLvfUmDo=
|
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 h1:0PC75Fz/kyMGhL0e1QnypqK2kQMqKt9csD1GnMJR+Zk=
|
|
||||||
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
|
||||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
|
||||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 h1:AB/lmRny7e2pLhFEYIbl5qkDAUt2h0ZRO4wGPhZf+ik=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405/go.mod h1:67X1fPuzjcrkymZzZV1vvkFeTn2Rvc6lYF9MYFGCcwE=
|
||||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c h1:lfpJ/2rWPa/kJgxyyXM8PrNnfCzcmxJ265mADgwmvLI=
|
||||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
|
||||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda h1:LI5DOvAxUPMv/50agcLLoo+AdWc1irS9Rzz4vPuD1V4=
|
||||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
|
||||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
|
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
|
||||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
|
||||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
|
||||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
|
||||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
google.golang.org/grpc v1.63.0 h1:WjKe+dnvABXyPJMD7KDNLxtoGk5tgk+YFWN6cBWjZE8=
|
||||||
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
google.golang.org/grpc v1.63.0/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
|
||||||
google.golang.org/grpc v1.37.0 h1:uSZWeQJX5j11bIQ4AJoj+McDBo29cY1MCoC1wO3ts+c=
|
|
||||||
google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
|
|
||||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
|
||||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
|
||||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
|
||||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
|
||||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
|
||||||
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
|
||||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
|
||||||
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
|
||||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
|
||||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
|
||||||
|
445
grpc.go
445
grpc.go
@@ -7,26 +7,30 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime/debug"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
greflection "google.golang.org/grpc/reflection"
|
||||||
|
reflectionv1pb "google.golang.org/grpc/reflection/grpc_reflection_v1"
|
||||||
|
|
||||||
// nolint: staticcheck
|
// nolint: staticcheck
|
||||||
oldproto "github.com/golang/protobuf/proto"
|
oldproto "github.com/golang/protobuf/proto"
|
||||||
"github.com/unistack-org/micro/v3/broker"
|
"go.unistack.org/micro/v3/broker"
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/errors"
|
"go.unistack.org/micro/v3/errors"
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"go.unistack.org/micro/v3/logger"
|
||||||
metadata "github.com/unistack-org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"github.com/unistack-org/micro/v3/register"
|
"go.unistack.org/micro/v3/meter"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/register"
|
||||||
|
"go.unistack.org/micro/v3/semconv"
|
||||||
|
"go.unistack.org/micro/v3/server"
|
||||||
|
msync "go.unistack.org/micro/v3/sync"
|
||||||
"golang.org/x/net/netutil"
|
"golang.org/x/net/netutil"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/credentials"
|
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
gmetadata "google.golang.org/grpc/metadata"
|
gmetadata "google.golang.org/grpc/metadata"
|
||||||
"google.golang.org/grpc/peer"
|
"google.golang.org/grpc/peer"
|
||||||
@@ -35,35 +39,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultContentType = "application/grpc"
|
DefaultContentType = "application/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
type grpcServerReflection struct {
|
type ServerReflection struct {
|
||||||
srv *grpc.Server
|
srv *grpc.Server
|
||||||
s *serverReflectionServer
|
s *serverReflectionServer
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type grpcServer struct {
|
type Server struct {
|
||||||
handlers map[string]server.Handler
|
handlers map[string]server.Handler
|
||||||
srv *grpc.Server
|
srv *grpc.Server
|
||||||
exit chan chan error
|
exit chan chan error
|
||||||
wg *sync.WaitGroup
|
wg *msync.WaitGroup
|
||||||
rsvc *register.Service
|
rsvc *register.Service
|
||||||
subscribers map[*subscriber][]broker.Subscriber
|
subscribers map[*subscriber][]broker.Subscriber
|
||||||
rpc *rServer
|
rpc *rServer
|
||||||
opts server.Options
|
opts server.Options
|
||||||
|
unknownHandler grpc.StreamHandler
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
init bool
|
|
||||||
started bool
|
started bool
|
||||||
registered bool
|
registered bool
|
||||||
reflection bool
|
reflection bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGRPCServer(opts ...server.Option) server.Server {
|
func newServer(opts ...server.Option) *Server {
|
||||||
// create a grpc server
|
// create a grpc server
|
||||||
g := &grpcServer{
|
g := &Server{
|
||||||
opts: server.NewOptions(opts...),
|
opts: server.NewOptions(opts...),
|
||||||
rpc: &rServer{
|
rpc: &rServer{
|
||||||
serviceMap: make(map[string]*service),
|
serviceMap: make(map[string]*service),
|
||||||
@@ -73,6 +77,8 @@ func newGRPCServer(opts ...server.Option) server.Server {
|
|||||||
exit: make(chan chan error),
|
exit: make(chan chan error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.opts.Meter = g.opts.Meter.Clone(meter.Labels("type", "grpc"))
|
||||||
|
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +98,7 @@ func (r grpcRouter) ServeRequest(ctx context.Context, req server.Request, rsp se
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func (g *grpcServer) configure(opts ...server.Option) error {
|
func (g *Server) configure(opts ...server.Option) error {
|
||||||
g.Lock()
|
g.Lock()
|
||||||
defer g.Unlock()
|
defer g.Unlock()
|
||||||
|
|
||||||
@@ -109,9 +115,6 @@ func (g *grpcServer) configure(opts ...server.Option) error {
|
|||||||
if err := g.opts.Tracer.Init(); err != nil {
|
if err := g.opts.Tracer.Init(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.opts.Auth.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := g.opts.Logger.Init(); err != nil {
|
if err := g.opts.Logger.Init(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -122,8 +125,6 @@ func (g *grpcServer) configure(opts ...server.Option) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
g.wg = g.opts.Wait
|
|
||||||
|
|
||||||
if g.opts.Context != nil {
|
if g.opts.Context != nil {
|
||||||
if codecs, ok := g.opts.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && codecs != nil {
|
if codecs, ok := g.opts.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && codecs != nil {
|
||||||
for k, v := range codecs {
|
for k, v := range codecs {
|
||||||
@@ -132,6 +133,10 @@ func (g *grpcServer) configure(opts ...server.Option) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, k := range g.opts.Codecs {
|
||||||
|
encoding.RegisterCodec(&wrapMicroCodec{k})
|
||||||
|
}
|
||||||
|
|
||||||
maxMsgSize := g.getMaxMsgSize()
|
maxMsgSize := g.getMaxMsgSize()
|
||||||
|
|
||||||
gopts := []grpc.ServerOption{
|
gopts := []grpc.ServerOption{
|
||||||
@@ -140,12 +145,8 @@ func (g *grpcServer) configure(opts ...server.Option) error {
|
|||||||
grpc.UnknownServiceHandler(g.handler),
|
grpc.UnknownServiceHandler(g.handler),
|
||||||
}
|
}
|
||||||
|
|
||||||
if creds := g.getCredentials(); creds != nil {
|
|
||||||
gopts = append(gopts, grpc.Creds(creds))
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts := g.getGrpcOptions(); opts != nil {
|
if opts := g.getGrpcOptions(); opts != nil {
|
||||||
gopts = append(gopts, opts...)
|
gopts = append(opts, gopts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
g.rsvc = nil
|
g.rsvc = nil
|
||||||
@@ -158,20 +159,29 @@ func (g *grpcServer) configure(opts ...server.Option) error {
|
|||||||
}
|
}
|
||||||
g.srv = grpc.NewServer(gopts...)
|
g.srv = grpc.NewServer(gopts...)
|
||||||
|
|
||||||
if v, ok := g.opts.Context.Value(reflectionKey{}).(bool); ok {
|
if v, ok := g.opts.Context.Value(reflectionKey{}).(Reflector); ok {
|
||||||
g.reflection = v
|
reflectionv1pb.RegisterServerReflectionServer(
|
||||||
|
g.srv,
|
||||||
|
greflection.NewServerV1(greflection.ServerOptions{
|
||||||
|
Services: v,
|
||||||
|
DescriptorResolver: v,
|
||||||
|
ExtensionResolver: v,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if h, ok := g.opts.Context.Value(unknownServiceHandlerKey{}).(grpc.StreamHandler); ok {
|
||||||
|
g.unknownHandler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
if restart {
|
if restart {
|
||||||
return g.Start()
|
return g.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
g.init = true
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) getMaxMsgSize() int {
|
func (g *Server) getMaxMsgSize() int {
|
||||||
if g.opts.Context == nil {
|
if g.opts.Context == nil {
|
||||||
return codec.DefaultMaxMsgSize
|
return codec.DefaultMaxMsgSize
|
||||||
}
|
}
|
||||||
@@ -182,14 +192,7 @@ func (g *grpcServer) getMaxMsgSize() int {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) getCredentials() credentials.TransportCredentials {
|
func (g *Server) getGrpcOptions() []grpc.ServerOption {
|
||||||
if g.opts.TLSConfig != nil {
|
|
||||||
return credentials.NewTLS(g.opts.TLSConfig)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *grpcServer) getGrpcOptions() []grpc.ServerOption {
|
|
||||||
if g.opts.Context == nil {
|
if g.opts.Context == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -202,40 +205,39 @@ func (g *grpcServer) getGrpcOptions() []grpc.ServerOption {
|
|||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err error) {
|
func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
||||||
defer func() {
|
var err error
|
||||||
if r := recover(); r != nil {
|
|
||||||
g.RLock()
|
|
||||||
config := g.opts
|
|
||||||
g.RUnlock()
|
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
|
||||||
config.Logger.Error(config.Context, "panic recovered: ", r)
|
|
||||||
config.Logger.Error(config.Context, string(debug.Stack()))
|
|
||||||
}
|
|
||||||
err = errors.InternalServerError(g.opts.Name, "panic recovered: %v", r)
|
|
||||||
} else if err != nil {
|
|
||||||
g.RLock()
|
|
||||||
config := g.opts
|
|
||||||
g.RUnlock()
|
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
|
||||||
config.Logger.Errorf(config.Context, "grpc handler got error: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if g.wg != nil {
|
|
||||||
g.wg.Add(1)
|
|
||||||
defer g.wg.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
fullMethod, ok := grpc.MethodFromServerStream(stream)
|
fullMethod, ok := grpc.MethodFromServerStream(stream)
|
||||||
if !ok {
|
if !ok {
|
||||||
return status.Errorf(codes.Internal, "method does not exist in context")
|
return status.Errorf(codes.Internal, "method does not exist in context")
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceName, methodName, err := serviceMethod(fullMethod)
|
ts := time.Now()
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Inc()
|
||||||
|
defer func() {
|
||||||
|
te := time.Since(ts)
|
||||||
|
g.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", fullMethod).Update(te.Seconds())
|
||||||
|
g.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", fullMethod).Update(te.Seconds())
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", fullMethod).Dec()
|
||||||
|
|
||||||
|
st := status.Convert(err)
|
||||||
|
if st == nil || st.Code() == codes.OK {
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc()
|
||||||
|
} else {
|
||||||
|
g.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", fullMethod, "status", "failure", "code", strconv.Itoa(int(st.Code()))).Inc()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var serviceName, methodName string
|
||||||
|
serviceName, methodName, err = serviceMethod(fullMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return status.New(codes.InvalidArgument, err.Error()).Err()
|
err = status.New(codes.InvalidArgument, err.Error()).Err()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.opts.Wait != nil {
|
||||||
|
g.opts.Wait.Add(1)
|
||||||
|
defer g.opts.Wait.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
// get grpc metadata
|
// get grpc metadata
|
||||||
@@ -248,15 +250,38 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err err
|
|||||||
for k, v := range gmd {
|
for k, v := range gmd {
|
||||||
md.Set(k, strings.Join(v, ", "))
|
md.Set(k, strings.Join(v, ", "))
|
||||||
}
|
}
|
||||||
|
md.Set("Path", fullMethod)
|
||||||
|
md.Set("Micro-Server", "grpc")
|
||||||
|
md.Set(metadata.HeaderEndpoint, methodName)
|
||||||
|
md.Set(metadata.HeaderService, serviceName)
|
||||||
|
|
||||||
|
var td string
|
||||||
// timeout for server deadline
|
// timeout for server deadline
|
||||||
to, ok := md.Get("timeout")
|
if v, ok := md.Get("timeout"); ok {
|
||||||
if ok {
|
|
||||||
md.Del("timeout")
|
md.Del("timeout")
|
||||||
|
td = v
|
||||||
|
}
|
||||||
|
if v, ok := md.Get("Grpc-Timeout"); ok {
|
||||||
|
md.Del("Grpc-Timeout")
|
||||||
|
td = v[:len(v)-1]
|
||||||
|
switch v[len(v)-1:] {
|
||||||
|
case "S":
|
||||||
|
td += "s"
|
||||||
|
case "M":
|
||||||
|
td += "m"
|
||||||
|
case "H":
|
||||||
|
td += "h"
|
||||||
|
case "m":
|
||||||
|
td += "ms"
|
||||||
|
case "u":
|
||||||
|
td += "us"
|
||||||
|
case "n":
|
||||||
|
td += "ns"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get content type
|
// get content type
|
||||||
ct := defaultContentType
|
ct := DefaultContentType
|
||||||
|
|
||||||
if ctype, ok := md.Get("content-type"); ok {
|
if ctype, ok := md.Get("content-type"); ok {
|
||||||
ct = ctype
|
ct = ctype
|
||||||
@@ -270,13 +295,14 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err err
|
|||||||
|
|
||||||
// get peer from context
|
// get peer from context
|
||||||
if p, ok := peer.FromContext(stream.Context()); ok {
|
if p, ok := peer.FromContext(stream.Context()); ok {
|
||||||
md["Remote"] = p.Addr.String()
|
md.Set("Remote", p.Addr.String())
|
||||||
ctx = peer.NewContext(ctx, p)
|
ctx = peer.NewContext(ctx, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the timeout if we have it
|
// set the timeout if we have it
|
||||||
if len(to) > 0 {
|
if len(td) > 0 {
|
||||||
if n, err := strconv.ParseUint(to, 10, 64); err == nil {
|
var n uint64
|
||||||
|
if n, err = strconv.ParseUint(td, 10, 64); err == nil {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, time.Duration(n))
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(n))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -287,48 +313,39 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err err
|
|||||||
svc := g.rpc.serviceMap[serviceName]
|
svc := g.rpc.serviceMap[serviceName]
|
||||||
g.rpc.mu.RUnlock()
|
g.rpc.mu.RUnlock()
|
||||||
|
|
||||||
/*
|
|
||||||
if svc == nil && g.reflection && methodName == "ServerReflectionInfo" {
|
|
||||||
rfl := &grpcServerReflection{srv: g.srv, s: &serverReflectionServer{s: g.srv}}
|
|
||||||
svc = &service{}
|
|
||||||
svc.typ = reflect.TypeOf(rfl)
|
|
||||||
svc.rcvr = reflect.ValueOf(rfl)
|
|
||||||
svc.name = reflect.Indirect(svc.rcvr).Type().Name()
|
|
||||||
svc.method = make(map[string]*methodType)
|
|
||||||
typ := reflect.TypeOf(rfl)
|
|
||||||
if me, ok := typ.MethodByName("ServerReflectionInfo"); ok {
|
|
||||||
g.rpc.mu.Lock()
|
|
||||||
ep, err := prepareEndpoint(me)
|
|
||||||
if ep != nil && err != nil {
|
|
||||||
svc.method["ServerReflectionInfo"] = ep
|
|
||||||
} else if err != nil {
|
|
||||||
return status.New(codes.Unimplemented, err.Error()).Err()
|
|
||||||
}
|
|
||||||
g.rpc.mu.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if svc == nil {
|
if svc == nil {
|
||||||
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err()
|
if g.unknownHandler != nil {
|
||||||
|
err = g.unknownHandler(srv, stream)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s", serviceName)).Err()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
mtype := svc.method[methodName]
|
mtype := svc.method[methodName]
|
||||||
if mtype == nil {
|
if mtype == nil {
|
||||||
return status.New(codes.Unimplemented, fmt.Sprintf("unknown service %s.%s", serviceName, methodName)).Err()
|
if g.unknownHandler != nil {
|
||||||
|
err = g.unknownHandler(srv, stream)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = status.New(codes.Unimplemented, fmt.Sprintf("unknown service method %s.%s", serviceName, methodName)).Err()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// process unary
|
// process unary
|
||||||
if !mtype.stream {
|
if !mtype.stream {
|
||||||
return g.processRequest(ctx, stream, svc, mtype, ct)
|
err = g.processRequest(ctx, stream, svc, mtype, ct)
|
||||||
|
} else {
|
||||||
|
// process stream
|
||||||
|
err = g.processStream(ctx, stream, svc, mtype, ct)
|
||||||
}
|
}
|
||||||
|
|
||||||
// process stream
|
return err
|
||||||
return g.processStream(ctx, stream, svc, mtype, ct)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStream, service *service, mtype *methodType, ct string) error {
|
func (g *Server) processRequest(ctx context.Context, stream grpc.ServerStream, service *service, mtype *methodType, ct string) error {
|
||||||
// for {
|
// for {
|
||||||
|
var err error
|
||||||
var argv, replyv reflect.Value
|
var argv, replyv reflect.Value
|
||||||
|
|
||||||
// Decode the argument value.
|
// Decode the argument value.
|
||||||
@@ -341,7 +358,7 @@ func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStrea
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal request
|
// Unmarshal request
|
||||||
if err := stream.RecvMsg(argv.Interface()); err != nil {
|
if err = stream.RecvMsg(argv.Interface()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,26 +372,17 @@ func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStrea
|
|||||||
function := mtype.method.Func
|
function := mtype.method.Func
|
||||||
var returnValues []reflect.Value
|
var returnValues []reflect.Value
|
||||||
|
|
||||||
cf, err := g.newCodec(ct)
|
|
||||||
if err != nil {
|
|
||||||
return errors.InternalServerError(g.opts.Name, err.Error())
|
|
||||||
}
|
|
||||||
b, err := cf.Marshal(argv.Interface())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a client.Request
|
// create a client.Request
|
||||||
r := &rpcRequest{
|
r := &rpcRequest{
|
||||||
service: g.opts.Name,
|
service: g.opts.Name,
|
||||||
contentType: ct,
|
contentType: ct,
|
||||||
method: fmt.Sprintf("%s.%s", service.name, mtype.method.Name),
|
method: fmt.Sprintf("%s.%s", service.name, mtype.method.Name),
|
||||||
body: b,
|
endpoint: fmt.Sprintf("%s.%s", service.name, mtype.method.Name),
|
||||||
payload: argv.Interface(),
|
payload: argv.Interface(),
|
||||||
}
|
}
|
||||||
// define the handler func
|
// define the handler func
|
||||||
fn := func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
|
fn := func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
|
||||||
returnValues = function.Call([]reflect.Value{service.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
|
returnValues = function.Call([]reflect.Value{service.rcvr, mtype.prepareContext(ctx), argv, reflect.ValueOf(rsp)})
|
||||||
|
|
||||||
// The return value for the method is an error.
|
// The return value for the method is an error.
|
||||||
if rerr := returnValues[0].Interface(); rerr != nil {
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
||||||
@@ -392,7 +400,13 @@ func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStrea
|
|||||||
statusCode := codes.OK
|
statusCode := codes.OK
|
||||||
statusDesc := ""
|
statusDesc := ""
|
||||||
// execute the handler
|
// execute the handler
|
||||||
if appErr := fn(ctx, r, replyv.Interface()); appErr != nil {
|
appErr := fn(ctx, r, replyv.Interface())
|
||||||
|
if outmd, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
|
if err = stream.SendHeader(gmetadata.New(outmd)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if appErr != nil {
|
||||||
var errStatus *status.Status
|
var errStatus *status.Status
|
||||||
switch verr := appErr.(type) {
|
switch verr := appErr.(type) {
|
||||||
case *errors.Error:
|
case *errors.Error:
|
||||||
@@ -407,6 +421,8 @@ func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStrea
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
case (interface{ GRPCStatus() *status.Status }):
|
||||||
|
errStatus = verr.GRPCStatus()
|
||||||
default:
|
default:
|
||||||
g.RLock()
|
g.RLock()
|
||||||
config := g.opts
|
config := g.opts
|
||||||
@@ -428,60 +444,16 @@ func (g *grpcServer) processRequest(ctx context.Context, stream grpc.ServerStrea
|
|||||||
}
|
}
|
||||||
|
|
||||||
return status.New(statusCode, statusDesc).Err()
|
return status.New(statusCode, statusDesc).Err()
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func (g *Server) processStream(ctx context.Context, stream grpc.ServerStream, service *service, mtype *methodType, ct string) error {
|
||||||
type reflectStream struct {
|
|
||||||
stream server.Stream
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) Send(rsp *grpcreflect.ServerReflectionResponse) error {
|
|
||||||
return s.stream.Send(rsp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) Recv() (*grpcreflect.ServerReflectionRequest, error) {
|
|
||||||
req := &grpcreflect.ServerReflectionRequest{}
|
|
||||||
err := s.stream.Recv(req)
|
|
||||||
return req, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) SetHeader(gmetadata.MD) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) SendHeader(gmetadata.MD) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) SetTrailer(gmetadata.MD) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) Context() context.Context {
|
|
||||||
return s.stream.Context()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) SendMsg(m interface{}) error {
|
|
||||||
return s.stream.Send(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *reflectStream) RecvMsg(m interface{}) error {
|
|
||||||
return s.stream.Recv(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *grpcServerReflection) ServerReflectionInfo(ctx context.Context, stream server.Stream) error {
|
|
||||||
return g.s.ServerReflectionInfo(&reflectStream{stream})
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
func (g *grpcServer) processStream(ctx context.Context, stream grpc.ServerStream, service *service, mtype *methodType, ct string) error {
|
|
||||||
opts := g.opts
|
opts := g.opts
|
||||||
|
|
||||||
r := &rpcRequest{
|
r := &rpcRequest{
|
||||||
service: opts.Name,
|
service: opts.Name,
|
||||||
contentType: ct,
|
contentType: ct,
|
||||||
method: fmt.Sprintf("%s.%s", service.name, mtype.method.Name),
|
method: fmt.Sprintf("%s.%s", service.name, mtype.method.Name),
|
||||||
|
endpoint: fmt.Sprintf("%s.%s", service.name, mtype.method.Name),
|
||||||
stream: true,
|
stream: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -510,7 +482,13 @@ func (g *grpcServer) processStream(ctx context.Context, stream grpc.ServerStream
|
|||||||
statusCode := codes.OK
|
statusCode := codes.OK
|
||||||
statusDesc := ""
|
statusDesc := ""
|
||||||
|
|
||||||
if appErr := fn(ctx, r, ss); appErr != nil {
|
appErr := fn(ctx, r, ss)
|
||||||
|
if outmd, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
|
if err := stream.SendHeader(gmetadata.New(outmd)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if appErr != nil {
|
||||||
var err error
|
var err error
|
||||||
var errStatus *status.Status
|
var errStatus *status.Status
|
||||||
switch verr := appErr.(type) {
|
switch verr := appErr.(type) {
|
||||||
@@ -542,7 +520,7 @@ func (g *grpcServer) processStream(ctx context.Context, stream grpc.ServerStream
|
|||||||
return status.New(statusCode, statusDesc).Err()
|
return status.New(statusCode, statusDesc).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) newCodec(ct string) (codec.Codec, error) {
|
func (g *Server) newCodec(ct string) (codec.Codec, error) {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
defer g.RUnlock()
|
defer g.RUnlock()
|
||||||
|
|
||||||
@@ -557,7 +535,7 @@ func (g *grpcServer) newCodec(ct string) (codec.Codec, error) {
|
|||||||
return nil, codec.ErrUnknownContentType
|
return nil, codec.ErrUnknownContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Options() server.Options {
|
func (g *Server) Options() server.Options {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
opts := g.opts
|
opts := g.opts
|
||||||
g.RUnlock()
|
g.RUnlock()
|
||||||
@@ -565,18 +543,15 @@ func (g *grpcServer) Options() server.Options {
|
|||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Init(opts ...server.Option) error {
|
func (g *Server) Init(opts ...server.Option) error {
|
||||||
if len(opts) == 0 && g.init {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return g.configure(opts...)
|
return g.configure(opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) NewHandler(h interface{}, opts ...server.HandlerOption) server.Handler {
|
func (g *Server) NewHandler(h interface{}, opts ...server.HandlerOption) server.Handler {
|
||||||
return newRPCHandler(h, opts...)
|
return newRPCHandler(h, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Handle(h server.Handler) error {
|
func (g *Server) Handle(h server.Handler) error {
|
||||||
if err := g.rpc.register(h.Handler()); err != nil {
|
if err := g.rpc.register(h.Handler()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -585,11 +560,11 @@ func (g *grpcServer) Handle(h server.Handler) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) NewSubscriber(topic string, sb interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
func (g *Server) NewSubscriber(topic string, sb interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
||||||
return newSubscriber(topic, sb, opts...)
|
return newSubscriber(topic, sb, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Subscribe(sb server.Subscriber) error {
|
func (g *Server) Subscribe(sb server.Subscriber) error {
|
||||||
sub, ok := sb.(*subscriber)
|
sub, ok := sb.(*subscriber)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("invalid subscriber: expected *subscriber")
|
return fmt.Errorf("invalid subscriber: expected *subscriber")
|
||||||
@@ -613,7 +588,7 @@ func (g *grpcServer) Subscribe(sb server.Subscriber) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Register() error {
|
func (g *Server) Register() error {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
rsvc := g.rsvc
|
rsvc := g.rsvc
|
||||||
config := g.opts
|
config := g.opts
|
||||||
@@ -670,7 +645,7 @@ func (g *grpcServer) Register() error {
|
|||||||
|
|
||||||
if !registered {
|
if !registered {
|
||||||
if config.Logger.V(logger.InfoLevel) {
|
if config.Logger.V(logger.InfoLevel) {
|
||||||
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].Id)
|
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -687,37 +662,13 @@ func (g *grpcServer) Register() error {
|
|||||||
g.Lock()
|
g.Lock()
|
||||||
defer g.Unlock()
|
defer g.Unlock()
|
||||||
|
|
||||||
for sb := range g.subscribers {
|
|
||||||
handler := g.createSubHandler(sb, config)
|
|
||||||
var opts []broker.SubscribeOption
|
|
||||||
if queue := sb.Options().Queue; len(queue) > 0 {
|
|
||||||
opts = append(opts, broker.SubscribeGroup(queue))
|
|
||||||
}
|
|
||||||
|
|
||||||
subCtx := config.Context
|
|
||||||
if cx := sb.Options().Context; cx != nil {
|
|
||||||
subCtx = cx
|
|
||||||
}
|
|
||||||
opts = append(opts, broker.SubscribeContext(subCtx))
|
|
||||||
opts = append(opts, broker.SubscribeAutoAck(sb.Options().AutoAck))
|
|
||||||
|
|
||||||
if config.Logger.V(logger.InfoLevel) {
|
|
||||||
config.Logger.Infof(config.Context, "Subscribing to topic: %s", sb.Topic())
|
|
||||||
}
|
|
||||||
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
g.subscribers[sb] = []broker.Subscriber{sub}
|
|
||||||
}
|
|
||||||
|
|
||||||
g.registered = true
|
g.registered = true
|
||||||
g.rsvc = service
|
g.rsvc = service
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Deregister() error {
|
func (g *Server) Deregister() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
g.RLock()
|
g.RLock()
|
||||||
@@ -730,7 +681,7 @@ func (g *grpcServer) Deregister() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.Logger.V(logger.InfoLevel) {
|
if config.Logger.V(logger.InfoLevel) {
|
||||||
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].Id)
|
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := server.DefaultDeregisterFunc(service, config); err != nil {
|
if err := server.DefaultDeregisterFunc(service, config); err != nil {
|
||||||
@@ -771,7 +722,7 @@ func (g *grpcServer) Deregister() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Start() error {
|
func (g *Server) Start() error {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
if g.started {
|
if g.started {
|
||||||
g.RUnlock()
|
g.RUnlock()
|
||||||
@@ -781,18 +732,13 @@ func (g *grpcServer) Start() error {
|
|||||||
|
|
||||||
config := g.Options()
|
config := g.Options()
|
||||||
|
|
||||||
for _, k := range config.Codecs {
|
|
||||||
encoding.RegisterCodec(&wrapMicroCodec{k})
|
|
||||||
}
|
|
||||||
|
|
||||||
// micro: config.Transport.Listen(config.Address)
|
// micro: config.Transport.Listen(config.Address)
|
||||||
var ts net.Listener
|
var ts net.Listener
|
||||||
|
var err error
|
||||||
|
|
||||||
if l := config.Listener; l != nil {
|
if l := config.Listener; l != nil {
|
||||||
ts = l
|
ts = l
|
||||||
} else {
|
} else {
|
||||||
var err error
|
|
||||||
|
|
||||||
// check the tls config for secure connect
|
// check the tls config for secure connect
|
||||||
if tc := config.TLSConfig; tc != nil {
|
if tc := config.TLSConfig; tc != nil {
|
||||||
ts, err = tls.Listen("tcp", config.Address, tc)
|
ts, err = tls.Listen("tcp", config.Address, tc)
|
||||||
@@ -822,7 +768,7 @@ func (g *grpcServer) Start() error {
|
|||||||
// only connect if we're subscribed
|
// only connect if we're subscribed
|
||||||
if len(g.subscribers) > 0 {
|
if len(g.subscribers) > 0 {
|
||||||
// connect to the broker
|
// connect to the broker
|
||||||
if err := config.Broker.Connect(config.Context); err != nil {
|
if err = config.Broker.Connect(config.Context); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Broker [%s] connect error: %v", config.Broker.String(), err)
|
config.Logger.Errorf(config.Context, "Broker [%s] connect error: %v", config.Broker.String(), err)
|
||||||
}
|
}
|
||||||
@@ -836,26 +782,30 @@ func (g *grpcServer) Start() error {
|
|||||||
|
|
||||||
// use RegisterCheck func before register
|
// use RegisterCheck func before register
|
||||||
// nolint: nestif
|
// nolint: nestif
|
||||||
if err := g.opts.RegisterCheck(config.Context); err != nil {
|
if err = g.opts.RegisterCheck(config.Context); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.Id, err)
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// announce self to the world
|
// announce self to the world
|
||||||
if err := g.Register(); err != nil {
|
if err = g.Register(); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server register error: %v", err)
|
config.Logger.Errorf(config.Context, "Server register error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = g.subscribe(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// micro: go ts.Accept(s.accept)
|
// micro: go ts.Accept(s.accept)
|
||||||
go func() {
|
go func() {
|
||||||
if err := g.srv.Serve(ts); err != nil {
|
if err = g.srv.Serve(ts); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "gRPC Server start error: %v", err)
|
config.Logger.Errorf(config.Context, "gRPC Server start error: %v", err)
|
||||||
}
|
}
|
||||||
if err := g.Stop(); err != nil {
|
if err = g.Stop(); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "gRPC Server stop error: %v", err)
|
config.Logger.Errorf(config.Context, "gRPC Server stop error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -887,23 +837,23 @@ func (g *grpcServer) Start() error {
|
|||||||
// nolint: nestif
|
// nolint: nestif
|
||||||
if rerr != nil && registered {
|
if rerr != nil && registered {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.Id, rerr)
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr)
|
||||||
}
|
}
|
||||||
// deregister self in case of error
|
// deregister self in case of error
|
||||||
if err := g.Deregister(); err != nil {
|
if err = g.Deregister(); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.Id, err)
|
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if rerr != nil && !registered {
|
} else if rerr != nil && !registered {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.Id, rerr)
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, rerr)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := g.Register(); err != nil {
|
if err = g.Register(); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.Id, err)
|
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// wait for exit
|
// wait for exit
|
||||||
@@ -913,15 +863,15 @@ func (g *grpcServer) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deregister self
|
// deregister self
|
||||||
if err := g.Deregister(); err != nil {
|
if err = g.Deregister(); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Server deregister error: ", err)
|
config.Logger.Errorf(config.Context, "Server deregister error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for waitgroup
|
// wait for waitgroup
|
||||||
if g.wg != nil {
|
if g.opts.Wait != nil {
|
||||||
g.wg.Wait()
|
g.opts.Wait.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop the grpc server
|
// stop the grpc server
|
||||||
@@ -934,7 +884,7 @@ func (g *grpcServer) Start() error {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-exit:
|
case <-exit:
|
||||||
case <-time.After(time.Second):
|
case <-time.After(g.opts.GracefulTimeout):
|
||||||
g.srv.Stop()
|
g.srv.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -945,7 +895,7 @@ func (g *grpcServer) Start() error {
|
|||||||
config.Logger.Infof(config.Context, "Broker [%s] Disconnected from %s", config.Broker.String(), config.Broker.Address())
|
config.Logger.Infof(config.Context, "Broker [%s] Disconnected from %s", config.Broker.String(), config.Broker.Address())
|
||||||
}
|
}
|
||||||
// disconnect broker
|
// disconnect broker
|
||||||
if err := config.Broker.Disconnect(config.Context); err != nil {
|
if err = config.Broker.Disconnect(config.Context); err != nil {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
config.Logger.Errorf(config.Context, "Broker [%s] disconnect error: %v", config.Broker.String(), err)
|
config.Logger.Errorf(config.Context, "Broker [%s] disconnect error: %v", config.Broker.String(), err)
|
||||||
}
|
}
|
||||||
@@ -960,7 +910,38 @@ func (g *grpcServer) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Stop() error {
|
func (g *Server) subscribe() error {
|
||||||
|
config := g.opts
|
||||||
|
|
||||||
|
for sb := range g.subscribers {
|
||||||
|
handler := g.createSubHandler(sb, config)
|
||||||
|
var opts []broker.SubscribeOption
|
||||||
|
if queue := sb.Options().Queue; len(queue) > 0 {
|
||||||
|
opts = append(opts, broker.SubscribeGroup(queue))
|
||||||
|
}
|
||||||
|
|
||||||
|
subCtx := config.Context
|
||||||
|
if cx := sb.Options().Context; cx != nil {
|
||||||
|
subCtx = cx
|
||||||
|
}
|
||||||
|
opts = append(opts, broker.SubscribeContext(subCtx))
|
||||||
|
opts = append(opts, broker.SubscribeAutoAck(sb.Options().AutoAck))
|
||||||
|
opts = append(opts, broker.SubscribeBodyOnly(sb.Options().BodyOnly))
|
||||||
|
|
||||||
|
if config.Logger.V(logger.InfoLevel) {
|
||||||
|
config.Logger.Infof(config.Context, "Subscribing to topic: %s", sb.Topic())
|
||||||
|
}
|
||||||
|
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
g.subscribers[sb] = []broker.Subscriber{sub}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Server) Stop() error {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
if !g.started {
|
if !g.started {
|
||||||
g.RUnlock()
|
g.RUnlock()
|
||||||
@@ -980,14 +961,18 @@ func (g *grpcServer) Stop() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) String() string {
|
func (g *Server) String() string {
|
||||||
return "grpc"
|
return "grpc"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) Name() string {
|
func (g *Server) Name() string {
|
||||||
return g.opts.Name
|
return g.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(opts ...server.Option) server.Server {
|
func (g *Server) GRPCServer() *grpc.Server {
|
||||||
return newGRPCServer(opts...)
|
return g.srv
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServer(opts ...server.Option) *Server {
|
||||||
|
return newServer(opts...)
|
||||||
}
|
}
|
||||||
|
@@ -3,8 +3,8 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/register"
|
"go.unistack.org/micro/v3/register"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
type rpcHandler struct {
|
type rpcHandler struct {
|
||||||
|
18
options.go
18
options.go
@@ -3,7 +3,7 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
)
|
)
|
||||||
@@ -13,6 +13,7 @@ type (
|
|||||||
grpcOptions struct{}
|
grpcOptions struct{}
|
||||||
maxMsgSizeKey struct{}
|
maxMsgSizeKey struct{}
|
||||||
reflectionKey struct{}
|
reflectionKey struct{}
|
||||||
|
unknownServiceHandlerKey struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// gRPC Codec to be used to encode/decode requests for a given content type
|
// gRPC Codec to be used to encode/decode requests for a given content type
|
||||||
@@ -32,18 +33,21 @@ func Codec(contentType string, c encoding.Codec) server.Option {
|
|||||||
|
|
||||||
// Options to be used to configure gRPC options
|
// Options to be used to configure gRPC options
|
||||||
func Options(opts ...grpc.ServerOption) server.Option {
|
func Options(opts ...grpc.ServerOption) server.Option {
|
||||||
return setServerOption(grpcOptions{}, opts)
|
return server.SetOption(grpcOptions{}, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// MaxMsgSize set the maximum message in bytes the server can receive and
|
// MaxMsgSize set the maximum message in bytes the server can receive and
|
||||||
// send. Default maximum message size is 4 MB.
|
// send. Default maximum message size is 4 MB.
|
||||||
//
|
|
||||||
func MaxMsgSize(s int) server.Option {
|
func MaxMsgSize(s int) server.Option {
|
||||||
return setServerOption(maxMsgSizeKey{}, s)
|
return server.SetOption(maxMsgSizeKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reflection enables reflection support in grpc server
|
// Reflection enables reflection support in grpc server
|
||||||
func Reflection(b bool) server.Option {
|
func Reflection(r Reflector) server.Option {
|
||||||
return setServerOption(reflectionKey{}, b)
|
return server.SetOption(reflectionKey{}, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnknownServiceHandler enables support for all services
|
||||||
|
func UnknownServiceHandler(h grpc.StreamHandler) server.Option {
|
||||||
|
return server.SetOption(unknownServiceHandlerKey{}, h)
|
||||||
}
|
}
|
||||||
|
208
proto/test.pb.go
Normal file
208
proto/test.pb.go
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// protoc-gen-go v1.26.0
|
||||||
|
// protoc v4.25.2
|
||||||
|
// source: test.proto
|
||||||
|
|
||||||
|
package testpb
|
||||||
|
|
||||||
|
import (
|
||||||
|
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
|
reflect "reflect"
|
||||||
|
sync "sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Verify that this generated code is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||||
|
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||||
|
)
|
||||||
|
|
||||||
|
type CallReq struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *CallReq) Reset() {
|
||||||
|
*x = CallReq{}
|
||||||
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_test_proto_msgTypes[0]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *CallReq) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*CallReq) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *CallReq) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_test_proto_msgTypes[0]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use CallReq.ProtoReflect.Descriptor instead.
|
||||||
|
func (*CallReq) Descriptor() ([]byte, []int) {
|
||||||
|
return file_test_proto_rawDescGZIP(), []int{0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *CallReq) GetData() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Data
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type CallRsp struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *CallRsp) Reset() {
|
||||||
|
*x = CallRsp{}
|
||||||
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_test_proto_msgTypes[1]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *CallRsp) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*CallRsp) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *CallRsp) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_test_proto_msgTypes[1]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use CallRsp.ProtoReflect.Descriptor instead.
|
||||||
|
func (*CallRsp) Descriptor() ([]byte, []int) {
|
||||||
|
return file_test_proto_rawDescGZIP(), []int{1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *CallRsp) GetData() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Data
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var File_test_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
|
var file_test_proto_rawDesc = []byte{
|
||||||
|
0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x74, 0x65,
|
||||||
|
0x73, 0x74, 0x22, 0x1d, 0x0a, 0x07, 0x43, 0x61, 0x6c, 0x6c, 0x52, 0x65, 0x71, 0x12, 0x12, 0x0a,
|
||||||
|
0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74,
|
||||||
|
0x61, 0x22, 0x1d, 0x0a, 0x07, 0x43, 0x61, 0x6c, 0x6c, 0x52, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04,
|
||||||
|
0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61,
|
||||||
|
0x32, 0x33, 0x0a, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12,
|
||||||
|
0x24, 0x0a, 0x04, 0x43, 0x61, 0x6c, 0x6c, 0x12, 0x0d, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x43,
|
||||||
|
0x61, 0x6c, 0x6c, 0x52, 0x65, 0x71, 0x1a, 0x0d, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x43, 0x61,
|
||||||
|
0x6c, 0x6c, 0x52, 0x73, 0x70, 0x42, 0x0b, 0x5a, 0x09, 0x2e, 0x2f, 0x3b, 0x74, 0x65, 0x73, 0x74,
|
||||||
|
0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
file_test_proto_rawDescOnce sync.Once
|
||||||
|
file_test_proto_rawDescData = file_test_proto_rawDesc
|
||||||
|
)
|
||||||
|
|
||||||
|
func file_test_proto_rawDescGZIP() []byte {
|
||||||
|
file_test_proto_rawDescOnce.Do(func() {
|
||||||
|
file_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_proto_rawDescData)
|
||||||
|
})
|
||||||
|
return file_test_proto_rawDescData
|
||||||
|
}
|
||||||
|
|
||||||
|
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||||
|
var file_test_proto_goTypes = []interface{}{
|
||||||
|
(*CallReq)(nil), // 0: test.CallReq
|
||||||
|
(*CallRsp)(nil), // 1: test.CallRsp
|
||||||
|
}
|
||||||
|
var file_test_proto_depIdxs = []int32{
|
||||||
|
0, // 0: test.TestService.Call:input_type -> test.CallReq
|
||||||
|
1, // 1: test.TestService.Call:output_type -> test.CallRsp
|
||||||
|
1, // [1:2] is the sub-list for method output_type
|
||||||
|
0, // [0:1] is the sub-list for method input_type
|
||||||
|
0, // [0:0] is the sub-list for extension type_name
|
||||||
|
0, // [0:0] is the sub-list for extension extendee
|
||||||
|
0, // [0:0] is the sub-list for field type_name
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { file_test_proto_init() }
|
||||||
|
func file_test_proto_init() {
|
||||||
|
if File_test_proto != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !protoimpl.UnsafeEnabled {
|
||||||
|
file_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*CallReq); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file_test_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*CallRsp); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type x struct{}
|
||||||
|
out := protoimpl.TypeBuilder{
|
||||||
|
File: protoimpl.DescBuilder{
|
||||||
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
|
RawDescriptor: file_test_proto_rawDesc,
|
||||||
|
NumEnums: 0,
|
||||||
|
NumMessages: 2,
|
||||||
|
NumExtensions: 0,
|
||||||
|
NumServices: 1,
|
||||||
|
},
|
||||||
|
GoTypes: file_test_proto_goTypes,
|
||||||
|
DependencyIndexes: file_test_proto_depIdxs,
|
||||||
|
MessageInfos: file_test_proto_msgTypes,
|
||||||
|
}.Build()
|
||||||
|
File_test_proto = out.File
|
||||||
|
file_test_proto_rawDesc = nil
|
||||||
|
file_test_proto_goTypes = nil
|
||||||
|
file_test_proto_depIdxs = nil
|
||||||
|
}
|
18
proto/test.proto
Normal file
18
proto/test.proto
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package test;
|
||||||
|
|
||||||
|
option go_package = "./;testpb";
|
||||||
|
|
||||||
|
|
||||||
|
service TestService {
|
||||||
|
rpc Call(CallReq) returns (CallRsp);
|
||||||
|
}
|
||||||
|
|
||||||
|
message CallReq {
|
||||||
|
string data = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message CallRsp {
|
||||||
|
string data = 1;
|
||||||
|
}
|
109
proto/test_grpc.pb.go
Normal file
109
proto/test_grpc.pb.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// - protoc-gen-go-grpc v1.3.0
|
||||||
|
// - protoc v4.25.2
|
||||||
|
// source: test.proto
|
||||||
|
|
||||||
|
package testpb
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
|
codes "google.golang.org/grpc/codes"
|
||||||
|
status "google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
// Requires gRPC-Go v1.32.0 or later.
|
||||||
|
const _ = grpc.SupportPackageIsVersion7
|
||||||
|
|
||||||
|
const (
|
||||||
|
TestService_Call_FullMethodName = "/test.TestService/Call"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestServiceClient is the client API for TestService service.
|
||||||
|
//
|
||||||
|
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||||
|
type TestServiceClient interface {
|
||||||
|
Call(ctx context.Context, in *CallReq, opts ...grpc.CallOption) (*CallRsp, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type testServiceClient struct {
|
||||||
|
cc grpc.ClientConnInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTestServiceClient(cc grpc.ClientConnInterface) TestServiceClient {
|
||||||
|
return &testServiceClient{cc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *testServiceClient) Call(ctx context.Context, in *CallReq, opts ...grpc.CallOption) (*CallRsp, error) {
|
||||||
|
out := new(CallRsp)
|
||||||
|
err := c.cc.Invoke(ctx, TestService_Call_FullMethodName, in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestServiceServer is the server API for TestService service.
|
||||||
|
// All implementations must embed UnimplementedTestServiceServer
|
||||||
|
// for forward compatibility
|
||||||
|
type TestServiceServer interface {
|
||||||
|
Call(context.Context, *CallReq) (*CallRsp, error)
|
||||||
|
mustEmbedUnimplementedTestServiceServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnimplementedTestServiceServer must be embedded to have forward compatible implementations.
|
||||||
|
type UnimplementedTestServiceServer struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (UnimplementedTestServiceServer) Call(context.Context, *CallReq) (*CallRsp, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method Call not implemented")
|
||||||
|
}
|
||||||
|
func (UnimplementedTestServiceServer) mustEmbedUnimplementedTestServiceServer() {}
|
||||||
|
|
||||||
|
// UnsafeTestServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||||
|
// Use of this interface is not recommended, as added methods to TestServiceServer will
|
||||||
|
// result in compilation errors.
|
||||||
|
type UnsafeTestServiceServer interface {
|
||||||
|
mustEmbedUnimplementedTestServiceServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterTestServiceServer(s grpc.ServiceRegistrar, srv TestServiceServer) {
|
||||||
|
s.RegisterService(&TestService_ServiceDesc, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _TestService_Call_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(CallReq)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(TestServiceServer).Call(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: TestService_Call_FullMethodName,
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(TestServiceServer).Call(ctx, req.(*CallReq))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestService_ServiceDesc is the grpc.ServiceDesc for TestService service.
|
||||||
|
// It's only intended for direct use with grpc.RegisterService,
|
||||||
|
// and not to be introspected or modified (even as a copy)
|
||||||
|
var TestService_ServiceDesc = grpc.ServiceDesc{
|
||||||
|
ServiceName: "test.TestService",
|
||||||
|
HandlerType: (*TestServiceServer)(nil),
|
||||||
|
Methods: []grpc.MethodDesc{
|
||||||
|
{
|
||||||
|
MethodName: "Call",
|
||||||
|
Handler: _TestService_Call_Handler,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Streams: []grpc.StreamDesc{},
|
||||||
|
Metadata: "test.proto",
|
||||||
|
}
|
495
reflection.go
495
reflection.go
@@ -1,488 +1,21 @@
|
|||||||
// +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
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"google.golang.org/grpc/reflection"
|
||||||
"compress/gzip"
|
"google.golang.org/protobuf/reflect/protodesc"
|
||||||
"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 {
|
type Reflector interface {
|
||||||
rpb.UnimplementedServerReflectionServer
|
protodesc.Resolver
|
||||||
s *grpc.Server
|
reflection.ServiceInfoProvider
|
||||||
|
reflection.ExtensionResolver
|
||||||
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.
|
const (
|
||||||
func Register(s *grpc.Server) {
|
// ReflectV1ServiceName is the fully-qualified name of the v1 version of the reflection service.
|
||||||
rpb.RegisterServerReflectionServer(s, &serverReflectionServer{
|
ReflectV1ServiceName = "grpc.reflection.v1.ServerReflection"
|
||||||
s: s,
|
// ReflectServiceURLPathV1 is the full path for reflection service endpoint
|
||||||
})
|
ReflectServiceURLPathV1 = "/" + ReflectV1ServiceName + "/"
|
||||||
}
|
// ReflectMethodName is the reflection service name
|
||||||
|
ReflectMethodName = "ServerReflectionInfo"
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
489
reflection_old.go
Normal file
489
reflection_old.go
Normal file
@@ -0,0 +1,489 @@
|
|||||||
|
//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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
62
reflection_test.go
Normal file
62
reflection_test.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package grpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
_ "go.unistack.org/micro-server-grpc/v3/proto"
|
||||||
|
"go.unistack.org/micro/v3/server"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
"google.golang.org/protobuf/reflect/protoregistry"
|
||||||
|
)
|
||||||
|
|
||||||
|
type reflector struct{}
|
||||||
|
|
||||||
|
func (r *reflector) FindFileByPath(path string) (protoreflect.FileDescriptor, error) {
|
||||||
|
fd, err := protoregistry.GlobalFiles.FindFileByPath(path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("err: %v\n", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return fd, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *reflector) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
|
||||||
|
fd, err := protoregistry.GlobalFiles.FindDescriptorByName(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return fd, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *reflector) GetServiceInfo() map[string]grpc.ServiceInfo {
|
||||||
|
fmt.Printf("GetServiceInfo\n")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *reflector) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
|
||||||
|
fmt.Printf("FindExtensionByName field %#+v\n", field)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *reflector) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
|
||||||
|
fmt.Printf("FindExtensionByNumber message %#+v field %#+v\n", message, field)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *reflector) RangeExtensionsByMessage(message protoreflect.FullName, f func(protoreflect.ExtensionType) bool) {
|
||||||
|
fmt.Printf("RangeExtensionsByMessage\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReflector(t *testing.T) {
|
||||||
|
srv := NewServer(Reflection(&reflector{}), server.Address(":12345"))
|
||||||
|
if err := srv.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := srv.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Logf("addr %s", srv.Options().Address)
|
||||||
|
select {}
|
||||||
|
}
|
45
request.go
45
request.go
@@ -3,15 +3,12 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var _ server.Request = &rpcRequest{}
|
||||||
_ server.Request = &rpcRequest{}
|
|
||||||
_ server.Message = &rpcMessage{}
|
|
||||||
)
|
|
||||||
|
|
||||||
type rpcRequest struct {
|
type rpcRequest struct {
|
||||||
rw io.ReadWriter
|
rw io.ReadWriter
|
||||||
@@ -22,19 +19,9 @@ type rpcRequest struct {
|
|||||||
endpoint string
|
endpoint string
|
||||||
contentType string
|
contentType string
|
||||||
service string
|
service string
|
||||||
body []byte
|
|
||||||
stream bool
|
stream bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type rpcMessage struct {
|
|
||||||
payload interface{}
|
|
||||||
codec codec.Codec
|
|
||||||
header metadata.Metadata
|
|
||||||
topic string
|
|
||||||
contentType string
|
|
||||||
body []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rpcRequest) ContentType() string {
|
func (r *rpcRequest) ContentType() string {
|
||||||
return r.contentType
|
return r.contentType
|
||||||
}
|
}
|
||||||
@@ -74,27 +61,3 @@ func (r *rpcRequest) Stream() bool {
|
|||||||
func (r *rpcRequest) Body() interface{} {
|
func (r *rpcRequest) Body() interface{} {
|
||||||
return r.payload
|
return r.payload
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rpcMessage) ContentType() string {
|
|
||||||
return r.contentType
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rpcMessage) Topic() string {
|
|
||||||
return r.topic
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rpcMessage) Payload() interface{} {
|
|
||||||
return r.payload
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rpcMessage) Header() metadata.Metadata {
|
|
||||||
return r.header
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rpcMessage) Body() []byte {
|
|
||||||
return r.body
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rpcMessage) Codec() codec.Codec {
|
|
||||||
return r.codec
|
|
||||||
}
|
|
||||||
|
@@ -3,9 +3,9 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ server.Response = &rpcResponse{}
|
var _ server.Response = &rpcResponse{}
|
||||||
|
@@ -14,7 +14,7 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Precompute the reflect type for error. Can't use error directly
|
// Precompute the reflect type for error. Can't use error directly
|
||||||
|
@@ -3,7 +3,7 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -4,17 +4,48 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime/debug"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/broker"
|
"go.unistack.org/micro/v3/broker"
|
||||||
"github.com/unistack-org/micro/v3/errors"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
// "go.unistack.org/micro/v3/errors"
|
||||||
"github.com/unistack-org/micro/v3/register"
|
// "go.unistack.org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
|
"go.unistack.org/micro/v3/register"
|
||||||
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ server.Message = &rpcMessage{}
|
||||||
|
|
||||||
|
type rpcMessage struct {
|
||||||
|
payload interface{}
|
||||||
|
codec codec.Codec
|
||||||
|
header metadata.Metadata
|
||||||
|
topic string
|
||||||
|
contentType string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rpcMessage) ContentType() string {
|
||||||
|
return r.contentType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rpcMessage) Topic() string {
|
||||||
|
return r.topic
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rpcMessage) Body() interface{} {
|
||||||
|
return r.payload
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rpcMessage) Header() metadata.Metadata {
|
||||||
|
return r.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rpcMessage) Codec() codec.Codec {
|
||||||
|
return r.codec
|
||||||
|
}
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
reqType reflect.Type
|
reqType reflect.Type
|
||||||
ctxType reflect.Type
|
ctxType reflect.Type
|
||||||
@@ -102,18 +133,8 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broker.Handler {
|
func (g *Server) createSubHandler(sb *subscriber, opts server.Options) broker.Handler {
|
||||||
return func(p broker.Event) (err error) {
|
return func(p broker.Event) (err error) {
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
if g.opts.Logger.V(logger.ErrorLevel) {
|
|
||||||
g.opts.Logger.Error(g.opts.Context, "panic recovered: ", r)
|
|
||||||
g.opts.Logger.Error(g.opts.Context, string(debug.Stack()))
|
|
||||||
}
|
|
||||||
err = errors.InternalServerError(g.opts.Name+".subscriber", "panic recovered: %v", r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
msg := p.Message()
|
msg := p.Message()
|
||||||
// if we don't have headers, create empty map
|
// if we don't have headers, create empty map
|
||||||
if msg.Header == nil {
|
if msg.Header == nil {
|
||||||
@@ -122,8 +143,8 @@ func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broke
|
|||||||
|
|
||||||
ct := msg.Header["Content-Type"]
|
ct := msg.Header["Content-Type"]
|
||||||
if len(ct) == 0 {
|
if len(ct) == 0 {
|
||||||
msg.Header["Content-Type"] = defaultContentType
|
msg.Header["Content-Type"] = DefaultContentType
|
||||||
ct = defaultContentType
|
ct = DefaultContentType
|
||||||
}
|
}
|
||||||
cf, err := g.newCodec(ct)
|
cf, err := g.newCodec(ct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -132,9 +153,6 @@ func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broke
|
|||||||
|
|
||||||
hdr := make(map[string]string, len(msg.Header))
|
hdr := make(map[string]string, len(msg.Header))
|
||||||
for k, v := range msg.Header {
|
for k, v := range msg.Header {
|
||||||
if k == "Content-Type" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
hdr[k] = v
|
hdr[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +189,7 @@ func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broke
|
|||||||
vals = append(vals, reflect.ValueOf(ctx))
|
vals = append(vals, reflect.ValueOf(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
vals = append(vals, reflect.ValueOf(msg.Payload()))
|
vals = append(vals, reflect.ValueOf(msg.Body()))
|
||||||
|
|
||||||
returnValues := handler.method.Call(vals)
|
returnValues := handler.method.Call(vals)
|
||||||
if rerr := returnValues[0].Interface(); rerr != nil {
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
||||||
@@ -196,7 +214,6 @@ func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broke
|
|||||||
contentType: ct,
|
contentType: ct,
|
||||||
payload: req.Interface(),
|
payload: req.Interface(),
|
||||||
header: msg.Header,
|
header: msg.Header,
|
||||||
body: msg.Body,
|
|
||||||
})
|
})
|
||||||
results <- cerr
|
results <- cerr
|
||||||
}()
|
}()
|
||||||
|
Reference in New Issue
Block a user