Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
6b95186e08 |
@@ -1,29 +0,0 @@
|
|||||||
name: lint
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
lint:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup deps
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run lint
|
|
||||||
uses: https://github.com/golangci/golangci-lint-action@v6
|
|
||||||
with:
|
|
||||||
version: 'latest'
|
|
@@ -1,34 +0,0 @@
|
|||||||
name: test
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup deps
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
@@ -1,53 +0,0 @@
|
|||||||
name: test
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: checkout tests
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: master
|
|
||||||
filter: 'blob:none'
|
|
||||||
repository: unistack-org/micro-tests
|
|
||||||
path: micro-tests
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup go work
|
|
||||||
env:
|
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
|
||||||
run: |
|
|
||||||
go work init
|
|
||||||
go work use .
|
|
||||||
go work use micro-tests
|
|
||||||
- name: setup deps
|
|
||||||
env:
|
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run tests
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
|
||||||
run: |
|
|
||||||
cd micro-tests
|
|
||||||
go test -mod readonly -v ./... || true
|
|
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
|
46
.github/workflows/build.yml
vendored
Normal file
46
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
name: build
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: setup
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.15
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: ${{ runner.os }}-go-
|
||||||
|
- name: deps
|
||||||
|
run: go get -v -t -d ./...
|
||||||
|
- name: test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
||||||
|
lint:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: lint
|
||||||
|
uses: golangci/golangci-lint-action@v1
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.30
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
46
.github/workflows/pr.yml
vendored
Normal file
46
.github/workflows/pr.yml
vendored
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
name: prbuild
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: setup
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.15
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: ${{ runner.os }}-go-
|
||||||
|
- name: deps
|
||||||
|
run: go get -v -t -d ./...
|
||||||
|
- name: test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
||||||
|
lint:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: lint
|
||||||
|
uses: golangci/golangci-lint-action@v1
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.30
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
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,5 +0,0 @@
|
|||||||
run:
|
|
||||||
concurrency: 8
|
|
||||||
deadline: 5m
|
|
||||||
issues-exit-code: 1
|
|
||||||
tests: true
|
|
192
LICENSE
192
LICENSE
@@ -1,192 +0,0 @@
|
|||||||
|
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
Copyright 2015-2020 Asim Aslam.
|
|
||||||
Copyright 2019-2020 Unistack LLC.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
25
README.md
Normal file
25
README.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# GRPC Client
|
||||||
|
|
||||||
|
The grpc client is a [micro.Client](https://godoc.org/github.com/micro/go-micro/client#Client) compatible client.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The client makes use of the [google.golang.org/grpc](google.golang.org/grpc) framework for the underlying communication mechanism.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Specify the client to your micro service
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/micro/go-micro"
|
||||||
|
"github.com/micro/go-plugins/client/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
service := micro.NewService(
|
||||||
|
micro.Name("greeter"),
|
||||||
|
micro.Client(grpc.NewClient()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
242
codec.go
242
codec.go
@@ -1,127 +1,231 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
b "bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
oldjsonpb "github.com/golang/protobuf/jsonpb"
|
||||||
|
oldproto "github.com/golang/protobuf/proto"
|
||||||
|
bytes "github.com/unistack-org/micro-codec-bytes"
|
||||||
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/encoding"
|
"google.golang.org/grpc/encoding"
|
||||||
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type jsonCodec struct{}
|
||||||
|
type protoCodec struct{}
|
||||||
|
type bytesCodec struct{}
|
||||||
|
type wrapCodec struct{ encoding.Codec }
|
||||||
|
|
||||||
|
var jsonpbMarshaler = &jsonpb.MarshalOptions{}
|
||||||
|
var oldjsonpbMarshaler = &oldjsonpb.Marshaler{}
|
||||||
|
var useNumber bool
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ encoding.Codec = &wrapMicroCodec{}
|
defaultGRPCCodecs = map[string]encoding.Codec{
|
||||||
_ codec.Codec = &wrapGrpcCodec{}
|
"application/json": jsonCodec{},
|
||||||
|
"application/proto": protoCodec{},
|
||||||
|
"application/protobuf": protoCodec{},
|
||||||
|
"application/octet-stream": protoCodec{},
|
||||||
|
"application/grpc": protoCodec{},
|
||||||
|
"application/grpc+json": jsonCodec{},
|
||||||
|
"application/grpc+proto": protoCodec{},
|
||||||
|
"application/grpc+bytes": bytesCodec{},
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type wrapStream struct{ grpc.ClientStream }
|
// UseNumber fix unmarshal Number(8234567890123456789) to interface(8.234567890123457e+18)
|
||||||
|
func UseNumber() {
|
||||||
func (w *wrapStream) Write(d []byte) (int, error) {
|
useNumber = true
|
||||||
n := len(d)
|
|
||||||
err := w.ClientStream.SendMsg(&codec.Frame{Data: d})
|
|
||||||
return n, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapStream) Read(d []byte) (int, error) {
|
func (w wrapCodec) String() string {
|
||||||
m := &codec.Frame{}
|
|
||||||
err := w.ClientStream.RecvMsg(m)
|
|
||||||
copy(d, m.Data)
|
|
||||||
return len(d), err
|
|
||||||
}
|
|
||||||
|
|
||||||
type wrapMicroCodec struct{ codec.Codec }
|
|
||||||
|
|
||||||
func (w *wrapMicroCodec) Name() string {
|
|
||||||
return w.Codec.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *wrapMicroCodec) Marshal(v interface{}) ([]byte, error) {
|
|
||||||
return w.Codec.Marshal(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *wrapMicroCodec) Unmarshal(d []byte, v interface{}) error {
|
|
||||||
return w.Codec.Unmarshal(d, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
type wrapGrpcCodec struct{ encoding.Codec }
|
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) String() string {
|
|
||||||
return w.Codec.Name()
|
return w.Codec.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
func (w wrapCodec) Marshal(v interface{}) ([]byte, error) {
|
||||||
if m, ok := v.(*codec.Frame); ok {
|
switch m := v.(type) {
|
||||||
|
case *bytes.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 wrapCodec) Unmarshal(data []byte, v interface{}) error {
|
||||||
if d == nil || v == nil {
|
if len(data) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if m, ok := v.(*codec.Frame); ok {
|
if v == nil {
|
||||||
m.Data = d
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return w.Codec.Unmarshal(d, v)
|
switch m := v.(type) {
|
||||||
|
case *bytes.Frame:
|
||||||
|
m.Data = data
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return w.Codec.Unmarshal(data, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *bytes.Frame:
|
||||||
|
return m.Data, nil
|
||||||
|
case proto.Message:
|
||||||
|
return proto.Marshal(m)
|
||||||
|
case oldproto.Message:
|
||||||
|
return oldproto.Marshal(m)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to marshal: %v is not type of *bytes.Frame or proto.Message", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *bytes.Frame:
|
||||||
|
m.Data = data
|
||||||
|
return nil
|
||||||
|
case proto.Message:
|
||||||
|
return proto.Unmarshal(data, m)
|
||||||
|
case oldproto.Message:
|
||||||
|
return oldproto.Unmarshal(data, m)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("failed to unmarshal: %v is not type of *bytes.Frame or proto.Message", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (protoCodec) Name() string {
|
||||||
|
return "proto"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bytesCodec) Marshal(v interface{}) ([]byte, error) {
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *[]byte:
|
||||||
|
return *m, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *[]byte:
|
||||||
|
*m = data
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bytesCodec) Name() string {
|
||||||
|
return "bytes"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *bytes.Frame:
|
||||||
|
return m.Data, nil
|
||||||
|
case proto.Message:
|
||||||
|
return jsonpbMarshaler.Marshal(m)
|
||||||
|
case oldproto.Message:
|
||||||
|
buf, err := oldjsonpbMarshaler.MarshalToString(m)
|
||||||
|
return []byte(buf), err
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
switch m := v.(type) {
|
||||||
|
case *bytes.Frame:
|
||||||
|
m.Data = data
|
||||||
|
return nil
|
||||||
|
case proto.Message:
|
||||||
|
return jsonpb.Unmarshal(data, m)
|
||||||
|
case oldproto.Message:
|
||||||
|
return oldjsonpb.Unmarshal(b.NewReader(data), m)
|
||||||
|
}
|
||||||
|
dec := json.NewDecoder(b.NewReader(data))
|
||||||
|
if useNumber {
|
||||||
|
dec.UseNumber()
|
||||||
|
}
|
||||||
|
return dec.Decode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jsonCodec) Name() string {
|
||||||
|
return "json"
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
type grpcCodec struct {
|
type grpcCodec struct {
|
||||||
grpc.ServerStream
|
|
||||||
// headers
|
// headers
|
||||||
id string
|
id string
|
||||||
target string
|
target string
|
||||||
method string
|
method string
|
||||||
endpoint string
|
endpoint string
|
||||||
|
|
||||||
|
s grpc.ClientStream
|
||||||
c encoding.Codec
|
c encoding.Codec
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
func (g *grpcCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
|
||||||
|
md, err := g.s.Header()
|
||||||
func (w *wrapGrpcCodec) ReadHeader(conn io.Reader, m *codec.Message, mt codec.MessageType) error {
|
if err != nil {
|
||||||
/*
|
return err
|
||||||
if m == nil {
|
}
|
||||||
m = codec.NewMessage(codec.Request)
|
if m == nil {
|
||||||
|
m = new(codec.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if md, ok := metadata.FromIncomingContext(g.ServerStream.Context()); ok {
|
|
||||||
if m.Header == nil {
|
if m.Header == nil {
|
||||||
m.Header = meta.New(len(md))
|
m.Header = make(map[string]string, len(md))
|
||||||
}
|
}
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
m.Header[k] = strings.Join(v, ",")
|
m.Header[k] = strings.Join(v, ",")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
m.Id = g.id
|
m.Id = g.id
|
||||||
m.Target = g.target
|
m.Target = g.target
|
||||||
m.Method = g.method
|
m.Method = g.method
|
||||||
m.Endpoint = g.endpoint
|
m.Endpoint = g.endpoint
|
||||||
*/
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) ReadBody(conn io.Reader, v interface{}) error {
|
func (g *grpcCodec) ReadBody(v interface{}) error {
|
||||||
// caller has requested a frame
|
switch m := v.(type) {
|
||||||
if m, ok := v.(*codec.Frame); ok {
|
case *bytes.Frame:
|
||||||
_, err := conn.Read(m.Data)
|
return g.s.RecvMsg(m)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return codec.ErrInvalidMessage
|
return g.s.RecvMsg(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapGrpcCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
func (g *grpcCodec) Write(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)
|
return g.s.SendMsg(v)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m.Body = b
|
|
||||||
}
|
}
|
||||||
// write the body using the framing codec
|
// write the body using the framing codec
|
||||||
_, err := conn.Write(m.Body)
|
return g.s.SendMsg(&bytes.Frame{Data: m.Body})
|
||||||
return err
|
}
|
||||||
|
|
||||||
|
func (g *grpcCodec) Close() error {
|
||||||
|
return g.s.CloseSend()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *grpcCodec) String() string {
|
||||||
|
return g.c.Name()
|
||||||
}
|
}
|
||||||
|
40
error.go
40
error.go
@@ -1,7 +1,8 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.unistack.org/micro/v3/errors"
|
pberr "github.com/unistack-org/micro-client-grpc/errors"
|
||||||
|
"github.com/unistack-org/micro/v3/errors"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -9,46 +10,35 @@ func microError(err error) error {
|
|||||||
// no error
|
// no error
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// nothing to do
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if verr, ok := err.(*errors.Error); ok {
|
if verr, ok := err.(*errors.Error); ok {
|
||||||
// micro error
|
|
||||||
return verr
|
return verr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if verr, ok := err.(*pberr.Error); ok {
|
||||||
|
return &errors.Error{Id: verr.Id, Code: verr.Code, Detail: verr.Detail, Status: verr.Status}
|
||||||
|
}
|
||||||
|
|
||||||
// grpc error
|
// grpc error
|
||||||
s, ok := status.FromError(err)
|
s, ok := status.FromError(err)
|
||||||
if !ok {
|
if !ok {
|
||||||
// can't get status detals from grpc error, return base error
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
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())
|
||||||
}
|
}
|
||||||
|
159
errors/errors.go
Normal file
159
errors/errors.go
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (e *Error) Error() string {
|
||||||
|
return fmt.Sprintf(`{"id":"%s","code":%d,"detail":"%s","status":"%s"}`, e.Id, e.Code, e.Detail, e.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New generates a custom error.
|
||||||
|
func New(id, detail string, code int32) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: code,
|
||||||
|
Detail: detail,
|
||||||
|
// Status: http.StatusText(int(code)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BadRequest generates a 400 error.
|
||||||
|
func BadRequest(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 400,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
// Status: http.StatusText(400),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unauthorized generates a 401 error.
|
||||||
|
func Unauthorized(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 401,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
// Status: http.StatusText(401),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forbidden generates a 403 error.
|
||||||
|
func Forbidden(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 403,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
// Status: http.StatusText(403),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotFound generates a 404 error.
|
||||||
|
func NotFound(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 404,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
// Status: http.StatusText(404),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MethodNotAllowed generates a 405 error.
|
||||||
|
func MethodNotAllowed(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 405,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
//Status: http.StatusText(405),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timeout generates a 408 error.
|
||||||
|
func Timeout(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 408,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
//Status: http.StatusText(408),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conflict generates a 409 error.
|
||||||
|
func Conflict(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 409,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
//Status: http.StatusText(409),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InternalServerError generates a 500 error.
|
||||||
|
func InternalServerError(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 500,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
//Status: http.StatusText(500),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotImplemented generates a 501 error
|
||||||
|
func NotImplemented(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 501,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
//Status: http.StatusText(501),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BadGateway generates a 502 error
|
||||||
|
func BadGateway(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 502,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
// Status: http.StatusText(502),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServiceUnavailable generates a 503 error
|
||||||
|
func ServiceUnavailable(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 503,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
//Status: http.StatusText(503),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GatewayTimeout generates a 504 error
|
||||||
|
func GatewayTimeout(id, format string, a ...interface{}) error {
|
||||||
|
return &Error{
|
||||||
|
Id: id,
|
||||||
|
Code: 504,
|
||||||
|
Detail: fmt.Sprintf(format, a...),
|
||||||
|
//Status: http.StatusText(504),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equal tries to compare errors
|
||||||
|
func Equal(err1 error, err2 error) bool {
|
||||||
|
verr1, ok1 := err1.(*Error)
|
||||||
|
verr2, ok2 := err2.(*Error)
|
||||||
|
|
||||||
|
if ok1 != ok2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ok1 {
|
||||||
|
return err1 == err2
|
||||||
|
}
|
||||||
|
|
||||||
|
if verr1.Code != verr2.Code {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
173
errors/errors.pb.go
Normal file
173
errors/errors.pb.go
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// protoc-gen-go v1.25.0-devel
|
||||||
|
// protoc v3.6.1
|
||||||
|
// source: errors.proto
|
||||||
|
|
||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
|
reflect "reflect"
|
||||||
|
sync "sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Verify that this generated code is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||||
|
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||||
|
)
|
||||||
|
|
||||||
|
// This is a compile-time assertion that a sufficiently up-to-date version
|
||||||
|
// of the legacy proto package is being used.
|
||||||
|
const _ = proto.ProtoPackageIsVersion4
|
||||||
|
|
||||||
|
type Error struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
|
Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
|
||||||
|
Detail string `protobuf:"bytes,3,opt,name=detail,proto3" json:"detail,omitempty"`
|
||||||
|
Status string `protobuf:"bytes,4,opt,name=status,proto3" json:"status,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Error) Reset() {
|
||||||
|
*x = Error{}
|
||||||
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_errors_proto_msgTypes[0]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Error) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Error) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Error) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_errors_proto_msgTypes[0]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Error.ProtoReflect.Descriptor instead.
|
||||||
|
func (*Error) Descriptor() ([]byte, []int) {
|
||||||
|
return file_errors_proto_rawDescGZIP(), []int{0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Error) GetId() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Id
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Error) GetCode() int32 {
|
||||||
|
if x != nil {
|
||||||
|
return x.Code
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Error) GetDetail() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Detail
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Error) GetStatus() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Status
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var File_errors_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
|
var file_errors_proto_rawDesc = []byte{
|
||||||
|
0x0a, 0x0c, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06,
|
||||||
|
0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x22, 0x5b, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12,
|
||||||
|
0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12,
|
||||||
|
0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63,
|
||||||
|
0x6f, 0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x18, 0x03, 0x20,
|
||||||
|
0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x73,
|
||||||
|
0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61,
|
||||||
|
0x74, 0x75, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
file_errors_proto_rawDescOnce sync.Once
|
||||||
|
file_errors_proto_rawDescData = file_errors_proto_rawDesc
|
||||||
|
)
|
||||||
|
|
||||||
|
func file_errors_proto_rawDescGZIP() []byte {
|
||||||
|
file_errors_proto_rawDescOnce.Do(func() {
|
||||||
|
file_errors_proto_rawDescData = protoimpl.X.CompressGZIP(file_errors_proto_rawDescData)
|
||||||
|
})
|
||||||
|
return file_errors_proto_rawDescData
|
||||||
|
}
|
||||||
|
|
||||||
|
var file_errors_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
|
||||||
|
var file_errors_proto_goTypes = []interface{}{
|
||||||
|
(*Error)(nil), // 0: errors.Error
|
||||||
|
}
|
||||||
|
var file_errors_proto_depIdxs = []int32{
|
||||||
|
0, // [0:0] is the sub-list for method output_type
|
||||||
|
0, // [0:0] is the sub-list for method input_type
|
||||||
|
0, // [0:0] is the sub-list for extension type_name
|
||||||
|
0, // [0:0] is the sub-list for extension extendee
|
||||||
|
0, // [0:0] is the sub-list for field type_name
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { file_errors_proto_init() }
|
||||||
|
func file_errors_proto_init() {
|
||||||
|
if File_errors_proto != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !protoimpl.UnsafeEnabled {
|
||||||
|
file_errors_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*Error); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type x struct{}
|
||||||
|
out := protoimpl.TypeBuilder{
|
||||||
|
File: protoimpl.DescBuilder{
|
||||||
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
|
RawDescriptor: file_errors_proto_rawDesc,
|
||||||
|
NumEnums: 0,
|
||||||
|
NumMessages: 1,
|
||||||
|
NumExtensions: 0,
|
||||||
|
NumServices: 0,
|
||||||
|
},
|
||||||
|
GoTypes: file_errors_proto_goTypes,
|
||||||
|
DependencyIndexes: file_errors_proto_depIdxs,
|
||||||
|
MessageInfos: file_errors_proto_msgTypes,
|
||||||
|
}.Build()
|
||||||
|
File_errors_proto = out.File
|
||||||
|
file_errors_proto_rawDesc = nil
|
||||||
|
file_errors_proto_goTypes = nil
|
||||||
|
file_errors_proto_depIdxs = nil
|
||||||
|
}
|
21
errors/errors.pb.micro.go
Normal file
21
errors/errors.pb.micro.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||||
|
// source: errors.proto
|
||||||
|
|
||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
fmt "fmt"
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
math "math"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
10
errors/errors.proto
Normal file
10
errors/errors.proto
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package errors;
|
||||||
|
|
||||||
|
message Error {
|
||||||
|
string id = 1;
|
||||||
|
int32 code = 2;
|
||||||
|
string detail = 3;
|
||||||
|
string status = 4;
|
||||||
|
};
|
3
generate.go
Normal file
3
generate.go
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package grpc
|
||||||
|
|
||||||
|
//go:generate protoc -I./errors -I. --go-grpc_out=paths=source_relative:./errors --go_out=paths=source_relative:./errors --micro_out=paths=source_relative:./errors errors/errors.proto
|
15
go.mod
15
go.mod
@@ -1,8 +1,15 @@
|
|||||||
module go.unistack.org/micro-client-grpc/v3
|
module github.com/unistack-org/micro-client-grpc
|
||||||
|
|
||||||
go 1.16
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
go.unistack.org/micro/v3 v3.10.22
|
github.com/golang/protobuf v1.4.2
|
||||||
google.golang.org/grpc v1.52.3
|
github.com/google/go-cmp v0.5.1 // indirect
|
||||||
|
github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844
|
||||||
|
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920124807-9b11ea527aeb
|
||||||
|
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 // indirect
|
||||||
|
golang.org/x/text v0.3.3 // indirect
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
|
google.golang.org/grpc v1.31.1
|
||||||
|
google.golang.org/protobuf v1.25.0
|
||||||
)
|
)
|
||||||
|
425
grpc.go
425
grpc.go
@@ -1,52 +1,57 @@
|
|||||||
// 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"
|
raw "github.com/unistack-org/micro-codec-bytes"
|
||||||
"go.unistack.org/micro/v3/client"
|
"github.com/unistack-org/micro/v3/broker"
|
||||||
"go.unistack.org/micro/v3/codec"
|
"github.com/unistack-org/micro/v3/client"
|
||||||
"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
|
||||||
|
codecs map[string]encoding.Codec
|
||||||
|
pool *pool
|
||||||
|
once atomic.Value
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
init bool
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
encoding.RegisterCodec(wrapCodec{jsonCodec{}})
|
||||||
|
encoding.RegisterCodec(wrapCodec{protoCodec{}})
|
||||||
|
encoding.RegisterCodec(wrapCodec{bytesCodec{}})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,41 +69,37 @@ 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 {
|
||||||
var header map[string]string
|
var header map[string]string
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
header = make(map[string]string)
|
||||||
|
if md, ok := metadata.FromContext(ctx); ok {
|
||||||
header = make(map[string]string, len(md))
|
header = make(map[string]string, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
header[strings.ToLower(k)] = v
|
header[strings.ToLower(k)] = v
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
header = make(map[string]string, 2)
|
header = make(map[string]string)
|
||||||
}
|
|
||||||
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 +118,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(cf),
|
||||||
|
grpc.CallContentSubtype(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,20 +153,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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
||||||
var header map[string]string
|
var header map[string]string
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
if md, ok := metadata.FromContext(ctx); ok {
|
||||||
header = make(map[string]string, len(md))
|
header = make(map[string]string, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
header[k] = v
|
header[k] = v
|
||||||
@@ -193,16 +170,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())
|
||||||
}
|
}
|
||||||
@@ -216,34 +192,17 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
wc := &wrapMicroCodec{cf}
|
wc := wrapCodec{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 +214,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(cf.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,14 +230,19 @@ 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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
codec := &grpcCodec{
|
||||||
|
s: st,
|
||||||
|
c: wc,
|
||||||
|
}
|
||||||
|
|
||||||
// set request codec
|
// set request codec
|
||||||
if r, ok := req.(*grpcRequest); ok {
|
if r, ok := req.(*grpcRequest); ok {
|
||||||
r.codec = cf
|
r.codec = codec
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup the stream response
|
// setup the stream response
|
||||||
@@ -295,23 +254,23 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
conn: cc,
|
conn: cc,
|
||||||
stream: st,
|
stream: st,
|
||||||
codec: cf,
|
codec: cf,
|
||||||
|
gcodec: codec,
|
||||||
},
|
},
|
||||||
conn: cc,
|
conn: cc,
|
||||||
close: func(err error) {
|
close: func(err error) {
|
||||||
// cancel the context if an error 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,35 +318,17 @@ func (g *grpcClient) maxSendMsgSizeValue() int {
|
|||||||
return v.(int)
|
return v.(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) newCodec(ct string) (codec.Codec, error) {
|
func (g *grpcClient) newGRPCCodec(contentType string) (encoding.Codec, error) {
|
||||||
g.RLock()
|
g.RLock()
|
||||||
defer g.RUnlock()
|
defer g.RUnlock()
|
||||||
|
|
||||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
if c, ok := g.codecs[contentType]; ok {
|
||||||
ct = ct[:idx]
|
return wrapCodec{c}, nil
|
||||||
}
|
}
|
||||||
|
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
||||||
if c, ok := g.opts.Codecs[ct]; ok {
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
return nil, codec.ErrUnknownContentType
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *grpcClient) serviceConfig() string {
|
|
||||||
if g.opts.Context == nil {
|
|
||||||
return DefaultServiceConfig
|
|
||||||
}
|
|
||||||
v := g.opts.Context.Value(serviceConfigKey{})
|
|
||||||
if v == nil {
|
|
||||||
return DefaultServiceConfig
|
|
||||||
}
|
|
||||||
return v.(string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) Init(opts ...client.Option) error {
|
func (g *grpcClient) Init(opts ...client.Option) error {
|
||||||
if len(opts) == 0 && g.init {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
size := g.opts.PoolSize
|
size := g.opts.PoolSize
|
||||||
ttl := g.opts.PoolTTL
|
ttl := g.opts.PoolTTL
|
||||||
|
|
||||||
@@ -403,27 +344,6 @@ func (g *grpcClient) Init(opts ...client.Option) error {
|
|||||||
g.pool.Unlock()
|
g.pool.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := g.opts.Broker.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := g.opts.Tracer.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := g.opts.Router.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := g.opts.Logger.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := g.opts.Meter.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := g.opts.Transport.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
g.init = true
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -447,7 +367,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 +415,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 +441,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 +532,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,29 +557,12 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
time.Sleep(t)
|
time.Sleep(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
if next == nil {
|
|
||||||
var routes []string
|
|
||||||
|
|
||||||
// lookup the route to send the reques to
|
|
||||||
// TODO apply any filtering here
|
|
||||||
routes, err = g.opts.Lookup(ctx, req, callOpts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// balance the list of nodes
|
|
||||||
next, err = callOpts.Selector.Select(routes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the next node
|
// get the next node
|
||||||
node := next()
|
node := next()
|
||||||
|
|
||||||
// make the call
|
// make the call
|
||||||
stream := &grpcStream{}
|
stream := &grpcStream{}
|
||||||
err = gstream(ctx, node, req, stream, callOpts)
|
err = g.stream(ctx, node, req, stream, callOpts)
|
||||||
|
|
||||||
// record the result of the call to inform future routing decisions
|
// record the result of the call to inform future routing decisions
|
||||||
if verr := g.opts.Selector.Record(node, err); verr != nil {
|
if verr := g.opts.Selector.Record(node, err); verr != nil {
|
||||||
@@ -666,10 +574,7 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
return nil, verr
|
return nil, verr
|
||||||
}
|
}
|
||||||
|
|
||||||
if rerr := g.opts.Selector.Record(node, err); rerr != nil {
|
g.opts.Selector.Record(node, err)
|
||||||
return nil, rerr
|
|
||||||
}
|
|
||||||
|
|
||||||
return stream, err
|
return stream, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -712,42 +617,35 @@ 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...)
|
var options client.PublishOptions
|
||||||
}
|
|
||||||
|
|
||||||
func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...client.PublishOption) error {
|
|
||||||
var body []byte
|
var body []byte
|
||||||
|
|
||||||
options := client.NewPublishOptions(opts...)
|
// fail early on connect error
|
||||||
|
if !g.once.Load().(bool) {
|
||||||
// get proxy
|
if err := g.opts.Broker.Connect(); err != nil {
|
||||||
exchange := ""
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
if v, ok := os.LookupEnv("MICRO_PROXY"); ok {
|
}
|
||||||
exchange = v
|
g.once.Store(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
msgs := make([]*broker.Message, 0, len(ps))
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
omd, ok := metadata.FromOutgoingContext(ctx)
|
md, ok := metadata.FromContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
omd = metadata.New(2)
|
md = make(map[string]string)
|
||||||
}
|
}
|
||||||
|
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().(*raw.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,87 +658,78 @@ 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(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 {
|
||||||
return "grpc"
|
return "grpc"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) Name() string {
|
func (g *grpcClient) getGrpcDialOptions() []grpc.DialOption {
|
||||||
return g.opts.Name
|
if g.opts.CallOptions.Context == nil {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcClient) getGrpcDialOptions(ctx context.Context) []grpc.DialOption {
|
v := g.opts.CallOptions.Context.Value(grpcDialOptions{})
|
||||||
var opts []grpc.DialOption
|
|
||||||
|
|
||||||
if g.opts.CallOptions.Context != nil {
|
if v == nil {
|
||||||
if v := g.opts.CallOptions.Context.Value(grpcDialOptions{}); v != nil {
|
return nil
|
||||||
if vopts, ok := v.([]grpc.DialOption); ok {
|
|
||||||
opts = append(opts, vopts...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx != nil {
|
opts, ok := v.([]grpc.DialOption)
|
||||||
if v := ctx.Value(grpcDialOptions{}); v != nil {
|
|
||||||
if vopts, ok := v.([]grpc.DialOption); ok {
|
if !ok {
|
||||||
opts = append(opts, vopts...)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(opts ...client.Option) client.Client {
|
func newClient(opts ...client.Option) client.Client {
|
||||||
options := client.NewOptions(opts...)
|
options := client.NewOptions()
|
||||||
// default content type for grpc
|
// default content type for grpc
|
||||||
if options.ContentType == "" {
|
options.ContentType = "application/grpc+proto"
|
||||||
options.ContentType = DefaultContentType
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
rc := &grpcClient{
|
rc := &grpcClient{
|
||||||
opts: options,
|
opts: options,
|
||||||
}
|
}
|
||||||
|
rc.once.Store(false)
|
||||||
|
|
||||||
|
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
||||||
|
|
||||||
rc.pool = NewConnPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
|
||||||
c := client.Client(rc)
|
c := client.Client(rc)
|
||||||
|
|
||||||
// wrap in reverse
|
// wrap in reverse
|
||||||
@@ -848,17 +737,25 @@ func NewClient(opts ...client.Option) client.Client {
|
|||||||
c = options.Wrappers[i-1](c)
|
c = options.Wrappers[i-1](c)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rc.opts.Context != nil {
|
rc.codecs = make(map[string]encoding.Codec, len(defaultGRPCCodecs))
|
||||||
if codecs, ok := rc.opts.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && codecs != nil {
|
for k, v := range defaultGRPCCodecs {
|
||||||
for k, v := range codecs {
|
rc.codecs[k] = v
|
||||||
rc.opts.Codecs[k] = &wrapGrpcCodec{v}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var codecs map[string]encoding.Codec
|
||||||
|
if rc.opts.Context != nil {
|
||||||
|
if v := rc.opts.Context.Value(codecsKey{}); v != nil {
|
||||||
|
codecs = v.(map[string]encoding.Codec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, k := range options.Codecs {
|
for k, v := range codecs {
|
||||||
encoding.RegisterCodec(&wrapMicroCodec{k})
|
rc.codecs[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewClient(opts ...client.Option) client.Client {
|
||||||
|
return newClient(opts...)
|
||||||
|
}
|
||||||
|
58
grpc_pool.go
58
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
|
||||||
@@ -180,13 +185,14 @@ func (p *ConnPool) Put(conn *PoolConn, err error) {
|
|||||||
sp.idle++
|
sp.idle++
|
||||||
}
|
}
|
||||||
p.Unlock()
|
p.Unlock()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -197,9 +203,10 @@ func removeConn(conn *PoolConn) {
|
|||||||
conn.next = nil
|
conn.next = nil
|
||||||
conn.in = false
|
conn.in = false
|
||||||
conn.sp.count--
|
conn.sp.count--
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func addConnAfter(conn *PoolConn, after *PoolConn) {
|
func addConnAfter(conn *poolConn, after *poolConn) {
|
||||||
conn.next = after.next
|
conn.next = after.next
|
||||||
conn.pre = after
|
conn.pre = after
|
||||||
if after.next != nil {
|
if after.next != nil {
|
||||||
@@ -208,4 +215,5 @@ func addConnAfter(conn *PoolConn, after *PoolConn) {
|
|||||||
after.next = conn
|
after.next = conn
|
||||||
conn.in = true
|
conn.in = true
|
||||||
conn.sp.count++
|
conn.sp.count++
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
16
message.go
16
message.go
@@ -1,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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
23
request.go
23
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +74,7 @@ func (g *grpcRequest) Endpoint() string {
|
|||||||
return g.method
|
return g.method
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grpcRequest) Codec() codec.Codec {
|
func (g *grpcRequest) Codec() codec.Writer {
|
||||||
return g.codec
|
return g.codec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
32
response.go
32
response.go
@@ -3,39 +3,41 @@ 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-codec-bytes"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
type response struct {
|
type response struct {
|
||||||
conn *PoolConn
|
conn *poolConn
|
||||||
stream grpc.ClientStream
|
stream grpc.ClientStream
|
||||||
codec codec.Codec
|
codec encoding.Codec
|
||||||
|
gcodec codec.Codec
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the response
|
// Read the response
|
||||||
func (r *response) Codec() codec.Codec {
|
func (r *response) Codec() codec.Reader {
|
||||||
return r.codec
|
return r.gcodec
|
||||||
}
|
}
|
||||||
|
|
||||||
// read the header
|
// read the header
|
||||||
func (r *response) Header() metadata.Metadata {
|
func (r *response) Header() map[string]string {
|
||||||
meta, err := r.stream.Header()
|
md, err := r.stream.Header()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return map[string]string{}
|
||||||
}
|
}
|
||||||
md := metadata.New(len(meta))
|
hdr := make(map[string]string, len(md))
|
||||||
for k, v := range meta {
|
for k, v := range md {
|
||||||
md.Set(k, strings.Join(v, ","))
|
hdr[k] = strings.Join(v, ",")
|
||||||
}
|
}
|
||||||
return md
|
return hdr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the undecoded response
|
// Read the undecoded response
|
||||||
func (r *response) Read() ([]byte, error) {
|
func (r *response) Read() ([]byte, error) {
|
||||||
f := &codec.Frame{}
|
f := &bytes.Frame{}
|
||||||
if err := r.codec.ReadBody(&wrapStream{r.stream}, f); err != nil {
|
if err := r.gcodec.ReadBody(f); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return f.Data, nil
|
return f.Data, nil
|
||||||
|
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()
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user