Compare commits
148 Commits
v3.7.8
...
40b5402aa5
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
20
.github/workflows/autoapprove.yml
vendored
Normal file
20
.github/workflows/autoapprove.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
name: "autoapprove"
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request_target:
|
||||||
|
types: [assigned, opened, synchronize, reopened]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
autoapprove:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: approve
|
||||||
|
uses: hmarr/auto-approve-action@v3
|
||||||
|
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
||||||
|
id: approve
|
||||||
|
with:
|
||||||
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
21
.github/workflows/automerge.yml
vendored
Normal file
21
.github/workflows/automerge.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
name: "automerge"
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request_target:
|
||||||
|
types: [assigned, opened, synchronize, reopened]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
automerge:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.actor == 'vtolstov'
|
||||||
|
steps:
|
||||||
|
- name: merge
|
||||||
|
id: merge
|
||||||
|
run: gh pr merge --auto --merge "$PR_URL"
|
||||||
|
env:
|
||||||
|
PR_URL: ${{github.event.pull_request.html_url}}
|
||||||
|
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
||||||
13
.github/workflows/build.yml
vendored
13
.github/workflows/build.yml
vendored
@@ -3,19 +3,20 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
|||||||
29
.github/workflows/codeql-analysis.yml
vendored
29
.github/workflows/codeql-analysis.yml
vendored
@@ -9,7 +9,7 @@
|
|||||||
# the `language` matrix defined below to confirm you have the correct set of
|
# the `language` matrix defined below to confirm you have the correct set of
|
||||||
# supported CodeQL languages.
|
# supported CodeQL languages.
|
||||||
#
|
#
|
||||||
name: "CodeQL"
|
name: "codeql"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_run:
|
workflow_run:
|
||||||
@@ -17,16 +17,16 @@ on:
|
|||||||
types:
|
types:
|
||||||
- completed
|
- completed
|
||||||
push:
|
push:
|
||||||
branches: [ master ]
|
branches: [ master, v3 ]
|
||||||
pull_request:
|
pull_request:
|
||||||
# The branches below must be a subset of the branches above
|
# The branches below must be a subset of the branches above
|
||||||
branches: [ master ]
|
branches: [ master, v3 ]
|
||||||
schedule:
|
schedule:
|
||||||
- cron: '34 1 * * 0'
|
- cron: '34 1 * * 0'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
analyze:
|
analyze:
|
||||||
name: Analyze
|
name: analyze
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
actions: read
|
actions: read
|
||||||
@@ -42,12 +42,15 @@ jobs:
|
|||||||
# 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
|
# 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:
|
steps:
|
||||||
- name: Checkout repository
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
|
- name: setup
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: 1.17
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: init
|
||||||
uses: github/codeql-action/init@v1
|
uses: github/codeql-action/init@v2
|
||||||
with:
|
with:
|
||||||
languages: ${{ matrix.language }}
|
languages: ${{ matrix.language }}
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
@@ -57,8 +60,8 @@ jobs:
|
|||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# 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)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
- name: Autobuild
|
- name: autobuild
|
||||||
uses: github/codeql-action/autobuild@v1
|
uses: github/codeql-action/autobuild@v2
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
# 📚 https://git.io/JvXDl
|
# 📚 https://git.io/JvXDl
|
||||||
@@ -71,5 +74,5 @@ jobs:
|
|||||||
# make bootstrap
|
# make bootstrap
|
||||||
# make release
|
# make release
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: analyze
|
||||||
uses: github/codeql-action/analyze@v1
|
uses: github/codeql-action/analyze@v2
|
||||||
|
|||||||
73
.github/workflows/dependabot-automerge.yml
vendored
73
.github/workflows/dependabot-automerge.yml
vendored
@@ -1,66 +1,27 @@
|
|||||||
name: "prautomerge"
|
name: "dependabot-automerge"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_run:
|
pull_request_target:
|
||||||
workflows: ["prbuild"]
|
types: [assigned, opened, synchronize, reopened]
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
Dependabot-Automerge:
|
automerge:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
# Contains workaround to execute if dependabot updates the PR by checking for the base branch in the linked PR
|
if: github.actor == 'dependabot[bot]'
|
||||||
# The the github.event.workflow_run.event value is 'push' and not 'pull_request'
|
|
||||||
# dont work with multiple workflows when last returns success
|
|
||||||
if: >-
|
|
||||||
github.event.workflow_run.conclusion == 'success'
|
|
||||||
&& github.actor == 'dependabot[bot]'
|
|
||||||
&& github.event.sender.login == 'dependabot[bot]'
|
|
||||||
&& github.event.sender.type == 'Bot'
|
|
||||||
&& (github.event.workflow_run.event == 'pull_request'
|
|
||||||
|| (github.event.workflow_run.event == 'push' && github.event.workflow_run.pull_requests[0].base.ref == github.event.repository.default_branch ))
|
|
||||||
steps:
|
steps:
|
||||||
- name: Approve Changes and Merge changes if label 'dependencies' is set
|
- name: metadata
|
||||||
uses: actions/github-script@v5
|
id: metadata
|
||||||
|
uses: dependabot/fetch-metadata@v1.3.6
|
||||||
with:
|
with:
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
github-token: "${{ secrets.TOKEN }}"
|
||||||
script: |
|
- name: merge
|
||||||
console.log(context.payload.workflow_run);
|
id: merge
|
||||||
|
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
||||||
var labelNames = await github.paginate(
|
run: gh pr merge --auto --merge "$PR_URL"
|
||||||
github.issues.listLabelsOnIssue,
|
env:
|
||||||
{
|
PR_URL: ${{github.event.pull_request.html_url}}
|
||||||
repo: context.repo.repo,
|
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
||||||
owner: context.repo.owner,
|
|
||||||
issue_number: context.payload.workflow_run.pull_requests[0].number,
|
|
||||||
},
|
|
||||||
(response) => response.data.map(
|
|
||||||
(label) => label.name
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(labelNames);
|
|
||||||
|
|
||||||
if (labelNames.includes('dependencies')) {
|
|
||||||
console.log('Found label');
|
|
||||||
|
|
||||||
await github.pulls.createReview({
|
|
||||||
repo: context.repo.repo,
|
|
||||||
owner: context.repo.owner,
|
|
||||||
pull_number: context.payload.workflow_run.pull_requests[0].number,
|
|
||||||
event: 'APPROVE'
|
|
||||||
});
|
|
||||||
console.log('Approved PR');
|
|
||||||
|
|
||||||
await github.pulls.merge({
|
|
||||||
repo: context.repo.repo,
|
|
||||||
owner: context.repo.owner,
|
|
||||||
pull_number: context.payload.workflow_run.pull_requests[0].number,
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('Merged PR');
|
|
||||||
}
|
|
||||||
|
|||||||
13
.github/workflows/pr.yml
vendored
13
.github/workflows/pr.yml
vendored
@@ -3,19 +3,20 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
|||||||
2
codec.go
2
codec.go
@@ -3,7 +3,7 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
)
|
)
|
||||||
|
|||||||
40
error.go
40
error.go
@@ -1,7 +1,7 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/unistack-org/micro/v3/errors"
|
"go.unistack.org/micro/v3/errors"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -9,36 +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
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.Error{
|
return err
|
||||||
ID: "go.micro.client",
|
|
||||||
Code: int32(s.Code()),
|
|
||||||
Detail: s.Message(),
|
|
||||||
Status: s.Code().String(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
8
go.mod
8
go.mod
@@ -1,10 +1,8 @@
|
|||||||
module github.com/unistack-org/micro-client-grpc/v3
|
module go.unistack.org/micro-client-grpc/v3
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/unistack-org/micro/v3 v3.7.8
|
go.unistack.org/micro/v3 v3.10.22
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
|
google.golang.org/grpc v1.52.3
|
||||||
google.golang.org/genproto v0.0.0-20210729151513-df9385d47c1b // indirect
|
|
||||||
google.golang.org/grpc v1.40.0
|
|
||||||
)
|
)
|
||||||
|
|||||||
217
grpc.go
217
grpc.go
@@ -1,5 +1,5 @@
|
|||||||
// 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"
|
||||||
@@ -12,19 +12,21 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/broker"
|
"go.unistack.org/micro/v3/broker"
|
||||||
"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"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultContentType = "application/grpc+proto"
|
DefaultContentType = "application/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type grpcClient struct {
|
type grpcClient struct {
|
||||||
@@ -62,7 +64,7 @@ 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 {
|
||||||
@@ -76,9 +78,15 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
} else {
|
} else {
|
||||||
header = make(map[string]string, 2)
|
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)
|
||||||
|
header["timeout"] = fmt.Sprintf("%dn", opts.RequestTimeout)
|
||||||
|
header["content-type"] = req.ContentType()
|
||||||
|
|
||||||
md := gmetadata.New(header)
|
md := gmetadata.New(header)
|
||||||
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
||||||
@@ -110,10 +118,18 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts := g.getGrpcDialOptions(); opts != nil {
|
if opts := g.getGrpcDialOptions(opts.Context); opts != nil {
|
||||||
grpcDialOptions = append(grpcDialOptions, opts...)
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contextDialer := g.opts.ContextDialer
|
||||||
|
if opts.ContextDialer != nil {
|
||||||
|
contextDialer = opts.ContextDialer
|
||||||
|
}
|
||||||
|
if contextDialer != nil {
|
||||||
|
grpcDialOptions = append(grpcDialOptions, grpc.WithContextDialer(contextDialer))
|
||||||
|
}
|
||||||
|
|
||||||
cc, err := g.pool.getConn(dialCtx, addr, grpcDialOptions...)
|
cc, err := g.pool.getConn(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))
|
||||||
@@ -124,15 +140,22 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
ch := make(chan error, 1)
|
ch := make(chan error, 1)
|
||||||
|
var gmd gmetadata.MD
|
||||||
|
|
||||||
|
grpcCallOptions := []grpc.CallOption{
|
||||||
|
grpc.CallContentSubtype((&wrapMicroCodec{cf}).Name()),
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts := g.getGrpcCallOptions(opts.Context); opts != nil {
|
||||||
|
grpcCallOptions = append(grpcCallOptions, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.ResponseMetadata != nil {
|
||||||
|
gmd = gmetadata.MD{}
|
||||||
|
grpcCallOptions = append(grpcCallOptions, grpc.Header(&gmd))
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
grpcCallOptions := []grpc.CallOption{
|
|
||||||
grpc.ForceCodec(&wrapMicroCodec{cf}),
|
|
||||||
grpc.CallContentSubtype((&wrapMicroCodec{cf}).Name()),
|
|
||||||
}
|
|
||||||
if opts := g.getGrpcCallOptions(); opts != nil {
|
|
||||||
grpcCallOptions = append(grpcCallOptions, opts...)
|
|
||||||
}
|
|
||||||
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)
|
||||||
}()
|
}()
|
||||||
@@ -144,6 +167,13 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,10 +191,11 @@ 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)
|
||||||
@@ -196,10 +227,18 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts := g.getGrpcDialOptions(); opts != nil {
|
if opts := g.getGrpcDialOptions(opts.Context); opts != nil {
|
||||||
grpcDialOptions = append(grpcDialOptions, opts...)
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contextDialer := g.opts.ContextDialer
|
||||||
|
if opts.ContextDialer != nil {
|
||||||
|
contextDialer = opts.ContextDialer
|
||||||
|
}
|
||||||
|
if contextDialer != nil {
|
||||||
|
grpcDialOptions = append(grpcDialOptions, grpc.WithContextDialer(contextDialer))
|
||||||
|
}
|
||||||
|
|
||||||
cc, err := g.pool.getConn(dialCtx, addr, grpcDialOptions...)
|
cc, err := g.pool.getConn(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))
|
||||||
@@ -212,12 +251,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(wc.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)
|
||||||
@@ -263,6 +307,7 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,6 +432,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)
|
||||||
}
|
}
|
||||||
@@ -435,20 +481,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)
|
||||||
@@ -461,6 +495,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()
|
||||||
|
|
||||||
@@ -552,18 +603,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
|
||||||
@@ -577,6 +617,23 @@ 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()
|
||||||
|
|
||||||
@@ -669,7 +726,6 @@ func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...c
|
|||||||
for _, p := range ps {
|
for _, p := range ps {
|
||||||
md := metadata.Copy(omd)
|
md := metadata.Copy(omd)
|
||||||
md[metadata.HeaderContentType] = p.ContentType()
|
md[metadata.HeaderContentType] = p.ContentType()
|
||||||
md[metadata.HeaderTopic] = p.Topic()
|
|
||||||
|
|
||||||
// passed in raw data
|
// passed in raw data
|
||||||
if d, ok := p.Payload().(*codec.Frame); ok {
|
if d, ok := p.Payload().(*codec.Frame); ok {
|
||||||
@@ -693,6 +749,9 @@ func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...c
|
|||||||
topic = exchange
|
topic = exchange
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for k, v := range p.Metadata() {
|
||||||
|
md.Set(k, v)
|
||||||
|
}
|
||||||
md.Set(metadata.HeaderTopic, topic)
|
md.Set(metadata.HeaderTopic, topic)
|
||||||
msgs = append(msgs, &broker.Message{Header: md, Body: body})
|
msgs = append(msgs, &broker.Message{Header: md, Body: body})
|
||||||
}
|
}
|
||||||
@@ -711,41 +770,45 @@ func (g *grpcClient) Name() string {
|
|||||||
return g.opts.Name
|
return g.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) getGrpcDialOptions() []grpc.DialOption {
|
func (g *grpcClient) getGrpcDialOptions(ctx context.Context) []grpc.DialOption {
|
||||||
if g.opts.CallOptions.Context == nil {
|
var opts []grpc.DialOption
|
||||||
return nil
|
|
||||||
|
if g.opts.CallOptions.Context != nil {
|
||||||
|
if v := g.opts.CallOptions.Context.Value(grpcDialOptions{}); v != nil {
|
||||||
|
if vopts, ok := v.([]grpc.DialOption); ok {
|
||||||
|
opts = append(opts, vopts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v := g.opts.CallOptions.Context.Value(grpcDialOptions{})
|
if ctx != nil {
|
||||||
|
if v := ctx.Value(grpcDialOptions{}); v != nil {
|
||||||
if v == nil {
|
if vopts, ok := v.([]grpc.DialOption); ok {
|
||||||
return nil
|
opts = append(opts, vopts...)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
opts, ok := v.([]grpc.DialOption)
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@@ -754,7 +817,9 @@ func (g *grpcClient) getGrpcCallOptions() []grpc.CallOption {
|
|||||||
func NewClient(opts ...client.Option) client.Client {
|
func NewClient(opts ...client.Option) client.Client {
|
||||||
options := client.NewOptions(opts...)
|
options := client.NewOptions(opts...)
|
||||||
// default content type for grpc
|
// default content type for grpc
|
||||||
options.ContentType = defaultContentType
|
if options.ContentType == "" {
|
||||||
|
options.ContentType = DefaultContentType
|
||||||
|
}
|
||||||
|
|
||||||
rc := &grpcClient{
|
rc := &grpcClient{
|
||||||
opts: options,
|
opts: options,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -59,6 +60,9 @@ 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 *pool) getConn(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]
|
||||||
@@ -126,7 +130,7 @@ 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
|
||||||
|
|||||||
14
message.go
14
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{}
|
payload interface{}
|
||||||
topic string
|
topic string
|
||||||
contentType string
|
contentType string
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
10
options.go
10
options.go
@@ -4,7 +4,7 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
@@ -70,9 +70,7 @@ func Codec(contentType string, c encoding.Codec) client.Option {
|
|||||||
|
|
||||||
type maxRecvMsgSizeKey struct{}
|
type maxRecvMsgSizeKey struct{}
|
||||||
|
|
||||||
//
|
|
||||||
// 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 {
|
||||||
@@ -84,9 +82,7 @@ func MaxRecvMsgSize(s int) client.Option {
|
|||||||
|
|
||||||
type maxSendMsgSizeKey struct{}
|
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 {
|
||||||
@@ -98,9 +94,7 @@ func MaxSendMsgSize(s int) client.Option {
|
|||||||
|
|
||||||
type grpcDialOptions struct{}
|
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 {
|
||||||
@@ -112,9 +106,7 @@ func DialOptions(opts ...grpc.DialOption) client.CallOption {
|
|||||||
|
|
||||||
type grpcCallOptions struct{}
|
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 {
|
||||||
|
|||||||
17
request.go
17
request.go
@@ -4,8 +4,8 @@ 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 {
|
||||||
@@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ func (r *response) Codec() codec.Codec {
|
|||||||
func (r *response) Header() metadata.Metadata {
|
func (r *response) Header() metadata.Metadata {
|
||||||
meta, err := r.stream.Header()
|
meta, err := r.stream.Header()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return metadata.New(0)
|
return nil
|
||||||
}
|
}
|
||||||
md := metadata.New(len(meta))
|
md := metadata.New(len(meta))
|
||||||
for k, v := range meta {
|
for k, v := range meta {
|
||||||
|
|||||||
42
stream.go
42
stream.go
@@ -5,7 +5,7 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,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)
|
||||||
|
|
||||||
@@ -60,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()
|
||||||
@@ -90,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()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user