Compare commits
No commits in common. "master" and "v3.3.0" have entirely different histories.
19
.github/dependabot.yml
vendored
19
.github/dependabot.yml
vendored
@ -1,19 +0,0 @@
|
|||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
|
|
||||||
# Maintain dependencies for GitHub Actions
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
19
.github/renovate.json
vendored
Normal file
19
.github/renovate.json
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"extends": [
|
||||||
|
"config:base"
|
||||||
|
],
|
||||||
|
"packageRules": [
|
||||||
|
{
|
||||||
|
"matchUpdateTypes": ["minor", "patch", "pin", "digest"],
|
||||||
|
"automerge": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"groupName": "all deps",
|
||||||
|
"separateMajorMinor": true,
|
||||||
|
"groupSlug": "all",
|
||||||
|
"packagePatterns": [
|
||||||
|
"*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
13
.github/stale.sh
vendored
Executable file
13
.github/stale.sh
vendored
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
#!/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
20
.github/workflows/autoapprove.yml
vendored
@ -1,20 +0,0 @@
|
|||||||
name: "autoapprove"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
autoapprove:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: approve
|
|
||||||
uses: hmarr/auto-approve-action@v3
|
|
||||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
|
||||||
id: approve
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
21
.github/workflows/automerge.yml
vendored
21
.github/workflows/automerge.yml
vendored
@ -1,21 +0,0 @@
|
|||||||
name: "automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'vtolstov'
|
|
||||||
steps:
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
13
.github/workflows/build.yml
vendored
13
.github/workflows/build.yml
vendored
@ -3,20 +3,19 @@ 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@v3
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.16
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v2
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v3
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -32,9 +31,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v2
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
uses: golangci/golangci-lint-action@v2
|
||||||
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
78
.github/workflows/codeql-analysis.yml
vendored
@ -1,78 +0,0 @@
|
|||||||
# For most projects, this workflow file will not need changing; you simply need
|
|
||||||
# to commit it to your repository.
|
|
||||||
#
|
|
||||||
# You may wish to alter this file to override the set of languages analyzed,
|
|
||||||
# or to provide custom queries or build logic.
|
|
||||||
#
|
|
||||||
# ******** NOTE ********
|
|
||||||
# We have attempted to detect the languages in your repository. Please check
|
|
||||||
# the `language` matrix defined below to confirm you have the correct set of
|
|
||||||
# supported CodeQL languages.
|
|
||||||
#
|
|
||||||
name: "codeql"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
||||||
# Learn more:
|
|
||||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: init
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 https://git.io/JvXDl
|
|
||||||
|
|
||||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
||||||
# and modify them (or add more) to build your code if your project
|
|
||||||
# uses a compiled language
|
|
||||||
|
|
||||||
#- run: |
|
|
||||||
# make bootstrap
|
|
||||||
# make release
|
|
||||||
|
|
||||||
- name: analyze
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
27
.github/workflows/dependabot-automerge.yml
vendored
27
.github/workflows/dependabot-automerge.yml
vendored
@ -1,27 +0,0 @@
|
|||||||
name: "dependabot-automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- name: metadata
|
|
||||||
id: metadata
|
|
||||||
uses: dependabot/fetch-metadata@v1.3.6
|
|
||||||
with:
|
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
13
.github/workflows/pr.yml
vendored
13
.github/workflows/pr.yml
vendored
@ -3,20 +3,19 @@ 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@v3
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.16
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v2
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v3
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -32,9 +31,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v2
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
uses: golangci/golangci-lint-action@v2
|
||||||
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
24
.gitignore
vendored
@ -1,24 +0,0 @@
|
|||||||
# 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
|
|
@ -1,44 +0,0 @@
|
|||||||
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
|
|
38
codec.go
38
codec.go
@ -3,16 +3,11 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
_ encoding.Codec = &wrapMicroCodec{}
|
|
||||||
_ codec.Codec = &wrapGrpcCodec{}
|
|
||||||
)
|
|
||||||
|
|
||||||
type wrapStream struct{ grpc.ClientStream }
|
type wrapStream struct{ grpc.ClientStream }
|
||||||
|
|
||||||
func (w *wrapStream) Write(d []byte) (int, error) {
|
func (w *wrapStream) Write(d []byte) (int, error) {
|
||||||
@ -24,7 +19,7 @@ func (w *wrapStream) Write(d []byte) (int, error) {
|
|||||||
func (w *wrapStream) Read(d []byte) (int, error) {
|
func (w *wrapStream) Read(d []byte) (int, error) {
|
||||||
m := &codec.Frame{}
|
m := &codec.Frame{}
|
||||||
err := w.ClientStream.RecvMsg(m)
|
err := w.ClientStream.RecvMsg(m)
|
||||||
copy(d, m.Data)
|
d = m.Data
|
||||||
return len(d), err
|
return len(d), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,32 +29,26 @@ func (w *wrapMicroCodec) Name() string {
|
|||||||
return w.Codec.String()
|
return w.Codec.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapMicroCodec) Marshal(v interface{}) ([]byte, error) {
|
|
||||||
return w.Codec.Marshal(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *wrapMicroCodec) Unmarshal(d []byte, v interface{}) error {
|
|
||||||
return w.Codec.Unmarshal(d, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
type wrapGrpcCodec struct{ encoding.Codec }
|
type wrapGrpcCodec struct{ encoding.Codec }
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) String() string {
|
func (w *wrapGrpcCodec) String() string {
|
||||||
return w.Codec.Name()
|
return w.Codec.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
func (w *wrapGrpcCodec) Marshal(v interface{}) ([]byte, error) {
|
||||||
if m, ok := v.(*codec.Frame); ok {
|
switch m := v.(type) {
|
||||||
|
case *codec.Frame:
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
}
|
}
|
||||||
return w.Codec.Marshal(v)
|
return w.Codec.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
func (w wrapGrpcCodec) Unmarshal(d []byte, v interface{}) error {
|
||||||
if d == nil || v == nil {
|
if d == nil || v == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if m, ok := v.(*codec.Frame); ok {
|
switch m := v.(type) {
|
||||||
|
case *codec.Frame:
|
||||||
m.Data = d
|
m.Data = d
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -80,7 +69,7 @@ type grpcCodec struct {
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) ReadHeader(conn io.Reader, m *codec.Message, mt codec.MessageType) error {
|
func (g *wrapGrpcCodec) ReadHeader(conn io.Reader, m *codec.Message, mt codec.MessageType) error {
|
||||||
/*
|
/*
|
||||||
if m == nil {
|
if m == nil {
|
||||||
m = codec.NewMessage(codec.Request)
|
m = codec.NewMessage(codec.Request)
|
||||||
@ -103,19 +92,20 @@ func (w *wrapGrpcCodec) ReadHeader(conn io.Reader, m *codec.Message, mt codec.Me
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) ReadBody(conn io.Reader, v interface{}) error {
|
func (g *wrapGrpcCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||||
// caller has requested a frame
|
// caller has requested a frame
|
||||||
if m, ok := v.(*codec.Frame); ok {
|
switch m := v.(type) {
|
||||||
|
case *codec.Frame:
|
||||||
_, err := conn.Read(m.Data)
|
_, err := conn.Read(m.Data)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return codec.ErrInvalidMessage
|
return codec.ErrInvalidMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
func (g *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 {
|
||||||
b, err := w.Marshal(v)
|
b, err := g.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
35
error.go
35
error.go
@ -1,7 +1,7 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.unistack.org/micro/v3/errors"
|
"github.com/unistack-org/micro/v3/errors"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,46 +9,31 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
details := s.Details()
|
// return first error from details
|
||||||
switch len(details) {
|
if details := s.Details(); len(details) > 0 {
|
||||||
case 0:
|
if verr, ok := details[0].(error); ok {
|
||||||
if verr := errors.Parse(s.Message()); verr.Code > 0 {
|
return microError(verr)
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// attached messages in details more then 1, try to fallback to micro error
|
// try to decode micro *errors.Error
|
||||||
if verr := errors.Parse(s.Message()); verr.Code > 0 {
|
if e := errors.Parse(s.Message()); e.Code > 0 {
|
||||||
// return micro error
|
return e // actually a micro error
|
||||||
return verr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// not micro error return base error
|
// fallback
|
||||||
return err
|
return errors.InternalServerError("go.micro.client", s.Message())
|
||||||
}
|
}
|
||||||
|
11
go.mod
11
go.mod
@ -1,8 +1,13 @@
|
|||||||
module go.unistack.org/micro-client-grpc/v3
|
module github.com/unistack-org/micro-client-grpc/v3
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
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/v3 v3.3.11
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
|
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect
|
||||||
|
google.golang.org/grpc v1.37.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//replace github.com/unistack-org/micro/v3 => ../../micro
|
||||||
|
325
grpc.go
325
grpc.go
@ -1,52 +1,51 @@
|
|||||||
// Package grpc provides a gRPC client
|
// Package grpc provides a gRPC client
|
||||||
package grpc // import "go.unistack.org/micro-client-grpc/v3"
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"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/codec"
|
||||||
"go.unistack.org/micro/v3/errors"
|
"github.com/unistack-org/micro/v3/errors"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"github.com/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 (
|
|
||||||
DefaultContentType = "application/grpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
type grpcClient struct {
|
type grpcClient struct {
|
||||||
pool *ConnPool
|
|
||||||
opts client.Options
|
opts client.Options
|
||||||
sync.RWMutex
|
codecs map[string]encoding.Codec
|
||||||
|
pool *pool
|
||||||
|
once atomic.Value
|
||||||
init bool
|
init bool
|
||||||
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.TLSConfig != nil {
|
if g.opts.Context != nil {
|
||||||
creds := credentials.NewTLS(g.opts.TLSConfig)
|
if v := g.opts.Context.Value(tlsAuth{}); v != nil {
|
||||||
|
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{MinVersion: tls.VersionTLS12}
|
tlsConfig := &tls.Config{}
|
||||||
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
|
||||||
@ -64,7 +63,7 @@ func (g *grpcClient) secure(addr string) grpc.DialOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// other fallback to insecure
|
// other fallback to insecure
|
||||||
return grpc.WithTransportCredentials(insecure.NewCredentials())
|
return grpc.WithInsecure()
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@ -78,27 +77,22 @@ 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["Grpc-Timeout"] = fmt.Sprintf("%dn", opts.RequestTimeout)
|
header["timeout"] = fmt.Sprintf("%d", opts.RequestTimeout)
|
||||||
header["timeout"] = fmt.Sprintf("%dn", opts.RequestTimeout)
|
// set the content type for the request
|
||||||
header["content-type"] = req.ContentType()
|
header["x-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.newCodec(req.ContentType())
|
cf, err := g.newGRPCCodec(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
|
||||||
|
|
||||||
@ -117,47 +111,30 @@ 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.Context); opts != nil {
|
if opts := g.getGrpcDialOptions(); opts != nil {
|
||||||
grpcDialOptions = append(grpcDialOptions, opts...)
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
contextDialer := g.opts.ContextDialer
|
cc, err := g.pool.getConn(dialCtx, addr, grpcDialOptions...)
|
||||||
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.Put(cc, grr)
|
g.pool.release(addr, cc, grr)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
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)
|
||||||
}()
|
}()
|
||||||
@ -169,13 +146,6 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,16 +163,15 @@ 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["Grpc-Timeout"] = fmt.Sprintf("%dn", opts.StreamTimeout)
|
header["timeout"] = fmt.Sprintf("%d", opts.StreamTimeout)
|
||||||
header["timeout"] = fmt.Sprintf("%dn", opts.StreamTimeout)
|
|
||||||
}
|
}
|
||||||
// set the content type for the request
|
// set the content type for the request
|
||||||
header["content-type"] = req.ContentType()
|
header["x-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.newCodec(req.ContentType())
|
cf, err := g.newGRPCCodec(req.ContentType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
@ -218,32 +187,15 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
|
|
||||||
wc := &wrapMicroCodec{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.Context); opts != nil {
|
if opts := g.getGrpcDialOptions(); opts != nil {
|
||||||
grpcDialOptions = append(grpcDialOptions, opts...)
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
contextDialer := g.opts.ContextDialer
|
cc, err := g.pool.getConn(dialCtx, addr, grpcDialOptions...)
|
||||||
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))
|
||||||
}
|
}
|
||||||
@ -255,17 +207,12 @@ 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.Context); opts != nil {
|
if opts := g.getGrpcCallOptions(); 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)
|
||||||
@ -276,7 +223,7 @@ 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.Put(cc, err)
|
g.pool.release(addr, 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))
|
||||||
}
|
}
|
||||||
@ -298,20 +245,19 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
},
|
},
|
||||||
conn: cc,
|
conn: cc,
|
||||||
close: func(err error) {
|
close: func(err error) {
|
||||||
// cancel the context if an error occurred
|
// cancel the context if an error occured
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// defer execution of release
|
// defer execution of release
|
||||||
g.pool.Put(cc, err)
|
g.pool.release(addr, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,31 +305,16 @@ func (g *grpcClient) maxSendMsgSizeValue() int {
|
|||||||
return v.(int)
|
return v.(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) newCodec(ct string) (codec.Codec, error) {
|
func (g *grpcClient) newGRPCCodec(ct string) (codec.Codec, error) {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
defer g.RUnlock()
|
defer g.RUnlock()
|
||||||
|
|
||||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
|
||||||
ct = ct[:idx]
|
|
||||||
}
|
|
||||||
|
|
||||||
if c, ok := g.opts.Codecs[ct]; ok {
|
if c, ok := g.opts.Codecs[ct]; ok {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
return nil, codec.ErrUnknownContentType
|
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 {
|
if len(opts) == 0 && g.init {
|
||||||
return nil
|
return nil
|
||||||
@ -447,7 +378,6 @@ 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)
|
||||||
}
|
}
|
||||||
@ -496,8 +426,20 @@ func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
callOpts.Address = []string{g.opts.Proxy}
|
callOpts.Address = []string{g.opts.Proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
var next selector.Next
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
@ -510,23 +452,6 @@ 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()
|
||||||
|
|
||||||
@ -618,7 +543,18 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
callOpts.Address = []string{g.opts.Proxy}
|
callOpts.Address = []string{g.opts.Proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
var next selector.Next
|
// lookup the route to send the reques to
|
||||||
|
// 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
|
||||||
@ -632,23 +568,6 @@ 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()
|
||||||
|
|
||||||
@ -712,42 +631,24 @@ 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 {
|
||||||
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
|
||||||
|
|
||||||
options := client.NewPublishOptions(opts...)
|
options := client.NewPublishOptions(opts...)
|
||||||
|
|
||||||
// get proxy
|
md, ok := metadata.FromOutgoingContext(ctx)
|
||||||
exchange := ""
|
|
||||||
if v, ok := os.LookupEnv("MICRO_PROXY"); ok {
|
|
||||||
exchange = v
|
|
||||||
}
|
|
||||||
|
|
||||||
msgs := make([]*broker.Message, 0, len(ps))
|
|
||||||
|
|
||||||
omd, ok := metadata.FromOutgoingContext(ctx)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
omd = metadata.New(2)
|
md = metadata.New(2)
|
||||||
}
|
}
|
||||||
|
md["Content-Type"] = p.ContentType()
|
||||||
for _, p := range ps {
|
md["Micro-Topic"] = p.Topic()
|
||||||
md := metadata.Copy(omd)
|
|
||||||
md[metadata.HeaderContentType] = p.ContentType()
|
|
||||||
|
|
||||||
// passed in raw data
|
// passed in raw data
|
||||||
if d, ok := p.Payload().(*codec.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.newCodec(p.ContentType())
|
cf, err := g.newGRPCCodec(p.ContentType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
@ -760,21 +661,16 @@ func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...c
|
|||||||
}
|
}
|
||||||
|
|
||||||
topic := p.Topic()
|
topic := p.Topic()
|
||||||
if len(exchange) > 0 {
|
|
||||||
topic = exchange
|
// get the exchange
|
||||||
|
if len(options.Exchange) > 0 {
|
||||||
|
topic = options.Exchange
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range p.Metadata() {
|
return g.opts.Broker.Publish(metadata.NewOutgoingContext(ctx, md), topic, &broker.Message{
|
||||||
md.Set(k, v)
|
Header: md,
|
||||||
}
|
Body: body,
|
||||||
md.Set(metadata.HeaderTopic, topic)
|
}, broker.PublishContext(options.Context))
|
||||||
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 {
|
||||||
@ -785,45 +681,41 @@ func (g *grpcClient) Name() string {
|
|||||||
return g.opts.Name
|
return g.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) getGrpcDialOptions(ctx context.Context) []grpc.DialOption {
|
func (g *grpcClient) getGrpcDialOptions() []grpc.DialOption {
|
||||||
var opts []grpc.DialOption
|
if g.opts.CallOptions.Context == nil {
|
||||||
|
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...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx != nil {
|
v := g.opts.CallOptions.Context.Value(grpcDialOptions{})
|
||||||
if v := ctx.Value(grpcDialOptions{}); v != nil {
|
|
||||||
if vopts, ok := v.([]grpc.DialOption); ok {
|
if v == nil {
|
||||||
opts = append(opts, vopts...)
|
return nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opts, ok := v.([]grpc.DialOption)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) getGrpcCallOptions(ctx context.Context) []grpc.CallOption {
|
func (g *grpcClient) getGrpcCallOptions() []grpc.CallOption {
|
||||||
var opts []grpc.CallOption
|
if g.opts.CallOptions.Context == nil {
|
||||||
|
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...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx != nil {
|
v := g.opts.CallOptions.Context.Value(grpcCallOptions{})
|
||||||
if v := ctx.Value(grpcCallOptions{}); v != nil {
|
|
||||||
if vopts, ok := v.([]grpc.CallOption); ok {
|
if v == nil {
|
||||||
opts = append(opts, vopts...)
|
return nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opts, ok := v.([]grpc.CallOption)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
@ -832,15 +724,14 @@ func (g *grpcClient) getGrpcCallOptions(ctx context.Context) []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
|
||||||
if options.ContentType == "" {
|
options.ContentType = "application/grpc+proto"
|
||||||
options.ContentType = DefaultContentType
|
|
||||||
}
|
|
||||||
|
|
||||||
rc := &grpcClient{
|
rc := &grpcClient{
|
||||||
opts: options,
|
opts: options,
|
||||||
}
|
}
|
||||||
|
|
||||||
rc.pool = NewConnPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
||||||
|
|
||||||
c := client.Client(rc)
|
c := client.Client(rc)
|
||||||
|
|
||||||
// wrap in reverse
|
// wrap in reverse
|
||||||
|
55
grpc_pool.go
55
grpc_pool.go
@ -2,7 +2,6 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -10,47 +9,56 @@ import (
|
|||||||
"google.golang.org/grpc/connectivity"
|
"google.golang.org/grpc/connectivity"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConnPool struct {
|
type pool 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 {
|
||||||
err error
|
// grpc conn
|
||||||
*grpc.ClientConn
|
*grpc.ClientConn
|
||||||
next *PoolConn
|
err error
|
||||||
pool *ConnPool
|
|
||||||
sp *streamsPool
|
|
||||||
pre *PoolConn
|
|
||||||
addr string
|
addr string
|
||||||
|
|
||||||
|
// pool and streams pool
|
||||||
|
pool *pool
|
||||||
|
sp *streamsPool
|
||||||
streams int
|
streams int
|
||||||
created int64
|
created int64
|
||||||
|
|
||||||
|
// list
|
||||||
|
pre *poolConn
|
||||||
|
next *poolConn
|
||||||
in bool
|
in bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnPool(size int, ttl time.Duration, idle int, ms int) *ConnPool {
|
func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
|
||||||
if ms <= 0 {
|
if ms <= 0 {
|
||||||
ms = 1
|
ms = 1
|
||||||
}
|
}
|
||||||
if idle < 0 {
|
if idle < 0 {
|
||||||
idle = 0
|
idle = 0
|
||||||
}
|
}
|
||||||
return &ConnPool{
|
return &pool{
|
||||||
size: size,
|
size: size,
|
||||||
ttl: int64(ttl.Seconds()),
|
ttl: int64(ttl.Seconds()),
|
||||||
maxStreams: ms,
|
maxStreams: ms,
|
||||||
@ -59,15 +67,12 @@ func NewConnPool(size int, ttl time.Duration, idle int, ms int) *ConnPool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) Get(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]
|
||||||
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
|
||||||
@ -130,12 +135,12 @@ func (p *ConnPool) Get(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{ClientConn: cc, err: nil, addr: addr, pool: p, sp: sp, streams: 1, created: time.Now().Unix(), pre: nil, next: nil, in: false}
|
conn = &poolConn{cc, nil, addr, p, sp, 1, time.Now().Unix(), nil, nil, false}
|
||||||
|
|
||||||
// add conn to streams pool
|
// add conn to streams pool
|
||||||
p.Lock()
|
p.Lock()
|
||||||
@ -147,7 +152,7 @@ func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) Put(conn *PoolConn, err error) {
|
func (p *pool) release(addr string, 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
|
||||||
@ -182,11 +187,11 @@ func (p *ConnPool) Put(conn *PoolConn, err error) {
|
|||||||
p.Unlock()
|
p.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *PoolConn) Close() {
|
func (conn *poolConn) Close() {
|
||||||
conn.pool.Put(conn, conn.err)
|
conn.pool.release(conn.addr, 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
|
||||||
}
|
}
|
||||||
@ -199,7 +204,7 @@ func removeConn(conn *PoolConn) {
|
|||||||
conn.sp.count--
|
conn.sp.count--
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
16
message.go
16
message.go
@ -1,19 +1,20 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.unistack.org/micro/v3/client"
|
"github.com/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
|
||||||
opts client.MessageOptions
|
payload interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
options := client.NewMessageOptions(opts...)
|
var options client.MessageOptions
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
if len(options.ContentType) > 0 {
|
if len(options.ContentType) > 0 {
|
||||||
contentType = options.ContentType
|
contentType = options.ContentType
|
||||||
@ -23,7 +24,6 @@ func newGRPCEvent(topic string, payload interface{}, contentType string, opts ..
|
|||||||
payload: payload,
|
payload: payload,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
opts: options,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +38,3 @@ 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,14 +3,15 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/client"
|
"github.com/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 connection
|
// DefaultPoolMaxStreams maximum streams on a connectioin
|
||||||
// (20)
|
// (20)
|
||||||
DefaultPoolMaxStreams = 20
|
DefaultPoolMaxStreams = 20
|
||||||
|
|
||||||
@ -25,12 +26,16 @@ 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 {
|
||||||
@ -42,8 +47,6 @@ 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) {
|
||||||
@ -54,8 +57,6 @@ 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) {
|
||||||
@ -71,9 +72,19 @@ func Codec(contentType string, c encoding.Codec) client.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type maxRecvMsgSizeKey struct{}
|
// AuthTLS should be used to setup a secure authentication using TLS
|
||||||
|
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 {
|
||||||
@ -83,9 +94,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 {
|
||||||
@ -95,9 +106,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 {
|
||||||
@ -107,9 +118,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 {
|
||||||
@ -118,14 +129,3 @@ 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
21
request.go
21
request.go
@ -4,17 +4,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"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/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,12 +38,15 @@ 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, opts ...client.RequestOption) client.Request {
|
func newGRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request {
|
||||||
options := client.NewRequestOptions(opts...)
|
var opts client.RequestOptions
|
||||||
|
for _, o := range reqOpts {
|
||||||
|
o(&opts)
|
||||||
|
}
|
||||||
|
|
||||||
// set the content-type specified
|
// set the content-type specified
|
||||||
if len(options.ContentType) > 0 {
|
if len(opts.ContentType) > 0 {
|
||||||
contentType = options.ContentType
|
contentType = opts.ContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
return &grpcRequest{
|
return &grpcRequest{
|
||||||
@ -51,7 +54,7 @@ func newGRPCRequest(service, method string, request interface{}, contentType str
|
|||||||
method: method,
|
method: method,
|
||||||
request: request,
|
request: request,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
opts: options,
|
opts: opts,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,13 +3,13 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type response struct {
|
type response struct {
|
||||||
conn *PoolConn
|
conn *poolConn
|
||||||
stream grpc.ClientStream
|
stream grpc.ClientStream
|
||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
}
|
}
|
||||||
@ -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 nil
|
return metadata.New(0)
|
||||||
}
|
}
|
||||||
md := metadata.New(len(meta))
|
md := metadata.New(len(meta))
|
||||||
for k, v := range meta {
|
for k, v := range meta {
|
||||||
|
56
stream.go
56
stream.go
@ -5,21 +5,23 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/client"
|
"github.com/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
|
|
||||||
err error
|
|
||||||
request client.Request
|
|
||||||
response client.Response
|
|
||||||
close func(err error)
|
|
||||||
conn *PoolConn
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
closed bool
|
closed bool
|
||||||
|
err error
|
||||||
|
conn *poolConn
|
||||||
|
request client.Request
|
||||||
|
response client.Response
|
||||||
|
context context.Context
|
||||||
|
close func(err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcStream) Context() context.Context {
|
func (g *grpcStream) Context() context.Context {
|
||||||
@ -42,14 +44,6 @@ 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)
|
||||||
|
|
||||||
@ -68,24 +62,6 @@ 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()
|
||||||
@ -116,17 +92,3 @@ 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