Compare commits
224 Commits
Author | SHA1 | Date | |
---|---|---|---|
e3fe27105b | |||
0f8ead6acc | |||
19a469c4e2 | |||
2a6a93a792 | |||
40b5402aa5 | |||
|
e8b7e30e4d | ||
55e7e3d61d | |||
|
d75c36938a | ||
a7cec360e9 | |||
|
3978256931 | ||
6dfff28203 | |||
|
373acda151 | ||
3f9ed4e83b | |||
|
0486780f36 | ||
36788b5fbf | |||
|
c79b83a3c1 | ||
a6585fd6d4 | |||
|
7fce5ccad9 | ||
b5a5e98f9e | |||
|
e419c6a67c | ||
17c46e63a2 | |||
|
cdf1a2c3e3 | ||
aba3df63df | |||
|
337df46029 | ||
bfe4839a00 | |||
|
5474c37f8f | ||
ba88a5568a | |||
|
57f903a8c2 | ||
9f918bd3f2 | |||
|
0d88050daf | ||
|
d1ed5bed51 | ||
|
42697318b1 | ||
28aea45725 | |||
|
ed72c05645 | ||
|
d239dcde63 | ||
6eb07dc351 | |||
|
a0d704f845 | ||
4b8a761f30 | |||
|
a2c711a1b5 | ||
be564f50aa | |||
|
b7b1eff81c | ||
671a6b6f7c | |||
27c1c4d86b | |||
76d37a99eb | |||
|
4c2827172f | ||
e3461dd23f | |||
|
d8b5c011e5 | ||
|
15abd38afd | ||
5865a0f388 | |||
|
67da8d1165 | ||
|
c7d24caa03 | ||
8c222c4715 | |||
6e91cd5cf5 | |||
a9e673b2ef | |||
251f06cc31 | |||
e97e4580a1 | |||
bd7dbe94ca | |||
|
0f32fad4c0 | ||
|
858111106e | ||
|
dc35dc6d3e | ||
|
24b1abfb9a | ||
|
205fd53047 | ||
|
b3b7d1af13 | ||
80e2184bba | |||
|
a920d15d95 | ||
5ecbfac164 | |||
|
61d7a322de | ||
|
b43c207f6a | ||
|
d243b884c7 | ||
107b470b9a | |||
|
878bac53ac | ||
e28c584056 | |||
b8dc821784 | |||
|
907d2591df | ||
|
8cc656eec7 | ||
|
5792434604 | ||
402ccee5b9 | |||
|
90986a26e2 | ||
6d41afd5a0 | |||
2e645748bb | |||
f6b1c8d745 | |||
ea503d0583 | |||
048773c669 | |||
|
e3cb87ffe3 | ||
b1af43c4b0 | |||
|
f994df9e04 | ||
c556f7157f | |||
|
17be582d19 | ||
131a150d3d | |||
|
7f874a286e | ||
2f40797303 | |||
|
3e1b2b7c5d | ||
6442f4c474 | |||
|
1dda7e6b83 | ||
2b85cabe1f | |||
02895dd712 | |||
42b93ce57e | |||
763c299ab7 | |||
|
937a6d62b2 | ||
fb6e2c8845 | |||
3e9a3a917d | |||
289a765784 | |||
b13ad231d1 | |||
0793e84da6 | |||
|
f2c6d7cc80 | ||
6969b228a7 | |||
84362e6dd9 | |||
|
b83cc26ca0 | ||
|
6591845ded | ||
7c7c93521f | |||
9e10237b97 | |||
|
856c7dae7c | ||
e85f2e1f45 | |||
|
0a8ccce4e1 | ||
1b7e22442f | |||
|
4c81ce6a9d | ||
89ffe47d06 | |||
|
a03274011f | ||
186ec6bf00 | |||
|
fefcc273d9 | ||
a5ca5ec499 | |||
|
7104528c7d | ||
eaa61e254e | |||
|
00aa5331f6 | ||
c165c0f1a8 | |||
|
fa6fe590bd | ||
8cae060e05 | |||
a10275ad1f | |||
|
f247a8c906 | ||
|
015b2a4b05 | ||
|
c9da9d36a0 | ||
1f69062916 | |||
c4ca900a56 | |||
|
569d95e3e3 | ||
d2f1b7b3b9 | |||
|
65c10f7b0a | ||
76537a045a | |||
|
7f19288476 | ||
c3d10b669b | |||
20c5840f47 | |||
8b2dd91711 | |||
|
08621f7cf3 | ||
|
8d7d802730 | ||
137fbae58e | |||
|
13093dd404 | ||
12cf576b9c | |||
|
b91014d287 | ||
6b867760e5 | |||
|
f61f056a8e | ||
0525dae1cd | |||
|
50d0d836e5 | ||
a1e7560d6f | |||
a510f982e2 | |||
1129a1e992 | |||
|
033fcd3e2b | ||
c2ef582962 | |||
4c9f1f21a9 | |||
|
4585b73513 | ||
|
728d3f2a8c | ||
c1a2cfae67 | |||
8edc3d35bb | |||
|
4d0bb35dd9 | ||
0539d08195 | |||
f40591a520 | |||
acfab7e10c | |||
6697ccddf2 | |||
d536140a5b | |||
db02559a00 | |||
0fae2e1bdd | |||
|
7a1fdef33a | ||
2bb81ff232 | |||
|
9ac268b2f0 | ||
510fa4b379 | |||
|
08def4d244 | ||
a4683c0b78 | |||
|
637e3df24a | ||
|
36c7fa6a23 | ||
2bef21a001 | |||
|
78c32dc3e8 | ||
|
4d5a2d1a4a | ||
|
9817e0c1af | ||
|
ca4d561efe | ||
|
0ffbfc36d7 | ||
|
8a1b5b1130 | ||
|
1048869e6c | ||
|
35e9b9b77e | ||
|
faeaff358d | ||
|
3d7a5f6122 | ||
9030637204 | |||
|
225cdfd469 | ||
|
d3696a4bf8 | ||
|
88432401ac | ||
|
e3383bae9b | ||
|
60c340cc86 | ||
|
8d237e1423 | ||
|
be27263047 | ||
|
41a60a8a16 | ||
2e09afca64 | |||
|
aeb25fec3b | ||
a4518a12eb | |||
2a6e2c72c1 | |||
|
1d17c6c962 | ||
|
11c265458c | ||
8d34fe5dc0 | |||
8c480b5f0e | |||
c1e03ca61d | |||
|
22dc300099 | ||
|
8584261b44 | ||
4d31515547 | |||
9f36491023 | |||
|
1378d624be | ||
0fe6470260 | |||
db1148c1d1 | |||
bbd07bb091 | |||
067cf68d23 | |||
7ef49b03ba | |||
60ad0903b9 | |||
2ed58a202b | |||
15a5d7d2cd | |||
131916baca | |||
3aa3132caa | |||
854839ba45 | |||
5350b6ec2b | |||
c59d400857 |
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"
|
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@v1
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
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@v1
|
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@v1
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
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@v1
|
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
|
44
.golangci.yml
Normal file
44
.golangci.yml
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
run:
|
||||||
|
concurrency: 4
|
||||||
|
deadline: 5m
|
||||||
|
issues-exit-code: 1
|
||||||
|
tests: true
|
||||||
|
|
||||||
|
linters-settings:
|
||||||
|
govet:
|
||||||
|
check-shadowing: true
|
||||||
|
enable:
|
||||||
|
- fieldalignment
|
||||||
|
|
||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- govet
|
||||||
|
- deadcode
|
||||||
|
- errcheck
|
||||||
|
- govet
|
||||||
|
- ineffassign
|
||||||
|
- staticcheck
|
||||||
|
- structcheck
|
||||||
|
- typecheck
|
||||||
|
- unused
|
||||||
|
- varcheck
|
||||||
|
- bodyclose
|
||||||
|
- gci
|
||||||
|
- goconst
|
||||||
|
- gocritic
|
||||||
|
- gosimple
|
||||||
|
- gofmt
|
||||||
|
- gofumpt
|
||||||
|
- goimports
|
||||||
|
- golint
|
||||||
|
- gosec
|
||||||
|
- makezero
|
||||||
|
- misspell
|
||||||
|
- nakedret
|
||||||
|
- nestif
|
||||||
|
- nilerr
|
||||||
|
- noctx
|
||||||
|
- prealloc
|
||||||
|
- unconvert
|
||||||
|
- unparam
|
||||||
|
disable-all: false
|
192
LICENSE
Normal file
192
LICENSE
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2015-2020 Asim Aslam.
|
||||||
|
Copyright 2019-2020 Unistack LLC.
|
||||||
|
|
||||||
|
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.
|
25
README.md
25
README.md
@ -1,25 +0,0 @@
|
|||||||
# GRPC Client
|
|
||||||
|
|
||||||
The grpc client is a [micro.Client](https://godoc.org/github.com/micro/go-micro/client#Client) compatible client.
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
The client makes use of the [google.golang.org/grpc](google.golang.org/grpc) framework for the underlying communication mechanism.
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Specify the client to your micro service
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"github.com/micro/go-micro"
|
|
||||||
"github.com/micro/go-plugins/client/grpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
service := micro.NewService(
|
|
||||||
micro.Name("greeter"),
|
|
||||||
micro.Client(grpc.NewClient()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
```
|
|
240
codec.go
240
codec.go
@ -1,231 +1,127 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
b "bytes"
|
"io"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
oldjsonpb "github.com/golang/protobuf/jsonpb"
|
"go.unistack.org/micro/v3/codec"
|
||||||
oldproto "github.com/golang/protobuf/proto"
|
|
||||||
bytes "github.com/unistack-org/micro-codec-bytes"
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
|
||||||
"google.golang.org/protobuf/proto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type jsonCodec struct{}
|
|
||||||
type protoCodec struct{}
|
|
||||||
type bytesCodec struct{}
|
|
||||||
type wrapCodec struct{ encoding.Codec }
|
|
||||||
|
|
||||||
var jsonpbMarshaler = &jsonpb.MarshalOptions{}
|
|
||||||
var oldjsonpbMarshaler = &oldjsonpb.Marshaler{}
|
|
||||||
var useNumber bool
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultGRPCCodecs = map[string]encoding.Codec{
|
_ encoding.Codec = &wrapMicroCodec{}
|
||||||
"application/json": jsonCodec{},
|
_ codec.Codec = &wrapGrpcCodec{}
|
||||||
"application/proto": protoCodec{},
|
|
||||||
"application/protobuf": protoCodec{},
|
|
||||||
"application/octet-stream": protoCodec{},
|
|
||||||
"application/grpc": protoCodec{},
|
|
||||||
"application/grpc+json": jsonCodec{},
|
|
||||||
"application/grpc+proto": protoCodec{},
|
|
||||||
"application/grpc+bytes": bytesCodec{},
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UseNumber fix unmarshal Number(8234567890123456789) to interface(8.234567890123457e+18)
|
type wrapStream struct{ grpc.ClientStream }
|
||||||
func UseNumber() {
|
|
||||||
useNumber = true
|
func (w *wrapStream) Write(d []byte) (int, error) {
|
||||||
|
n := len(d)
|
||||||
|
err := w.ClientStream.SendMsg(&codec.Frame{Data: d})
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w wrapCodec) String() string {
|
func (w *wrapStream) Read(d []byte) (int, error) {
|
||||||
|
m := &codec.Frame{}
|
||||||
|
err := w.ClientStream.RecvMsg(m)
|
||||||
|
copy(d, m.Data)
|
||||||
|
return len(d), err
|
||||||
|
}
|
||||||
|
|
||||||
|
type wrapMicroCodec struct{ codec.Codec }
|
||||||
|
|
||||||
|
func (w *wrapMicroCodec) Name() 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 }
|
||||||
|
|
||||||
|
func (w *wrapGrpcCodec) String() string {
|
||||||
return w.Codec.Name()
|
return w.Codec.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w wrapCodec) Marshal(v interface{}) ([]byte, error) {
|
func (w *wrapGrpcCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||||
switch m := v.(type) {
|
if m, ok := v.(*codec.Frame); ok {
|
||||||
case *bytes.Frame:
|
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
}
|
}
|
||||||
return w.Codec.Marshal(v)
|
return w.Codec.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w wrapCodec) Unmarshal(data []byte, v interface{}) error {
|
func (w *wrapGrpcCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
||||||
if len(data) == 0 {
|
if d == nil || v == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if v == nil {
|
if m, ok := v.(*codec.Frame); ok {
|
||||||
|
m.Data = d
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
switch m := v.(type) {
|
return w.Codec.Unmarshal(d, v)
|
||||||
case *bytes.Frame:
|
|
||||||
m.Data = data
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return w.Codec.Unmarshal(data, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
|
|
||||||
switch m := v.(type) {
|
|
||||||
case *bytes.Frame:
|
|
||||||
return m.Data, nil
|
|
||||||
case proto.Message:
|
|
||||||
return proto.Marshal(m)
|
|
||||||
case oldproto.Message:
|
|
||||||
return oldproto.Marshal(m)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("failed to marshal: %v is not type of *bytes.Frame or proto.Message", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
|
|
||||||
if len(data) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if v == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
switch m := v.(type) {
|
|
||||||
case *bytes.Frame:
|
|
||||||
m.Data = data
|
|
||||||
return nil
|
|
||||||
case proto.Message:
|
|
||||||
return proto.Unmarshal(data, m)
|
|
||||||
case oldproto.Message:
|
|
||||||
return oldproto.Unmarshal(data, m)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to unmarshal: %v is not type of *bytes.Frame or proto.Message", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (protoCodec) Name() string {
|
|
||||||
return "proto"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bytesCodec) Marshal(v interface{}) ([]byte, error) {
|
|
||||||
switch m := v.(type) {
|
|
||||||
case *[]byte:
|
|
||||||
return *m, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
|
|
||||||
if len(data) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if v == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
switch m := v.(type) {
|
|
||||||
case *[]byte:
|
|
||||||
*m = data
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bytesCodec) Name() string {
|
|
||||||
return "bytes"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
|
||||||
switch m := v.(type) {
|
|
||||||
case *bytes.Frame:
|
|
||||||
return m.Data, nil
|
|
||||||
case proto.Message:
|
|
||||||
return jsonpbMarshaler.Marshal(m)
|
|
||||||
case oldproto.Message:
|
|
||||||
buf, err := oldjsonpbMarshaler.MarshalToString(m)
|
|
||||||
return []byte(buf), err
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.Marshal(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
|
|
||||||
if len(data) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if v == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
switch m := v.(type) {
|
|
||||||
case *bytes.Frame:
|
|
||||||
m.Data = data
|
|
||||||
return nil
|
|
||||||
case proto.Message:
|
|
||||||
return jsonpb.Unmarshal(data, m)
|
|
||||||
case oldproto.Message:
|
|
||||||
return oldjsonpb.Unmarshal(b.NewReader(data), m)
|
|
||||||
}
|
|
||||||
dec := json.NewDecoder(b.NewReader(data))
|
|
||||||
if useNumber {
|
|
||||||
dec.UseNumber()
|
|
||||||
}
|
|
||||||
return dec.Decode(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (jsonCodec) Name() string {
|
|
||||||
return "json"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
type grpcCodec struct {
|
type grpcCodec struct {
|
||||||
|
grpc.ServerStream
|
||||||
// headers
|
// headers
|
||||||
id string
|
id string
|
||||||
target string
|
target string
|
||||||
method string
|
method string
|
||||||
endpoint string
|
endpoint string
|
||||||
|
|
||||||
s grpc.ClientStream
|
|
||||||
c encoding.Codec
|
c encoding.Codec
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
|
*/
|
||||||
md, err := g.s.Header()
|
|
||||||
if err != nil {
|
func (w *wrapGrpcCodec) ReadHeader(conn io.Reader, m *codec.Message, mt codec.MessageType) error {
|
||||||
return err
|
/*
|
||||||
}
|
|
||||||
if m == nil {
|
if m == nil {
|
||||||
m = new(codec.Message)
|
m = codec.NewMessage(codec.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if md, ok := metadata.FromIncomingContext(g.ServerStream.Context()); ok {
|
||||||
if m.Header == nil {
|
if m.Header == nil {
|
||||||
m.Header = make(map[string]string, len(md))
|
m.Header = meta.New(len(md))
|
||||||
}
|
}
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
m.Header[k] = strings.Join(v, ",")
|
m.Header[k] = strings.Join(v, ",")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m.Id = g.id
|
m.Id = g.id
|
||||||
m.Target = g.target
|
m.Target = g.target
|
||||||
m.Method = g.method
|
m.Method = g.method
|
||||||
m.Endpoint = g.endpoint
|
m.Endpoint = g.endpoint
|
||||||
|
*/
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcCodec) ReadBody(v interface{}) error {
|
func (w *wrapGrpcCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||||
switch m := v.(type) {
|
// caller has requested a frame
|
||||||
case *bytes.Frame:
|
if m, ok := v.(*codec.Frame); ok {
|
||||||
return g.s.RecvMsg(m)
|
_, err := conn.Read(m.Data)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return g.s.RecvMsg(v)
|
return codec.ErrInvalidMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcCodec) Write(m *codec.Message, v interface{}) error {
|
func (w *wrapGrpcCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
||||||
// if we don't have a body
|
// if we don't have a body
|
||||||
if v != nil {
|
if v != nil {
|
||||||
return g.s.SendMsg(v)
|
b, err := w.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.Body = b
|
||||||
}
|
}
|
||||||
// write the body using the framing codec
|
// write the body using the framing codec
|
||||||
return g.s.SendMsg(&bytes.Frame{Data: m.Body})
|
_, err := conn.Write(m.Body)
|
||||||
}
|
return err
|
||||||
|
|
||||||
func (g *grpcCodec) Close() error {
|
|
||||||
return g.s.CloseSend()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *grpcCodec) String() string {
|
|
||||||
return g.c.Name()
|
|
||||||
}
|
}
|
||||||
|
40
error.go
40
error.go
@ -1,8 +1,7 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
pberr "github.com/unistack-org/micro-client-grpc/errors"
|
"go.unistack.org/micro/v3/errors"
|
||||||
"github.com/unistack-org/micro/v3/errors"
|
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,35 +9,46 @@ func microError(err error) error {
|
|||||||
// no error
|
// no error
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
// nothing to do
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if verr, ok := err.(*errors.Error); ok {
|
if verr, ok := err.(*errors.Error); ok {
|
||||||
|
// micro error
|
||||||
return verr
|
return verr
|
||||||
}
|
}
|
||||||
|
|
||||||
if verr, ok := err.(*pberr.Error); ok {
|
|
||||||
return &errors.Error{Id: verr.Id, Code: verr.Code, Detail: verr.Detail, Status: verr.Status}
|
|
||||||
}
|
|
||||||
|
|
||||||
// grpc error
|
// grpc error
|
||||||
s, ok := status.FromError(err)
|
s, ok := status.FromError(err)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
// can't get status detals from grpc error, return base error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// return first error from details
|
details := s.Details()
|
||||||
if details := s.Details(); len(details) > 0 {
|
switch len(details) {
|
||||||
if verr, ok := details[0].(error); ok {
|
case 0:
|
||||||
return microError(verr)
|
if verr := errors.Parse(s.Message()); verr.Code > 0 {
|
||||||
|
// return micro error
|
||||||
|
return verr
|
||||||
}
|
}
|
||||||
|
// return base error as it not micro error
|
||||||
|
return err
|
||||||
|
case 1:
|
||||||
|
if verr, ok := details[0].(*errors.Error); ok {
|
||||||
|
// return nested micro error
|
||||||
|
return verr
|
||||||
|
}
|
||||||
|
// return base error as it not holds micro error
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to decode micro *errors.Error
|
// attached messages in details more then 1, try to fallback to micro error
|
||||||
if e := errors.Parse(s.Message()); e.Code > 0 {
|
if verr := errors.Parse(s.Message()); verr.Code > 0 {
|
||||||
return e // actually a micro error
|
// return micro error
|
||||||
|
return verr
|
||||||
}
|
}
|
||||||
|
|
||||||
// fallback
|
// not micro error return base error
|
||||||
return errors.InternalServerError("go.micro.client", s.Message())
|
return err
|
||||||
}
|
}
|
||||||
|
159
errors/errors.go
159
errors/errors.go
@ -1,159 +0,0 @@
|
|||||||
package errors
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (e *Error) Error() string {
|
|
||||||
return fmt.Sprintf(`{"id":"%s","code":%d,"detail":"%s","status":"%s"}`, e.Id, e.Code, e.Detail, e.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
// New generates a custom error.
|
|
||||||
func New(id, detail string, code int32) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: code,
|
|
||||||
Detail: detail,
|
|
||||||
// Status: http.StatusText(int(code)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BadRequest generates a 400 error.
|
|
||||||
func BadRequest(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 400,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
// Status: http.StatusText(400),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unauthorized generates a 401 error.
|
|
||||||
func Unauthorized(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 401,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
// Status: http.StatusText(401),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forbidden generates a 403 error.
|
|
||||||
func Forbidden(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 403,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
// Status: http.StatusText(403),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotFound generates a 404 error.
|
|
||||||
func NotFound(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 404,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
// Status: http.StatusText(404),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MethodNotAllowed generates a 405 error.
|
|
||||||
func MethodNotAllowed(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 405,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
//Status: http.StatusText(405),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Timeout generates a 408 error.
|
|
||||||
func Timeout(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 408,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
//Status: http.StatusText(408),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Conflict generates a 409 error.
|
|
||||||
func Conflict(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 409,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
//Status: http.StatusText(409),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// InternalServerError generates a 500 error.
|
|
||||||
func InternalServerError(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 500,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
//Status: http.StatusText(500),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotImplemented generates a 501 error
|
|
||||||
func NotImplemented(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 501,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
//Status: http.StatusText(501),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BadGateway generates a 502 error
|
|
||||||
func BadGateway(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 502,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
// Status: http.StatusText(502),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServiceUnavailable generates a 503 error
|
|
||||||
func ServiceUnavailable(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 503,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
//Status: http.StatusText(503),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GatewayTimeout generates a 504 error
|
|
||||||
func GatewayTimeout(id, format string, a ...interface{}) error {
|
|
||||||
return &Error{
|
|
||||||
Id: id,
|
|
||||||
Code: 504,
|
|
||||||
Detail: fmt.Sprintf(format, a...),
|
|
||||||
//Status: http.StatusText(504),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Equal tries to compare errors
|
|
||||||
func Equal(err1 error, err2 error) bool {
|
|
||||||
verr1, ok1 := err1.(*Error)
|
|
||||||
verr2, ok2 := err2.(*Error)
|
|
||||||
|
|
||||||
if ok1 != ok2 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ok1 {
|
|
||||||
return err1 == err2
|
|
||||||
}
|
|
||||||
|
|
||||||
if verr1.Code != verr2.Code {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
@ -1,176 +0,0 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
|
||||||
// versions:
|
|
||||||
// protoc-gen-go v1.25.0-devel
|
|
||||||
// protoc v3.6.1
|
|
||||||
// source: errors.proto
|
|
||||||
|
|
||||||
package errors
|
|
||||||
|
|
||||||
import (
|
|
||||||
proto "github.com/golang/protobuf/proto"
|
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
// This is a compile-time assertion that a sufficiently up-to-date version
|
|
||||||
// of the legacy proto package is being used.
|
|
||||||
const _ = proto.ProtoPackageIsVersion4
|
|
||||||
|
|
||||||
type Error struct {
|
|
||||||
state protoimpl.MessageState
|
|
||||||
sizeCache protoimpl.SizeCache
|
|
||||||
unknownFields protoimpl.UnknownFields
|
|
||||||
|
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
|
||||||
Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
|
|
||||||
Detail string `protobuf:"bytes,3,opt,name=detail,proto3" json:"detail,omitempty"`
|
|
||||||
Status string `protobuf:"bytes,4,opt,name=status,proto3" json:"status,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *Error) Reset() {
|
|
||||||
*x = Error{}
|
|
||||||
if protoimpl.UnsafeEnabled {
|
|
||||||
mi := &file_errors_proto_msgTypes[0]
|
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *Error) String() string {
|
|
||||||
return protoimpl.X.MessageStringOf(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*Error) ProtoMessage() {}
|
|
||||||
|
|
||||||
func (x *Error) ProtoReflect() protoreflect.Message {
|
|
||||||
mi := &file_errors_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 Error.ProtoReflect.Descriptor instead.
|
|
||||||
func (*Error) Descriptor() ([]byte, []int) {
|
|
||||||
return file_errors_proto_rawDescGZIP(), []int{0}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *Error) GetId() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Id
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *Error) GetCode() int32 {
|
|
||||||
if x != nil {
|
|
||||||
return x.Code
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *Error) GetDetail() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Detail
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *Error) GetStatus() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Status
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var File_errors_proto protoreflect.FileDescriptor
|
|
||||||
|
|
||||||
var file_errors_proto_rawDesc = []byte{
|
|
||||||
0x0a, 0x0c, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x25,
|
|
||||||
0x6f, 0x72, 0x67, 0x2e, 0x75, 0x6e, 0x69, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2e, 0x6d, 0x69, 0x63,
|
|
||||||
0x72, 0x6f, 0x2e, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x65,
|
|
||||||
0x72, 0x72, 0x6f, 0x72, 0x73, 0x22, 0x5b, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x0e,
|
|
||||||
0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12,
|
|
||||||
0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f,
|
|
||||||
0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x18, 0x03, 0x20, 0x01,
|
|
||||||
0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74,
|
|
||||||
0x61, 0x74, 0x75, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74,
|
|
||||||
0x75, 0x73, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x3b, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x62, 0x06,
|
|
||||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
file_errors_proto_rawDescOnce sync.Once
|
|
||||||
file_errors_proto_rawDescData = file_errors_proto_rawDesc
|
|
||||||
)
|
|
||||||
|
|
||||||
func file_errors_proto_rawDescGZIP() []byte {
|
|
||||||
file_errors_proto_rawDescOnce.Do(func() {
|
|
||||||
file_errors_proto_rawDescData = protoimpl.X.CompressGZIP(file_errors_proto_rawDescData)
|
|
||||||
})
|
|
||||||
return file_errors_proto_rawDescData
|
|
||||||
}
|
|
||||||
|
|
||||||
var file_errors_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
|
|
||||||
var file_errors_proto_goTypes = []interface{}{
|
|
||||||
(*Error)(nil), // 0: org.unistack.micro.client.grpc.errors.Error
|
|
||||||
}
|
|
||||||
var file_errors_proto_depIdxs = []int32{
|
|
||||||
0, // [0:0] is the sub-list for method output_type
|
|
||||||
0, // [0:0] 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_errors_proto_init() }
|
|
||||||
func file_errors_proto_init() {
|
|
||||||
if File_errors_proto != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !protoimpl.UnsafeEnabled {
|
|
||||||
file_errors_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
|
||||||
switch v := v.(*Error); 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_errors_proto_rawDesc,
|
|
||||||
NumEnums: 0,
|
|
||||||
NumMessages: 1,
|
|
||||||
NumExtensions: 0,
|
|
||||||
NumServices: 0,
|
|
||||||
},
|
|
||||||
GoTypes: file_errors_proto_goTypes,
|
|
||||||
DependencyIndexes: file_errors_proto_depIdxs,
|
|
||||||
MessageInfos: file_errors_proto_msgTypes,
|
|
||||||
}.Build()
|
|
||||||
File_errors_proto = out.File
|
|
||||||
file_errors_proto_rawDesc = nil
|
|
||||||
file_errors_proto_goTypes = nil
|
|
||||||
file_errors_proto_depIdxs = nil
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
|
||||||
// source: errors.proto
|
|
||||||
|
|
||||||
package errors
|
|
||||||
|
|
||||||
import (
|
|
||||||
fmt "fmt"
|
|
||||||
proto "github.com/golang/protobuf/proto"
|
|
||||||
math "math"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ = proto.Marshal
|
|
||||||
var _ = fmt.Errorf
|
|
||||||
var _ = math.Inf
|
|
||||||
|
|
||||||
// This is a compile-time assertion to ensure that this generated file
|
|
||||||
// is compatible with the proto package it is being compiled against.
|
|
||||||
// A compilation error at this line likely means your copy of the
|
|
||||||
// proto package needs to be updated.
|
|
||||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
|
@ -1,11 +0,0 @@
|
|||||||
syntax = "proto3";
|
|
||||||
|
|
||||||
option go_package = ".;errors";
|
|
||||||
package org.unistack.micro.client.grpc.errors;
|
|
||||||
|
|
||||||
message Error {
|
|
||||||
string id = 1;
|
|
||||||
int32 code = 2;
|
|
||||||
string detail = 3;
|
|
||||||
string status = 4;
|
|
||||||
};
|
|
@ -1,3 +0,0 @@
|
|||||||
package grpc
|
|
||||||
|
|
||||||
//go:generate protoc -I./errors -I. --go-grpc_out=paths=source_relative:./errors --go_out=paths=source_relative:./errors --micro_out=paths=source_relative:./errors errors/errors.proto
|
|
15
go.mod
15
go.mod
@ -1,15 +1,8 @@
|
|||||||
module github.com/unistack-org/micro-client-grpc
|
module go.unistack.org/micro-client-grpc/v3
|
||||||
|
|
||||||
go 1.15
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang/protobuf v1.4.2
|
go.unistack.org/micro/v3 v3.10.22
|
||||||
github.com/google/go-cmp v0.5.1 // indirect
|
google.golang.org/grpc v1.52.3
|
||||||
github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844
|
|
||||||
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920124807-9b11ea527aeb
|
|
||||||
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 // indirect
|
|
||||||
golang.org/x/text v0.3.3 // indirect
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
|
||||||
google.golang.org/grpc v1.31.1
|
|
||||||
google.golang.org/protobuf v1.25.0
|
|
||||||
)
|
)
|
||||||
|
425
grpc.go
425
grpc.go
@ -1,57 +1,52 @@
|
|||||||
// Package grpc provides a gRPC client
|
// Package grpc provides a gRPC client
|
||||||
package grpc
|
package grpc // import "go.unistack.org/micro-client-grpc/v3"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
raw "github.com/unistack-org/micro-codec-bytes"
|
"go.unistack.org/micro/v3/broker"
|
||||||
"github.com/unistack-org/micro/v3/broker"
|
"go.unistack.org/micro/v3/client"
|
||||||
"github.com/unistack-org/micro/v3/client"
|
"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/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
|
"go.unistack.org/micro/v3/selector"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials"
|
"google.golang.org/grpc/credentials"
|
||||||
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
gmetadata "google.golang.org/grpc/metadata"
|
gmetadata "google.golang.org/grpc/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type grpcClient struct {
|
const (
|
||||||
opts client.Options
|
DefaultContentType = "application/grpc"
|
||||||
codecs map[string]encoding.Codec
|
)
|
||||||
pool *pool
|
|
||||||
once atomic.Value
|
|
||||||
sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
type grpcClient struct {
|
||||||
encoding.RegisterCodec(wrapCodec{jsonCodec{}})
|
pool *ConnPool
|
||||||
encoding.RegisterCodec(wrapCodec{protoCodec{}})
|
opts client.Options
|
||||||
encoding.RegisterCodec(wrapCodec{bytesCodec{}})
|
sync.RWMutex
|
||||||
|
init bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// secure returns the dial option for whether its a secure or insecure connection
|
// secure returns the dial option for whether its a secure or insecure connection
|
||||||
func (g *grpcClient) secure(addr string) grpc.DialOption {
|
func (g *grpcClient) secure(addr string) grpc.DialOption {
|
||||||
// first we check if theres'a tls config
|
// first we check if theres'a tls config
|
||||||
if g.opts.Context != nil {
|
if g.opts.TLSConfig != nil {
|
||||||
if v := g.opts.Context.Value(tlsAuth{}); v != nil {
|
creds := credentials.NewTLS(g.opts.TLSConfig)
|
||||||
tls := v.(*tls.Config)
|
|
||||||
creds := credentials.NewTLS(tls)
|
|
||||||
// return tls config if it exists
|
// return tls config if it exists
|
||||||
return grpc.WithTransportCredentials(creds)
|
return grpc.WithTransportCredentials(creds)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// default config
|
// default config
|
||||||
tlsConfig := &tls.Config{}
|
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12}
|
||||||
defaultCreds := grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
|
defaultCreds := grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
|
||||||
|
|
||||||
// check if the address is prepended with https
|
// check if the address is prepended with https
|
||||||
@ -69,37 +64,41 @@ func (g *grpcClient) secure(addr string) grpc.DialOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// other fallback to insecure
|
// other fallback to insecure
|
||||||
return grpc.WithInsecure()
|
return grpc.WithTransportCredentials(insecure.NewCredentials())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
func (g *grpcClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
||||||
var header map[string]string
|
var header map[string]string
|
||||||
|
|
||||||
header = make(map[string]string)
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
if md, ok := metadata.FromContext(ctx); ok {
|
|
||||||
header = make(map[string]string, len(md))
|
header = make(map[string]string, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
header[strings.ToLower(k)] = v
|
header[strings.ToLower(k)] = v
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
header = make(map[string]string)
|
header = make(map[string]string, 2)
|
||||||
|
}
|
||||||
|
if opts.RequestMetadata != nil {
|
||||||
|
for k, v := range opts.RequestMetadata {
|
||||||
|
header[k] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// set timeout in nanoseconds
|
// set timeout in nanoseconds
|
||||||
header["timeout"] = fmt.Sprintf("%d", opts.RequestTimeout)
|
header["Grpc-Timeout"] = fmt.Sprintf("%dn", opts.RequestTimeout)
|
||||||
// set the content type for the request
|
header["timeout"] = fmt.Sprintf("%dn", opts.RequestTimeout)
|
||||||
header["x-content-type"] = req.ContentType()
|
header["content-type"] = req.ContentType()
|
||||||
|
|
||||||
md := gmetadata.New(header)
|
md := gmetadata.New(header)
|
||||||
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
||||||
|
|
||||||
cf, err := g.newGRPCCodec(req.ContentType())
|
cf, err := g.newCodec(req.ContentType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
maxRecvMsgSize := g.maxRecvMsgSizeValue()
|
maxRecvMsgSize := g.maxRecvMsgSizeValue()
|
||||||
maxSendMsgSize := g.maxSendMsgSizeValue()
|
maxSendMsgSize := g.maxSendMsgSizeValue()
|
||||||
|
cfgService := g.serviceConfig()
|
||||||
|
|
||||||
var grr error
|
var grr error
|
||||||
|
|
||||||
@ -118,30 +117,47 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
|
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
|
||||||
grpc.MaxCallSendMsgSize(maxSendMsgSize),
|
grpc.MaxCallSendMsgSize(maxSendMsgSize),
|
||||||
),
|
),
|
||||||
|
grpc.WithDefaultServiceConfig(cfgService),
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts := g.getGrpcDialOptions(); opts != nil {
|
if opts := g.getGrpcDialOptions(opts.Context); opts != nil {
|
||||||
grpcDialOptions = append(grpcDialOptions, opts...)
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
cc, err := g.pool.getConn(dialCtx, addr, grpcDialOptions...)
|
contextDialer := g.opts.ContextDialer
|
||||||
|
if opts.ContextDialer != nil {
|
||||||
|
contextDialer = opts.ContextDialer
|
||||||
|
}
|
||||||
|
if contextDialer != nil {
|
||||||
|
grpcDialOptions = append(grpcDialOptions, grpc.WithContextDialer(contextDialer))
|
||||||
|
}
|
||||||
|
|
||||||
|
cc, err := g.pool.Get(dialCtx, addr, grpcDialOptions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
// defer execution of release
|
// defer execution of release
|
||||||
g.pool.release(addr, cc, grr)
|
g.pool.Put(cc, grr)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ch := make(chan error, 1)
|
ch := make(chan error, 1)
|
||||||
|
var gmd gmetadata.MD
|
||||||
|
|
||||||
go func() {
|
|
||||||
grpcCallOptions := []grpc.CallOption{
|
grpcCallOptions := []grpc.CallOption{
|
||||||
grpc.ForceCodec(cf),
|
grpc.CallContentSubtype((&wrapMicroCodec{cf}).Name()),
|
||||||
grpc.CallContentSubtype(cf.Name())}
|
}
|
||||||
if opts := g.getGrpcCallOptions(); opts != nil {
|
|
||||||
|
if opts := g.getGrpcCallOptions(opts.Context); opts != nil {
|
||||||
grpcCallOptions = append(grpcCallOptions, opts...)
|
grpcCallOptions = append(grpcCallOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.ResponseMetadata != nil {
|
||||||
|
gmd = gmetadata.MD{}
|
||||||
|
grpcCallOptions = append(grpcCallOptions, grpc.Header(&gmd))
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
err := cc.Invoke(ctx, methodToGRPC(req.Service(), req.Endpoint()), req.Body(), rsp, grpcCallOptions...)
|
err := cc.Invoke(ctx, methodToGRPC(req.Service(), req.Endpoint()), req.Body(), rsp, grpcCallOptions...)
|
||||||
ch <- microError(err)
|
ch <- microError(err)
|
||||||
}()
|
}()
|
||||||
@ -153,13 +169,20 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
grr = errors.Timeout("go.micro.client", "%v", ctx.Err())
|
grr = errors.Timeout("go.micro.client", "%v", ctx.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.ResponseMetadata != nil {
|
||||||
|
*opts.ResponseMetadata = metadata.New(gmd.Len())
|
||||||
|
for k, v := range gmd {
|
||||||
|
opts.ResponseMetadata.Set(k, strings.Join(v, ","))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return grr
|
return grr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
||||||
var header map[string]string
|
var header map[string]string
|
||||||
|
|
||||||
if md, ok := metadata.FromContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
header = make(map[string]string, len(md))
|
header = make(map[string]string, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
header[k] = v
|
header[k] = v
|
||||||
@ -170,15 +193,16 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
|
|
||||||
// set timeout in nanoseconds
|
// set timeout in nanoseconds
|
||||||
if opts.StreamTimeout > time.Duration(0) {
|
if opts.StreamTimeout > time.Duration(0) {
|
||||||
header["timeout"] = fmt.Sprintf("%d", opts.StreamTimeout)
|
header["Grpc-Timeout"] = fmt.Sprintf("%dn", opts.StreamTimeout)
|
||||||
|
header["timeout"] = fmt.Sprintf("%dn", opts.StreamTimeout)
|
||||||
}
|
}
|
||||||
// set the content type for the request
|
// set the content type for the request
|
||||||
header["x-content-type"] = req.ContentType()
|
header["content-type"] = req.ContentType()
|
||||||
|
|
||||||
md := gmetadata.New(header)
|
md := gmetadata.New(header)
|
||||||
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
||||||
|
|
||||||
cf, err := g.newGRPCCodec(req.ContentType())
|
cf, err := g.newCodec(req.ContentType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
@ -192,17 +216,34 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
wc := wrapCodec{cf}
|
wc := &wrapMicroCodec{cf}
|
||||||
|
|
||||||
|
maxRecvMsgSize := g.maxRecvMsgSizeValue()
|
||||||
|
maxSendMsgSize := g.maxSendMsgSizeValue()
|
||||||
|
cfgService := g.serviceConfig()
|
||||||
|
|
||||||
grpcDialOptions := []grpc.DialOption{
|
grpcDialOptions := []grpc.DialOption{
|
||||||
g.secure(addr),
|
g.secure(addr),
|
||||||
|
grpc.WithDefaultCallOptions(
|
||||||
|
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
|
||||||
|
grpc.MaxCallSendMsgSize(maxSendMsgSize),
|
||||||
|
),
|
||||||
|
grpc.WithDefaultServiceConfig(cfgService),
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts := g.getGrpcDialOptions(); opts != nil {
|
if opts := g.getGrpcDialOptions(opts.Context); opts != nil {
|
||||||
grpcDialOptions = append(grpcDialOptions, opts...)
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
cc, err := g.pool.getConn(dialCtx, addr, grpcDialOptions...)
|
contextDialer := g.opts.ContextDialer
|
||||||
|
if opts.ContextDialer != nil {
|
||||||
|
contextDialer = opts.ContextDialer
|
||||||
|
}
|
||||||
|
if contextDialer != nil {
|
||||||
|
grpcDialOptions = append(grpcDialOptions, grpc.WithContextDialer(contextDialer))
|
||||||
|
}
|
||||||
|
|
||||||
|
cc, err := g.pool.Get(dialCtx, addr, grpcDialOptions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
||||||
}
|
}
|
||||||
@ -214,12 +255,17 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
grpcCallOptions := []grpc.CallOption{
|
grpcCallOptions := []grpc.CallOption{
|
||||||
grpc.ForceCodec(wc),
|
// grpc.ForceCodec(wc),
|
||||||
grpc.CallContentSubtype(cf.Name()),
|
grpc.CallContentSubtype(wc.Name()),
|
||||||
}
|
}
|
||||||
if opts := g.getGrpcCallOptions(); opts != nil {
|
if opts := g.getGrpcCallOptions(opts.Context); opts != nil {
|
||||||
grpcCallOptions = append(grpcCallOptions, opts...)
|
grpcCallOptions = append(grpcCallOptions, opts...)
|
||||||
}
|
}
|
||||||
|
var gmd gmetadata.MD
|
||||||
|
if opts.ResponseMetadata != nil {
|
||||||
|
gmd = gmetadata.MD{}
|
||||||
|
grpcCallOptions = append(grpcCallOptions, grpc.Header(&gmd))
|
||||||
|
}
|
||||||
|
|
||||||
// create a new cancelling context
|
// create a new cancelling context
|
||||||
newCtx, cancel := context.WithCancel(ctx)
|
newCtx, cancel := context.WithCancel(ctx)
|
||||||
@ -230,19 +276,14 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
// cancel the context
|
// cancel the context
|
||||||
cancel()
|
cancel()
|
||||||
// release the connection
|
// release the connection
|
||||||
g.pool.release(addr, cc, err)
|
g.pool.Put(cc, err)
|
||||||
// now return the error
|
// now return the error
|
||||||
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error creating stream: %v", err))
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error creating stream: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
codec := &grpcCodec{
|
|
||||||
s: st,
|
|
||||||
c: wc,
|
|
||||||
}
|
|
||||||
|
|
||||||
// set request codec
|
// set request codec
|
||||||
if r, ok := req.(*grpcRequest); ok {
|
if r, ok := req.(*grpcRequest); ok {
|
||||||
r.codec = codec
|
r.codec = cf
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup the stream response
|
// setup the stream response
|
||||||
@ -254,23 +295,23 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
conn: cc,
|
conn: cc,
|
||||||
stream: st,
|
stream: st,
|
||||||
codec: cf,
|
codec: cf,
|
||||||
gcodec: codec,
|
|
||||||
},
|
},
|
||||||
conn: cc,
|
conn: cc,
|
||||||
close: func(err error) {
|
close: func(err error) {
|
||||||
// cancel the context if an error occured
|
// cancel the context if an error occurred
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// defer execution of release
|
// defer execution of release
|
||||||
g.pool.release(addr, cc, err)
|
g.pool.Put(cc, err)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the stream as the response
|
// set the stream as the response
|
||||||
val := reflect.ValueOf(rsp).Elem()
|
val := reflect.ValueOf(rsp).Elem()
|
||||||
val.Set(reflect.ValueOf(stream).Elem())
|
val.Set(reflect.ValueOf(stream).Elem())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,17 +359,35 @@ func (g *grpcClient) maxSendMsgSizeValue() int {
|
|||||||
return v.(int)
|
return v.(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) newGRPCCodec(contentType string) (encoding.Codec, error) {
|
func (g *grpcClient) newCodec(ct string) (codec.Codec, error) {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
defer g.RUnlock()
|
defer g.RUnlock()
|
||||||
|
|
||||||
if c, ok := g.codecs[contentType]; ok {
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
return wrapCodec{c}, nil
|
ct = ct[:idx]
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
|
||||||
|
if c, ok := g.opts.Codecs[ct]; ok {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
return nil, codec.ErrUnknownContentType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *grpcClient) serviceConfig() string {
|
||||||
|
if g.opts.Context == nil {
|
||||||
|
return DefaultServiceConfig
|
||||||
|
}
|
||||||
|
v := g.opts.Context.Value(serviceConfigKey{})
|
||||||
|
if v == nil {
|
||||||
|
return DefaultServiceConfig
|
||||||
|
}
|
||||||
|
return v.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) Init(opts ...client.Option) error {
|
func (g *grpcClient) Init(opts ...client.Option) error {
|
||||||
|
if len(opts) == 0 && g.init {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
size := g.opts.PoolSize
|
size := g.opts.PoolSize
|
||||||
ttl := g.opts.PoolTTL
|
ttl := g.opts.PoolTTL
|
||||||
|
|
||||||
@ -344,6 +403,27 @@ func (g *grpcClient) Init(opts ...client.Option) error {
|
|||||||
g.pool.Unlock()
|
g.pool.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := g.opts.Broker.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.opts.Tracer.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.opts.Router.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.opts.Logger.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.opts.Meter.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.opts.Transport.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
g.init = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -367,6 +447,7 @@ func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
}
|
}
|
||||||
// make a copy of call opts
|
// make a copy of call opts
|
||||||
callOpts := g.opts.CallOptions
|
callOpts := g.opts.CallOptions
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&callOpts)
|
opt(&callOpts)
|
||||||
}
|
}
|
||||||
@ -415,20 +496,8 @@ func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
callOpts.Address = []string{g.opts.Proxy}
|
callOpts.Address = []string{g.opts.Proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookup the route to send the reques to
|
var next selector.Next
|
||||||
// TODO apply any filtering here
|
|
||||||
routes, err := g.opts.Lookup(ctx, req, callOpts)
|
|
||||||
if err != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// balance the list of nodes
|
|
||||||
next, err := callOpts.Selector.Select(routes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// return errors.New("go.micro.client", "request timeout", 408)
|
|
||||||
call := func(i int) error {
|
call := func(i int) error {
|
||||||
// call backoff first. Someone may want an initial start delay
|
// call backoff first. Someone may want an initial start delay
|
||||||
t, err := callOpts.Backoff(ctx, req, i)
|
t, err := callOpts.Backoff(ctx, req, i)
|
||||||
@ -441,6 +510,23 @@ func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
time.Sleep(t)
|
time.Sleep(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if next == nil {
|
||||||
|
var routes []string
|
||||||
|
|
||||||
|
// lookup the route to send the reques to
|
||||||
|
// TODO apply any filtering here
|
||||||
|
routes, err = g.opts.Lookup(ctx, req, callOpts)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// balance the list of nodes
|
||||||
|
next, err = callOpts.Selector.Select(routes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// get the next node
|
// get the next node
|
||||||
node := next()
|
node := next()
|
||||||
|
|
||||||
@ -532,18 +618,7 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
callOpts.Address = []string{g.opts.Proxy}
|
callOpts.Address = []string{g.opts.Proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookup the route to send the reques to
|
var next selector.Next
|
||||||
// TODO: move to internal lookup func
|
|
||||||
routes, err := g.opts.Lookup(ctx, req, callOpts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// balance the list of nodes
|
|
||||||
next, err := callOpts.Selector.Select(routes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
call := func(i int) (client.Stream, error) {
|
call := func(i int) (client.Stream, error) {
|
||||||
// call backoff first. Someone may want an initial start delay
|
// call backoff first. Someone may want an initial start delay
|
||||||
@ -557,12 +632,29 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
time.Sleep(t)
|
time.Sleep(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if next == nil {
|
||||||
|
var routes []string
|
||||||
|
|
||||||
|
// lookup the route to send the reques to
|
||||||
|
// TODO apply any filtering here
|
||||||
|
routes, err = g.opts.Lookup(ctx, req, callOpts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// balance the list of nodes
|
||||||
|
next, err = callOpts.Selector.Select(routes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// get the next node
|
// get the next node
|
||||||
node := next()
|
node := next()
|
||||||
|
|
||||||
// make the call
|
// make the call
|
||||||
stream := &grpcStream{}
|
stream := &grpcStream{}
|
||||||
err = g.stream(ctx, node, req, stream, callOpts)
|
err = gstream(ctx, node, req, stream, callOpts)
|
||||||
|
|
||||||
// record the result of the call to inform future routing decisions
|
// record the result of the call to inform future routing decisions
|
||||||
if verr := g.opts.Selector.Record(node, err); verr != nil {
|
if verr := g.opts.Selector.Record(node, err); verr != nil {
|
||||||
@ -574,7 +666,10 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
return nil, verr
|
return nil, verr
|
||||||
}
|
}
|
||||||
|
|
||||||
g.opts.Selector.Record(node, err)
|
if rerr := g.opts.Selector.Record(node, err); rerr != nil {
|
||||||
|
return nil, rerr
|
||||||
|
}
|
||||||
|
|
||||||
return stream, err
|
return stream, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -617,35 +712,42 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
return nil, grr
|
return nil, grr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *grpcClient) BatchPublish(ctx context.Context, ps []client.Message, opts ...client.PublishOption) error {
|
||||||
|
return g.publish(ctx, ps, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
||||||
var options client.PublishOptions
|
return g.publish(ctx, []client.Message{p}, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...client.PublishOption) error {
|
||||||
var body []byte
|
var body []byte
|
||||||
|
|
||||||
// fail early on connect error
|
options := client.NewPublishOptions(opts...)
|
||||||
if !g.once.Load().(bool) {
|
|
||||||
if err := g.opts.Broker.Connect(); err != nil {
|
// get proxy
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
exchange := ""
|
||||||
}
|
if v, ok := os.LookupEnv("MICRO_PROXY"); ok {
|
||||||
g.once.Store(true)
|
exchange = v
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
msgs := make([]*broker.Message, 0, len(ps))
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
md, ok := metadata.FromContext(ctx)
|
omd, ok := metadata.FromOutgoingContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
md = make(map[string]string)
|
omd = metadata.New(2)
|
||||||
}
|
}
|
||||||
md["Content-Type"] = p.ContentType()
|
|
||||||
md["Micro-Topic"] = p.Topic()
|
for _, p := range ps {
|
||||||
|
md := metadata.Copy(omd)
|
||||||
|
md[metadata.HeaderContentType] = p.ContentType()
|
||||||
|
|
||||||
// passed in raw data
|
// passed in raw data
|
||||||
if d, ok := p.Payload().(*raw.Frame); ok {
|
if d, ok := p.Payload().(*codec.Frame); ok {
|
||||||
body = d.Data
|
body = d.Data
|
||||||
} else {
|
} else {
|
||||||
// use codec for payload
|
// use codec for payload
|
||||||
cf, err := g.newGRPCCodec(p.ContentType())
|
cf, err := g.newCodec(p.ContentType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
@ -658,78 +760,87 @@ func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...clie
|
|||||||
}
|
}
|
||||||
|
|
||||||
topic := p.Topic()
|
topic := p.Topic()
|
||||||
|
if len(exchange) > 0 {
|
||||||
// get the exchange
|
topic = exchange
|
||||||
if len(options.Exchange) > 0 {
|
|
||||||
topic = options.Exchange
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.opts.Broker.Publish(topic, &broker.Message{
|
for k, v := range p.Metadata() {
|
||||||
Header: md,
|
md.Set(k, v)
|
||||||
Body: body,
|
}
|
||||||
}, broker.PublishContext(options.Context))
|
md.Set(metadata.HeaderTopic, topic)
|
||||||
|
msgs = append(msgs, &broker.Message{Header: md, Body: body})
|
||||||
|
}
|
||||||
|
|
||||||
|
return g.opts.Broker.BatchPublish(ctx, msgs,
|
||||||
|
broker.PublishContext(options.Context),
|
||||||
|
broker.PublishBodyOnly(options.BodyOnly),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) String() string {
|
func (g *grpcClient) String() string {
|
||||||
return "grpc"
|
return "grpc"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) getGrpcDialOptions() []grpc.DialOption {
|
func (g *grpcClient) Name() string {
|
||||||
if g.opts.CallOptions.Context == nil {
|
return g.opts.Name
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
v := g.opts.CallOptions.Context.Value(grpcDialOptions{})
|
func (g *grpcClient) getGrpcDialOptions(ctx context.Context) []grpc.DialOption {
|
||||||
|
var opts []grpc.DialOption
|
||||||
|
|
||||||
if v == nil {
|
if g.opts.CallOptions.Context != nil {
|
||||||
return nil
|
if v := g.opts.CallOptions.Context.Value(grpcDialOptions{}); v != nil {
|
||||||
|
if vopts, ok := v.([]grpc.DialOption); ok {
|
||||||
|
opts = append(opts, vopts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opts, ok := v.([]grpc.DialOption)
|
if ctx != nil {
|
||||||
|
if v := ctx.Value(grpcDialOptions{}); v != nil {
|
||||||
if !ok {
|
if vopts, ok := v.([]grpc.DialOption); ok {
|
||||||
return nil
|
opts = append(opts, vopts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) getGrpcCallOptions() []grpc.CallOption {
|
func (g *grpcClient) getGrpcCallOptions(ctx context.Context) []grpc.CallOption {
|
||||||
if g.opts.CallOptions.Context == nil {
|
var opts []grpc.CallOption
|
||||||
return nil
|
|
||||||
|
if g.opts.CallOptions.Context != nil {
|
||||||
|
if v := g.opts.CallOptions.Context.Value(grpcCallOptions{}); v != nil {
|
||||||
|
if vopts, ok := v.([]grpc.CallOption); ok {
|
||||||
|
opts = append(opts, vopts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v := g.opts.CallOptions.Context.Value(grpcCallOptions{})
|
if ctx != nil {
|
||||||
|
if v := ctx.Value(grpcCallOptions{}); v != nil {
|
||||||
if v == nil {
|
if vopts, ok := v.([]grpc.CallOption); ok {
|
||||||
return nil
|
opts = append(opts, vopts...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opts, ok := v.([]grpc.CallOption)
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func newClient(opts ...client.Option) client.Client {
|
func NewClient(opts ...client.Option) client.Client {
|
||||||
options := client.NewOptions()
|
options := client.NewOptions(opts...)
|
||||||
// default content type for grpc
|
// default content type for grpc
|
||||||
options.ContentType = "application/grpc+proto"
|
if options.ContentType == "" {
|
||||||
|
options.ContentType = DefaultContentType
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rc := &grpcClient{
|
rc := &grpcClient{
|
||||||
opts: options,
|
opts: options,
|
||||||
}
|
}
|
||||||
rc.once.Store(false)
|
|
||||||
|
|
||||||
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
|
||||||
|
|
||||||
|
rc.pool = NewConnPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
||||||
c := client.Client(rc)
|
c := client.Client(rc)
|
||||||
|
|
||||||
// wrap in reverse
|
// wrap in reverse
|
||||||
@ -737,25 +848,17 @@ func newClient(opts ...client.Option) client.Client {
|
|||||||
c = options.Wrappers[i-1](c)
|
c = options.Wrappers[i-1](c)
|
||||||
}
|
}
|
||||||
|
|
||||||
rc.codecs = make(map[string]encoding.Codec, len(defaultGRPCCodecs))
|
|
||||||
for k, v := range defaultGRPCCodecs {
|
|
||||||
rc.codecs[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
var codecs map[string]encoding.Codec
|
|
||||||
if rc.opts.Context != nil {
|
if rc.opts.Context != nil {
|
||||||
if v := rc.opts.Context.Value(codecsKey{}); v != nil {
|
if codecs, ok := rc.opts.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && codecs != nil {
|
||||||
codecs = v.(map[string]encoding.Codec)
|
for k, v := range codecs {
|
||||||
|
rc.opts.Codecs[k] = &wrapGrpcCodec{v}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range codecs {
|
for _, k := range options.Codecs {
|
||||||
rc.codecs[k] = v
|
encoding.RegisterCodec(&wrapMicroCodec{k})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(opts ...client.Option) client.Client {
|
|
||||||
return newClient(opts...)
|
|
||||||
}
|
|
||||||
|
58
grpc_pool.go
58
grpc_pool.go
@ -2,6 +2,7 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -9,56 +10,47 @@ import (
|
|||||||
"google.golang.org/grpc/connectivity"
|
"google.golang.org/grpc/connectivity"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pool struct {
|
type ConnPool struct {
|
||||||
|
conns map[string]*streamsPool
|
||||||
size int
|
size int
|
||||||
ttl int64
|
ttl int64
|
||||||
|
|
||||||
// max streams on a *poolConn
|
|
||||||
maxStreams int
|
maxStreams int
|
||||||
// max idle conns
|
|
||||||
maxIdle int
|
maxIdle int
|
||||||
|
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
conns map[string]*streamsPool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type streamsPool struct {
|
type streamsPool struct {
|
||||||
// head of list
|
// head of list
|
||||||
head *poolConn
|
head *PoolConn
|
||||||
// busy conns list
|
// busy conns list
|
||||||
busy *poolConn
|
busy *PoolConn
|
||||||
// the siza of list
|
// the siza of list
|
||||||
count int
|
count int
|
||||||
// idle conn
|
// idle conn
|
||||||
idle int
|
idle int
|
||||||
}
|
}
|
||||||
|
|
||||||
type poolConn struct {
|
type PoolConn struct {
|
||||||
// grpc conn
|
|
||||||
*grpc.ClientConn
|
|
||||||
err error
|
err error
|
||||||
addr string
|
*grpc.ClientConn
|
||||||
|
next *PoolConn
|
||||||
// pool and streams pool
|
pool *ConnPool
|
||||||
pool *pool
|
|
||||||
sp *streamsPool
|
sp *streamsPool
|
||||||
|
pre *PoolConn
|
||||||
|
addr string
|
||||||
streams int
|
streams int
|
||||||
created int64
|
created int64
|
||||||
|
|
||||||
// list
|
|
||||||
pre *poolConn
|
|
||||||
next *poolConn
|
|
||||||
in bool
|
in bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
|
func NewConnPool(size int, ttl time.Duration, idle int, ms int) *ConnPool {
|
||||||
if ms <= 0 {
|
if ms <= 0 {
|
||||||
ms = 1
|
ms = 1
|
||||||
}
|
}
|
||||||
if idle < 0 {
|
if idle < 0 {
|
||||||
idle = 0
|
idle = 0
|
||||||
}
|
}
|
||||||
return &pool{
|
return &ConnPool{
|
||||||
size: size,
|
size: size,
|
||||||
ttl: int64(ttl.Seconds()),
|
ttl: int64(ttl.Seconds()),
|
||||||
maxStreams: ms,
|
maxStreams: ms,
|
||||||
@ -67,12 +59,15 @@ func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pool) getConn(ctx context.Context, addr string, opts ...grpc.DialOption) (*poolConn, error) {
|
func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption) (*PoolConn, error) {
|
||||||
|
if strings.HasPrefix(addr, "http") {
|
||||||
|
addr = addr[strings.Index(addr, ":")+3:]
|
||||||
|
}
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
p.Lock()
|
p.Lock()
|
||||||
sp, ok := p.conns[addr]
|
sp, ok := p.conns[addr]
|
||||||
if !ok {
|
if !ok {
|
||||||
sp = &streamsPool{head: &poolConn{}, busy: &poolConn{}, count: 0, idle: 0}
|
sp = &streamsPool{head: &PoolConn{}, busy: &PoolConn{}, count: 0, idle: 0}
|
||||||
p.conns[addr] = sp
|
p.conns[addr] = sp
|
||||||
}
|
}
|
||||||
// while we have conns check streams and then return one
|
// while we have conns check streams and then return one
|
||||||
@ -135,12 +130,12 @@ func (p *pool) getConn(ctx context.Context, addr string, opts ...grpc.DialOption
|
|||||||
}
|
}
|
||||||
p.Unlock()
|
p.Unlock()
|
||||||
|
|
||||||
// create new conn
|
// create new conn)
|
||||||
cc, err := grpc.DialContext(ctx, addr, opts...)
|
cc, err := grpc.DialContext(ctx, addr, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn = &poolConn{cc, nil, addr, p, sp, 1, time.Now().Unix(), nil, nil, false}
|
conn = &PoolConn{ClientConn: cc, err: nil, addr: addr, pool: p, sp: sp, streams: 1, created: time.Now().Unix(), pre: nil, next: nil, in: false}
|
||||||
|
|
||||||
// add conn to streams pool
|
// add conn to streams pool
|
||||||
p.Lock()
|
p.Lock()
|
||||||
@ -152,7 +147,7 @@ func (p *pool) getConn(ctx context.Context, addr string, opts ...grpc.DialOption
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pool) release(addr string, conn *poolConn, err error) {
|
func (p *ConnPool) Put(conn *PoolConn, err error) {
|
||||||
p.Lock()
|
p.Lock()
|
||||||
p, sp, created := conn.pool, conn.sp, conn.created
|
p, sp, created := conn.pool, conn.sp, conn.created
|
||||||
// try to add conn
|
// try to add conn
|
||||||
@ -185,14 +180,13 @@ func (p *pool) release(addr string, conn *poolConn, err error) {
|
|||||||
sp.idle++
|
sp.idle++
|
||||||
}
|
}
|
||||||
p.Unlock()
|
p.Unlock()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *poolConn) Close() {
|
func (conn *PoolConn) Close() {
|
||||||
conn.pool.release(conn.addr, conn, conn.err)
|
conn.pool.Put(conn, conn.err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeConn(conn *poolConn) {
|
func removeConn(conn *PoolConn) {
|
||||||
if conn.pre != nil {
|
if conn.pre != nil {
|
||||||
conn.pre.next = conn.next
|
conn.pre.next = conn.next
|
||||||
}
|
}
|
||||||
@ -203,10 +197,9 @@ func removeConn(conn *poolConn) {
|
|||||||
conn.next = nil
|
conn.next = nil
|
||||||
conn.in = false
|
conn.in = false
|
||||||
conn.sp.count--
|
conn.sp.count--
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func addConnAfter(conn *poolConn, after *poolConn) {
|
func addConnAfter(conn *PoolConn, after *PoolConn) {
|
||||||
conn.next = after.next
|
conn.next = after.next
|
||||||
conn.pre = after
|
conn.pre = after
|
||||||
if after.next != nil {
|
if after.next != nil {
|
||||||
@ -215,5 +208,4 @@ func addConnAfter(conn *poolConn, after *poolConn) {
|
|||||||
after.next = conn
|
after.next = conn
|
||||||
conn.in = true
|
conn.in = true
|
||||||
conn.sp.count++
|
conn.sp.count++
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
16
message.go
16
message.go
@ -1,20 +1,19 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/unistack-org/micro/v3/client"
|
"go.unistack.org/micro/v3/client"
|
||||||
|
"go.unistack.org/micro/v3/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type grpcEvent struct {
|
type grpcEvent struct {
|
||||||
|
payload interface{}
|
||||||
topic string
|
topic string
|
||||||
contentType string
|
contentType string
|
||||||
payload interface{}
|
opts client.MessageOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGRPCEvent(topic string, payload interface{}, contentType string, opts ...client.MessageOption) client.Message {
|
func newGRPCEvent(topic string, payload interface{}, contentType string, opts ...client.MessageOption) client.Message {
|
||||||
var options client.MessageOptions
|
options := client.NewMessageOptions(opts...)
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(options.ContentType) > 0 {
|
if len(options.ContentType) > 0 {
|
||||||
contentType = options.ContentType
|
contentType = options.ContentType
|
||||||
@ -24,6 +23,7 @@ func newGRPCEvent(topic string, payload interface{}, contentType string, opts ..
|
|||||||
payload: payload,
|
payload: payload,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
|
opts: options,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,3 +38,7 @@ func (g *grpcEvent) Topic() string {
|
|||||||
func (g *grpcEvent) Payload() interface{} {
|
func (g *grpcEvent) Payload() interface{} {
|
||||||
return g.payload
|
return g.payload
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *grpcEvent) Metadata() metadata.Metadata {
|
||||||
|
return g.opts.Metadata
|
||||||
|
}
|
||||||
|
54
options.go
54
options.go
@ -3,15 +3,14 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/client"
|
"go.unistack.org/micro/v3/client"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultPoolMaxStreams maximum streams on a connectioin
|
// DefaultPoolMaxStreams maximum streams on a connection
|
||||||
// (20)
|
// (20)
|
||||||
DefaultPoolMaxStreams = 20
|
DefaultPoolMaxStreams = 20
|
||||||
|
|
||||||
@ -26,16 +25,12 @@ var (
|
|||||||
// DefaultMaxSendMsgSize maximum message that client can send
|
// DefaultMaxSendMsgSize maximum message that client can send
|
||||||
// (4 MB).
|
// (4 MB).
|
||||||
DefaultMaxSendMsgSize = 1024 * 1024 * 4
|
DefaultMaxSendMsgSize = 1024 * 1024 * 4
|
||||||
|
|
||||||
|
// DefaultServiceConfig enable load balancing
|
||||||
|
DefaultServiceConfig = `{"loadBalancingPolicy":"round_robin"}`
|
||||||
)
|
)
|
||||||
|
|
||||||
type poolMaxStreams struct{}
|
type poolMaxStreams struct{}
|
||||||
type poolMaxIdle struct{}
|
|
||||||
type codecsKey struct{}
|
|
||||||
type tlsAuth struct{}
|
|
||||||
type maxRecvMsgSizeKey struct{}
|
|
||||||
type maxSendMsgSizeKey struct{}
|
|
||||||
type grpcDialOptions struct{}
|
|
||||||
type grpcCallOptions struct{}
|
|
||||||
|
|
||||||
// maximum streams on a connectioin
|
// maximum streams on a connectioin
|
||||||
func PoolMaxStreams(n int) client.Option {
|
func PoolMaxStreams(n int) client.Option {
|
||||||
@ -47,6 +42,8 @@ func PoolMaxStreams(n int) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type poolMaxIdle struct{}
|
||||||
|
|
||||||
// maximum idle conns of a pool
|
// maximum idle conns of a pool
|
||||||
func PoolMaxIdle(d int) client.Option {
|
func PoolMaxIdle(d int) client.Option {
|
||||||
return func(o *client.Options) {
|
return func(o *client.Options) {
|
||||||
@ -57,6 +54,8 @@ func PoolMaxIdle(d int) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type codecsKey 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
|
||||||
func Codec(contentType string, c encoding.Codec) client.Option {
|
func Codec(contentType string, c encoding.Codec) client.Option {
|
||||||
return func(o *client.Options) {
|
return func(o *client.Options) {
|
||||||
@ -72,19 +71,9 @@ func Codec(contentType string, c encoding.Codec) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthTLS should be used to setup a secure authentication using TLS
|
type maxRecvMsgSizeKey struct{}
|
||||||
func AuthTLS(t *tls.Config) client.Option {
|
|
||||||
return func(o *client.Options) {
|
|
||||||
if o.Context == nil {
|
|
||||||
o.Context = context.Background()
|
|
||||||
}
|
|
||||||
o.Context = context.WithValue(o.Context, tlsAuth{}, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// MaxRecvMsgSize set the maximum size of message that client can receive.
|
// MaxRecvMsgSize set the maximum size of message that client can receive.
|
||||||
//
|
|
||||||
func MaxRecvMsgSize(s int) client.Option {
|
func MaxRecvMsgSize(s int) client.Option {
|
||||||
return func(o *client.Options) {
|
return func(o *client.Options) {
|
||||||
if o.Context == nil {
|
if o.Context == nil {
|
||||||
@ -94,9 +83,9 @@ func MaxRecvMsgSize(s int) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
type maxSendMsgSizeKey struct{}
|
||||||
|
|
||||||
// MaxSendMsgSize set the maximum size of message that client can send.
|
// MaxSendMsgSize set the maximum size of message that client can send.
|
||||||
//
|
|
||||||
func MaxSendMsgSize(s int) client.Option {
|
func MaxSendMsgSize(s int) client.Option {
|
||||||
return func(o *client.Options) {
|
return func(o *client.Options) {
|
||||||
if o.Context == nil {
|
if o.Context == nil {
|
||||||
@ -106,9 +95,9 @@ func MaxSendMsgSize(s int) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
type grpcDialOptions struct{}
|
||||||
|
|
||||||
// DialOptions to be used to configure gRPC dial options
|
// DialOptions to be used to configure gRPC dial options
|
||||||
//
|
|
||||||
func DialOptions(opts ...grpc.DialOption) client.CallOption {
|
func DialOptions(opts ...grpc.DialOption) client.CallOption {
|
||||||
return func(o *client.CallOptions) {
|
return func(o *client.CallOptions) {
|
||||||
if o.Context == nil {
|
if o.Context == nil {
|
||||||
@ -118,9 +107,9 @@ func DialOptions(opts ...grpc.DialOption) client.CallOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
type grpcCallOptions struct{}
|
||||||
|
|
||||||
// CallOptions to be used to configure gRPC call options
|
// CallOptions to be used to configure gRPC call options
|
||||||
//
|
|
||||||
func CallOptions(opts ...grpc.CallOption) client.CallOption {
|
func CallOptions(opts ...grpc.CallOption) client.CallOption {
|
||||||
return func(o *client.CallOptions) {
|
return func(o *client.CallOptions) {
|
||||||
if o.Context == nil {
|
if o.Context == nil {
|
||||||
@ -129,3 +118,14 @@ func CallOptions(opts ...grpc.CallOption) client.CallOption {
|
|||||||
o.Context = context.WithValue(o.Context, grpcCallOptions{}, opts)
|
o.Context = context.WithValue(o.Context, grpcCallOptions{}, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type serviceConfigKey struct{}
|
||||||
|
|
||||||
|
func ServiceConfig(str string) client.CallOption {
|
||||||
|
return func(options *client.CallOptions) {
|
||||||
|
if options.Context == nil {
|
||||||
|
options.Context = context.Background()
|
||||||
|
}
|
||||||
|
options.Context = context.WithValue(options.Context, serviceConfigKey{}, str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
23
request.go
23
request.go
@ -4,17 +4,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/client"
|
"go.unistack.org/micro/v3/client"
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
type grpcRequest struct {
|
type grpcRequest struct {
|
||||||
|
request interface{}
|
||||||
|
codec codec.Codec
|
||||||
service string
|
service string
|
||||||
method string
|
method string
|
||||||
contentType string
|
contentType string
|
||||||
request interface{}
|
|
||||||
opts client.RequestOptions
|
opts client.RequestOptions
|
||||||
codec codec.Codec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// service Struct.Method /service.Struct/Method
|
// service Struct.Method /service.Struct/Method
|
||||||
@ -38,15 +38,12 @@ func methodToGRPC(service, method string) string {
|
|||||||
return fmt.Sprintf("/%s.%s/%s", service, mParts[0], mParts[1])
|
return fmt.Sprintf("/%s.%s/%s", service, mParts[0], mParts[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request {
|
func newGRPCRequest(service, method string, request interface{}, contentType string, opts ...client.RequestOption) client.Request {
|
||||||
var opts client.RequestOptions
|
options := client.NewRequestOptions(opts...)
|
||||||
for _, o := range reqOpts {
|
|
||||||
o(&opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the content-type specified
|
// set the content-type specified
|
||||||
if len(opts.ContentType) > 0 {
|
if len(options.ContentType) > 0 {
|
||||||
contentType = opts.ContentType
|
contentType = options.ContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
return &grpcRequest{
|
return &grpcRequest{
|
||||||
@ -54,7 +51,7 @@ func newGRPCRequest(service, method string, request interface{}, contentType str
|
|||||||
method: method,
|
method: method,
|
||||||
request: request,
|
request: request,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
opts: opts,
|
opts: options,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +71,7 @@ func (g *grpcRequest) Endpoint() string {
|
|||||||
return g.method
|
return g.method
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcRequest) Codec() codec.Writer {
|
func (g *grpcRequest) Codec() codec.Codec {
|
||||||
return g.codec
|
return g.codec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
32
response.go
32
response.go
@ -3,41 +3,39 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro-codec-bytes"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/encoding"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type response struct {
|
type response struct {
|
||||||
conn *poolConn
|
conn *PoolConn
|
||||||
stream grpc.ClientStream
|
stream grpc.ClientStream
|
||||||
codec encoding.Codec
|
codec codec.Codec
|
||||||
gcodec codec.Codec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the response
|
// Read the response
|
||||||
func (r *response) Codec() codec.Reader {
|
func (r *response) Codec() codec.Codec {
|
||||||
return r.gcodec
|
return r.codec
|
||||||
}
|
}
|
||||||
|
|
||||||
// read the header
|
// read the header
|
||||||
func (r *response) Header() map[string]string {
|
func (r *response) Header() metadata.Metadata {
|
||||||
md, err := r.stream.Header()
|
meta, err := r.stream.Header()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return map[string]string{}
|
return nil
|
||||||
}
|
}
|
||||||
hdr := make(map[string]string, len(md))
|
md := metadata.New(len(meta))
|
||||||
for k, v := range md {
|
for k, v := range meta {
|
||||||
hdr[k] = strings.Join(v, ",")
|
md.Set(k, strings.Join(v, ","))
|
||||||
}
|
}
|
||||||
return hdr
|
return md
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the undecoded response
|
// Read the undecoded response
|
||||||
func (r *response) Read() ([]byte, error) {
|
func (r *response) Read() ([]byte, error) {
|
||||||
f := &bytes.Frame{}
|
f := &codec.Frame{}
|
||||||
if err := r.gcodec.ReadBody(f); err != nil {
|
if err := r.codec.ReadBody(&wrapStream{r.stream}, f); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return f.Data, nil
|
return f.Data, nil
|
||||||
|
52
stream.go
52
stream.go
@ -5,23 +5,21 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/client"
|
"go.unistack.org/micro/v3/client"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Implements the streamer interface
|
// Implements the streamer interface
|
||||||
type grpcStream struct {
|
type grpcStream struct {
|
||||||
// embed so we can access if need be
|
|
||||||
grpc.ClientStream
|
grpc.ClientStream
|
||||||
|
context context.Context
|
||||||
sync.RWMutex
|
|
||||||
closed bool
|
|
||||||
err error
|
err error
|
||||||
conn *poolConn
|
|
||||||
request client.Request
|
request client.Request
|
||||||
response client.Response
|
response client.Response
|
||||||
context context.Context
|
|
||||||
close func(err error)
|
close func(err error)
|
||||||
|
conn *PoolConn
|
||||||
|
sync.RWMutex
|
||||||
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcStream) Context() context.Context {
|
func (g *grpcStream) Context() context.Context {
|
||||||
@ -44,6 +42,14 @@ func (g *grpcStream) Send(msg interface{}) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *grpcStream) SendMsg(msg interface{}) error {
|
||||||
|
if err := g.ClientStream.SendMsg(msg); err != nil {
|
||||||
|
g.setError(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g *grpcStream) Recv(msg interface{}) (err error) {
|
func (g *grpcStream) Recv(msg interface{}) (err error) {
|
||||||
defer g.setError(err)
|
defer g.setError(err)
|
||||||
|
|
||||||
@ -62,6 +68,24 @@ func (g *grpcStream) Recv(msg interface{}) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *grpcStream) RecvMsg(msg interface{}) (err error) {
|
||||||
|
defer g.setError(err)
|
||||||
|
|
||||||
|
if err = g.ClientStream.RecvMsg(msg); err != nil {
|
||||||
|
// #202 - inconsistent gRPC stream behavior
|
||||||
|
// the only way to tell if the stream is done is when we get a EOF on the Recv
|
||||||
|
// here we should close the underlying gRPC ClientConn
|
||||||
|
closeErr := g.Close()
|
||||||
|
if err == io.EOF && closeErr != nil {
|
||||||
|
err = closeErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (g *grpcStream) Error() error {
|
func (g *grpcStream) Error() error {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
defer g.RUnlock()
|
defer g.RUnlock()
|
||||||
@ -92,3 +116,17 @@ func (g *grpcStream) Close() error {
|
|||||||
g.close(g.err)
|
g.close(g.err)
|
||||||
return g.ClientStream.CloseSend()
|
return g.ClientStream.CloseSend()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *grpcStream) CloseSend() error {
|
||||||
|
g.Lock()
|
||||||
|
defer g.Unlock()
|
||||||
|
|
||||||
|
if g.closed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// close the connection
|
||||||
|
g.closed = true
|
||||||
|
g.close(g.err)
|
||||||
|
return g.ClientStream.CloseSend()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user