Compare commits
	
		
			133 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | e219ace4b4 | ||
|  | e26a0889ae | ||
|  | 5ea51f5a29 | ||
|  | ee1c429e37 | ||
|  | 4a5b91c215 | ||
|  | 321446ce29 | ||
|  | b508565a78 | ||
|  | 1ee348109e | ||
|  | 1fb2d4c51e | ||
|  | 968a4f89c4 | ||
|  | 5f3b1daf82 | ||
|  | 65a5feab3c | ||
|  | f679b560bb | ||
|  | 91dfd46eb1 | ||
|  | 95f8aa68f2 | ||
|  | 79834cc131 | ||
|  | 04b5fc19c2 | ||
|  | 48de9f25a8 | ||
|  | 68402dcb96 | ||
|  | c3a9013863 | ||
|  | 8215db3be9 | ||
|  | 2e45567e56 | ||
|  | 07b32ad3b9 | ||
|  | 2ecd049871 | ||
|  | 6bf1bcf1b5 | ||
|  | 7058b444ab | ||
|  | c917566ce4 | ||
|  | ef9d975d9b | ||
|  | be06de5c47 | ||
|  | 975da990a9 | ||
|  | f2728a7fee | ||
|  | d3b37f14cf | ||
|  | 525b436bf1 | ||
|  | 7935276c54 | ||
|  | 1088fa0a31 | ||
|  | 0ebf401c21 | ||
|  | 916a2a1a99 | ||
|  | c459164ad4 | ||
|  | d7295810da | ||
|  | 1b8ccd13a6 | ||
|  | ffc9bff208 | ||
|  | fe67b26bfc | ||
|  | 1bdbe0fad5 | ||
|  | c7f24aa425 | ||
|  | 1ae2ac9020 | ||
|  | 274aacda1d | ||
|  | 00c39f4f2c | ||
|  | cc547a9f1a | ||
|  | d31708f8e9 | ||
|  | 25ec6331e8 | ||
|  | 97b4762ab2 | ||
|  | a511f1dc64 | ||
|  | e809cb9117 | ||
|  | 04726dbea5 | ||
|  | c49fef49b8 | ||
|  | 1a962e46fd | ||
|  | 5e35d89b38 | ||
|  | dad05be95e | ||
|  | c701f96a09 | ||
|  | 27aa1ff2ab | ||
|  | 8f6ba4a56e | ||
|  | 0a6e451539 | ||
|  | 231cfc48f0 | ||
|  | a365c51c2b | ||
|  | 02b74a5487 | ||
|  | 87e898f4fc | ||
|  | d246ccbeef | ||
|  | 017e156440 | ||
|  | d8f17ac827 | ||
|  | 6e2c9e7cd4 | ||
|  | 4028e0156b | ||
|  | 76275e857c | ||
|  | a18806c9ef | ||
|  | 255fecb4f4 | ||
|  | af8d55eb7f | ||
|  | 354a169050 | ||
|  | 6e083b9aca | ||
|  | 9dbd75f2cc | ||
|  | 8975184b88 | ||
|  | 19a54f2970 | ||
|  | 3c7f663e8b | ||
|  | 8fdc8f05ce | ||
|  | 35349bd313 | ||
|  | d5bfa1e795 | ||
|  | 3bb76868d1 | ||
|  | 275e92be32 | ||
|  | d2728b498c | ||
|  | 601b223cfb | ||
|  | 04d2aa4696 | ||
|  | b8f79a3fc6 | ||
|  | acd3bea0c6 | ||
|  | a02a25d955 | ||
|  | d5e345d41d | ||
|  | 71f8cbd5e2 | ||
|  | f12473f4b1 | ||
|  | 724e2b5830 | ||
|  | 6bdf33c4ee | ||
|  | 84f52fd7ac | ||
|  | 6e30b53280 | ||
|  | a60426c884 | ||
|  | 2998735bf3 | ||
|  | 3a96135df8 | ||
|  | bf8b3aeac7 | ||
|  | 5a52b5929c | ||
|  | 0adb469a85 | ||
|  | 21004341bf | ||
|  | cc26f2b8b1 | ||
|  | 1a6652fe6b | ||
|  | d28f0670d6 | ||
|  | 7bdd619e1b | ||
|  | c62d1d5eb8 | ||
|  | d60d85de5c | ||
|  | 44f281f8d9 | ||
|  | f698feac9c | ||
|  | f55701b374 | ||
|  | 82e8298b73 | ||
|  | fc54503232 | ||
|  | 6f0594eebe | ||
|  | 6b52f859cf | ||
|  | a3d4b8f79b | ||
|  | 7c7df6b35d | ||
|  | e80eab397a | ||
|  | 6cda6ef92e | ||
|  | f9f61d29de | ||
|  | 1ae825032c | ||
|  | f146b52418 | ||
|  | 78a79ca9e1 | ||
|  | 329bc2f265 | ||
|  | 8738ed7757 | ||
|  | 29e8cdbfe9 | ||
|  | 47f356fc5f | ||
|  | 21ffc73c4f | ||
|  | 81a9342b83 | 
| @@ -1,24 +0,0 @@ | ||||
| --- | ||||
| name: Bug report | ||||
| about: For reporting bugs in go-micro | ||||
| title: "[BUG]" | ||||
| labels: '' | ||||
| assignees: '' | ||||
|  | ||||
| --- | ||||
|  | ||||
| **Describe the bug** | ||||
|  | ||||
| 1. What are you trying to do? | ||||
| 2. What did you expect to happen? | ||||
| 3. What happens instead? | ||||
|  | ||||
| **How to reproduce the bug:** | ||||
|  | ||||
| If possible, please include a minimal code snippet here. | ||||
|  | ||||
| **Environment:** | ||||
| Go Version: please paste `go version` output here | ||||
| ``` | ||||
| please paste `go env` output here | ||||
| ``` | ||||
| @@ -1,17 +0,0 @@ | ||||
| --- | ||||
| name: Feature request / Enhancement | ||||
| about: If you have a need not served by go-micro | ||||
| title: "[FEATURE]" | ||||
| labels: '' | ||||
| assignees: '' | ||||
|  | ||||
| --- | ||||
|  | ||||
| **Is your feature request related to a problem? Please describe.** | ||||
| A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] | ||||
|  | ||||
| **Describe the solution you'd like** | ||||
| A clear and concise description of what you want to happen. | ||||
|  | ||||
| **Additional context** | ||||
| Add any other context or screenshots about the feature request here. | ||||
| @@ -1,14 +0,0 @@ | ||||
| --- | ||||
| name: Question | ||||
| about: Ask a question about go-micro | ||||
| title: '' | ||||
| labels: '' | ||||
| assignees: '' | ||||
|  | ||||
| --- | ||||
|  | ||||
| Before asking, please check if your question has already been answered:  | ||||
|  | ||||
| 1. Check the documentation - https://micro.mu/docs/ | ||||
| 2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins | ||||
| 3. Search existing issues | ||||
| @@ -1,9 +0,0 @@ | ||||
| ## Pull Request template | ||||
| Please, go through these steps before clicking submit on this PR. | ||||
|  | ||||
| 1. Give a descriptive title to your PR. | ||||
| 2. Provide a description of your changes. | ||||
| 3. Make sure you have some relevant tests. | ||||
| 4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable). | ||||
|  | ||||
| **PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING** | ||||
| @@ -1,26 +0,0 @@ | ||||
| # To get started with Dependabot version updates, you'll need to specify which | ||||
| # package ecosystems to update and where the package manifests are located. | ||||
| # Please see the documentation for all configuration options: | ||||
| # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||||
|  | ||||
| version: 2 | ||||
| updates: | ||||
|  | ||||
|   # Maintain dependencies for GitHub Actions | ||||
|   - package-ecosystem: "github-actions" | ||||
|     directory: "/" | ||||
|     schedule: | ||||
|       interval: "daily" | ||||
|     commit-message: | ||||
|       prefix: "chore" | ||||
|       include: "scope" | ||||
|  | ||||
|   # Maintain dependencies for Golang | ||||
|   - package-ecosystem: "gomod" | ||||
|     directory: "/" | ||||
|     schedule: | ||||
|       interval: "daily" | ||||
|     commit-message: | ||||
|       prefix: "chore" | ||||
|       include: "scope" | ||||
|  | ||||
| @@ -1,24 +0,0 @@ | ||||
| name: "autoapprove" | ||||
|  | ||||
| on: | ||||
|   pull_request_target: | ||||
|     types: [assigned, opened, synchronize, reopened] | ||||
|   workflow_run: | ||||
|     workflows: ["prbuild"] | ||||
|     types: | ||||
|       - completed | ||||
|  | ||||
| permissions: | ||||
|   pull-requests: write | ||||
|   contents: write | ||||
|  | ||||
| jobs: | ||||
|   autoapprove: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - name: approve | ||||
|         uses: hmarr/auto-approve-action@v3 | ||||
|         if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]' | ||||
|         id: approve | ||||
|         with: | ||||
|           github-token: ${{ secrets.GITHUB_TOKEN }} | ||||
| @@ -1,21 +0,0 @@ | ||||
| name: "automerge" | ||||
|  | ||||
| on: | ||||
|   pull_request_target: | ||||
|     types: [assigned, opened, synchronize, reopened] | ||||
|  | ||||
| permissions: | ||||
|   pull-requests: write | ||||
|   contents: write | ||||
|  | ||||
| jobs: | ||||
|   automerge: | ||||
|     runs-on: ubuntu-latest | ||||
|     if: github.actor == 'vtolstov' | ||||
|     steps: | ||||
|       - name: merge | ||||
|         id: merge | ||||
|         run: gh pr merge --auto --merge "$PR_URL" | ||||
|         env: | ||||
|           PR_URL: ${{github.event.pull_request.html_url}} | ||||
|           GITHUB_TOKEN: ${{secrets.TOKEN}} | ||||
| @@ -1,47 +0,0 @@ | ||||
| name: build | ||||
| on: | ||||
|  push: | ||||
|     branches: | ||||
|     - master | ||||
|     - v3 | ||||
| jobs: | ||||
|   test: | ||||
|     name: test | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|     - name: setup | ||||
|       uses: actions/setup-go@v3 | ||||
|       with: | ||||
|         go-version: 1.17 | ||||
|     - name: checkout | ||||
|       uses: actions/checkout@v3 | ||||
|     - name: cache | ||||
|       uses: actions/cache@v3 | ||||
|       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@v3 | ||||
|       - name: lint | ||||
|         uses: golangci/golangci-lint-action@v3.4.0 | ||||
|         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 | ||||
| @@ -1,39 +0,0 @@ | ||||
| name: "codecov" | ||||
|  | ||||
| on: | ||||
|   workflow_run: | ||||
|     workflows: ["build"] | ||||
|     types: | ||||
|       - completed | ||||
|   push: | ||||
|     branches: [ v3 ] | ||||
|   pull_request: | ||||
|     branches: [ v3 ] | ||||
|   schedule: | ||||
|     - cron: '34 1 * * 0' | ||||
|  | ||||
| jobs: | ||||
|   codecov: | ||||
|     name: codecov | ||||
|     runs-on: ubuntu-latest | ||||
|     permissions: | ||||
|       actions: read | ||||
|       contents: read | ||||
|       security-events: write | ||||
|  | ||||
|     strategy: | ||||
|       fail-fast: false | ||||
|       matrix: | ||||
|         language: [ 'go' ] | ||||
|  | ||||
|     steps: | ||||
|     - name: checkout | ||||
|       uses: actions/checkout@v3 | ||||
|     - name: setup | ||||
|       uses: actions/setup-go@v3 | ||||
|       with: | ||||
|         go-version: 1.17 | ||||
|     - name: Run coverage | ||||
|       run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./... | ||||
|     - name: codecov | ||||
|       uses: codecov/codecov-action@v3.1.1 | ||||
| @@ -1,78 +0,0 @@ | ||||
| # For most projects, this workflow file will not need changing; you simply need | ||||
| # to commit it to your repository. | ||||
| # | ||||
| # You may wish to alter this file to override the set of languages analyzed, | ||||
| # or to provide custom queries or build logic. | ||||
| # | ||||
| # ******** NOTE ******** | ||||
| # We have attempted to detect the languages in your repository. Please check | ||||
| # the `language` matrix defined below to confirm you have the correct set of | ||||
| # supported CodeQL languages. | ||||
| # | ||||
| name: "codeql" | ||||
|  | ||||
| on: | ||||
|   workflow_run: | ||||
|     workflows: ["prbuild"] | ||||
|     types: | ||||
|       - completed | ||||
|   push: | ||||
|     branches: [ master, v3 ] | ||||
|   pull_request: | ||||
|     # The branches below must be a subset of the branches above | ||||
|     branches: [ master, v3 ] | ||||
|   schedule: | ||||
|     - cron: '34 1 * * 0' | ||||
|  | ||||
| jobs: | ||||
|   analyze: | ||||
|     name: analyze | ||||
|     runs-on: ubuntu-latest | ||||
|     permissions: | ||||
|       actions: read | ||||
|       contents: read | ||||
|       security-events: write | ||||
|  | ||||
|     strategy: | ||||
|       fail-fast: false | ||||
|       matrix: | ||||
|         language: [ 'go' ] | ||||
|         # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] | ||||
|         # Learn more: | ||||
|         # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed | ||||
|  | ||||
|     steps: | ||||
|     - name: checkout | ||||
|       uses: actions/checkout@v3 | ||||
|     - name: setup | ||||
|       uses: actions/setup-go@v3 | ||||
|       with: | ||||
|         go-version: 1.17 | ||||
|     # Initializes the CodeQL tools for scanning. | ||||
|     - name: init | ||||
|       uses: github/codeql-action/init@v2 | ||||
|       with: | ||||
|         languages: ${{ matrix.language }} | ||||
|         # If you wish to specify custom queries, you can do so here or in a config file. | ||||
|         # By default, queries listed here will override any specified in a config file. | ||||
|         # Prefix the list here with "+" to use these queries and those in the config file. | ||||
|         # queries: ./path/to/local/query, your-org/your-repo/queries@main | ||||
|  | ||||
|     # Autobuild attempts to build any compiled languages  (C/C++, C#, or Java). | ||||
|     # If this step fails, then you should remove it and run the build manually (see below) | ||||
|     - name: autobuild | ||||
|       uses: github/codeql-action/autobuild@v2 | ||||
|  | ||||
|     # ℹ️ Command-line programs to run using the OS shell. | ||||
|     # 📚 https://git.io/JvXDl | ||||
|  | ||||
|     # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines | ||||
|     #    and modify them (or add more) to build your code if your project | ||||
|     #    uses a compiled language | ||||
|  | ||||
|     #- run: | | ||||
|     #   make bootstrap | ||||
|     #   make release | ||||
|  | ||||
|     - name: analyze | ||||
|       uses: github/codeql-action/analyze@v2 | ||||
| @@ -1,27 +0,0 @@ | ||||
| name: "dependabot-automerge" | ||||
|  | ||||
| on: | ||||
|   pull_request_target: | ||||
|     types: [assigned, opened, synchronize, reopened] | ||||
|  | ||||
| permissions: | ||||
|   pull-requests: write | ||||
|   contents: write | ||||
|  | ||||
| jobs: | ||||
|   automerge: | ||||
|     runs-on: ubuntu-latest | ||||
|     if: github.actor == 'dependabot[bot]' | ||||
|     steps: | ||||
|       - name: metadata | ||||
|         id: metadata | ||||
|         uses: dependabot/fetch-metadata@v1.3.6 | ||||
|         with: | ||||
|           github-token: "${{ secrets.TOKEN }}" | ||||
|       - name: merge | ||||
|         id: merge | ||||
|         if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}} | ||||
|         run: gh pr merge --auto --merge "$PR_URL" | ||||
|         env: | ||||
|           PR_URL: ${{github.event.pull_request.html_url}} | ||||
|           GITHUB_TOKEN: ${{secrets.TOKEN}} | ||||
| @@ -1,40 +0,0 @@ | ||||
| name: prbuild | ||||
| on: | ||||
|   pull_request: | ||||
|     branches: | ||||
|     - master | ||||
|     - v3 | ||||
| jobs: | ||||
|   test: | ||||
|     name: test | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|     - name: setup | ||||
|       uses: actions/setup-go@v3 | ||||
|       with: | ||||
|         go-version: 1.17 | ||||
|     - name: checkout | ||||
|       uses: actions/checkout@v3 | ||||
|     - name: cache | ||||
|       uses: actions/cache@v3 | ||||
|       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@v3 | ||||
|       - name: lint | ||||
|         uses: golangci/golangci-lint-action@v3.4.0 | ||||
|         continue-on-error: true | ||||
|         with: | ||||
|           version: v1.30 | ||||
							
								
								
									
										3
									
								
								.github/FUNDING.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.github/FUNDING.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| # These are supported funding model platforms | ||||
|  | ||||
| github: asim | ||||
							
								
								
									
										8
									
								
								.github/ISSUE_TEMPLATE/bug_report.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								.github/ISSUE_TEMPLATE/bug_report.md
									
									
									
									
										vendored
									
									
								
							| @@ -1,6 +1,6 @@ | ||||
| --- | ||||
| name: Bug report | ||||
| about: For reporting bugs in micro | ||||
| about: For reporting bugs in go-micro | ||||
| title: "[BUG]" | ||||
| labels: '' | ||||
| assignees: '' | ||||
| @@ -16,3 +16,9 @@ assignees: '' | ||||
| **How to reproduce the bug:** | ||||
|  | ||||
| If possible, please include a minimal code snippet here. | ||||
|  | ||||
| **Environment:** | ||||
| Go Version: please paste `go version` output here | ||||
| ``` | ||||
| please paste `go env` output here | ||||
| ``` | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| --- | ||||
| name: Feature request / Enhancement | ||||
| about: If you have a need not served by micro | ||||
| about: If you have a need not served by go-micro | ||||
| title: "[FEATURE]" | ||||
| labels: '' | ||||
| assignees: '' | ||||
|   | ||||
							
								
								
									
										8
									
								
								.github/ISSUE_TEMPLATE/question.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								.github/ISSUE_TEMPLATE/question.md
									
									
									
									
										vendored
									
									
								
							| @@ -1,8 +1,14 @@ | ||||
| --- | ||||
| name: Question | ||||
| about: Ask a question about micro | ||||
| about: Ask a question about go-micro | ||||
| title: '' | ||||
| labels: '' | ||||
| assignees: '' | ||||
|  | ||||
| --- | ||||
|  | ||||
| Before asking, please check if your question has already been answered:  | ||||
|  | ||||
| 1. Check the documentation - https://micro.mu/docs/ | ||||
| 2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins | ||||
| 3. Search existing issues | ||||
|   | ||||
							
								
								
									
										28
									
								
								.github/autoapprove.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										28
									
								
								.github/autoapprove.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,28 +0,0 @@ | ||||
| name: "autoapprove" | ||||
|  | ||||
| on: | ||||
|   pull_request_target: | ||||
|     types: [assigned, opened, synchronize, reopened] | ||||
|   workflow_run: | ||||
|     workflows: ["prbuild"] | ||||
|     types: | ||||
|       - completed | ||||
|  | ||||
| permissions: | ||||
|   pull-requests: write | ||||
|   contents: write | ||||
|  | ||||
| jobs: | ||||
|   autoapprove: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - name: approve | ||||
|         run: [ "curl -o tea https://dl.gitea.com/tea/main/tea-main-linux-amd64", | ||||
|                "chmod +x ./tea", | ||||
|                "./tea login add --name unistack --token ${{ secrets.GITHUB_TOKEN }} --url https://git.unistack.org", | ||||
|                "./tea pr --repo ${{ github.event.repository.name }}" | ||||
|         ] | ||||
|         if: github.actor == 'vtolstov' | ||||
|         id: approve | ||||
|         with: | ||||
|           github-token: ${{ secrets.GITHUB_TOKEN }} | ||||
							
								
								
									
										15
									
								
								.github/generate.sh
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
							
						
						
									
										15
									
								
								.github/generate.sh
									
									
									
									
										vendored
									
									
										Executable file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| #!/bin/bash -e | ||||
|  | ||||
| find . -type f -name '*.pb.*.go' -o -name '*.pb.go' -a ! -name 'message.pb.go' -delete | ||||
| PROTOS=$(find . -type f -name '*.proto' | grep -v proto/google/api) | ||||
|  | ||||
| mkdir -p proto/google/api | ||||
| curl -s -o proto/google/api/annotations.proto -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto | ||||
| curl -s -o proto/google/api/http.proto -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto | ||||
|  | ||||
| for PROTO in $PROTOS; do | ||||
|   echo $PROTO | ||||
|   protoc -I./proto -I. -I$(dirname $PROTO) --go_out=plugins=grpc,paths=source_relative:. --micro_out=paths=source_relative:. $PROTO | ||||
| done | ||||
|  | ||||
| rm -r proto | ||||
							
								
								
									
										22
									
								
								.github/workflows/docker.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								.github/workflows/docker.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| name: Docker | ||||
|  | ||||
| on: | ||||
|   push: | ||||
|     branches: | ||||
|     - master | ||||
|     tags: | ||||
|     - v2.* | ||||
|     - v3.* | ||||
| jobs: | ||||
|   build: | ||||
|      runs-on: ubuntu-latest | ||||
|      steps: | ||||
|        - uses: actions/checkout@v2 | ||||
|          name: Check out repository | ||||
|        - uses: elgohr/Publish-Docker-Github-Action@2.12 | ||||
|          name: Build and Push Docker Image | ||||
|          with: | ||||
|            name: micro/go-micro | ||||
|            username: ${{ secrets.DOCKER_USERNAME }} | ||||
|            password: ${{ secrets.DOCKER_PASSWORD }} | ||||
|            tag_names: true | ||||
							
								
								
									
										53
									
								
								.github/workflows/job_coverage.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										53
									
								
								.github/workflows/job_coverage.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,53 +0,0 @@ | ||||
| name: coverage | ||||
|  | ||||
| on: | ||||
|   push: | ||||
|     branches: [ main, v3, v4 ] | ||||
|     paths-ignore: | ||||
|       - '.github/**' | ||||
|       - '.gitea/**' | ||||
|   pull_request: | ||||
|     branches: [ main, v3, v4 ] | ||||
|  | ||||
| jobs: | ||||
|  | ||||
|   build: | ||||
|     if: github.server_url != 'https://github.com' | ||||
|     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: test coverage | ||||
|       run: | | ||||
|         go test -v -cover ./... -covermode=count -coverprofile coverage.out -coverpkg ./... | ||||
|         go tool cover -func coverage.out -o coverage.out | ||||
|  | ||||
|     - name: coverage badge | ||||
|       uses: tj-actions/coverage-badge-go@v2 | ||||
|       with: | ||||
|         green: 80 | ||||
|         filename: coverage.out | ||||
|  | ||||
|     - uses: stefanzweifel/git-auto-commit-action@v4 | ||||
|       name: autocommit | ||||
|       with: | ||||
|         commit_message: Apply Code Coverage Badge | ||||
|         skip_fetch: false | ||||
|         skip_checkout: false | ||||
|         file_pattern: ./README.md | ||||
|  | ||||
|     - name: push | ||||
|       if: steps.auto-commit-action.outputs.changes_detected == 'true' | ||||
|       uses: ad-m/github-push-action@master | ||||
|       with: | ||||
|         github_token: ${{ github.token }} | ||||
|         branch: ${{ github.ref }} | ||||
							
								
								
									
										29
									
								
								.github/workflows/job_lint.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										29
									
								
								.github/workflows/job_lint.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,29 +0,0 @@ | ||||
| name: lint | ||||
|  | ||||
| on: | ||||
|   pull_request: | ||||
|     types: [opened, reopened, synchronize] | ||||
|     branches: [ master, v3, v4 ] | ||||
|     paths-ignore: | ||||
|       - '.github/**' | ||||
|       - '.gitea/**' | ||||
|  | ||||
| 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: golangci/golangci-lint-action@v6 | ||||
|       with: | ||||
|         version: 'latest' | ||||
							
								
								
									
										94
									
								
								.github/workflows/job_sync.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										94
									
								
								.github/workflows/job_sync.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,94 +0,0 @@ | ||||
| name: sync | ||||
|  | ||||
| on: | ||||
|   schedule: | ||||
|     - cron: '*/5 * * * *' | ||||
|   # Allows you to run this workflow manually from the Actions tab | ||||
|   workflow_dispatch: | ||||
|  | ||||
| jobs: | ||||
|   sync: | ||||
|     if: github.server_url != 'https://github.com' | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|     - name: init | ||||
|       run: | | ||||
|         git config --global user.email "vtolstov <vtolstov@users.noreply.github.com>" | ||||
|         git config --global user.name "github-actions[bot]" | ||||
|         echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc | ||||
|         echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc | ||||
|  | ||||
|     - name: check master | ||||
|       id: check_master | ||||
|       run: | | ||||
|         src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/master | cut -f1) | ||||
|         dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/master | cut -f1) | ||||
|         echo "src_hash=$src_hash" | ||||
|         echo "dst_hash=$dst_hash" | ||||
|         if [ "$src_hash" != "$dst_hash" ]; then | ||||
|           echo "sync_needed=true" >> $GITHUB_OUTPUT | ||||
|         else | ||||
|           echo "sync_needed=false" >> $GITHUB_OUTPUT | ||||
|         fi | ||||
|  | ||||
|     - name: sync master | ||||
|       if: steps.check_master.outputs.sync_needed == 'true' | ||||
|       run: | | ||||
|         git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo | ||||
|         cd repo | ||||
|         git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY} | ||||
|         git pull --rebase upstream master | ||||
|         git push upstream master --progress | ||||
|         git push origin master --progress | ||||
|         cd ../ | ||||
|         rm -rf repo | ||||
|  | ||||
|     - name: check v3 | ||||
|       id: check_v3 | ||||
|       run: | | ||||
|         src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1) | ||||
|         dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1) | ||||
|         echo "src_hash=$src_hash" | ||||
|         echo "dst_hash=$dst_hash" | ||||
|         if [ "$src_hash" != "$dst_hash" ]; then | ||||
|           echo "sync_needed=true" >> $GITHUB_OUTPUT | ||||
|         else | ||||
|           echo "sync_needed=false" >> $GITHUB_OUTPUT | ||||
|         fi | ||||
|  | ||||
|     - name: sync v3 | ||||
|       if: steps.check_v3.outputs.sync_needed == 'true' | ||||
|       run: | | ||||
|         git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo | ||||
|         cd repo | ||||
|         git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY} | ||||
|         git pull --rebase upstream v3 | ||||
|         git push upstream v3 --progress | ||||
|         git push origin v3 --progress | ||||
|         cd ../ | ||||
|         rm -rf repo | ||||
|  | ||||
|     - name: check v4 | ||||
|       id: check_v4 | ||||
|       run: | | ||||
|         src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1) | ||||
|         dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1) | ||||
|         echo "src_hash=$src_hash" | ||||
|         echo "dst_hash=$dst_hash" | ||||
|         if [ "$src_hash" != "$dst_hash" ]; then | ||||
|           echo "sync_needed=true" >> $GITHUB_OUTPUT | ||||
|         else | ||||
|           echo "sync_needed=false" >> $GITHUB_OUTPUT | ||||
|         fi | ||||
|  | ||||
|     - name: sync v4 | ||||
|       if: steps.check_v4.outputs.sync_needed == 'true' | ||||
|       run: | | ||||
|         git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo | ||||
|         cd repo | ||||
|         git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY} | ||||
|         git pull --rebase upstream v4 | ||||
|         git push upstream v4 --progress | ||||
|         git push origin v4 --progress | ||||
|         cd ../ | ||||
|         rm -rf repo | ||||
							
								
								
									
										31
									
								
								.github/workflows/job_test.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										31
									
								
								.github/workflows/job_test.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,31 +0,0 @@ | ||||
| name: test | ||||
|  | ||||
| on: | ||||
|   pull_request: | ||||
|     types: [opened, reopened, synchronize] | ||||
|     branches: [ master, v3, v4 ] | ||||
|   push: | ||||
|     branches: [ master, v3, v4 ] | ||||
|     paths-ignore: | ||||
|       - '.github/**' | ||||
|       - '.gitea/**' | ||||
|  | ||||
| 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 ./... | ||||
							
								
								
									
										50
									
								
								.github/workflows/job_tests.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										50
									
								
								.github/workflows/job_tests.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,50 +0,0 @@ | ||||
| name: test | ||||
|  | ||||
| on: | ||||
|   pull_request: | ||||
|     types: [opened, reopened, synchronize] | ||||
|     branches: [ master, v3, v4 ] | ||||
|   push: | ||||
|     branches: [ master, v3, v4 ] | ||||
|     paths-ignore: | ||||
|       - '.github/**' | ||||
|       - '.gitea/**' | ||||
|  | ||||
| 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: ${{ github.workspace }}/go.work | ||||
|       run: | | ||||
|         go work init | ||||
|         go work use . | ||||
|         go work use micro-tests | ||||
|     - name: setup deps | ||||
|       env: | ||||
|         GOWORK: ${{ github.workspace }}/go.work | ||||
|       run: go get -v ./... | ||||
|     - name: run tests | ||||
|       env: | ||||
|         INTEGRATION_TESTS: yes | ||||
|         GOWORK: ${{ github.workspace }}/go.work | ||||
|       run: | | ||||
|         cd micro-tests | ||||
|         go test -mod readonly -v ./... || true | ||||
							
								
								
									
										35
									
								
								.github/workflows/pr.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								.github/workflows/pr.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| name: PR Sanity Check | ||||
| on: pull_request | ||||
|  | ||||
| jobs: | ||||
|  | ||||
|   prtest: | ||||
|     name: PR sanity check | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|  | ||||
|     - name: Set up Go 1.13 | ||||
|       uses: actions/setup-go@v1 | ||||
|       with: | ||||
|         go-version: 1.13 | ||||
|       id: go | ||||
|  | ||||
|     - name: Check out code into the Go module directory | ||||
|       uses: actions/checkout@v2 | ||||
|  | ||||
|     - name: Get dependencies | ||||
|       run: | | ||||
|         go get -v -t -d ./... | ||||
|  | ||||
|     - name: Run tests | ||||
|       id: tests | ||||
|       env: | ||||
|         IN_TRAVIS_CI: yes | ||||
|         S3_BLOB_STORE_REGION: ${{ secrets.SCALEWAY_REGION }} | ||||
|         S3_BLOB_STORE_ENDPOINT: ${{ secrets.SCALEWAY_ENDPOINT }} | ||||
|         S3_BLOB_STORE_ACCESS_KEY: ${{ secrets.SCALEWAY_ACCESS_KEY }} | ||||
|         S3_BLOB_STORE_SECRET_KEY: ${{ secrets.SCALEWAY_SECRET_KEY }} | ||||
|       run: | | ||||
|         wget -qO- https://binaries.cockroachdb.com/cockroach-v20.1.4.linux-amd64.tgz | tar  xvz | ||||
|         cockroach-v20.1.4.linux-amd64/cockroach start-single-node --insecure & | ||||
|         go test -v ./... | ||||
							
								
								
									
										41
									
								
								.github/workflows/scripts/build-all-examples.sh
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
							
						
						
									
										41
									
								
								.github/workflows/scripts/build-all-examples.sh
									
									
									
									
										vendored
									
									
										Executable file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| #!/bin/bash | ||||
| # set -x | ||||
|  | ||||
| function build_binary { | ||||
|     echo building $1 | ||||
|     pushd $1 | ||||
|     go build -o _main | ||||
|     local ret=$? | ||||
|     if [ $ret -gt 0 ]; then  | ||||
|         failed=1 | ||||
|         failed_arr+=($1) | ||||
|     fi | ||||
|     popd | ||||
| } | ||||
|  | ||||
| function is_main { | ||||
|     grep "package main" -l -dskip $1/*.go > /dev/null 2>&1 | ||||
| } | ||||
|  | ||||
|  | ||||
| function check_dir { | ||||
|     is_main $1 | ||||
|     local ret=$? | ||||
|     if [ $ret == 0 ]; then | ||||
|         build_binary $1 $2 | ||||
|     fi | ||||
|     for filename in $1/*; do | ||||
|         if [ -d $filename ]; then | ||||
|             check_dir $filename $2 | ||||
|         fi | ||||
|     done | ||||
| } | ||||
| failed_arr=() | ||||
| failed=0 | ||||
| go mod edit -replace github.com/micro/go-micro/v2=github.com/$2/v2@$1  | ||||
| check_dir . $1 | ||||
| if [ $failed -gt 0 ]; then | ||||
|     echo Some builds failed | ||||
|     printf '%s\n' "${failed_arr[@]}" | ||||
| fi | ||||
| exit $failed | ||||
							
								
								
									
										19
									
								
								.github/workflows/scripts/build-micro.sh
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
							
						
						
									
										19
									
								
								.github/workflows/scripts/build-micro.sh
									
									
									
									
										vendored
									
									
										Executable file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| #!/bin/bash | ||||
| # set -x | ||||
|  | ||||
| failed=0 | ||||
| go mod edit -replace github.com/micro/go-micro/v2=github.com/$2/v2@$1  | ||||
| # basic test, build the binary | ||||
| go install | ||||
| failed=$? | ||||
| if [ $failed -gt 0 ]; then | ||||
|     exit $failed | ||||
| fi | ||||
| # unit tests | ||||
| IN_TRAVIS_CI=yes go test -v ./... | ||||
|  | ||||
| ./scripts/test-docker.sh | ||||
| # Generate keys for JWT tests | ||||
| ssh-keygen -f /tmp/sshkey -m pkcs8 -q -N "" | ||||
| ssh-keygen -f /tmp/sshkey -e  -m pkcs8 > /tmp/sshkey.pub | ||||
| go clean -testcache && IN_TRAVIS_CI=yes go test --tags=integration -v ./test | ||||
							
								
								
									
										48
									
								
								.github/workflows/tests.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								.github/workflows/tests.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | ||||
| name: Run tests | ||||
| on: [push] | ||||
|  | ||||
| jobs: | ||||
|  | ||||
|   test: | ||||
|     name: Test repo | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|     - name: Set up Go 1.13 | ||||
|       uses: actions/setup-go@v1 | ||||
|       with: | ||||
|         go-version: 1.13 | ||||
|       id: go | ||||
|  | ||||
|     - name: Setup Kind | ||||
|       uses: engineerd/setup-kind@v0.4.0 | ||||
|       with: | ||||
|         version: v0.8.1 | ||||
|  | ||||
|     - name: Check out code into the Go module directory | ||||
|       uses: actions/checkout@v2 | ||||
|  | ||||
|     - name: Get dependencies | ||||
|       run: | | ||||
|         go get -v -t -d ./... | ||||
|  | ||||
|     - name: Run tests | ||||
|       id: tests | ||||
|       env: | ||||
|         IN_TRAVIS_CI: yes | ||||
|         S3_BLOB_STORE_REGION: ${{ secrets.SCALEWAY_REGION }} | ||||
|         S3_BLOB_STORE_ENDPOINT: ${{ secrets.SCALEWAY_ENDPOINT }} | ||||
|         S3_BLOB_STORE_ACCESS_KEY: ${{ secrets.SCALEWAY_ACCESS_KEY }} | ||||
|         S3_BLOB_STORE_SECRET_KEY: ${{ secrets.SCALEWAY_SECRET_KEY }} | ||||
|       run: | | ||||
|         kubectl apply -f runtime/kubernetes/test/test.yaml | ||||
|         sudo mkdir -p /var/run/secrets/kubernetes.io/serviceaccount | ||||
|         sudo chmod 777 /var/run/secrets/kubernetes.io/serviceaccount | ||||
|         wget -qO- https://binaries.cockroachdb.com/cockroach-v20.1.4.linux-amd64.tgz | tar -xvz | ||||
|         cockroach-v20.1.4.linux-amd64/cockroach start-single-node --insecure & | ||||
|         wget -q https://github.com/nats-io/nats-streaming-server/releases/download/v0.18.0/nats-streaming-server-v0.18.0-linux-amd64.zip | ||||
|         unzip ./nats-streaming-server-v0.18.0-linux-amd64.zip | ||||
|         export PATH=$PATH:./nats-streaming-server-v0.18.0-linux-amd64 | ||||
|         nats-streaming-server & | ||||
|         go test -tags kubernetes,nats -v ./... | ||||
|    | ||||
|  | ||||
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,8 +1,6 @@ | ||||
| # Develop tools | ||||
| /.vscode/ | ||||
| /.idea/ | ||||
| .idea | ||||
| .vscode | ||||
|  | ||||
| # Binaries for programs and plugins | ||||
| *.exe | ||||
| @@ -15,7 +13,6 @@ | ||||
| _obj | ||||
| _test | ||||
| _build | ||||
| .DS_Store | ||||
|  | ||||
| # Architecture specific extensions/prefixes | ||||
| *.[568vq] | ||||
|   | ||||
| @@ -1,5 +1,26 @@ | ||||
| run: | ||||
|   concurrency: 8 | ||||
|   timeout: 5m | ||||
|   issues-exit-code: 1 | ||||
|   tests: true | ||||
|   deadline: 10m | ||||
| linters: | ||||
|   disable-all: false | ||||
|   enable-all: false | ||||
|   enable: | ||||
|     - megacheck | ||||
|     - staticcheck | ||||
|     - deadcode | ||||
|     - varcheck | ||||
|     - gosimple | ||||
|     - unused | ||||
|     - prealloc | ||||
|     - scopelint | ||||
|     - gocritic | ||||
|     - goimports | ||||
|     - unconvert | ||||
|     - govet | ||||
|     - nakedret | ||||
|     - structcheck | ||||
|     - gosec | ||||
|   disable: | ||||
|     - maligned | ||||
|     - interfacer | ||||
|     - typecheck | ||||
|     - dupl | ||||
|   | ||||
							
								
								
									
										13
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| FROM golang:1.13-alpine | ||||
|  | ||||
| RUN mkdir /user && \ | ||||
|     echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \ | ||||
|     echo 'nobody:x:65534:' > /user/group | ||||
|  | ||||
| ENV GO111MODULE=on | ||||
| RUN apk --no-cache add make git gcc libtool musl-dev ca-certificates dumb-init && \ | ||||
|     rm -rf /var/cache/apk/* /tmp/* | ||||
|  | ||||
| WORKDIR / | ||||
| COPY ./go.mod ./go.sum ./ | ||||
| RUN go mod download && rm go.mod go.sum | ||||
							
								
								
									
										238
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										238
									
								
								LICENSE
									
									
									
									
									
								
							| @@ -1,192 +1,104 @@ | ||||
| # PolyForm Strict License 1.0.0 | ||||
|  | ||||
|                                  Apache License | ||||
|                            Version 2.0, January 2004 | ||||
|                         http://www.apache.org/licenses/ | ||||
| <https://polyformproject.org/licenses/strict/1.0.0> | ||||
|  | ||||
|    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||||
| ## Acceptance | ||||
|  | ||||
|    1. Definitions. | ||||
| In order to get any license under these terms, you must agree | ||||
| to them as both strict obligations and conditions to all | ||||
| your licenses. | ||||
|  | ||||
|       "License" shall mean the terms and conditions for use, reproduction, | ||||
|       and distribution as defined by Sections 1 through 9 of this document. | ||||
| ## Copyright License | ||||
|  | ||||
|       "Licensor" shall mean the copyright owner or entity authorized by | ||||
|       the copyright owner that is granting the License. | ||||
| The licensor grants you a copyright license for the software | ||||
| to do everything you might do with the software that would | ||||
| otherwise infringe the licensor's copyright in it for any | ||||
| permitted purpose, other than distributing the software or | ||||
| making changes or new works based on the software. | ||||
|  | ||||
|       "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. | ||||
| ## Patent License | ||||
|  | ||||
|       "You" (or "Your") shall mean an individual or Legal Entity | ||||
|       exercising permissions granted by this License. | ||||
| The licensor grants you a patent license for the software that | ||||
| covers patent claims the licensor can license, or becomes able | ||||
| to license, that you would infringe by using the software. | ||||
|  | ||||
|       "Source" form shall mean the preferred form for making modifications, | ||||
|       including but not limited to software source code, documentation | ||||
|       source, and configuration files. | ||||
| ## Noncommercial Purposes | ||||
|  | ||||
|       "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. | ||||
| Any noncommercial purpose is a permitted purpose. | ||||
|  | ||||
|       "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). | ||||
| ## Personal Uses | ||||
|  | ||||
|       "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. | ||||
| Personal use for research, experiment, and testing for | ||||
| the benefit of public knowledge, personal study, private | ||||
| entertainment, hobby projects, amateur pursuits, or religious | ||||
| observance, without any anticipated commercial application, | ||||
| is use for a permitted purpose. | ||||
|  | ||||
|       "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." | ||||
| ## Noncommercial Organizations | ||||
|  | ||||
|       "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. | ||||
| Use by any charitable organization, educational institution, | ||||
| public research organization, public safety or health | ||||
| organization, environmental protection organization, | ||||
| or government institution is use for a permitted purpose | ||||
| regardless of the source of funding or obligations resulting | ||||
| from the funding. | ||||
|  | ||||
|    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. | ||||
| ## Fair Use | ||||
|  | ||||
|    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. | ||||
| You may have "fair use" rights for the software under the | ||||
| law. These terms do not limit them. | ||||
|  | ||||
|    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: | ||||
| ## No Other Rights | ||||
|  | ||||
|       (a) You must give any other recipients of the Work or | ||||
|           Derivative Works a copy of this License; and | ||||
| These terms do not allow you to sublicense or transfer any of | ||||
| your licenses to anyone else, or prevent the licensor from | ||||
| granting licenses to anyone else.  These terms do not imply | ||||
| any other licenses. | ||||
|  | ||||
|       (b) You must cause any modified files to carry prominent notices | ||||
|           stating that You changed the files; and | ||||
| ## Patent Defense | ||||
|  | ||||
|       (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 | ||||
| If you make any written claim that the software infringes or | ||||
| contributes to infringement of any patent, your patent license | ||||
| for the software granted under these terms ends immediately. If | ||||
| your company makes such a claim, your patent license ends | ||||
| immediately for work on behalf of your company. | ||||
|  | ||||
|       (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. | ||||
| ## Violations | ||||
|  | ||||
|       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. | ||||
| The first time you are notified in writing that you have | ||||
| violated any of these terms, or done anything with the software | ||||
| not covered by your licenses, your licenses can nonetheless | ||||
| continue if you come into full compliance with these terms, | ||||
| and take practical steps to correct past violations, within | ||||
| 32 days of receiving notice.  Otherwise, all your licenses | ||||
| end immediately. | ||||
|  | ||||
|    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. | ||||
| ## No Liability | ||||
|  | ||||
|    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. | ||||
| ***As far as the law allows, the software comes as is, without | ||||
| any warranty or condition, and the licensor will not be liable | ||||
| to you for any damages arising out of these terms or the use | ||||
| or nature of the software, under any kind of legal claim.*** | ||||
|  | ||||
|    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. | ||||
| ## Definitions | ||||
|  | ||||
|    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. | ||||
| The **licensor** is the individual or entity offering these | ||||
| terms, and the **software** is the software the licensor makes | ||||
| available under these terms. | ||||
|  | ||||
|    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. | ||||
| **You** refers to the individual or entity agreeing to these | ||||
| terms. | ||||
|  | ||||
|    END OF TERMS AND CONDITIONS | ||||
| **Your company** is any legal entity, sole proprietorship, | ||||
| or other kind of organization that you work for, plus all | ||||
| organizations that have control over, are under the control of, | ||||
| or are under common control with that organization.  **Control** | ||||
| means ownership of substantially all the assets of an entity, | ||||
| or the power to direct its management and policies by vote, | ||||
| contract, or otherwise.  Control can be direct or indirect. | ||||
|  | ||||
|    Copyright 2015-2020 Asim Aslam. | ||||
|    Copyright 2019-2020 Unistack LLC. | ||||
| **Your licenses** are all the licenses granted to you for the | ||||
| software under these terms. | ||||
|  | ||||
|    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. | ||||
| **Use** means anything you do with the software requiring one | ||||
| of your licenses. | ||||
|   | ||||
							
								
								
									
										50
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								README.md
									
									
									
									
									
								
							| @@ -1,43 +1,55 @@ | ||||
| # Micro | ||||
|  | ||||
| [](https://opensource.org/licenses/Apache-2.0) | ||||
| [](https://pkg.go.dev/go.unistack.org/micro/v4?tab=overview) | ||||
| [](https://git.unistack.org/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Av4+event%3Apush) | ||||
| [](https://goreportcard.com/report/go.unistack.org/micro/v4) | ||||
| # Go Micro [](https://polyformproject.org/licenses/perimeter/1.0.0/) [](https://pkg.go.dev/github.com/asim/go-micro/v3?tab=overview) | ||||
|  | ||||
| Micro is a standard library for microservices. | ||||
| Go Micro is a framework for microservices development. | ||||
|  | ||||
| ## Overview | ||||
|  | ||||
| Micro provides the core requirements for distributed systems development including RPC and Event driven communication.  | ||||
| Go Micro provides the core requirements for distributed systems development including RPC and Event driven communication.  | ||||
| The **Micro** philosophy is sane defaults with a pluggable architecture. We provide defaults to get you started quickly  | ||||
| but everything can be easily swapped out.  | ||||
|  | ||||
| ## Features | ||||
|  | ||||
| Micro abstracts away the details of distributed systems. Here are the main features. | ||||
| Go Micro abstracts away the details of distributed systems. Here are the main features. | ||||
|  | ||||
| - **Authentication** - Auth is built in as a first class citizen. Authentication and authorization enable secure  | ||||
| zero trust networking by providing every service an identity and certificates. This additionally includes rule  | ||||
| based access control. | ||||
|  | ||||
| - **Dynamic Config** - Load and hot reload dynamic config from anywhere. The config interface provides a way to load application  | ||||
| level config from any source such as env vars, cmdline, file, consul, vault... You can merge the sources and even define fallbacks. | ||||
| level config from any source such as env vars, file, etcd. You can merge the sources and even define fallbacks. | ||||
|  | ||||
| - **Data Storage** - A simple data store interface to read, write and delete records. It includes support for memory, file and  | ||||
| s3. State and persistence becomes a core requirement beyond prototyping and Micro looks to build that into the framework. | ||||
| CockroachDB by default. State and persistence becomes a core requirement beyond prototyping and Micro looks to build that into the framework. | ||||
|  | ||||
| - **Service Discovery** - Automatic service registration and name resolution. Service discovery is at the core of micro service  | ||||
| development. When service A needs to speak to service B it needs the location of that service. | ||||
| development. When service A needs to speak to service B it needs the location of that service. The default discovery mechanism is  | ||||
| multicast DNS (mdns), a zeroconf system. | ||||
|  | ||||
| - **Load Balancing** - Client side load balancing built on service discovery. Once we have the addresses of any number of instances  | ||||
| of a service we now need a way to decide which node to route to. We use random hashed load balancing to provide even distribution  | ||||
| across the services and retry a different node if there's a problem.  | ||||
|  | ||||
| - **Message Encoding** - Dynamic message encoding based on content-type. The client and server will use codecs along with content-type  | ||||
| to seamlessly encode and decode Go types for you. Any variety of messages could be encoded and sent from different clients. The client  | ||||
| and server handle this by default. | ||||
| and server handle this by default. This includes protobuf and json by default. | ||||
|  | ||||
| - **Async Messaging** - Pub/Sub is built in as a first class citizen for asynchronous communication and event driven architectures. | ||||
| Event notifications are a core pattern in micro service development. | ||||
| - **RPC Communication** - gRPC based request/response with support for bidirectional streaming. We provide an abstraction for synchronous communication. A request made to a service will be automatically resolved, load balanced, dialled and streamed. | ||||
|  | ||||
| - **Async Messaging** - PubSub is built in as a first class citizen for asynchronous communication and event driven architectures.  | ||||
| Event notifications are a core pattern in micro service development. The default messaging system is a HTTP event message broker. | ||||
|  | ||||
| - **Synchronization** - Distributed systems are often built in an eventually consistent manner. Support for distributed locking and  | ||||
| leadership are built in as a Sync interface. When using an eventually consistent database or scheduling use the Sync interface. | ||||
|  | ||||
| - **Pluggable Interfaces** - Micro makes use of Go interfaces for each system abstraction. Because of this these interfaces  | ||||
| are pluggable and allows Micro to be runtime agnostic. | ||||
| - **Pluggable Interfaces** - Go Micro makes use of Go interfaces for each distributed system abstraction. Because of this these interfaces  | ||||
| are pluggable and allows Go Micro to be runtime agnostic. You can plugin any underlying technology. Find external third party (non stdlib)  | ||||
| plugins in [github.com/asim/go-plugins](https://github.com/asim/go-plugins). | ||||
|  | ||||
| ## Getting Started | ||||
|  | ||||
| See [pkg.go.dev](https://pkg.go.dev/github.com/asim/go-micro/v3?tab=overview) for usage. | ||||
|  | ||||
| ## License | ||||
|  | ||||
| Micro is Apache 2.0 licensed. | ||||
|  | ||||
| [Polyform Strict](https://polyformproject.org/licenses/strict/1.0.0/). | ||||
|   | ||||
							
								
								
									
										1
									
								
								_config.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								_config.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| theme: jekyll-theme-architect | ||||
							
								
								
									
										30
									
								
								acme/acme.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								acme/acme.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| // Package acme abstracts away various ACME libraries | ||||
| package acme | ||||
|  | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
| 	"errors" | ||||
| 	"net" | ||||
| ) | ||||
|  | ||||
| // The Let's Encrypt ACME endpoints | ||||
| const ( | ||||
| 	LetsEncryptStagingCA    = "https://acme-staging-v02.api.letsencrypt.org/directory" | ||||
| 	LetsEncryptProductionCA = "https://acme-v02.api.letsencrypt.org/directory" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	// ErrProviderNotImplemented can be returned when attempting to | ||||
| 	// instantiate an unimplemented provider | ||||
| 	ErrProviderNotImplemented = errors.New("Provider not implemented") | ||||
| ) | ||||
|  | ||||
| // Provider is a ACME provider interface | ||||
| type Provider interface { | ||||
| 	// Listen returns a new listener | ||||
| 	Listen(...string) (net.Listener, error) | ||||
| 	// TLSConfig returns a tls config | ||||
| 	TLSConfig(...string) (*tls.Config, error) | ||||
| 	// Implementation of the acme provider | ||||
| 	String() string | ||||
| } | ||||
							
								
								
									
										50
									
								
								acme/autocert/autocert.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								acme/autocert/autocert.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | ||||
| // Package autocert is the ACME provider from golang.org/x/crypto/acme/autocert | ||||
| // This provider does not take any config. | ||||
| package autocert | ||||
|  | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
| 	"net" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/acme" | ||||
| 	"github.com/asim/go-micro/v3/logger" | ||||
| 	"golang.org/x/crypto/acme/autocert" | ||||
| ) | ||||
|  | ||||
| // autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert | ||||
| type autocertProvider struct{} | ||||
|  | ||||
| // Listen implements acme.Provider | ||||
| func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) { | ||||
| 	return autocert.NewListener(hosts...), nil | ||||
| } | ||||
|  | ||||
| // TLSConfig returns a new tls config | ||||
| func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) { | ||||
| 	// create a new manager | ||||
| 	m := &autocert.Manager{ | ||||
| 		Prompt: autocert.AcceptTOS, | ||||
| 	} | ||||
| 	if len(hosts) > 0 { | ||||
| 		m.HostPolicy = autocert.HostWhitelist(hosts...) | ||||
| 	} | ||||
| 	dir := cacheDir() | ||||
| 	if err := os.MkdirAll(dir, 0700); err != nil { | ||||
| 		if logger.V(logger.InfoLevel, logger.DefaultLogger) { | ||||
| 			logger.Infof("warning: autocert not using a cache: %v", err) | ||||
| 		} | ||||
| 	} else { | ||||
| 		m.Cache = autocert.DirCache(dir) | ||||
| 	} | ||||
| 	return m.TLSConfig(), nil | ||||
| } | ||||
|  | ||||
| func (a *autocertProvider) String() string { | ||||
| 	return "autocert" | ||||
| } | ||||
|  | ||||
| // New returns an autocert acme.Provider | ||||
| func NewProvider() acme.Provider { | ||||
| 	return &autocertProvider{} | ||||
| } | ||||
							
								
								
									
										16
									
								
								acme/autocert/autocert_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								acme/autocert/autocert_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| package autocert | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestAutocert(t *testing.T) { | ||||
| 	l := NewProvider() | ||||
| 	if _, ok := l.(*autocertProvider); !ok { | ||||
| 		t.Error("NewProvider() didn't return an autocertProvider") | ||||
| 	} | ||||
| 	// TODO: Travis CI doesn't let us bind :443 | ||||
| 	// if _, err := l.NewListener(); err != nil { | ||||
| 	// 	t.Error(err.Error()) | ||||
| 	// } | ||||
| } | ||||
							
								
								
									
										37
									
								
								acme/autocert/cache.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								acme/autocert/cache.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| package autocert | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"runtime" | ||||
| ) | ||||
|  | ||||
| func homeDir() string { | ||||
| 	if runtime.GOOS == "windows" { | ||||
| 		return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") | ||||
| 	} | ||||
| 	if h := os.Getenv("HOME"); h != "" { | ||||
| 		return h | ||||
| 	} | ||||
| 	return "/" | ||||
| } | ||||
|  | ||||
| func cacheDir() string { | ||||
| 	const base = "golang-autocert" | ||||
| 	switch runtime.GOOS { | ||||
| 	case "darwin": | ||||
| 		return filepath.Join(homeDir(), "Library", "Caches", base) | ||||
| 	case "windows": | ||||
| 		for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} { | ||||
| 			if v := os.Getenv(ev); v != "" { | ||||
| 				return filepath.Join(v, base) | ||||
| 			} | ||||
| 		} | ||||
| 		// Worst case: | ||||
| 		return filepath.Join(homeDir(), base) | ||||
| 	} | ||||
| 	if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" { | ||||
| 		return filepath.Join(xdg, base) | ||||
| 	} | ||||
| 	return filepath.Join(homeDir(), ".cache", base) | ||||
| } | ||||
							
								
								
									
										73
									
								
								acme/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								acme/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| package acme | ||||
|  | ||||
| import "github.com/go-acme/lego/v3/challenge" | ||||
|  | ||||
| // Option (or Options) are passed to New() to configure providers | ||||
| type Option func(o *Options) | ||||
|  | ||||
| // Options represents various options you can present to ACME providers | ||||
| type Options struct { | ||||
| 	// AcceptTLS must be set to true to indicate that you have read your | ||||
| 	// provider's terms of service. | ||||
| 	AcceptToS bool | ||||
| 	// CA is the CA to use | ||||
| 	CA string | ||||
| 	// ChallengeProvider is a go-acme/lego challenge provider. Set this if you | ||||
| 	// want to use DNS Challenges. Otherwise, tls-alpn-01 will be used | ||||
| 	ChallengeProvider challenge.Provider | ||||
| 	// Issue certificates for domains on demand. Otherwise, certs will be | ||||
| 	// retrieved / issued on start-up. | ||||
| 	OnDemand bool | ||||
| 	// Cache is a storage interface. Most ACME libraries have an cache, but | ||||
| 	// there's no defined interface, so if you consume this option | ||||
| 	// sanity check it before using. | ||||
| 	Cache interface{} | ||||
| } | ||||
|  | ||||
| // AcceptToS indicates whether you accept your CA's terms of service | ||||
| func AcceptToS(b bool) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.AcceptToS = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // CA sets the CA of an acme.Options | ||||
| func CA(CA string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.CA = CA | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // ChallengeProvider sets the Challenge provider of an acme.Options | ||||
| // if set, it enables the DNS challenge, otherwise tls-alpn-01 will be used. | ||||
| func ChallengeProvider(p challenge.Provider) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.ChallengeProvider = p | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // OnDemand enables on-demand certificate issuance. Not recommended for use | ||||
| // with the DNS challenge, as the first connection may be very slow. | ||||
| func OnDemand(b bool) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.OnDemand = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Cache provides a cache / storage interface to the underlying ACME library | ||||
| // as there is no standard, this needs to be validated by the underlying | ||||
| // implentation. | ||||
| func Cache(c interface{}) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Cache = c | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // DefaultOptions uses the Let's Encrypt Production CA, with DNS Challenge disabled. | ||||
| func DefaultOptions() Options { | ||||
| 	return Options{ | ||||
| 		AcceptToS: true, | ||||
| 		CA:        LetsEncryptProductionCA, | ||||
| 		OnDemand:  true, | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										151
									
								
								api/api.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										151
									
								
								api/api.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,151 @@ | ||||
| package api | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"net/http" | ||||
| 	"regexp" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/asim/go-micro/v3/server" | ||||
| ) | ||||
|  | ||||
| // Gateway is an api gateway interface | ||||
| type Gateway interface { | ||||
| 	// Initialise options | ||||
| 	Init(...Option) error | ||||
| 	// Get the options | ||||
| 	Options() Options | ||||
| 	// Register an endpoint | ||||
| 	Register(*Endpoint) error | ||||
| 	// Deregister a route | ||||
| 	Deregister(*Endpoint) error | ||||
| 	// Register http handler | ||||
| 	Handle(string, http.Handler) | ||||
| 	// Start serving requests | ||||
| 	Serve() error | ||||
| 	// Implementation of api | ||||
| 	String() string | ||||
| } | ||||
|  | ||||
| // Endpoint is a mapping between an RPC method and HTTP endpoint | ||||
| type Endpoint struct { | ||||
| 	// RPC Method e.g. Greeter.Hello | ||||
| 	Name string | ||||
| 	// Description e.g what's this endpoint for | ||||
| 	Description string | ||||
| 	// API Handler e.g rpc, proxy | ||||
| 	Handler string | ||||
| 	// HTTP Host e.g example.com | ||||
| 	Host []string | ||||
| 	// HTTP Methods e.g GET, POST | ||||
| 	Method []string | ||||
| 	// HTTP Path e.g /greeter. Expect POSIX regex | ||||
| 	Path []string | ||||
| 	// Body destination | ||||
| 	// "*" or "" - top level message value | ||||
| 	// "string" - inner message value | ||||
| 	Body string | ||||
| 	// Stream flag | ||||
| 	Stream bool | ||||
| } | ||||
|  | ||||
| // Service represents an API service | ||||
| type Service struct { | ||||
| 	// Name of service | ||||
| 	Name string | ||||
| 	// The endpoint for this service | ||||
| 	Endpoint *Endpoint | ||||
| 	// Versions of this service | ||||
| 	Services []*registry.Service | ||||
| } | ||||
|  | ||||
| // Encode encodes an endpoint to endpoint metadata | ||||
| func Encode(e *Endpoint) map[string]string { | ||||
| 	if e == nil { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	// endpoint map | ||||
| 	ep := make(map[string]string) | ||||
|  | ||||
| 	// set vals only if they exist | ||||
| 	set := func(k, v string) { | ||||
| 		if len(v) == 0 { | ||||
| 			return | ||||
| 		} | ||||
| 		ep[k] = v | ||||
| 	} | ||||
|  | ||||
| 	set("endpoint", e.Name) | ||||
| 	set("description", e.Description) | ||||
| 	set("handler", e.Handler) | ||||
| 	set("method", strings.Join(e.Method, ",")) | ||||
| 	set("path", strings.Join(e.Path, ",")) | ||||
| 	set("host", strings.Join(e.Host, ",")) | ||||
|  | ||||
| 	return ep | ||||
| } | ||||
|  | ||||
| // Decode decodes endpoint metadata into an endpoint | ||||
| func Decode(e map[string]string) *Endpoint { | ||||
| 	if e == nil { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	return &Endpoint{ | ||||
| 		Name:        e["endpoint"], | ||||
| 		Description: e["description"], | ||||
| 		Method:      slice(e["method"]), | ||||
| 		Path:        slice(e["path"]), | ||||
| 		Host:        slice(e["host"]), | ||||
| 		Handler:     e["handler"], | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Validate validates an endpoint to guarantee it won't blow up when being served | ||||
| func Validate(e *Endpoint) error { | ||||
| 	if e == nil { | ||||
| 		return errors.New("endpoint is nil") | ||||
| 	} | ||||
|  | ||||
| 	if len(e.Name) == 0 { | ||||
| 		return errors.New("name required") | ||||
| 	} | ||||
|  | ||||
| 	for _, p := range e.Path { | ||||
| 		ps := p[0] | ||||
| 		pe := p[len(p)-1] | ||||
|  | ||||
| 		if ps == '^' && pe == '$' { | ||||
| 			_, err := regexp.CompilePOSIX(p) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} else if ps == '^' && pe != '$' { | ||||
| 			return errors.New("invalid path") | ||||
| 		} else if ps != '^' && pe == '$' { | ||||
| 			return errors.New("invalid path") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if len(e.Handler) == 0 { | ||||
| 		return errors.New("invalid handler") | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // WithEndpoint returns a server.HandlerOption with endpoint metadata set | ||||
| // | ||||
| // Usage: | ||||
| // | ||||
| // 	proto.RegisterHandler(service.Server(), new(Handler), api.WithEndpoint( | ||||
| //		&api.Endpoint{ | ||||
| //			Name: "Greeter.Hello", | ||||
| //			Path: []string{"/greeter"}, | ||||
| //		}, | ||||
| //	)) | ||||
| func WithEndpoint(e *Endpoint) server.HandlerOption { | ||||
| 	return server.EndpointMetadata(e.Name, Encode(e)) | ||||
| } | ||||
							
								
								
									
										152
									
								
								api/api_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										152
									
								
								api/api_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,152 @@ | ||||
| package api | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestEncoding(t *testing.T) { | ||||
| 	testData := []*Endpoint{ | ||||
| 		nil, | ||||
| 		{ | ||||
| 			Name:        "Foo.Bar", | ||||
| 			Description: "A test endpoint", | ||||
| 			Handler:     "meta", | ||||
| 			Host:        []string{"foo.com"}, | ||||
| 			Method:      []string{"GET"}, | ||||
| 			Path:        []string{"/test"}, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	compare := func(expect, got []string) bool { | ||||
| 		// no data to compare, return true | ||||
| 		if len(expect) == 0 && len(got) == 0 { | ||||
| 			return true | ||||
| 		} | ||||
| 		// no data expected but got some return false | ||||
| 		if len(expect) == 0 && len(got) > 0 { | ||||
| 			return false | ||||
| 		} | ||||
|  | ||||
| 		// compare expected with what we got | ||||
| 		for _, e := range expect { | ||||
| 			var seen bool | ||||
| 			for _, g := range got { | ||||
| 				if e == g { | ||||
| 					seen = true | ||||
| 					break | ||||
| 				} | ||||
| 			} | ||||
| 			if !seen { | ||||
| 				return false | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// we're done, return true | ||||
| 		return true | ||||
| 	} | ||||
|  | ||||
| 	for _, d := range testData { | ||||
| 		// encode | ||||
| 		e := Encode(d) | ||||
| 		// decode | ||||
| 		de := Decode(e) | ||||
|  | ||||
| 		// nil endpoint returns nil | ||||
| 		if d == nil { | ||||
| 			if e != nil { | ||||
| 				t.Fatalf("expected nil got %v", e) | ||||
| 			} | ||||
| 			if de != nil { | ||||
| 				t.Fatalf("expected nil got %v", de) | ||||
| 			} | ||||
|  | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// check encoded map | ||||
| 		name := e["endpoint"] | ||||
| 		desc := e["description"] | ||||
| 		method := strings.Split(e["method"], ",") | ||||
| 		path := strings.Split(e["path"], ",") | ||||
| 		host := strings.Split(e["host"], ",") | ||||
| 		handler := e["handler"] | ||||
|  | ||||
| 		if name != d.Name { | ||||
| 			t.Fatalf("expected %v got %v", d.Name, name) | ||||
| 		} | ||||
| 		if desc != d.Description { | ||||
| 			t.Fatalf("expected %v got %v", d.Description, desc) | ||||
| 		} | ||||
| 		if handler != d.Handler { | ||||
| 			t.Fatalf("expected %v got %v", d.Handler, handler) | ||||
| 		} | ||||
| 		if ok := compare(d.Method, method); !ok { | ||||
| 			t.Fatalf("expected %v got %v", d.Method, method) | ||||
| 		} | ||||
| 		if ok := compare(d.Path, path); !ok { | ||||
| 			t.Fatalf("expected %v got %v", d.Path, path) | ||||
| 		} | ||||
| 		if ok := compare(d.Host, host); !ok { | ||||
| 			t.Fatalf("expected %v got %v", d.Host, host) | ||||
| 		} | ||||
|  | ||||
| 		if de.Name != d.Name { | ||||
| 			t.Fatalf("expected %v got %v", d.Name, de.Name) | ||||
| 		} | ||||
| 		if de.Description != d.Description { | ||||
| 			t.Fatalf("expected %v got %v", d.Description, de.Description) | ||||
| 		} | ||||
| 		if de.Handler != d.Handler { | ||||
| 			t.Fatalf("expected %v got %v", d.Handler, de.Handler) | ||||
| 		} | ||||
| 		if ok := compare(d.Method, de.Method); !ok { | ||||
| 			t.Fatalf("expected %v got %v", d.Method, de.Method) | ||||
| 		} | ||||
| 		if ok := compare(d.Path, de.Path); !ok { | ||||
| 			t.Fatalf("expected %v got %v", d.Path, de.Path) | ||||
| 		} | ||||
| 		if ok := compare(d.Host, de.Host); !ok { | ||||
| 			t.Fatalf("expected %v got %v", d.Host, de.Host) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestValidate(t *testing.T) { | ||||
| 	epPcre := &Endpoint{ | ||||
| 		Name:        "Foo.Bar", | ||||
| 		Description: "A test endpoint", | ||||
| 		Handler:     "meta", | ||||
| 		Host:        []string{"foo.com"}, | ||||
| 		Method:      []string{"GET"}, | ||||
| 		Path:        []string{"^/test/?$"}, | ||||
| 	} | ||||
| 	if err := Validate(epPcre); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	epGpath := &Endpoint{ | ||||
| 		Name:        "Foo.Bar", | ||||
| 		Description: "A test endpoint", | ||||
| 		Handler:     "meta", | ||||
| 		Host:        []string{"foo.com"}, | ||||
| 		Method:      []string{"GET"}, | ||||
| 		Path:        []string{"/test/{id}"}, | ||||
| 	} | ||||
| 	if err := Validate(epGpath); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	epPcreInvalid := &Endpoint{ | ||||
| 		Name:        "Foo.Bar", | ||||
| 		Description: "A test endpoint", | ||||
| 		Handler:     "meta", | ||||
| 		Host:        []string{"foo.com"}, | ||||
| 		Method:      []string{"GET"}, | ||||
| 		Path:        []string{"/test/?$"}, | ||||
| 	} | ||||
| 	if err := Validate(epPcreInvalid); err == nil { | ||||
| 		t.Fatalf("invalid pcre %v", epPcreInvalid.Path[0]) | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										14
									
								
								api/handler/handler.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								api/handler/handler.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| // Package handler provides http handlers | ||||
| package handler | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
| ) | ||||
|  | ||||
| // Handler represents a HTTP handler that manages a request | ||||
| type Handler interface { | ||||
| 	// standard http handler | ||||
| 	http.Handler | ||||
| 	// name of handler | ||||
| 	String() string | ||||
| } | ||||
							
								
								
									
										104
									
								
								api/handler/http/http.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								api/handler/http/http.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | ||||
| // Package http is a http reverse proxy handler | ||||
| package http | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"math/rand" | ||||
| 	"net/http" | ||||
| 	"net/http/httputil" | ||||
| 	"net/url" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api" | ||||
| 	"github.com/asim/go-micro/v3/api/handler" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	Handler = "http" | ||||
| ) | ||||
|  | ||||
| type httpHandler struct { | ||||
| 	options handler.Options | ||||
|  | ||||
| 	// set with different initializer | ||||
| 	s *api.Service | ||||
| } | ||||
|  | ||||
| func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||||
| 	service, err := h.getService(r) | ||||
| 	if err != nil { | ||||
| 		w.WriteHeader(http.StatusInternalServerError) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	if len(service) == 0 { | ||||
| 		w.WriteHeader(404) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	rp, err := url.Parse(service) | ||||
| 	if err != nil { | ||||
| 		w.WriteHeader(http.StatusInternalServerError) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	httputil.NewSingleHostReverseProxy(rp).ServeHTTP(w, r) | ||||
| } | ||||
|  | ||||
| // getService returns the service for this request from the selector | ||||
| func (h *httpHandler) getService(r *http.Request) (string, error) { | ||||
| 	var service *api.Service | ||||
|  | ||||
| 	if h.s != nil { | ||||
| 		// we were given the service | ||||
| 		service = h.s | ||||
| 	} else if h.options.Router != nil { | ||||
| 		// try get service from router | ||||
| 		s, err := h.options.Router.Route(r) | ||||
| 		if err != nil { | ||||
| 			return "", err | ||||
| 		} | ||||
| 		service = s | ||||
| 	} else { | ||||
| 		// we have no way of routing the request | ||||
| 		return "", errors.New("no route found") | ||||
| 	} | ||||
|  | ||||
| 	// get the nodes for this service | ||||
| 	var nodes []*registry.Node | ||||
| 	for _, srv := range service.Services { | ||||
| 		nodes = append(nodes, srv.Nodes...) | ||||
| 	} | ||||
|  | ||||
| 	// select a random node | ||||
| 	if len(nodes) == 0 { | ||||
| 		return "", errors.New("no route found") | ||||
| 	} | ||||
| 	node := nodes[rand.Int()%len(nodes)] | ||||
|  | ||||
| 	return fmt.Sprintf("http://%s", node.Address), nil | ||||
| } | ||||
|  | ||||
| func (h *httpHandler) String() string { | ||||
| 	return "http" | ||||
| } | ||||
|  | ||||
| // NewHandler returns a http proxy handler | ||||
| func NewHandler(opts ...handler.Option) handler.Handler { | ||||
| 	options := handler.NewOptions(opts...) | ||||
|  | ||||
| 	return &httpHandler{ | ||||
| 		options: options, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithService creates a handler with a service | ||||
| func WithService(s *api.Service, opts ...handler.Option) handler.Handler { | ||||
| 	options := handler.NewOptions(opts...) | ||||
|  | ||||
| 	return &httpHandler{ | ||||
| 		options: options, | ||||
| 		s:       s, | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										102
									
								
								api/handler/http/http_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								api/handler/http/http_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,102 @@ | ||||
| package http | ||||
|  | ||||
| import ( | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api/handler" | ||||
| 	"github.com/asim/go-micro/v3/api/resolver" | ||||
| 	rpath "github.com/asim/go-micro/v3/api/resolver/path" | ||||
| 	"github.com/asim/go-micro/v3/api/router" | ||||
| 	regRouter "github.com/asim/go-micro/v3/api/router/registry" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/asim/go-micro/v3/registry/memory" | ||||
| ) | ||||
|  | ||||
| func testHttp(t *testing.T, path, service, ns string) { | ||||
| 	r := memory.NewRegistry() | ||||
|  | ||||
| 	l, err := net.Listen("tcp", "127.0.0.1:0") | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	defer l.Close() | ||||
|  | ||||
| 	s := ®istry.Service{ | ||||
| 		Name: service, | ||||
| 		Nodes: []*registry.Node{ | ||||
| 			{ | ||||
| 				Id:      service + "-1", | ||||
| 				Address: l.Addr().String(), | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	r.Register(s) | ||||
| 	defer r.Deregister(s) | ||||
|  | ||||
| 	// setup the test handler | ||||
| 	m := http.NewServeMux() | ||||
| 	m.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { | ||||
| 		w.Write([]byte(`you got served`)) | ||||
| 	}) | ||||
|  | ||||
| 	// start http test serve | ||||
| 	go http.Serve(l, m) | ||||
|  | ||||
| 	// create new request and writer | ||||
| 	w := httptest.NewRecorder() | ||||
| 	req, err := http.NewRequest("POST", path, nil) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	// initialise the handler | ||||
| 	rt := regRouter.NewRouter( | ||||
| 		router.WithHandler("http"), | ||||
| 		router.WithRegistry(r), | ||||
| 		router.WithResolver(rpath.NewResolver( | ||||
| 			resolver.WithServicePrefix(ns), | ||||
| 		)), | ||||
| 	) | ||||
|  | ||||
| 	p := NewHandler(handler.WithRouter(rt)) | ||||
|  | ||||
| 	// execute the handler | ||||
| 	p.ServeHTTP(w, req) | ||||
|  | ||||
| 	if w.Code != 200 { | ||||
| 		t.Fatalf("Expected 200 response got %d %s", w.Code, w.Body.String()) | ||||
| 	} | ||||
|  | ||||
| 	if w.Body.String() != "you got served" { | ||||
| 		t.Fatalf("Expected body: you got served. Got: %s", w.Body.String()) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestHttpHandler(t *testing.T) { | ||||
| 	testData := []struct { | ||||
| 		path      string | ||||
| 		service   string | ||||
| 		namespace string | ||||
| 	}{ | ||||
| 		{ | ||||
| 			"/test/foo", | ||||
| 			"go.micro.api.test", | ||||
| 			"go.micro.api", | ||||
| 		}, | ||||
| 		{ | ||||
| 			"/test/foo/baz", | ||||
| 			"go.micro.api.test", | ||||
| 			"go.micro.api", | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	for _, d := range testData { | ||||
| 		t.Run(d.service, func(t *testing.T) { | ||||
| 			testHttp(t, d.path, d.service, d.namespace) | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										70
									
								
								api/handler/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								api/handler/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,70 @@ | ||||
| package handler | ||||
|  | ||||
| import ( | ||||
| 	"github.com/asim/go-micro/v3/api/router" | ||||
| 	"github.com/asim/go-micro/v3/client" | ||||
| 	"github.com/asim/go-micro/v3/client/grpc" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	DefaultMaxRecvSize int64 = 1024 * 1024 * 100 // 10Mb | ||||
| ) | ||||
|  | ||||
| type Options struct { | ||||
| 	MaxRecvSize int64 | ||||
| 	Namespace   string | ||||
| 	Router      router.Router | ||||
| 	Client      client.Client | ||||
| } | ||||
|  | ||||
| type Option func(o *Options) | ||||
|  | ||||
| // NewOptions fills in the blanks | ||||
| func NewOptions(opts ...Option) Options { | ||||
| 	var options Options | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	if options.Client == nil { | ||||
| 		WithClient(grpc.NewClient())(&options) | ||||
| 	} | ||||
|  | ||||
| 	// set namespace if blank | ||||
| 	if len(options.Namespace) == 0 { | ||||
| 		WithNamespace("go.micro.api")(&options) | ||||
| 	} | ||||
|  | ||||
| 	if options.MaxRecvSize == 0 { | ||||
| 		options.MaxRecvSize = DefaultMaxRecvSize | ||||
| 	} | ||||
|  | ||||
| 	return options | ||||
| } | ||||
|  | ||||
| // WithNamespace specifies the namespace for the handler | ||||
| func WithNamespace(s string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Namespace = s | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithRouter specifies a router to be used by the handler | ||||
| func WithRouter(r router.Router) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Router = r | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func WithClient(c client.Client) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Client = c | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithMaxRecvSize specifies max body size | ||||
| func WithMaxRecvSize(size int64) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.MaxRecvSize = size | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										28
									
								
								api/handler/rpc/proto/message.pb.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								api/handler/rpc/proto/message.pb.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| package proto | ||||
|  | ||||
| type Message struct { | ||||
| 	data []byte | ||||
| } | ||||
|  | ||||
| func (m *Message) ProtoMessage() {} | ||||
|  | ||||
| func (m *Message) Reset() { | ||||
| 	*m = Message{} | ||||
| } | ||||
|  | ||||
| func (m *Message) String() string { | ||||
| 	return string(m.data) | ||||
| } | ||||
|  | ||||
| func (m *Message) Marshal() ([]byte, error) { | ||||
| 	return m.data, nil | ||||
| } | ||||
|  | ||||
| func (m *Message) Unmarshal(data []byte) error { | ||||
| 	m.data = data | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func NewMessage(data []byte) *Message { | ||||
| 	return &Message{data} | ||||
| } | ||||
							
								
								
									
										499
									
								
								api/handler/rpc/rpc.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										499
									
								
								api/handler/rpc/rpc.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,499 @@ | ||||
| // Package rpc is a go-micro rpc handler. | ||||
| package rpc | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api" | ||||
| 	"github.com/asim/go-micro/v3/api/handler" | ||||
| 	"github.com/asim/go-micro/v3/api/handler/rpc/proto" | ||||
| 	"github.com/asim/go-micro/v3/client" | ||||
| 	"github.com/asim/go-micro/v3/codec" | ||||
| 	"github.com/asim/go-micro/v3/codec/jsonrpc" | ||||
| 	"github.com/asim/go-micro/v3/codec/protorpc" | ||||
| 	"github.com/asim/go-micro/v3/errors" | ||||
| 	"github.com/asim/go-micro/v3/logger" | ||||
| 	"github.com/asim/go-micro/v3/metadata" | ||||
| 	"github.com/asim/go-micro/v3/util/ctx" | ||||
| 	"github.com/asim/go-micro/v3/util/qson" | ||||
| 	"github.com/asim/go-micro/v3/util/router" | ||||
| 	jsonpatch "github.com/evanphx/json-patch/v5" | ||||
| 	"github.com/oxtoacart/bpool" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	Handler = "rpc" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	// supported json codecs | ||||
| 	jsonCodecs = []string{ | ||||
| 		"application/grpc+json", | ||||
| 		"application/json", | ||||
| 		"application/json-rpc", | ||||
| 	} | ||||
|  | ||||
| 	// support proto codecs | ||||
| 	protoCodecs = []string{ | ||||
| 		"application/grpc", | ||||
| 		"application/grpc+proto", | ||||
| 		"application/proto", | ||||
| 		"application/protobuf", | ||||
| 		"application/proto-rpc", | ||||
| 		"application/octet-stream", | ||||
| 	} | ||||
|  | ||||
| 	bufferPool = bpool.NewSizedBufferPool(1024, 8) | ||||
| ) | ||||
|  | ||||
| type rpcHandler struct { | ||||
| 	opts handler.Options | ||||
| 	s    *api.Service | ||||
| } | ||||
|  | ||||
| type buffer struct { | ||||
| 	io.ReadCloser | ||||
| } | ||||
|  | ||||
| func (b *buffer) Write(_ []byte) (int, error) { | ||||
| 	return 0, nil | ||||
| } | ||||
|  | ||||
| func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||||
| 	bsize := handler.DefaultMaxRecvSize | ||||
| 	if h.opts.MaxRecvSize > 0 { | ||||
| 		bsize = h.opts.MaxRecvSize | ||||
| 	} | ||||
|  | ||||
| 	r.Body = http.MaxBytesReader(w, r.Body, bsize) | ||||
|  | ||||
| 	defer r.Body.Close() | ||||
| 	var service *api.Service | ||||
|  | ||||
| 	if h.s != nil { | ||||
| 		// we were given the service | ||||
| 		service = h.s | ||||
| 	} else if h.opts.Router != nil { | ||||
| 		// try get service from router | ||||
| 		s, err := h.opts.Router.Route(r) | ||||
| 		if err != nil { | ||||
| 			writeError(w, r, errors.InternalServerError("go.micro.api", err.Error())) | ||||
| 			return | ||||
| 		} | ||||
| 		service = s | ||||
| 	} else { | ||||
| 		// we have no way of routing the request | ||||
| 		writeError(w, r, errors.InternalServerError("go.micro.api", "no route found")) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	ct := r.Header.Get("Content-Type") | ||||
|  | ||||
| 	// Strip charset from Content-Type (like `application/json; charset=UTF-8`) | ||||
| 	if idx := strings.IndexRune(ct, ';'); idx >= 0 { | ||||
| 		ct = ct[:idx] | ||||
| 	} | ||||
|  | ||||
| 	// micro client | ||||
| 	c := h.opts.Client | ||||
|  | ||||
| 	// create context | ||||
| 	cx := ctx.FromRequest(r) | ||||
|  | ||||
| 	// set merged context to request | ||||
| 	*r = *r.Clone(cx) | ||||
| 	// if stream we currently only support json | ||||
| 	if isStream(r, service) { | ||||
| 		serveWebsocket(cx, w, r, service, c) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	// create custom router | ||||
| 	callOpt := client.WithRouter(router.New(service.Services)) | ||||
|  | ||||
| 	// walk the standard call path | ||||
| 	// get payload | ||||
| 	br, err := requestPayload(r) | ||||
| 	if err != nil { | ||||
| 		writeError(w, r, err) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	var rsp []byte | ||||
|  | ||||
| 	switch { | ||||
| 	// proto codecs | ||||
| 	case hasCodec(ct, protoCodecs): | ||||
| 		request := &proto.Message{} | ||||
| 		// if the extracted payload isn't empty lets use it | ||||
| 		if len(br) > 0 { | ||||
| 			request = proto.NewMessage(br) | ||||
| 		} | ||||
|  | ||||
| 		// create request/response | ||||
| 		response := &proto.Message{} | ||||
|  | ||||
| 		req := c.NewRequest( | ||||
| 			service.Name, | ||||
| 			service.Endpoint.Name, | ||||
| 			request, | ||||
| 			client.WithContentType(ct), | ||||
| 		) | ||||
|  | ||||
| 		// make the call | ||||
| 		if err := c.Call(cx, req, response, callOpt); err != nil { | ||||
| 			writeError(w, r, err) | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		// marshall response | ||||
| 		rsp, err = response.Marshal() | ||||
| 		if err != nil { | ||||
| 			writeError(w, r, err) | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 	default: | ||||
| 		// if json codec is not present set to json | ||||
| 		if !hasCodec(ct, jsonCodecs) { | ||||
| 			ct = "application/json" | ||||
| 		} | ||||
|  | ||||
| 		// default to trying json | ||||
| 		var request json.RawMessage | ||||
| 		// if the extracted payload isn't empty lets use it | ||||
| 		if len(br) > 0 { | ||||
| 			request = json.RawMessage(br) | ||||
| 		} | ||||
|  | ||||
| 		// create request/response | ||||
| 		var response json.RawMessage | ||||
|  | ||||
| 		req := c.NewRequest( | ||||
| 			service.Name, | ||||
| 			service.Endpoint.Name, | ||||
| 			&request, | ||||
| 			client.WithContentType(ct), | ||||
| 		) | ||||
| 		// make the call | ||||
| 		if err := c.Call(cx, req, &response, callOpt); err != nil { | ||||
| 			writeError(w, r, err) | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		// marshall response | ||||
| 		rsp, err = response.MarshalJSON() | ||||
| 		if err != nil { | ||||
| 			writeError(w, r, err) | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// write the response | ||||
| 	writeResponse(w, r, rsp) | ||||
| } | ||||
|  | ||||
| func (rh *rpcHandler) String() string { | ||||
| 	return "rpc" | ||||
| } | ||||
|  | ||||
| func hasCodec(ct string, codecs []string) bool { | ||||
| 	for _, codec := range codecs { | ||||
| 		if ct == codec { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|  | ||||
| // requestPayload takes a *http.Request. | ||||
| // If the request is a GET the query string parameters are extracted and marshaled to JSON and the raw bytes are returned. | ||||
| // If the request method is a POST the request body is read and returned | ||||
| func requestPayload(r *http.Request) ([]byte, error) { | ||||
| 	var err error | ||||
|  | ||||
| 	// we have to decode json-rpc and proto-rpc because we suck | ||||
| 	// well actually because there's no proxy codec right now | ||||
|  | ||||
| 	ct := r.Header.Get("Content-Type") | ||||
| 	switch { | ||||
| 	case strings.Contains(ct, "application/json-rpc"): | ||||
| 		msg := codec.Message{ | ||||
| 			Type:   codec.Request, | ||||
| 			Header: make(map[string]string), | ||||
| 		} | ||||
| 		c := jsonrpc.NewCodec(&buffer{r.Body}) | ||||
| 		if err = c.ReadHeader(&msg, codec.Request); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		var raw json.RawMessage | ||||
| 		if err = c.ReadBody(&raw); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		return ([]byte)(raw), nil | ||||
| 	case strings.Contains(ct, "application/proto-rpc"), strings.Contains(ct, "application/octet-stream"): | ||||
| 		msg := codec.Message{ | ||||
| 			Type:   codec.Request, | ||||
| 			Header: make(map[string]string), | ||||
| 		} | ||||
| 		c := protorpc.NewCodec(&buffer{r.Body}) | ||||
| 		if err = c.ReadHeader(&msg, codec.Request); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		var raw proto.Message | ||||
| 		if err = c.ReadBody(&raw); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		return raw.Marshal() | ||||
| 	case strings.Contains(ct, "application/x-www-form-urlencoded"): | ||||
| 		r.ParseForm() | ||||
|  | ||||
| 		// generate a new set of values from the form | ||||
| 		vals := make(map[string]string) | ||||
| 		for k, v := range r.Form { | ||||
| 			vals[k] = strings.Join(v, ",") | ||||
| 		} | ||||
|  | ||||
| 		// marshal | ||||
| 		return json.Marshal(vals) | ||||
| 		// TODO: application/grpc | ||||
| 	} | ||||
|  | ||||
| 	// otherwise as per usual | ||||
| 	ctx := r.Context() | ||||
| 	// dont user metadata.FromContext as it mangles names | ||||
| 	md, ok := metadata.FromContext(ctx) | ||||
| 	if !ok { | ||||
| 		md = make(map[string]string) | ||||
| 	} | ||||
|  | ||||
| 	// allocate maximum | ||||
| 	matches := make(map[string]interface{}, len(md)) | ||||
| 	bodydst := "" | ||||
|  | ||||
| 	// get fields from url path | ||||
| 	for k, v := range md { | ||||
| 		k = strings.ToLower(k) | ||||
| 		// filter own keys | ||||
| 		if strings.HasPrefix(k, "x-api-field-") { | ||||
| 			matches[strings.TrimPrefix(k, "x-api-field-")] = v | ||||
| 			delete(md, k) | ||||
| 		} else if k == "x-api-body" { | ||||
| 			bodydst = v | ||||
| 			delete(md, k) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// map of all fields | ||||
| 	req := make(map[string]interface{}, len(md)) | ||||
|  | ||||
| 	// get fields from url values | ||||
| 	if len(r.URL.RawQuery) > 0 { | ||||
| 		umd := make(map[string]interface{}) | ||||
| 		err = qson.Unmarshal(&umd, r.URL.RawQuery) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		for k, v := range umd { | ||||
| 			matches[k] = v | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// restore context without fields | ||||
| 	*r = *r.Clone(metadata.NewContext(ctx, md)) | ||||
|  | ||||
| 	for k, v := range matches { | ||||
| 		ps := strings.Split(k, ".") | ||||
| 		if len(ps) == 1 { | ||||
| 			req[k] = v | ||||
| 			continue | ||||
| 		} | ||||
| 		em := make(map[string]interface{}) | ||||
| 		em[ps[len(ps)-1]] = v | ||||
| 		for i := len(ps) - 2; i > 0; i-- { | ||||
| 			nm := make(map[string]interface{}) | ||||
| 			nm[ps[i]] = em | ||||
| 			em = nm | ||||
| 		} | ||||
| 		if vm, ok := req[ps[0]]; ok { | ||||
| 			// nested map | ||||
| 			nm := vm.(map[string]interface{}) | ||||
| 			for vk, vv := range em { | ||||
| 				nm[vk] = vv | ||||
| 			} | ||||
| 			req[ps[0]] = nm | ||||
| 		} else { | ||||
| 			req[ps[0]] = em | ||||
| 		} | ||||
| 	} | ||||
| 	pathbuf := []byte("{}") | ||||
| 	if len(req) > 0 { | ||||
| 		pathbuf, err = json.Marshal(req) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	urlbuf := []byte("{}") | ||||
| 	out, err := jsonpatch.MergeMergePatches(urlbuf, pathbuf) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	switch r.Method { | ||||
| 	case "GET": | ||||
| 		// empty response | ||||
| 		if strings.Contains(ct, "application/json") && string(out) == "{}" { | ||||
| 			return out, nil | ||||
| 		} else if string(out) == "{}" && !strings.Contains(ct, "application/json") { | ||||
| 			return []byte{}, nil | ||||
| 		} | ||||
| 		return out, nil | ||||
| 	case "PATCH", "POST", "PUT", "DELETE": | ||||
| 		bodybuf := []byte("{}") | ||||
| 		buf := bufferPool.Get() | ||||
| 		defer bufferPool.Put(buf) | ||||
| 		if _, err := buf.ReadFrom(r.Body); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		if b := buf.Bytes(); len(b) > 0 { | ||||
| 			bodybuf = b | ||||
| 		} | ||||
| 		if bodydst == "" || bodydst == "*" { | ||||
| 			// jsonpatch resequences the json object so we avoid it if possible (some usecases such as | ||||
| 			// validating signatures require the request body to be unchangedd). We're keeping support | ||||
| 			// for the custom paramaters for backwards compatability reasons. | ||||
| 			if string(out) == "{}" { | ||||
| 				return bodybuf, nil | ||||
| 			} | ||||
|  | ||||
| 			if out, err = jsonpatch.MergeMergePatches(out, bodybuf); err == nil { | ||||
| 				return out, nil | ||||
| 			} | ||||
| 		} | ||||
| 		var jsonbody map[string]interface{} | ||||
| 		if json.Valid(bodybuf) { | ||||
| 			if err = json.Unmarshal(bodybuf, &jsonbody); err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| 		} | ||||
| 		dstmap := make(map[string]interface{}) | ||||
| 		ps := strings.Split(bodydst, ".") | ||||
| 		if len(ps) == 1 { | ||||
| 			if jsonbody != nil { | ||||
| 				dstmap[ps[0]] = jsonbody | ||||
| 			} else { | ||||
| 				// old unexpected behaviour | ||||
| 				dstmap[ps[0]] = bodybuf | ||||
| 			} | ||||
| 		} else { | ||||
| 			em := make(map[string]interface{}) | ||||
| 			if jsonbody != nil { | ||||
| 				em[ps[len(ps)-1]] = jsonbody | ||||
| 			} else { | ||||
| 				// old unexpected behaviour | ||||
| 				em[ps[len(ps)-1]] = bodybuf | ||||
| 			} | ||||
| 			for i := len(ps) - 2; i > 0; i-- { | ||||
| 				nm := make(map[string]interface{}) | ||||
| 				nm[ps[i]] = em | ||||
| 				em = nm | ||||
| 			} | ||||
| 			dstmap[ps[0]] = em | ||||
| 		} | ||||
|  | ||||
| 		bodyout, err := json.Marshal(dstmap) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 		if out, err = jsonpatch.MergeMergePatches(out, bodyout); err == nil { | ||||
| 			return out, nil | ||||
| 		} | ||||
|  | ||||
| 		//fallback to previous unknown behaviour | ||||
| 		return bodybuf, nil | ||||
| 	} | ||||
|  | ||||
| 	return []byte{}, nil | ||||
| } | ||||
|  | ||||
| func writeError(w http.ResponseWriter, r *http.Request, err error) { | ||||
| 	ce := errors.Parse(err.Error()) | ||||
|  | ||||
| 	switch ce.Code { | ||||
| 	case 0: | ||||
| 		// assuming it's totally screwed | ||||
| 		ce.Code = http.StatusInternalServerError | ||||
| 		ce.Id = "go.micro.api" | ||||
| 		ce.Status = http.StatusText(http.StatusInternalServerError) | ||||
| 		ce.Detail = "error during request: " + ce.Detail | ||||
| 		w.WriteHeader(http.StatusInternalServerError) | ||||
| 	default: | ||||
| 		w.WriteHeader(int(ce.Code)) | ||||
| 	} | ||||
|  | ||||
| 	// response content type | ||||
| 	w.Header().Set("Content-Type", "application/json") | ||||
|  | ||||
| 	// Set trailers | ||||
| 	if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { | ||||
| 		w.Header().Set("Trailer", "grpc-status") | ||||
| 		w.Header().Set("Trailer", "grpc-message") | ||||
| 		w.Header().Set("grpc-status", "13") | ||||
| 		w.Header().Set("grpc-message", ce.Detail) | ||||
| 	} | ||||
|  | ||||
| 	_, werr := w.Write([]byte(ce.Error())) | ||||
| 	if werr != nil { | ||||
| 		if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 			logger.Error(werr) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) { | ||||
| 	w.Header().Set("Content-Type", r.Header.Get("Content-Type")) | ||||
| 	w.Header().Set("Content-Length", strconv.Itoa(len(rsp))) | ||||
|  | ||||
| 	// Set trailers | ||||
| 	if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { | ||||
| 		w.Header().Set("Trailer", "grpc-status") | ||||
| 		w.Header().Set("Trailer", "grpc-message") | ||||
| 		w.Header().Set("grpc-status", "0") | ||||
| 		w.Header().Set("grpc-message", "") | ||||
| 	} | ||||
|  | ||||
| 	// write 204 status if rsp is nil | ||||
| 	if len(rsp) == 0 { | ||||
| 		w.WriteHeader(http.StatusNoContent) | ||||
| 	} | ||||
|  | ||||
| 	// write response | ||||
| 	_, err := w.Write(rsp) | ||||
| 	if err != nil { | ||||
| 		if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 			logger.Error(err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| func NewHandler(opts ...handler.Option) handler.Handler { | ||||
| 	options := handler.NewOptions(opts...) | ||||
| 	return &rpcHandler{ | ||||
| 		opts: options, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func WithService(s *api.Service, opts ...handler.Option) handler.Handler { | ||||
| 	options := handler.NewOptions(opts...) | ||||
| 	return &rpcHandler{ | ||||
| 		opts: options, | ||||
| 		s:    s, | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										261
									
								
								api/handler/rpc/stream.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										261
									
								
								api/handler/rpc/stream.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,261 @@ | ||||
| package rpc | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"context" | ||||
| 	"encoding/json" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api" | ||||
| 	"github.com/asim/go-micro/v3/client" | ||||
| 	raw "github.com/asim/go-micro/v3/codec/bytes" | ||||
| 	"github.com/asim/go-micro/v3/logger" | ||||
| 	"github.com/asim/go-micro/v3/util/router" | ||||
| 	"github.com/gobwas/httphead" | ||||
| 	"github.com/gobwas/ws" | ||||
| 	"github.com/gobwas/ws/wsutil" | ||||
| ) | ||||
|  | ||||
| // serveWebsocket will stream rpc back over websockets assuming json | ||||
| func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, service *api.Service, c client.Client) { | ||||
| 	var op ws.OpCode | ||||
|  | ||||
| 	ct := r.Header.Get("Content-Type") | ||||
| 	// Strip charset from Content-Type (like `application/json; charset=UTF-8`) | ||||
| 	if idx := strings.IndexRune(ct, ';'); idx >= 0 { | ||||
| 		ct = ct[:idx] | ||||
| 	} | ||||
|  | ||||
| 	// check proto from request | ||||
| 	switch ct { | ||||
| 	case "application/json": | ||||
| 		op = ws.OpText | ||||
| 	default: | ||||
| 		op = ws.OpBinary | ||||
| 	} | ||||
|  | ||||
| 	hdr := make(http.Header) | ||||
| 	if proto, ok := r.Header["Sec-WebSocket-Protocol"]; ok { | ||||
| 		for _, p := range proto { | ||||
| 			switch p { | ||||
| 			case "binary": | ||||
| 				hdr["Sec-WebSocket-Protocol"] = []string{"binary"} | ||||
| 				op = ws.OpBinary | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	payload, err := requestPayload(r) | ||||
| 	if err != nil { | ||||
| 		if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 			logger.Error(err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	upgrader := ws.HTTPUpgrader{Timeout: 5 * time.Second, | ||||
| 		Protocol: func(proto string) bool { | ||||
| 			if strings.Contains(proto, "binary") { | ||||
| 				return true | ||||
| 			} | ||||
| 			// fallback to support all protocols now | ||||
| 			return true | ||||
| 		}, | ||||
| 		Extension: func(httphead.Option) bool { | ||||
| 			// disable extensions for compatibility | ||||
| 			return false | ||||
| 		}, | ||||
| 		Header: hdr, | ||||
| 	} | ||||
|  | ||||
| 	conn, rw, _, err := upgrader.Upgrade(r, w) | ||||
| 	if err != nil { | ||||
| 		if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 			logger.Error(err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	defer func() { | ||||
| 		if err := conn.Close(); err != nil { | ||||
| 			if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 				logger.Error(err) | ||||
| 			} | ||||
| 			return | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	var request interface{} | ||||
| 	if !bytes.Equal(payload, []byte(`{}`)) { | ||||
| 		switch ct { | ||||
| 		case "application/json", "": | ||||
| 			m := json.RawMessage(payload) | ||||
| 			request = &m | ||||
| 		default: | ||||
| 			request = &raw.Frame{Data: payload} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// we always need to set content type for message | ||||
| 	if ct == "" { | ||||
| 		ct = "application/json" | ||||
| 	} | ||||
| 	req := c.NewRequest( | ||||
| 		service.Name, | ||||
| 		service.Endpoint.Name, | ||||
| 		request, | ||||
| 		client.WithContentType(ct), | ||||
| 		client.StreamingRequest(), | ||||
| 	) | ||||
|  | ||||
| 	// create custom router | ||||
| 	callOpt := client.WithRouter(router.New(service.Services)) | ||||
|  | ||||
| 	// create a new stream | ||||
| 	stream, err := c.Stream(ctx, req, callOpt) | ||||
| 	if err != nil { | ||||
| 		if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 			logger.Error(err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	if request != nil { | ||||
| 		if err = stream.Send(request); err != nil { | ||||
| 			if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 				logger.Error(err) | ||||
| 			} | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	go writeLoop(rw, stream) | ||||
|  | ||||
| 	rsp := stream.Response() | ||||
|  | ||||
| 	// receive from stream and send to client | ||||
| 	for { | ||||
| 		select { | ||||
| 		case <-ctx.Done(): | ||||
| 			return | ||||
| 		case <-stream.Context().Done(): | ||||
| 			return | ||||
| 		default: | ||||
| 			// read backend response body | ||||
| 			buf, err := rsp.Read() | ||||
| 			if err != nil { | ||||
| 				// wants to avoid import  grpc/status.Status | ||||
| 				if strings.Contains(err.Error(), "context canceled") { | ||||
| 					return | ||||
| 				} | ||||
| 				if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 					logger.Error(err) | ||||
| 				} | ||||
| 				return | ||||
| 			} | ||||
|  | ||||
| 			// write the response | ||||
| 			if err := wsutil.WriteServerMessage(rw, op, buf); err != nil { | ||||
| 				if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 					logger.Error(err) | ||||
| 				} | ||||
| 				return | ||||
| 			} | ||||
| 			if err = rw.Flush(); err != nil { | ||||
| 				if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 					logger.Error(err) | ||||
| 				} | ||||
| 				return | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // writeLoop | ||||
| func writeLoop(rw io.ReadWriter, stream client.Stream) { | ||||
| 	// close stream when done | ||||
| 	defer stream.Close() | ||||
|  | ||||
| 	for { | ||||
| 		select { | ||||
| 		case <-stream.Context().Done(): | ||||
| 			return | ||||
| 		default: | ||||
| 			buf, op, err := wsutil.ReadClientData(rw) | ||||
| 			if err != nil { | ||||
| 				if wserr, ok := err.(wsutil.ClosedError); ok { | ||||
| 					switch wserr.Code { | ||||
| 					case ws.StatusGoingAway: | ||||
| 						// this happens when user leave the page | ||||
| 						return | ||||
| 					case ws.StatusNormalClosure, ws.StatusNoStatusRcvd: | ||||
| 						// this happens when user close ws connection, or we don't get any status | ||||
| 						return | ||||
| 					} | ||||
| 				} | ||||
| 				if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 					logger.Error(err) | ||||
| 				} | ||||
| 				return | ||||
| 			} | ||||
| 			switch op { | ||||
| 			default: | ||||
| 				// not relevant | ||||
| 				continue | ||||
| 			case ws.OpText, ws.OpBinary: | ||||
| 				break | ||||
| 			} | ||||
| 			// send to backend | ||||
| 			// default to trying json | ||||
| 			// if the extracted payload isn't empty lets use it | ||||
| 			request := &raw.Frame{Data: buf} | ||||
| 			if err := stream.Send(request); err != nil { | ||||
| 				if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 					logger.Error(err) | ||||
| 				} | ||||
| 				return | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func isStream(r *http.Request, srv *api.Service) bool { | ||||
| 	// check if it's a web socket | ||||
| 	if !isWebSocket(r) { | ||||
| 		return false | ||||
| 	} | ||||
| 	// check if the endpoint supports streaming | ||||
| 	for _, service := range srv.Services { | ||||
| 		for _, ep := range service.Endpoints { | ||||
| 			// skip if it doesn't match the name | ||||
| 			if ep.Name != srv.Endpoint.Name { | ||||
| 				continue | ||||
| 			} | ||||
| 			// matched if the name | ||||
| 			if v := ep.Metadata["stream"]; v == "true" { | ||||
| 				return true | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|  | ||||
| func isWebSocket(r *http.Request) bool { | ||||
| 	contains := func(key, val string) bool { | ||||
| 		vv := strings.Split(r.Header.Get(key), ",") | ||||
| 		for _, v := range vv { | ||||
| 			if val == strings.ToLower(strings.TrimSpace(v)) { | ||||
| 				return true | ||||
| 			} | ||||
| 		} | ||||
| 		return false | ||||
| 	} | ||||
|  | ||||
| 	if contains("Connection", "upgrade") && contains("Upgrade", "websocket") { | ||||
| 		return true | ||||
| 	} | ||||
|  | ||||
| 	return false | ||||
| } | ||||
							
								
								
									
										108
									
								
								api/http/http.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								api/http/http.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,108 @@ | ||||
| // Package http provides a http server with features; acme, cors, etc | ||||
| package http | ||||
|  | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api" | ||||
| 	"github.com/asim/go-micro/v3/logger" | ||||
| ) | ||||
|  | ||||
| type httpServer struct { | ||||
| 	mux  *http.ServeMux | ||||
| 	opts api.Options | ||||
|  | ||||
| 	mtx     sync.RWMutex | ||||
| 	address string | ||||
| 	exit    chan chan error | ||||
| } | ||||
|  | ||||
| // NewGateway returns a new HTTP api gateway | ||||
| func NewGateway(opts ...api.Option) api.Gateway { | ||||
| 	var options api.Options | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	return &httpServer{ | ||||
| 		opts: options, | ||||
| 		mux:  http.NewServeMux(), | ||||
| 		exit: make(chan chan error), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (s *httpServer) Init(opts ...api.Option) error { | ||||
| 	for _, o := range opts { | ||||
| 		o(&s.opts) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *httpServer) Options() api.Options { | ||||
| 	return s.opts | ||||
| } | ||||
|  | ||||
| func (s *httpServer) Register(ep *api.Endpoint) error   { return nil } | ||||
| func (s *httpServer) Deregister(ep *api.Endpoint) error { return nil } | ||||
|  | ||||
| func (s *httpServer) Handle(path string, handler http.Handler) { | ||||
| 	s.mux.Handle(path, handler) | ||||
| } | ||||
|  | ||||
| func (s *httpServer) Serve() error { | ||||
| 	if err := s.Start(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	<-s.exit | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *httpServer) Start() error { | ||||
| 	var l net.Listener | ||||
| 	var err error | ||||
|  | ||||
| 	if s.opts.EnableACME && s.opts.ACMEProvider != nil { | ||||
| 		// should we check the address to make sure its using :443? | ||||
| 		l, err = s.opts.ACMEProvider.Listen(s.opts.ACMEHosts...) | ||||
| 	} else if s.opts.EnableTLS && s.opts.TLSConfig != nil { | ||||
| 		l, err = tls.Listen("tcp", s.opts.Address, s.opts.TLSConfig) | ||||
| 	} else { | ||||
| 		// otherwise plain listen | ||||
| 		l, err = net.Listen("tcp", s.opts.Address) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if logger.V(logger.InfoLevel, logger.DefaultLogger) { | ||||
| 		logger.Infof("HTTP API Listening on %s", l.Addr().String()) | ||||
| 	} | ||||
|  | ||||
| 	go func() { | ||||
| 		if err := http.Serve(l, s.mux); err != nil { | ||||
| 			// temporary fix | ||||
| 			//logger.Fatal(err) | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	go func() { | ||||
| 		ch := <-s.exit | ||||
| 		ch <- l.Close() | ||||
| 	}() | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *httpServer) Stop() error { | ||||
| 	ch := make(chan error) | ||||
| 	s.exit <- ch | ||||
| 	return <-ch | ||||
| } | ||||
|  | ||||
| func (s *httpServer) String() string { | ||||
| 	return "http" | ||||
| } | ||||
							
								
								
									
										62
									
								
								api/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								api/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| package api | ||||
|  | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/acme" | ||||
| 	"github.com/asim/go-micro/v3/api/resolver" | ||||
| ) | ||||
|  | ||||
| type Options struct { | ||||
| 	Address      string | ||||
| 	EnableACME   bool | ||||
| 	ACMEProvider acme.Provider | ||||
| 	EnableTLS    bool | ||||
| 	ACMEHosts    []string | ||||
| 	TLSConfig    *tls.Config | ||||
| 	Resolver     resolver.Resolver | ||||
| } | ||||
|  | ||||
| type Option func(o *Options) | ||||
|  | ||||
| func Address(a string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Address = a | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func EnableACME(b bool) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.EnableACME = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func ACMEHosts(hosts ...string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.ACMEHosts = hosts | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func ACMEProvider(p acme.Provider) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.ACMEProvider = p | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func EnableTLS(b bool) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.EnableTLS = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TLSConfig(t *tls.Config) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.TLSConfig = t | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func Resolver(r resolver.Resolver) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Resolver = r | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										44
									
								
								api/resolver/grpc/grpc.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								api/resolver/grpc/grpc.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| // Package grpc resolves a grpc service like /greeter.Say/Hello to greeter service | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api/resolver" | ||||
| ) | ||||
|  | ||||
| type Resolver struct { | ||||
| 	opts resolver.Options | ||||
| } | ||||
|  | ||||
| func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) { | ||||
| 	// parse options | ||||
| 	options := resolver.NewResolveOptions(opts...) | ||||
|  | ||||
| 	// /foo.Bar/Service | ||||
| 	if req.URL.Path == "/" { | ||||
| 		return nil, errors.New("unknown name") | ||||
| 	} | ||||
| 	// [foo.Bar, Service] | ||||
| 	parts := strings.Split(req.URL.Path[1:], "/") | ||||
| 	// [foo, Bar] | ||||
| 	name := strings.Split(parts[0], ".") | ||||
| 	// foo | ||||
| 	return &resolver.Endpoint{ | ||||
| 		Name:   strings.Join(name[:len(name)-1], "."), | ||||
| 		Host:   req.Host, | ||||
| 		Method: req.Method, | ||||
| 		Path:   req.URL.Path, | ||||
| 		Domain: options.Domain, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (r *Resolver) String() string { | ||||
| 	return "grpc" | ||||
| } | ||||
|  | ||||
| func NewResolver(opts ...resolver.Option) resolver.Resolver { | ||||
| 	return &Resolver{opts: resolver.NewOptions(opts...)} | ||||
| } | ||||
							
								
								
									
										33
									
								
								api/resolver/host/host.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								api/resolver/host/host.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| // Package host resolves using http host | ||||
| package host | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api/resolver" | ||||
| ) | ||||
|  | ||||
| type Resolver struct { | ||||
| 	opts resolver.Options | ||||
| } | ||||
|  | ||||
| func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) { | ||||
| 	// parse options | ||||
| 	options := resolver.NewResolveOptions(opts...) | ||||
|  | ||||
| 	return &resolver.Endpoint{ | ||||
| 		Name:   req.Host, | ||||
| 		Host:   req.Host, | ||||
| 		Method: req.Method, | ||||
| 		Path:   req.URL.Path, | ||||
| 		Domain: options.Domain, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (r *Resolver) String() string { | ||||
| 	return "host" | ||||
| } | ||||
|  | ||||
| func NewResolver(opts ...resolver.Option) resolver.Resolver { | ||||
| 	return &Resolver{opts: resolver.NewOptions(opts...)} | ||||
| } | ||||
							
								
								
									
										63
									
								
								api/resolver/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								api/resolver/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| package resolver | ||||
|  | ||||
| import ( | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| ) | ||||
|  | ||||
| type Options struct { | ||||
| 	Handler       string | ||||
| 	ServicePrefix string | ||||
| } | ||||
|  | ||||
| type Option func(o *Options) | ||||
|  | ||||
| // WithHandler sets the handler being used | ||||
| func WithHandler(h string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Handler = h | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithServicePrefix sets the ServicePrefix option | ||||
| func WithServicePrefix(p string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.ServicePrefix = p | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewOptions returns new initialised options | ||||
| func NewOptions(opts ...Option) Options { | ||||
| 	var options Options | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
| 	return options | ||||
| } | ||||
|  | ||||
| // ResolveOptions are used when resolving a request | ||||
| type ResolveOptions struct { | ||||
| 	Domain string | ||||
| } | ||||
|  | ||||
| // ResolveOption sets an option | ||||
| type ResolveOption func(*ResolveOptions) | ||||
|  | ||||
| // Domain sets the resolve Domain option | ||||
| func Domain(n string) ResolveOption { | ||||
| 	return func(o *ResolveOptions) { | ||||
| 		o.Domain = n | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewResolveOptions returns new initialised resolve options | ||||
| func NewResolveOptions(opts ...ResolveOption) ResolveOptions { | ||||
| 	var options ResolveOptions | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
| 	if len(options.Domain) == 0 { | ||||
| 		options.Domain = registry.DefaultDomain | ||||
| 	} | ||||
|  | ||||
| 	return options | ||||
| } | ||||
							
								
								
									
										40
									
								
								api/resolver/path/path.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								api/resolver/path/path.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| // Package path resolves using http path | ||||
| package path | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api/resolver" | ||||
| ) | ||||
|  | ||||
| type Resolver struct { | ||||
| 	opts resolver.Options | ||||
| } | ||||
|  | ||||
| func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) { | ||||
| 	// parse options | ||||
| 	options := resolver.NewResolveOptions(opts...) | ||||
|  | ||||
| 	if req.URL.Path == "/" { | ||||
| 		return nil, resolver.ErrNotFound | ||||
| 	} | ||||
|  | ||||
| 	parts := strings.Split(req.URL.Path[1:], "/") | ||||
|  | ||||
| 	return &resolver.Endpoint{ | ||||
| 		Name:   r.opts.ServicePrefix + "." + parts[0], | ||||
| 		Host:   req.Host, | ||||
| 		Method: req.Method, | ||||
| 		Path:   req.URL.Path, | ||||
| 		Domain: options.Domain, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (r *Resolver) String() string { | ||||
| 	return "path" | ||||
| } | ||||
|  | ||||
| func NewResolver(opts ...resolver.Option) resolver.Resolver { | ||||
| 	return &Resolver{opts: resolver.NewOptions(opts...)} | ||||
| } | ||||
							
								
								
									
										32
									
								
								api/resolver/resolver.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								api/resolver/resolver.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| // Package resolver resolves a http request to an endpoint | ||||
| package resolver | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"net/http" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	ErrNotFound    = errors.New("not found") | ||||
| 	ErrInvalidPath = errors.New("invalid path") | ||||
| ) | ||||
|  | ||||
| // Resolver resolves requests to endpoints | ||||
| type Resolver interface { | ||||
| 	Resolve(r *http.Request, opts ...ResolveOption) (*Endpoint, error) | ||||
| 	String() string | ||||
| } | ||||
|  | ||||
| // Endpoint is the endpoint for a http request | ||||
| type Endpoint struct { | ||||
| 	// e.g greeter | ||||
| 	Name string | ||||
| 	// HTTP Host e.g example.com | ||||
| 	Host string | ||||
| 	// HTTP Methods e.g GET, POST | ||||
| 	Method string | ||||
| 	// HTTP Path e.g /greeter. | ||||
| 	Path string | ||||
| 	// Domain endpoint exists within | ||||
| 	Domain string | ||||
| } | ||||
							
								
								
									
										53
									
								
								api/router/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								api/router/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | ||||
| package router | ||||
|  | ||||
| import ( | ||||
| 	"github.com/asim/go-micro/v3/api/resolver" | ||||
| 	"github.com/asim/go-micro/v3/api/resolver/path" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/asim/go-micro/v3/registry/mdns" | ||||
| ) | ||||
|  | ||||
| type Options struct { | ||||
| 	Handler  string | ||||
| 	Registry registry.Registry | ||||
| 	Resolver resolver.Resolver | ||||
| } | ||||
|  | ||||
| type Option func(o *Options) | ||||
|  | ||||
| func NewOptions(opts ...Option) Options { | ||||
| 	options := Options{ | ||||
| 		Handler:  "meta", | ||||
| 		Registry: mdns.NewRegistry(), | ||||
| 	} | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	if options.Resolver == nil { | ||||
| 		options.Resolver = path.NewResolver( | ||||
| 			resolver.WithHandler(options.Handler), | ||||
| 		) | ||||
| 	} | ||||
|  | ||||
| 	return options | ||||
| } | ||||
|  | ||||
| func WithHandler(h string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Handler = h | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func WithRegistry(r registry.Registry) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Registry = r | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func WithResolver(r resolver.Resolver) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Resolver = r | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										501
									
								
								api/router/registry/registry.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										501
									
								
								api/router/registry/registry.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,501 @@ | ||||
| // Package registry provides a dynamic api service router | ||||
| package registry | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
| 	"regexp" | ||||
| 	"strings" | ||||
| 	"sync" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api" | ||||
| 	"github.com/asim/go-micro/v3/api/router" | ||||
| 	"github.com/asim/go-micro/v3/logger" | ||||
| 	"github.com/asim/go-micro/v3/metadata" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/asim/go-micro/v3/registry/cache" | ||||
| 	util "github.com/asim/go-micro/v3/util/router" | ||||
| ) | ||||
|  | ||||
| // endpoint struct, that holds compiled pcre | ||||
| type endpoint struct { | ||||
| 	hostregs []*regexp.Regexp | ||||
| 	pathregs []util.Pattern | ||||
| 	pcreregs []*regexp.Regexp | ||||
| } | ||||
|  | ||||
| // router is the default router | ||||
| type registryRouter struct { | ||||
| 	exit chan bool | ||||
| 	opts router.Options | ||||
|  | ||||
| 	// registry cache | ||||
| 	rc cache.Cache | ||||
|  | ||||
| 	sync.RWMutex | ||||
| 	eps map[string]*api.Service | ||||
| 	// compiled regexp for host and path | ||||
| 	ceps map[string]*endpoint | ||||
| } | ||||
|  | ||||
| func (r *registryRouter) isClosed() bool { | ||||
| 	select { | ||||
| 	case <-r.exit: | ||||
| 		return true | ||||
| 	default: | ||||
| 		return false | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // refresh list of api services | ||||
| func (r *registryRouter) refresh() { | ||||
| 	var attempts int | ||||
|  | ||||
| 	for { | ||||
| 		services, err := r.opts.Registry.ListServices() | ||||
| 		if err != nil { | ||||
| 			attempts++ | ||||
| 			if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 				logger.Errorf("unable to list services: %v", err) | ||||
| 			} | ||||
| 			time.Sleep(time.Duration(attempts) * time.Second) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		attempts = 0 | ||||
|  | ||||
| 		// for each service, get service and store endpoints | ||||
| 		for _, s := range services { | ||||
| 			service, err := r.rc.GetService(s.Name) | ||||
| 			if err != nil { | ||||
| 				if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 					logger.Errorf("unable to get service: %v", err) | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
| 			r.store(service) | ||||
| 		} | ||||
|  | ||||
| 		// refresh list in 10 minutes... cruft | ||||
| 		// use registry watching | ||||
| 		select { | ||||
| 		case <-time.After(time.Minute * 10): | ||||
| 		case <-r.exit: | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // process watch event | ||||
| func (r *registryRouter) process(res *registry.Result) { | ||||
| 	// skip these things | ||||
| 	if res == nil || res.Service == nil { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	// get entry from cache | ||||
| 	service, err := r.rc.GetService(res.Service.Name) | ||||
| 	if err != nil { | ||||
| 		if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 			logger.Errorf("unable to get %v service: %v", res.Service.Name, err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	// update our local endpoints | ||||
| 	r.store(service) | ||||
| } | ||||
|  | ||||
| // store local endpoint cache | ||||
| func (r *registryRouter) store(services []*registry.Service) { | ||||
| 	// endpoints | ||||
| 	eps := map[string]*api.Service{} | ||||
|  | ||||
| 	// services | ||||
| 	names := map[string]bool{} | ||||
|  | ||||
| 	// create a new endpoint mapping | ||||
| 	for _, service := range services { | ||||
| 		// set names we need later | ||||
| 		names[service.Name] = true | ||||
|  | ||||
| 		// map per endpoint | ||||
| 		for _, sep := range service.Endpoints { | ||||
| 			// create a key service:endpoint_name | ||||
| 			key := fmt.Sprintf("%s.%s", service.Name, sep.Name) | ||||
| 			// decode endpoint | ||||
| 			end := api.Decode(sep.Metadata) | ||||
| 			// no endpoint or no name | ||||
| 			if end == nil || len(end.Name) == 0 { | ||||
| 				continue | ||||
| 			} | ||||
| 			// if we got nothing skip | ||||
| 			if err := api.Validate(end); err != nil { | ||||
| 				if logger.V(logger.TraceLevel, logger.DefaultLogger) { | ||||
| 					logger.Tracef("endpoint validation failed: %v", err) | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
|  | ||||
| 			// try get endpoint | ||||
| 			ep, ok := eps[key] | ||||
| 			if !ok { | ||||
| 				ep = &api.Service{Name: service.Name} | ||||
| 			} | ||||
|  | ||||
| 			// overwrite the endpoint | ||||
| 			ep.Endpoint = end | ||||
| 			// append services | ||||
| 			ep.Services = append(ep.Services, service) | ||||
| 			// store it | ||||
| 			eps[key] = ep | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	r.Lock() | ||||
| 	defer r.Unlock() | ||||
|  | ||||
| 	// delete any existing eps for services we know | ||||
| 	for key, service := range r.eps { | ||||
| 		// skip what we don't care about | ||||
| 		if !names[service.Name] { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// ok we know this thing | ||||
| 		// delete delete delete | ||||
| 		delete(r.eps, key) | ||||
| 	} | ||||
|  | ||||
| 	// now set the eps we have | ||||
| 	for name, ep := range eps { | ||||
| 		r.eps[name] = ep | ||||
| 		cep := &endpoint{} | ||||
|  | ||||
| 		for _, h := range ep.Endpoint.Host { | ||||
| 			if h == "" || h == "*" { | ||||
| 				continue | ||||
| 			} | ||||
| 			hostreg, err := regexp.CompilePOSIX(h) | ||||
| 			if err != nil { | ||||
| 				if logger.V(logger.TraceLevel, logger.DefaultLogger) { | ||||
| 					logger.Tracef("endpoint have invalid host regexp: %v", err) | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
| 			cep.hostregs = append(cep.hostregs, hostreg) | ||||
| 		} | ||||
|  | ||||
| 		for _, p := range ep.Endpoint.Path { | ||||
| 			var pcreok bool | ||||
|  | ||||
| 			if p[0] == '^' && p[len(p)-1] == '$' { | ||||
| 				pcrereg, err := regexp.CompilePOSIX(p) | ||||
| 				if err == nil { | ||||
| 					cep.pcreregs = append(cep.pcreregs, pcrereg) | ||||
| 					pcreok = true | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			rule, err := util.Parse(p) | ||||
| 			if err != nil && !pcreok { | ||||
| 				if logger.V(logger.TraceLevel, logger.DefaultLogger) { | ||||
| 					logger.Tracef("endpoint have invalid path pattern: %v", err) | ||||
| 				} | ||||
| 				continue | ||||
| 			} else if err != nil && pcreok { | ||||
| 				continue | ||||
| 			} | ||||
|  | ||||
| 			tpl := rule.Compile() | ||||
| 			pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "") | ||||
| 			if err != nil { | ||||
| 				if logger.V(logger.TraceLevel, logger.DefaultLogger) { | ||||
| 					logger.Tracef("endpoint have invalid path pattern: %v", err) | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
| 			cep.pathregs = append(cep.pathregs, pathreg) | ||||
| 		} | ||||
|  | ||||
| 		r.ceps[name] = cep | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // watch for endpoint changes | ||||
| func (r *registryRouter) watch() { | ||||
| 	var attempts int | ||||
|  | ||||
| 	for { | ||||
| 		if r.isClosed() { | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		// watch for changes | ||||
| 		w, err := r.opts.Registry.Watch() | ||||
| 		if err != nil { | ||||
| 			attempts++ | ||||
| 			if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 				logger.Errorf("error watching endpoints: %v", err) | ||||
| 			} | ||||
| 			time.Sleep(time.Duration(attempts) * time.Second) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		ch := make(chan bool) | ||||
|  | ||||
| 		go func() { | ||||
| 			select { | ||||
| 			case <-ch: | ||||
| 				w.Stop() | ||||
| 			case <-r.exit: | ||||
| 				w.Stop() | ||||
| 			} | ||||
| 		}() | ||||
|  | ||||
| 		// reset if we get here | ||||
| 		attempts = 0 | ||||
|  | ||||
| 		for { | ||||
| 			// process next event | ||||
| 			res, err := w.Next() | ||||
| 			if err != nil { | ||||
| 				if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||||
| 					logger.Errorf("error getting next endpoint: %v", err) | ||||
| 				} | ||||
| 				close(ch) | ||||
| 				break | ||||
| 			} | ||||
| 			r.process(res) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (r *registryRouter) Options() router.Options { | ||||
| 	return r.opts | ||||
| } | ||||
|  | ||||
| func (r *registryRouter) Close() error { | ||||
| 	select { | ||||
| 	case <-r.exit: | ||||
| 		return nil | ||||
| 	default: | ||||
| 		close(r.exit) | ||||
| 		r.rc.Stop() | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *registryRouter) Register(ep *api.Endpoint) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *registryRouter) Deregister(ep *api.Endpoint) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) { | ||||
| 	if r.isClosed() { | ||||
| 		return nil, errors.New("router closed") | ||||
| 	} | ||||
|  | ||||
| 	r.RLock() | ||||
| 	defer r.RUnlock() | ||||
|  | ||||
| 	var idx int | ||||
| 	if len(req.URL.Path) > 0 && req.URL.Path != "/" { | ||||
| 		idx = 1 | ||||
| 	} | ||||
| 	path := strings.Split(req.URL.Path[idx:], "/") | ||||
|  | ||||
| 	// use the first match | ||||
| 	// TODO: weighted matching | ||||
| 	for n, e := range r.eps { | ||||
| 		cep, ok := r.ceps[n] | ||||
| 		if !ok { | ||||
| 			continue | ||||
| 		} | ||||
| 		ep := e.Endpoint | ||||
| 		var mMatch, hMatch, pMatch bool | ||||
| 		// 1. try method | ||||
| 		for _, m := range ep.Method { | ||||
| 			if m == req.Method { | ||||
| 				mMatch = true | ||||
| 				break | ||||
| 			} | ||||
| 		} | ||||
| 		if !mMatch { | ||||
| 			continue | ||||
| 		} | ||||
| 		if logger.V(logger.DebugLevel, logger.DefaultLogger) { | ||||
| 			logger.Debugf("api method match %s", req.Method) | ||||
| 		} | ||||
|  | ||||
| 		// 2. try host | ||||
| 		if len(ep.Host) == 0 { | ||||
| 			hMatch = true | ||||
| 		} else { | ||||
| 			for idx, h := range ep.Host { | ||||
| 				if h == "" || h == "*" { | ||||
| 					hMatch = true | ||||
| 					break | ||||
| 				} else { | ||||
| 					if cep.hostregs[idx].MatchString(req.URL.Host) { | ||||
| 						hMatch = true | ||||
| 						break | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		if !hMatch { | ||||
| 			continue | ||||
| 		} | ||||
| 		if logger.V(logger.DebugLevel, logger.DefaultLogger) { | ||||
| 			logger.Debugf("api host match %s", req.URL.Host) | ||||
| 		} | ||||
|  | ||||
| 		// 3. try path via google.api path matching | ||||
| 		for _, pathreg := range cep.pathregs { | ||||
| 			matches, err := pathreg.Match(path, "") | ||||
| 			if err != nil { | ||||
| 				if logger.V(logger.DebugLevel, logger.DefaultLogger) { | ||||
| 					logger.Debugf("api gpath not match %s != %v", path, pathreg) | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
| 			if logger.V(logger.DebugLevel, logger.DefaultLogger) { | ||||
| 				logger.Debugf("api gpath match %s = %v", path, pathreg) | ||||
| 			} | ||||
| 			pMatch = true | ||||
| 			ctx := req.Context() | ||||
| 			md, ok := metadata.FromContext(ctx) | ||||
| 			if !ok { | ||||
| 				md = make(metadata.Metadata) | ||||
| 			} | ||||
| 			for k, v := range matches { | ||||
| 				md[fmt.Sprintf("x-api-field-%s", k)] = v | ||||
| 			} | ||||
| 			md["x-api-body"] = ep.Body | ||||
| 			*req = *req.Clone(metadata.NewContext(ctx, md)) | ||||
| 			break | ||||
| 		} | ||||
|  | ||||
| 		if !pMatch { | ||||
| 			// 4. try path via pcre path matching | ||||
| 			for _, pathreg := range cep.pcreregs { | ||||
| 				if !pathreg.MatchString(req.URL.Path) { | ||||
| 					if logger.V(logger.DebugLevel, logger.DefaultLogger) { | ||||
| 						logger.Debugf("api pcre path not match %s != %v", path, pathreg) | ||||
| 					} | ||||
| 					continue | ||||
| 				} | ||||
| 				if logger.V(logger.DebugLevel, logger.DefaultLogger) { | ||||
| 					logger.Debugf("api pcre path match %s != %v", path, pathreg) | ||||
| 				} | ||||
| 				pMatch = true | ||||
| 				break | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		if !pMatch { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// TODO: Percentage traffic | ||||
| 		// we got here, so its a match | ||||
| 		return e, nil | ||||
| 	} | ||||
|  | ||||
| 	// no match | ||||
| 	return nil, errors.New("not found") | ||||
| } | ||||
|  | ||||
| func (r *registryRouter) Route(req *http.Request) (*api.Service, error) { | ||||
| 	if r.isClosed() { | ||||
| 		return nil, errors.New("router closed") | ||||
| 	} | ||||
|  | ||||
| 	// try get an endpoint | ||||
| 	ep, err := r.Endpoint(req) | ||||
| 	if err == nil { | ||||
| 		return ep, nil | ||||
| 	} | ||||
|  | ||||
| 	// error not nil | ||||
| 	// ignore that shit | ||||
| 	// TODO: don't ignore that shit | ||||
|  | ||||
| 	// get the service name | ||||
| 	rp, err := r.opts.Resolver.Resolve(req) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	// service name | ||||
| 	name := rp.Name | ||||
|  | ||||
| 	// get service | ||||
| 	services, err := r.rc.GetService(name, registry.GetDomain(rp.Domain)) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	// only use endpoint matching when the meta handler is set aka api.Default | ||||
| 	switch r.opts.Handler { | ||||
| 	// rpc handlers | ||||
| 	case "meta", "api", "rpc": | ||||
| 		handler := r.opts.Handler | ||||
|  | ||||
| 		// set default handler to api | ||||
| 		if r.opts.Handler == "meta" { | ||||
| 			handler = "rpc" | ||||
| 		} | ||||
|  | ||||
| 		// construct api service | ||||
| 		return &api.Service{ | ||||
| 			Name: name, | ||||
| 			Endpoint: &api.Endpoint{ | ||||
| 				Name:    rp.Method, | ||||
| 				Handler: handler, | ||||
| 			}, | ||||
| 			Services: services, | ||||
| 		}, nil | ||||
| 	// http handler | ||||
| 	case "http", "proxy", "web": | ||||
| 		// construct api service | ||||
| 		return &api.Service{ | ||||
| 			Name: name, | ||||
| 			Endpoint: &api.Endpoint{ | ||||
| 				Name:    req.URL.String(), | ||||
| 				Handler: r.opts.Handler, | ||||
| 				Host:    []string{req.Host}, | ||||
| 				Method:  []string{req.Method}, | ||||
| 				Path:    []string{req.URL.Path}, | ||||
| 			}, | ||||
| 			Services: services, | ||||
| 		}, nil | ||||
| 	} | ||||
|  | ||||
| 	return nil, errors.New("unknown handler") | ||||
| } | ||||
|  | ||||
| func newRouter(opts ...router.Option) *registryRouter { | ||||
| 	options := router.NewOptions(opts...) | ||||
| 	r := ®istryRouter{ | ||||
| 		exit: make(chan bool), | ||||
| 		opts: options, | ||||
| 		rc:   cache.New(options.Registry), | ||||
| 		eps:  make(map[string]*api.Service), | ||||
| 		ceps: make(map[string]*endpoint), | ||||
| 	} | ||||
| 	go r.watch() | ||||
| 	go r.refresh() | ||||
| 	return r | ||||
| } | ||||
|  | ||||
| // NewRouter returns the default router | ||||
| func NewRouter(opts ...router.Option) router.Router { | ||||
| 	return newRouter(opts...) | ||||
| } | ||||
							
								
								
									
										34
									
								
								api/router/registry/registry_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								api/router/registry/registry_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| package registry | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestStoreRegex(t *testing.T) { | ||||
| 	router := newRouter() | ||||
| 	router.store([]*registry.Service{ | ||||
| 		{ | ||||
| 			Name:    "Foobar", | ||||
| 			Version: "latest", | ||||
| 			Endpoints: []*registry.Endpoint{ | ||||
| 				{ | ||||
| 					Name: "foo", | ||||
| 					Metadata: map[string]string{ | ||||
| 						"endpoint":    "FooEndpoint", | ||||
| 						"description": "Some description", | ||||
| 						"method":      "POST", | ||||
| 						"path":        "^/foo/$", | ||||
| 						"handler":     "rpc", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Metadata: map[string]string{}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	) | ||||
|  | ||||
| 	assert.Len(t, router.ceps["Foobar.foo"].pcreregs, 1) | ||||
| } | ||||
							
								
								
									
										24
									
								
								api/router/router.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								api/router/router.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| // Package router provides api service routing | ||||
| package router | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api" | ||||
| ) | ||||
|  | ||||
| // Router is used to determine an endpoint for a request | ||||
| type Router interface { | ||||
| 	// Returns options | ||||
| 	Options() Options | ||||
| 	// Stop the router | ||||
| 	Close() error | ||||
| 	// Endpoint returns an api.Service endpoint or an error if it does not exist | ||||
| 	Endpoint(r *http.Request) (*api.Service, error) | ||||
| 	// Register endpoint in router | ||||
| 	Register(ep *api.Endpoint) error | ||||
| 	// Deregister endpoint from router | ||||
| 	Deregister(ep *api.Endpoint) error | ||||
| 	// Route returns an api.Service route | ||||
| 	Route(r *http.Request) (*api.Service, error) | ||||
| } | ||||
							
								
								
									
										128
									
								
								api/router/router_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								api/router/router_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,128 @@ | ||||
| package router_test | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/api/handler" | ||||
| 	"github.com/asim/go-micro/v3/api/handler/rpc" | ||||
| 	"github.com/asim/go-micro/v3/api/router" | ||||
| 	rregistry "github.com/asim/go-micro/v3/api/router/registry" | ||||
| 	"github.com/asim/go-micro/v3/client" | ||||
| 	gcli "github.com/asim/go-micro/v3/client/grpc" | ||||
| 	rmemory "github.com/asim/go-micro/v3/registry/memory" | ||||
| 	rt "github.com/asim/go-micro/v3/router" | ||||
| 	regRouter "github.com/asim/go-micro/v3/router/registry" | ||||
| 	"github.com/asim/go-micro/v3/server" | ||||
| 	gsrv "github.com/asim/go-micro/v3/server/grpc" | ||||
| 	pb "github.com/asim/go-micro/v3/server/grpc/proto" | ||||
| ) | ||||
|  | ||||
| // server is used to implement helloworld.GreeterServer. | ||||
| type testServer struct { | ||||
| 	msgCount int | ||||
| } | ||||
|  | ||||
| // TestHello implements helloworld.GreeterServer | ||||
| func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error { | ||||
| 	rsp.Msg = "Hello " + req.Uuid | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // TestHello implements helloworld.GreeterServer | ||||
| func (s *testServer) CallPcre(ctx context.Context, req *pb.Request, rsp *pb.Response) error { | ||||
| 	rsp.Msg = "Hello " + req.Uuid | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // TestHello implements helloworld.GreeterServer | ||||
| func (s *testServer) CallPcreInvalid(ctx context.Context, req *pb.Request, rsp *pb.Response) error { | ||||
| 	rsp.Msg = "Hello " + req.Uuid | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func initial(t *testing.T) (server.Server, client.Client) { | ||||
| 	r := rmemory.NewRegistry() | ||||
|  | ||||
| 	// create a new client | ||||
| 	s := gsrv.NewServer( | ||||
| 		server.Name("foo"), | ||||
| 		server.Registry(r), | ||||
| 	) | ||||
|  | ||||
| 	rtr := regRouter.NewRouter( | ||||
| 		rt.Registry(r), | ||||
| 	) | ||||
|  | ||||
| 	// create a new server | ||||
| 	c := gcli.NewClient( | ||||
| 		client.Router(rtr), | ||||
| 	) | ||||
|  | ||||
| 	h := &testServer{} | ||||
| 	pb.RegisterTestHandler(s, h) | ||||
|  | ||||
| 	if err := s.Start(); err != nil { | ||||
| 		t.Fatalf("failed to start: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	return s, c | ||||
| } | ||||
|  | ||||
| func check(t *testing.T, addr string, path string, expected string) { | ||||
| 	req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), nil) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Failed to created http.Request: %v", err) | ||||
| 	} | ||||
| 	req.Header.Set("Content-Type", "application/json") | ||||
| 	rsp, err := (&http.Client{}).Do(req) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Failed to created http.Request: %v", err) | ||||
| 	} | ||||
| 	defer rsp.Body.Close() | ||||
|  | ||||
| 	buf, err := ioutil.ReadAll(rsp.Body) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	jsonMsg := expected | ||||
| 	if string(buf) != jsonMsg { | ||||
| 		t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestRouterRegistryPcre(t *testing.T) { | ||||
| 	s, c := initial(t) | ||||
| 	defer s.Stop() | ||||
|  | ||||
| 	router := rregistry.NewRouter( | ||||
| 		router.WithHandler(rpc.Handler), | ||||
| 		router.WithRegistry(s.Options().Registry), | ||||
| 	) | ||||
| 	hrpc := rpc.NewHandler( | ||||
| 		handler.WithClient(c), | ||||
| 		handler.WithRouter(router), | ||||
| 	) | ||||
| 	hsrv := &http.Server{ | ||||
| 		Handler:        hrpc, | ||||
| 		Addr:           "127.0.0.1:6543", | ||||
| 		WriteTimeout:   15 * time.Second, | ||||
| 		ReadTimeout:    15 * time.Second, | ||||
| 		IdleTimeout:    20 * time.Second, | ||||
| 		MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb | ||||
| 	} | ||||
|  | ||||
| 	go func() { | ||||
| 		log.Println(hsrv.ListenAndServe()) | ||||
| 	}() | ||||
|  | ||||
| 	defer hsrv.Close() | ||||
| 	time.Sleep(1 * time.Second) | ||||
| 	check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`) | ||||
| } | ||||
							
								
								
									
										21
									
								
								api/util.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								api/util.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| package api | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| func strip(s string) string { | ||||
| 	return strings.TrimSpace(s) | ||||
| } | ||||
|  | ||||
| func slice(s string) []string { | ||||
| 	var sl []string | ||||
|  | ||||
| 	for _, p := range strings.Split(s, ",") { | ||||
| 		if str := strip(p); len(str) > 0 { | ||||
| 			sl = append(sl, strip(p)) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return sl | ||||
| } | ||||
							
								
								
									
										116
									
								
								auth/auth.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								auth/auth.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,116 @@ | ||||
| // Package auth provides authentication and authorization capability | ||||
| package auth | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	// ScopePublic is the scope applied to a rule to allow access to the public | ||||
| 	ScopePublic = "" | ||||
| 	// ScopeAccount is the scope applied to a rule to limit to users with any valid account | ||||
| 	ScopeAccount = "*" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	// ErrInvalidToken is when the token provided is not valid | ||||
| 	ErrInvalidToken = errors.New("invalid token provided") | ||||
| 	// ErrForbidden is when a user does not have the necessary scope to access a resource | ||||
| 	ErrForbidden = errors.New("resource forbidden") | ||||
| ) | ||||
|  | ||||
| // Auth provides authentication and authorization | ||||
| type Auth interface { | ||||
| 	// Init the auth | ||||
| 	Init(opts ...Option) | ||||
| 	// Options set for auth | ||||
| 	Options() Options | ||||
| 	// Generate a new account | ||||
| 	Generate(id string, opts ...GenerateOption) (*Account, error) | ||||
| 	// Verify an account has access to a resource using the rules | ||||
| 	Verify(acc *Account, res *Resource, opts ...VerifyOption) error | ||||
| 	// Inspect a token | ||||
| 	Inspect(token string) (*Account, error) | ||||
| 	// Token generated using refresh token or credentials | ||||
| 	Token(opts ...TokenOption) (*Token, error) | ||||
| 	// Grant access to a resource | ||||
| 	Grant(rule *Rule) error | ||||
| 	// Revoke access to a resource | ||||
| 	Revoke(rule *Rule) error | ||||
| 	// Rules returns all the rules used to verify requests | ||||
| 	Rules(...RulesOption) ([]*Rule, error) | ||||
| 	// String returns the name of the implementation | ||||
| 	String() string | ||||
| } | ||||
|  | ||||
| // Account provided by an auth provider | ||||
| type Account struct { | ||||
| 	// ID of the account e.g. UUID. Should not change | ||||
| 	ID string `json:"id"` | ||||
| 	// Type of the account, e.g. service | ||||
| 	Type string `json:"type"` | ||||
| 	// Issuer of the account | ||||
| 	Issuer string `json:"issuer"` | ||||
| 	// Any other associated metadata | ||||
| 	Metadata map[string]string `json:"metadata"` | ||||
| 	// Scopes the account has access to | ||||
| 	Scopes []string `json:"scopes"` | ||||
| 	// Secret for the account, e.g. the password | ||||
| 	Secret string `json:"secret"` | ||||
| 	// Name of the account. User friendly name that might change e.g. a username or email | ||||
| 	Name string `json:"name"` | ||||
| } | ||||
|  | ||||
| // Token can be short or long lived | ||||
| type Token struct { | ||||
| 	// The token to be used for accessing resources | ||||
| 	AccessToken string `json:"access_token"` | ||||
| 	// RefreshToken to be used to generate a new token | ||||
| 	RefreshToken string `json:"refresh_token"` | ||||
| 	// Time of token creation | ||||
| 	Created time.Time `json:"created"` | ||||
| 	// Time of token expiry | ||||
| 	Expiry time.Time `json:"expiry"` | ||||
| } | ||||
|  | ||||
| // Expired returns a boolean indicating if the token needs to be refreshed | ||||
| func (t *Token) Expired() bool { | ||||
| 	return t.Expiry.Unix() < time.Now().Unix() | ||||
| } | ||||
|  | ||||
| // Resource is an entity such as a user or | ||||
| type Resource struct { | ||||
| 	// Name of the resource, e.g. go.micro.service.notes | ||||
| 	Name string `json:"name"` | ||||
| 	// Type of resource, e.g. service | ||||
| 	Type string `json:"type"` | ||||
| 	// Endpoint resource e.g NotesService.Create | ||||
| 	Endpoint string `json:"endpoint"` | ||||
| } | ||||
|  | ||||
| // Access defines the type of access a rule grants | ||||
| type Access int | ||||
|  | ||||
| const ( | ||||
| 	// AccessGranted to a resource | ||||
| 	AccessGranted Access = iota | ||||
| 	// AccessDenied to a resource | ||||
| 	AccessDenied | ||||
| ) | ||||
|  | ||||
| // Rule is used to verify access to a resource | ||||
| type Rule struct { | ||||
| 	// ID of the rule, e.g. "public" | ||||
| 	ID string | ||||
| 	// Scope the rule requires, a blank scope indicates open to the public and * indicates the rule | ||||
| 	// applies to any valid account | ||||
| 	Scope string | ||||
| 	// Resource the rule applies to | ||||
| 	Resource *Resource | ||||
| 	// Access determines if the rule grants or denies access to the resource | ||||
| 	Access Access | ||||
| 	// Priority the rule should take when verifying a request, the higher the value the sooner the | ||||
| 	// rule will be applied | ||||
| 	Priority int32 | ||||
| } | ||||
							
								
								
									
										155
									
								
								auth/jwt/jwt.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								auth/jwt/jwt.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,155 @@ | ||||
| // Package jwt is a jwt implementation of the auth interface | ||||
| package jwt | ||||
|  | ||||
| import ( | ||||
| 	"sync" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/auth" | ||||
| 	"github.com/asim/go-micro/v3/util/token" | ||||
| 	"github.com/asim/go-micro/v3/util/token/jwt" | ||||
| ) | ||||
|  | ||||
| // NewAuth returns a new instance of the Auth service | ||||
| func NewAuth(opts ...auth.Option) auth.Auth { | ||||
| 	j := new(jwtAuth) | ||||
| 	j.Init(opts...) | ||||
| 	return j | ||||
| } | ||||
|  | ||||
| type jwtAuth struct { | ||||
| 	options auth.Options | ||||
| 	token   token.Provider | ||||
| 	rules   []*auth.Rule | ||||
|  | ||||
| 	sync.Mutex | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) String() string { | ||||
| 	return "jwt" | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Init(opts ...auth.Option) { | ||||
| 	j.Lock() | ||||
| 	defer j.Unlock() | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&j.options) | ||||
| 	} | ||||
|  | ||||
| 	j.token = jwt.NewTokenProvider( | ||||
| 		token.WithPrivateKey(j.options.PrivateKey), | ||||
| 		token.WithPublicKey(j.options.PublicKey), | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Options() auth.Options { | ||||
| 	j.Lock() | ||||
| 	defer j.Unlock() | ||||
| 	return j.options | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Generate(id string, opts ...auth.GenerateOption) (*auth.Account, error) { | ||||
| 	options := auth.NewGenerateOptions(opts...) | ||||
| 	if len(options.Issuer) == 0 { | ||||
| 		options.Issuer = j.Options().Issuer | ||||
| 	} | ||||
| 	name := options.Name | ||||
| 	if name == "" { | ||||
| 		name = id | ||||
| 	} | ||||
| 	account := &auth.Account{ | ||||
| 		ID:       id, | ||||
| 		Type:     options.Type, | ||||
| 		Scopes:   options.Scopes, | ||||
| 		Metadata: options.Metadata, | ||||
| 		Issuer:   options.Issuer, | ||||
| 		Name:     name, | ||||
| 	} | ||||
|  | ||||
| 	// generate a JWT secret which can be provided to the Token() method | ||||
| 	// and exchanged for an access token | ||||
| 	secret, err := j.token.Generate(account, token.WithExpiry(time.Hour*24*365)) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	account.Secret = secret.Token | ||||
|  | ||||
| 	// return the account | ||||
| 	return account, nil | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Grant(rule *auth.Rule) error { | ||||
| 	j.Lock() | ||||
| 	defer j.Unlock() | ||||
| 	j.rules = append(j.rules, rule) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Revoke(rule *auth.Rule) error { | ||||
| 	j.Lock() | ||||
| 	defer j.Unlock() | ||||
|  | ||||
| 	rules := []*auth.Rule{} | ||||
| 	for _, r := range j.rules { | ||||
| 		if r.ID != rule.ID { | ||||
| 			rules = append(rules, r) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	j.rules = rules | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyOption) error { | ||||
| 	j.Lock() | ||||
| 	defer j.Unlock() | ||||
|  | ||||
| 	var options auth.VerifyOptions | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	return auth.VerifyAccess(j.rules, acc, res) | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Rules(opts ...auth.RulesOption) ([]*auth.Rule, error) { | ||||
| 	j.Lock() | ||||
| 	defer j.Unlock() | ||||
| 	return j.rules, nil | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Inspect(token string) (*auth.Account, error) { | ||||
| 	return j.token.Inspect(token) | ||||
| } | ||||
|  | ||||
| func (j *jwtAuth) Token(opts ...auth.TokenOption) (*auth.Token, error) { | ||||
| 	options := auth.NewTokenOptions(opts...) | ||||
|  | ||||
| 	secret := options.RefreshToken | ||||
| 	if len(options.Secret) > 0 { | ||||
| 		secret = options.Secret | ||||
| 	} | ||||
|  | ||||
| 	account, err := j.token.Inspect(secret) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	access, err := j.token.Generate(account, token.WithExpiry(options.Expiry)) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	refresh, err := j.token.Generate(account, token.WithExpiry(options.Expiry+time.Hour)) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return &auth.Token{ | ||||
| 		Created:      access.Created, | ||||
| 		Expiry:       access.Expiry, | ||||
| 		AccessToken:  access.Token, | ||||
| 		RefreshToken: refresh.Token, | ||||
| 	}, nil | ||||
| } | ||||
							
								
								
									
										85
									
								
								auth/noop/noop.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								auth/noop/noop.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,85 @@ | ||||
| package noop | ||||
|  | ||||
| import ( | ||||
| 	"github.com/asim/go-micro/v3/auth" | ||||
| 	"github.com/google/uuid" | ||||
| ) | ||||
|  | ||||
| func NewAuth(opts ...auth.Option) auth.Auth { | ||||
| 	var options auth.Options | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	return &noop{ | ||||
| 		opts: options, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type noop struct { | ||||
| 	opts auth.Options | ||||
| } | ||||
|  | ||||
| // String returns the name of the implementation | ||||
| func (n *noop) String() string { | ||||
| 	return "noop" | ||||
| } | ||||
|  | ||||
| // Init the auth | ||||
| func (n *noop) Init(opts ...auth.Option) { | ||||
| 	for _, o := range opts { | ||||
| 		o(&n.opts) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Options set for auth | ||||
| func (n *noop) Options() auth.Options { | ||||
| 	return n.opts | ||||
| } | ||||
|  | ||||
| // Generate a new account | ||||
| func (n *noop) Generate(id string, opts ...auth.GenerateOption) (*auth.Account, error) { | ||||
| 	options := auth.NewGenerateOptions(opts...) | ||||
| 	name := options.Name | ||||
| 	if name == "" { | ||||
| 		name = id | ||||
| 	} | ||||
| 	return &auth.Account{ | ||||
| 		ID:       id, | ||||
| 		Secret:   options.Secret, | ||||
| 		Metadata: options.Metadata, | ||||
| 		Scopes:   options.Scopes, | ||||
| 		Issuer:   n.Options().Issuer, | ||||
| 		Name:     name, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| // Grant access to a resource | ||||
| func (n *noop) Grant(rule *auth.Rule) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Revoke access to a resource | ||||
| func (n *noop) Revoke(rule *auth.Rule) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Rules used to verify requests | ||||
| func (n *noop) Rules(opts ...auth.RulesOption) ([]*auth.Rule, error) { | ||||
| 	return []*auth.Rule{}, nil | ||||
| } | ||||
|  | ||||
| // Verify an account has access to a resource | ||||
| func (n *noop) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyOption) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Inspect a token | ||||
| func (n *noop) Inspect(token string) (*auth.Account, error) { | ||||
| 	return &auth.Account{ID: uuid.New().String(), Issuer: n.Options().Issuer}, nil | ||||
| } | ||||
|  | ||||
| // Token generation using an account id and secret | ||||
| func (n *noop) Token(opts ...auth.TokenOption) (*auth.Token, error) { | ||||
| 	return &auth.Token{}, nil | ||||
| } | ||||
							
								
								
									
										268
									
								
								auth/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										268
									
								
								auth/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,268 @@ | ||||
| package auth | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/store" | ||||
| ) | ||||
|  | ||||
| func NewOptions(opts ...Option) Options { | ||||
| 	var options Options | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
| 	return options | ||||
| } | ||||
|  | ||||
| type Options struct { | ||||
| 	// Issuer of the service's account | ||||
| 	Issuer string | ||||
| 	// ID is the services auth ID | ||||
| 	ID string | ||||
| 	// Secret is used to authenticate the service | ||||
| 	Secret string | ||||
| 	// Token is the services token used to authenticate itself | ||||
| 	Token *Token | ||||
| 	// PublicKey for decoding JWTs | ||||
| 	PublicKey string | ||||
| 	// PrivateKey for encoding JWTs | ||||
| 	PrivateKey string | ||||
| 	// LoginURL is the relative url path where a user can login | ||||
| 	LoginURL string | ||||
| 	// Store to back auth | ||||
| 	Store store.Store | ||||
| 	// Addrs sets the addresses of auth | ||||
| 	Addrs []string | ||||
| 	// Context to store other options | ||||
| 	Context context.Context | ||||
| } | ||||
|  | ||||
| type Option func(o *Options) | ||||
|  | ||||
| // Addrs is the auth addresses to use | ||||
| func Addrs(addrs ...string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Addrs = addrs | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Issuer of the services account | ||||
| func Issuer(i string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Issuer = i | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Store to back auth | ||||
| func Store(s store.Store) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Store = s | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // PublicKey is the JWT public key | ||||
| func PublicKey(key string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.PublicKey = key | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // PrivateKey is the JWT private key | ||||
| func PrivateKey(key string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.PrivateKey = key | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Credentials sets the auth credentials | ||||
| func Credentials(id, secret string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.ID = id | ||||
| 		o.Secret = secret | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // ClientToken sets the auth token to use when making requests | ||||
| func ClientToken(token *Token) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Token = token | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // LoginURL sets the auth LoginURL | ||||
| func LoginURL(url string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.LoginURL = url | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type GenerateOptions struct { | ||||
| 	// Metadata associated with the account | ||||
| 	Metadata map[string]string | ||||
| 	// Scopes the account has access too | ||||
| 	Scopes []string | ||||
| 	// Provider of the account, e.g. oauth | ||||
| 	Provider string | ||||
| 	// Type of the account, e.g. user | ||||
| 	Type string | ||||
| 	// Secret used to authenticate the account | ||||
| 	Secret string | ||||
| 	// Issuer of the account, e.g. micro | ||||
| 	Issuer string | ||||
| 	// Name of the acouunt e.g. an email or username | ||||
| 	Name string | ||||
| } | ||||
|  | ||||
| type GenerateOption func(o *GenerateOptions) | ||||
|  | ||||
| // WithSecret for the generated account | ||||
| func WithSecret(s string) GenerateOption { | ||||
| 	return func(o *GenerateOptions) { | ||||
| 		o.Secret = s | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithType for the generated account | ||||
| func WithType(t string) GenerateOption { | ||||
| 	return func(o *GenerateOptions) { | ||||
| 		o.Type = t | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithMetadata for the generated account | ||||
| func WithMetadata(md map[string]string) GenerateOption { | ||||
| 	return func(o *GenerateOptions) { | ||||
| 		o.Metadata = md | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithProvider for the generated account | ||||
| func WithProvider(p string) GenerateOption { | ||||
| 	return func(o *GenerateOptions) { | ||||
| 		o.Provider = p | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithScopes for the generated account | ||||
| func WithScopes(s ...string) GenerateOption { | ||||
| 	return func(o *GenerateOptions) { | ||||
| 		o.Scopes = s | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithIssuer for the generated account | ||||
| func WithIssuer(i string) GenerateOption { | ||||
| 	return func(o *GenerateOptions) { | ||||
| 		o.Issuer = i | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithName for the generated account | ||||
| func WithName(n string) GenerateOption { | ||||
| 	return func(o *GenerateOptions) { | ||||
| 		o.Name = n | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewGenerateOptions from a slice of options | ||||
| func NewGenerateOptions(opts ...GenerateOption) GenerateOptions { | ||||
| 	var options GenerateOptions | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
| 	return options | ||||
| } | ||||
|  | ||||
| type TokenOptions struct { | ||||
| 	// ID for the account | ||||
| 	ID string | ||||
| 	// Secret for the account | ||||
| 	Secret string | ||||
| 	// RefreshToken is used to refesh a token | ||||
| 	RefreshToken string | ||||
| 	// Expiry is the time the token should live for | ||||
| 	Expiry time.Duration | ||||
| 	// Issuer of the account | ||||
| 	Issuer string | ||||
| } | ||||
|  | ||||
| type TokenOption func(o *TokenOptions) | ||||
|  | ||||
| // WithExpiry for the token | ||||
| func WithExpiry(ex time.Duration) TokenOption { | ||||
| 	return func(o *TokenOptions) { | ||||
| 		o.Expiry = ex | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func WithCredentials(id, secret string) TokenOption { | ||||
| 	return func(o *TokenOptions) { | ||||
| 		o.ID = id | ||||
| 		o.Secret = secret | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func WithToken(rt string) TokenOption { | ||||
| 	return func(o *TokenOptions) { | ||||
| 		o.RefreshToken = rt | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func WithTokenIssuer(iss string) TokenOption { | ||||
| 	return func(o *TokenOptions) { | ||||
| 		o.Issuer = iss | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewTokenOptions from a slice of options | ||||
| func NewTokenOptions(opts ...TokenOption) TokenOptions { | ||||
| 	var options TokenOptions | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	// set defualt expiry of token | ||||
| 	if options.Expiry == 0 { | ||||
| 		options.Expiry = time.Minute | ||||
| 	} | ||||
|  | ||||
| 	return options | ||||
| } | ||||
|  | ||||
| type VerifyOptions struct { | ||||
| 	Context   context.Context | ||||
| 	Namespace string | ||||
| } | ||||
|  | ||||
| type VerifyOption func(o *VerifyOptions) | ||||
|  | ||||
| func VerifyContext(ctx context.Context) VerifyOption { | ||||
| 	return func(o *VerifyOptions) { | ||||
| 		o.Context = ctx | ||||
| 	} | ||||
| } | ||||
| func VerifyNamespace(ns string) VerifyOption { | ||||
| 	return func(o *VerifyOptions) { | ||||
| 		o.Namespace = ns | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type RulesOptions struct { | ||||
| 	Context   context.Context | ||||
| 	Namespace string | ||||
| } | ||||
|  | ||||
| type RulesOption func(o *RulesOptions) | ||||
|  | ||||
| func RulesContext(ctx context.Context) RulesOption { | ||||
| 	return func(o *RulesOptions) { | ||||
| 		o.Context = ctx | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func RulesNamespace(ns string) RulesOption { | ||||
| 	return func(o *RulesOptions) { | ||||
| 		o.Namespace = ns | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										91
									
								
								auth/rules.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								auth/rules.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,91 @@ | ||||
| package auth | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"sort" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| // VerifyAccess an account has access to a resource using the rules provided. If the account does not have | ||||
| // access an error will be returned. If there are no rules provided which match the resource, an error | ||||
| // will be returned | ||||
| func VerifyAccess(rules []*Rule, acc *Account, res *Resource) error { | ||||
| 	// the rule is only to be applied if the type matches the resource or is catch-all (*) | ||||
| 	validTypes := []string{"*", res.Type} | ||||
|  | ||||
| 	// the rule is only to be applied if the name matches the resource or is catch-all (*) | ||||
| 	validNames := []string{"*", res.Name} | ||||
|  | ||||
| 	// rules can have wildcard excludes on endpoints since this can also be a path for web services, | ||||
| 	// e.g. /foo/* would include /foo/bar. We also want to check for wildcards and the exact endpoint | ||||
| 	validEndpoints := []string{"*", res.Endpoint} | ||||
| 	if comps := strings.Split(res.Endpoint, "/"); len(comps) > 1 { | ||||
| 		for i := 1; i < len(comps)+1; i++ { | ||||
| 			wildcard := fmt.Sprintf("%v/*", strings.Join(comps[0:i], "/")) | ||||
| 			validEndpoints = append(validEndpoints, wildcard) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// filter the rules to the ones which match the criteria above | ||||
| 	filteredRules := make([]*Rule, 0) | ||||
| 	for _, rule := range rules { | ||||
| 		if !include(validTypes, rule.Resource.Type) { | ||||
| 			continue | ||||
| 		} | ||||
| 		if !include(validNames, rule.Resource.Name) { | ||||
| 			continue | ||||
| 		} | ||||
| 		if !include(validEndpoints, rule.Resource.Endpoint) { | ||||
| 			continue | ||||
| 		} | ||||
| 		filteredRules = append(filteredRules, rule) | ||||
| 	} | ||||
|  | ||||
| 	// sort the filtered rules by priority, highest to lowest | ||||
| 	sort.SliceStable(filteredRules, func(i, j int) bool { | ||||
| 		return filteredRules[i].Priority > filteredRules[j].Priority | ||||
| 	}) | ||||
|  | ||||
| 	// loop through the rules and check for a rule which applies to this account | ||||
| 	for _, rule := range filteredRules { | ||||
| 		// a blank scope indicates the rule applies to everyone, even nil accounts | ||||
| 		if rule.Scope == ScopePublic && rule.Access == AccessDenied { | ||||
| 			return ErrForbidden | ||||
| 		} else if rule.Scope == ScopePublic && rule.Access == AccessGranted { | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		// all further checks require an account | ||||
| 		if acc == nil { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// this rule applies to any account | ||||
| 		if rule.Scope == ScopeAccount && rule.Access == AccessDenied { | ||||
| 			return ErrForbidden | ||||
| 		} else if rule.Scope == ScopeAccount && rule.Access == AccessGranted { | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		// if the account has the necessary scope | ||||
| 		if include(acc.Scopes, rule.Scope) && rule.Access == AccessDenied { | ||||
| 			return ErrForbidden | ||||
| 		} else if include(acc.Scopes, rule.Scope) && rule.Access == AccessGranted { | ||||
| 			return nil | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// if no rules matched then return forbidden | ||||
| 	return ErrForbidden | ||||
| } | ||||
|  | ||||
| // include is a helper function which checks to see if the slice contains the value. includes is | ||||
| // not case sensitive. | ||||
| func include(slice []string, val string) bool { | ||||
| 	for _, s := range slice { | ||||
| 		if strings.ToLower(s) == strings.ToLower(val) { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
							
								
								
									
										288
									
								
								auth/rules_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										288
									
								
								auth/rules_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,288 @@ | ||||
| package auth | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestVerify(t *testing.T) { | ||||
| 	srvResource := &Resource{ | ||||
| 		Type:     "service", | ||||
| 		Name:     "go.micro.service.foo", | ||||
| 		Endpoint: "Foo.Bar", | ||||
| 	} | ||||
|  | ||||
| 	webResource := &Resource{ | ||||
| 		Type:     "service", | ||||
| 		Name:     "go.micro.web.foo", | ||||
| 		Endpoint: "/foo/bar", | ||||
| 	} | ||||
|  | ||||
| 	catchallResource := &Resource{ | ||||
| 		Type:     "*", | ||||
| 		Name:     "*", | ||||
| 		Endpoint: "*", | ||||
| 	} | ||||
|  | ||||
| 	tt := []struct { | ||||
| 		Name     string | ||||
| 		Rules    []*Rule | ||||
| 		Account  *Account | ||||
| 		Resource *Resource | ||||
| 		Error    error | ||||
| 	}{ | ||||
| 		{ | ||||
| 			Name:     "NoRules", | ||||
| 			Rules:    []*Rule{}, | ||||
| 			Account:  nil, | ||||
| 			Resource: srvResource, | ||||
| 			Error:    ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallPublicAccount", | ||||
| 			Account:  &Account{}, | ||||
| 			Resource: srvResource, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "", | ||||
| 					Resource: catchallResource, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallPublicNoAccount", | ||||
| 			Resource: srvResource, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "", | ||||
| 					Resource: catchallResource, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallPrivateAccount", | ||||
| 			Account:  &Account{}, | ||||
| 			Resource: srvResource, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallPrivateNoAccount", | ||||
| 			Resource: srvResource, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallServiceRuleMatch", | ||||
| 			Resource: srvResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope: "*", | ||||
| 					Resource: &Resource{ | ||||
| 						Type:     srvResource.Type, | ||||
| 						Name:     srvResource.Name, | ||||
| 						Endpoint: "*", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallServiceRuleNoMatch", | ||||
| 			Resource: srvResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope: "*", | ||||
| 					Resource: &Resource{ | ||||
| 						Type:     srvResource.Type, | ||||
| 						Name:     "wrongname", | ||||
| 						Endpoint: "*", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "ExactRuleValidScope", | ||||
| 			Resource: srvResource, | ||||
| 			Account: &Account{ | ||||
| 				Scopes: []string{"neededscope"}, | ||||
| 			}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "neededscope", | ||||
| 					Resource: srvResource, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "ExactRuleInvalidScope", | ||||
| 			Resource: srvResource, | ||||
| 			Account: &Account{ | ||||
| 				Scopes: []string{"neededscope"}, | ||||
| 			}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "invalidscope", | ||||
| 					Resource: srvResource, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallDenyWithAccount", | ||||
| 			Resource: srvResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 					Access:   AccessDenied, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "CatchallDenyWithNoAccount", | ||||
| 			Resource: srvResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 					Access:   AccessDenied, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "RulePriorityGrantFirst", | ||||
| 			Resource: srvResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 					Access:   AccessGranted, | ||||
| 					Priority: 1, | ||||
| 				}, | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 					Access:   AccessDenied, | ||||
| 					Priority: 0, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "RulePriorityDenyFirst", | ||||
| 			Resource: srvResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 					Access:   AccessGranted, | ||||
| 					Priority: 0, | ||||
| 				}, | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: catchallResource, | ||||
| 					Access:   AccessDenied, | ||||
| 					Priority: 1, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "WebExactEndpointValid", | ||||
| 			Resource: webResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope:    "*", | ||||
| 					Resource: webResource, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "WebExactEndpointInalid", | ||||
| 			Resource: webResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope: "*", | ||||
| 					Resource: &Resource{ | ||||
| 						Type:     webResource.Type, | ||||
| 						Name:     webResource.Name, | ||||
| 						Endpoint: "invalidendpoint", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "WebWildcardEndpoint", | ||||
| 			Resource: webResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope: "*", | ||||
| 					Resource: &Resource{ | ||||
| 						Type:     webResource.Type, | ||||
| 						Name:     webResource.Name, | ||||
| 						Endpoint: "*", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "WebWildcardPathEndpointValid", | ||||
| 			Resource: webResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope: "*", | ||||
| 					Resource: &Resource{ | ||||
| 						Type:     webResource.Type, | ||||
| 						Name:     webResource.Name, | ||||
| 						Endpoint: "/foo/*", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name:     "WebWildcardPathEndpointInvalid", | ||||
| 			Resource: webResource, | ||||
| 			Account:  &Account{}, | ||||
| 			Rules: []*Rule{ | ||||
| 				&Rule{ | ||||
| 					Scope: "*", | ||||
| 					Resource: &Resource{ | ||||
| 						Type:     webResource.Type, | ||||
| 						Name:     webResource.Name, | ||||
| 						Endpoint: "/bar/*", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Error: ErrForbidden, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	for _, tc := range tt { | ||||
| 		t.Run(tc.Name, func(t *testing.T) { | ||||
| 			if err := VerifyAccess(tc.Rules, tc.Account, tc.Resource); err != tc.Error { | ||||
| 				t.Errorf("Expected %v but got %v", tc.Error, err) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
| @@ -1,91 +1,31 @@ | ||||
| // Package broker is an interface used for asynchronous messaging | ||||
| package broker | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"errors" | ||||
| 	"time" | ||||
|  | ||||
| 	"go.unistack.org/micro/v4/codec" | ||||
| 	"go.unistack.org/micro/v4/metadata" | ||||
| ) | ||||
|  | ||||
| // DefaultBroker default memory broker | ||||
| var DefaultBroker Broker = NewBroker() | ||||
|  | ||||
| var ( | ||||
| 	// ErrNotConnected returns when broker used but not connected yet | ||||
| 	ErrNotConnected = errors.New("broker not connected") | ||||
| 	// ErrDisconnected returns when broker disconnected | ||||
| 	ErrDisconnected = errors.New("broker disconnected") | ||||
| 	// ErrInvalidMessage returns when invalid Message passed | ||||
| 	ErrInvalidMessage = errors.New("invalid message") | ||||
| 	// ErrInvalidHandler returns when subscriber passed to Subscribe | ||||
| 	ErrInvalidHandler = errors.New("invalid handler, ony func(Message) error and func([]Message) error supported") | ||||
| 	// DefaultGracefulTimeout | ||||
| 	DefaultGracefulTimeout = 5 * time.Second | ||||
| ) | ||||
|  | ||||
| // Broker is an interface used for asynchronous messaging. | ||||
| type Broker interface { | ||||
| 	// Name returns broker instance name | ||||
| 	Name() string | ||||
| 	// Init initilize broker | ||||
| 	Init(opts ...Option) error | ||||
| 	// Options returns broker options | ||||
| 	Init(...Option) error | ||||
| 	Options() Options | ||||
| 	// Address return configured address | ||||
| 	Address() string | ||||
| 	// Connect connects to broker | ||||
| 	Connect(ctx context.Context) error | ||||
| 	// Disconnect disconnect from broker | ||||
| 	Disconnect(ctx context.Context) error | ||||
| 	// NewMessage create new broker message to publish. | ||||
| 	NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...MessageOption) (Message, error) | ||||
| 	// Publish message to broker topic | ||||
| 	Publish(ctx context.Context, topic string, messages ...Message) error | ||||
| 	// Subscribe subscribes to topic message via handler | ||||
| 	Subscribe(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error) | ||||
| 	// String type of broker | ||||
| 	Connect() error | ||||
| 	Disconnect() error | ||||
| 	Publish(topic string, m *Message, opts ...PublishOption) error | ||||
| 	Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) | ||||
| 	String() string | ||||
| 	// Live returns broker liveness | ||||
| 	Live() bool | ||||
| 	// Ready returns broker readiness | ||||
| 	Ready() bool | ||||
| 	// Health returns broker health | ||||
| 	Health() bool | ||||
| } | ||||
|  | ||||
| type ( | ||||
| 	FuncPublish   func(ctx context.Context, topic string, messages ...Message) error | ||||
| 	HookPublish   func(next FuncPublish) FuncPublish | ||||
| 	FuncSubscribe func(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error) | ||||
| 	HookSubscribe func(next FuncSubscribe) FuncSubscribe | ||||
| ) | ||||
| // Handler is used to process messages via a subscription of a topic. | ||||
| type Handler func(*Message) error | ||||
|  | ||||
| // Message is given to a subscription handler for processing | ||||
| type Message interface { | ||||
| 	// Context for the message. | ||||
| 	Context() context.Context | ||||
| 	// Topic returns message destination topic. | ||||
| 	Topic() string | ||||
| 	// Header returns message headers. | ||||
| 	Header() metadata.Metadata | ||||
| 	// Body returns broker message []byte slice | ||||
| 	Body() []byte | ||||
| 	// Unmarshal try to decode message body to dst. | ||||
| 	// This is helper method that uses codec.Unmarshal. | ||||
| 	Unmarshal(dst interface{}, opts ...codec.Option) error | ||||
| 	// Ack acknowledge message if supported. | ||||
| 	Ack() error | ||||
| type ErrorHandler func(*Message, error) | ||||
|  | ||||
| type Message struct { | ||||
| 	Header map[string]string | ||||
| 	Body   []byte | ||||
| } | ||||
|  | ||||
| // Subscriber is a convenience return type for the Subscribe method | ||||
| type Subscriber interface { | ||||
| 	// Options returns subscriber options | ||||
| 	Options() SubscribeOptions | ||||
| 	// Topic returns topic for subscription | ||||
| 	Topic() string | ||||
| 	// Unsubscribe from topic | ||||
| 	Unsubscribe(ctx context.Context) error | ||||
| 	Unsubscribe() error | ||||
| } | ||||
|   | ||||
| @@ -1,63 +0,0 @@ | ||||
| package broker | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| ) | ||||
|  | ||||
| type brokerKey struct{} | ||||
|  | ||||
| // FromContext returns broker from passed context | ||||
| func FromContext(ctx context.Context) (Broker, bool) { | ||||
| 	if ctx == nil { | ||||
| 		return nil, false | ||||
| 	} | ||||
| 	c, ok := ctx.Value(brokerKey{}).(Broker) | ||||
| 	return c, ok | ||||
| } | ||||
|  | ||||
| // MustContext returns broker from passed context | ||||
| func MustContext(ctx context.Context) Broker { | ||||
| 	b, ok := FromContext(ctx) | ||||
| 	if !ok { | ||||
| 		panic("missing broker") | ||||
| 	} | ||||
| 	return b | ||||
| } | ||||
|  | ||||
| // NewContext savess broker in context | ||||
| func NewContext(ctx context.Context, s Broker) context.Context { | ||||
| 	if ctx == nil { | ||||
| 		ctx = context.Background() | ||||
| 	} | ||||
| 	return context.WithValue(ctx, brokerKey{}, s) | ||||
| } | ||||
|  | ||||
| // SetSubscribeOption returns a function to setup a context with given value | ||||
| func SetSubscribeOption(k, v interface{}) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		if o.Context == nil { | ||||
| 			o.Context = context.Background() | ||||
| 		} | ||||
| 		o.Context = context.WithValue(o.Context, k, v) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SetMessageOption returns a function to setup a context with given value | ||||
| func SetMessageOption(k, v interface{}) MessageOption { | ||||
| 	return func(o *MessageOptions) { | ||||
| 		if o.Context == nil { | ||||
| 			o.Context = context.Background() | ||||
| 		} | ||||
| 		o.Context = context.WithValue(o.Context, k, v) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SetOption returns a function to setup a context with given value | ||||
| func SetOption(k, v interface{}) Option { | ||||
| 	return func(o *Options) { | ||||
| 		if o.Context == nil { | ||||
| 			o.Context = context.Background() | ||||
| 		} | ||||
| 		o.Context = context.WithValue(o.Context, k, v) | ||||
| 	} | ||||
| } | ||||
| @@ -1,61 +0,0 @@ | ||||
| package broker | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestFromContext(t *testing.T) { | ||||
| 	ctx := context.WithValue(context.TODO(), brokerKey{}, NewBroker()) | ||||
| 	c, ok := FromContext(ctx) | ||||
| 	if c == nil || !ok { | ||||
| 		t.Fatal("FromContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestFromNilContext(t *testing.T) { | ||||
| 	// nolint: staticcheck | ||||
| 	c, ok := FromContext(nil) | ||||
| 	if ok || c != nil { | ||||
| 		t.Fatal("FromContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestNewContext(t *testing.T) { | ||||
| 	ctx := NewContext(context.TODO(), NewBroker()) | ||||
| 	c, ok := FromContext(ctx) | ||||
| 	if c == nil || !ok { | ||||
| 		t.Fatal("NewContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestNewNilContext(t *testing.T) { | ||||
| 	// nolint: staticcheck | ||||
| 	ctx := NewContext(nil, NewBroker()) | ||||
| 	c, ok := FromContext(ctx) | ||||
| 	if c == nil || !ok { | ||||
| 		t.Fatal("NewContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestSetSubscribeOption(t *testing.T) { | ||||
| 	type key struct{} | ||||
| 	o := SetSubscribeOption(key{}, "test") | ||||
| 	opts := &SubscribeOptions{} | ||||
| 	o(opts) | ||||
|  | ||||
| 	if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { | ||||
| 		t.Fatal("SetSubscribeOption not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestSetOption(t *testing.T) { | ||||
| 	type key struct{} | ||||
| 	o := SetOption(key{}, "test") | ||||
| 	opts := &Options{} | ||||
| 	o(opts) | ||||
|  | ||||
| 	if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { | ||||
| 		t.Fatal("SetOption not works") | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										689
									
								
								broker/http/http.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										689
									
								
								broker/http/http.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,689 @@ | ||||
| // Package http provides a http based message broker | ||||
| package http | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"context" | ||||
| 	"crypto/tls" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"io/ioutil" | ||||
| 	"math/rand" | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| 	"net/url" | ||||
| 	"runtime" | ||||
| 	"sync" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/broker" | ||||
| 	"github.com/asim/go-micro/v3/codec/json" | ||||
| 	merr "github.com/asim/go-micro/v3/errors" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/asim/go-micro/v3/registry/cache" | ||||
| 	"github.com/asim/go-micro/v3/registry/mdns" | ||||
| 	maddr "github.com/asim/go-micro/v3/util/addr" | ||||
| 	mnet "github.com/asim/go-micro/v3/util/net" | ||||
| 	mls "github.com/asim/go-micro/v3/util/tls" | ||||
| 	"github.com/google/uuid" | ||||
| 	"golang.org/x/net/http2" | ||||
| ) | ||||
|  | ||||
| // HTTP Broker is a point to point async broker | ||||
| type httpBroker struct { | ||||
| 	id      string | ||||
| 	address string | ||||
| 	opts    broker.Options | ||||
|  | ||||
| 	mux *http.ServeMux | ||||
|  | ||||
| 	c *http.Client | ||||
| 	r registry.Registry | ||||
|  | ||||
| 	sync.RWMutex | ||||
| 	subscribers map[string][]*httpSubscriber | ||||
| 	running     bool | ||||
| 	exit        chan chan error | ||||
|  | ||||
| 	// offline message inbox | ||||
| 	mtx   sync.RWMutex | ||||
| 	inbox map[string][][]byte | ||||
| } | ||||
|  | ||||
| type httpSubscriber struct { | ||||
| 	opts  broker.SubscribeOptions | ||||
| 	id    string | ||||
| 	topic string | ||||
| 	fn    broker.Handler | ||||
| 	svc   *registry.Service | ||||
| 	hb    *httpBroker | ||||
| } | ||||
|  | ||||
| var ( | ||||
| 	DefaultPath      = "/" | ||||
| 	DefaultAddress   = "127.0.0.1:0" | ||||
| 	serviceName      = "micro.http.broker" | ||||
| 	broadcastVersion = "ff.http.broadcast" | ||||
| 	registerTTL      = time.Minute | ||||
| 	registerInterval = time.Second * 30 | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	rand.Seed(time.Now().Unix()) | ||||
| } | ||||
|  | ||||
| func newTransport(config *tls.Config) *http.Transport { | ||||
| 	if config == nil { | ||||
| 		config = &tls.Config{ | ||||
| 			InsecureSkipVerify: true, | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	dialTLS := func(network string, addr string) (net.Conn, error) { | ||||
| 		return tls.Dial(network, addr, config) | ||||
| 	} | ||||
|  | ||||
| 	t := &http.Transport{ | ||||
| 		Proxy: http.ProxyFromEnvironment, | ||||
| 		Dial: (&net.Dialer{ | ||||
| 			Timeout:   30 * time.Second, | ||||
| 			KeepAlive: 30 * time.Second, | ||||
| 		}).Dial, | ||||
| 		TLSHandshakeTimeout: 10 * time.Second, | ||||
| 		DialTLS:             dialTLS, | ||||
| 	} | ||||
| 	runtime.SetFinalizer(&t, func(tr **http.Transport) { | ||||
| 		(*tr).CloseIdleConnections() | ||||
| 	}) | ||||
|  | ||||
| 	// setup http2 | ||||
| 	http2.ConfigureTransport(t) | ||||
|  | ||||
| 	return t | ||||
| } | ||||
|  | ||||
| func newHttpBroker(opts ...broker.Option) broker.Broker { | ||||
| 	options := broker.Options{ | ||||
| 		Codec:    json.Marshaler{}, | ||||
| 		Context:  context.TODO(), | ||||
| 		Registry: mdns.NewRegistry(), | ||||
| 	} | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	// set address | ||||
| 	addr := DefaultAddress | ||||
|  | ||||
| 	if len(options.Addrs) > 0 && len(options.Addrs[0]) > 0 { | ||||
| 		addr = options.Addrs[0] | ||||
| 	} | ||||
|  | ||||
| 	h := &httpBroker{ | ||||
| 		id:          uuid.New().String(), | ||||
| 		address:     addr, | ||||
| 		opts:        options, | ||||
| 		r:           options.Registry, | ||||
| 		c:           &http.Client{Transport: newTransport(options.TLSConfig)}, | ||||
| 		subscribers: make(map[string][]*httpSubscriber), | ||||
| 		exit:        make(chan chan error), | ||||
| 		mux:         http.NewServeMux(), | ||||
| 		inbox:       make(map[string][][]byte), | ||||
| 	} | ||||
|  | ||||
| 	// specify the message handler | ||||
| 	h.mux.Handle(DefaultPath, h) | ||||
|  | ||||
| 	// get optional handlers | ||||
| 	if h.opts.Context != nil { | ||||
| 		handlers, ok := h.opts.Context.Value("http_handlers").(map[string]http.Handler) | ||||
| 		if ok { | ||||
| 			for pattern, handler := range handlers { | ||||
| 				h.mux.Handle(pattern, handler) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return h | ||||
| } | ||||
|  | ||||
| func (h *httpSubscriber) Options() broker.SubscribeOptions { | ||||
| 	return h.opts | ||||
| } | ||||
|  | ||||
| func (h *httpSubscriber) Topic() string { | ||||
| 	return h.topic | ||||
| } | ||||
|  | ||||
| func (h *httpSubscriber) Unsubscribe() error { | ||||
| 	return h.hb.unsubscribe(h) | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) saveMessage(topic string, msg []byte) { | ||||
| 	h.mtx.Lock() | ||||
| 	defer h.mtx.Unlock() | ||||
|  | ||||
| 	// get messages | ||||
| 	c := h.inbox[topic] | ||||
|  | ||||
| 	// save message | ||||
| 	c = append(c, msg) | ||||
|  | ||||
| 	// max length 64 | ||||
| 	if len(c) > 64 { | ||||
| 		c = c[:64] | ||||
| 	} | ||||
|  | ||||
| 	// save inbox | ||||
| 	h.inbox[topic] = c | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) getMessage(topic string, num int) [][]byte { | ||||
| 	h.mtx.Lock() | ||||
| 	defer h.mtx.Unlock() | ||||
|  | ||||
| 	// get messages | ||||
| 	c, ok := h.inbox[topic] | ||||
| 	if !ok { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	// more message than requests | ||||
| 	if len(c) >= num { | ||||
| 		msg := c[:num] | ||||
| 		h.inbox[topic] = c[num:] | ||||
| 		return msg | ||||
| 	} | ||||
|  | ||||
| 	// reset inbox | ||||
| 	h.inbox[topic] = nil | ||||
|  | ||||
| 	// return all messages | ||||
| 	return c | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) subscribe(s *httpSubscriber) error { | ||||
| 	h.Lock() | ||||
| 	defer h.Unlock() | ||||
|  | ||||
| 	if err := h.r.Register(s.svc, registry.RegisterTTL(registerTTL)); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	h.subscribers[s.topic] = append(h.subscribers[s.topic], s) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) unsubscribe(s *httpSubscriber) error { | ||||
| 	h.Lock() | ||||
| 	defer h.Unlock() | ||||
|  | ||||
| 	//nolint:prealloc | ||||
| 	var subscribers []*httpSubscriber | ||||
|  | ||||
| 	// look for subscriber | ||||
| 	for _, sub := range h.subscribers[s.topic] { | ||||
| 		// deregister and skip forward | ||||
| 		if sub == s { | ||||
| 			_ = h.r.Deregister(sub.svc) | ||||
| 			continue | ||||
| 		} | ||||
| 		// keep subscriber | ||||
| 		subscribers = append(subscribers, sub) | ||||
| 	} | ||||
|  | ||||
| 	// set subscribers | ||||
| 	h.subscribers[s.topic] = subscribers | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) run(l net.Listener) { | ||||
| 	t := time.NewTicker(registerInterval) | ||||
| 	defer t.Stop() | ||||
|  | ||||
| 	for { | ||||
| 		select { | ||||
| 		// heartbeat for each subscriber | ||||
| 		case <-t.C: | ||||
| 			h.RLock() | ||||
| 			for _, subs := range h.subscribers { | ||||
| 				for _, sub := range subs { | ||||
| 					_ = h.r.Register(sub.svc, registry.RegisterTTL(registerTTL)) | ||||
| 				} | ||||
| 			} | ||||
| 			h.RUnlock() | ||||
| 		// received exit signal | ||||
| 		case ch := <-h.exit: | ||||
| 			ch <- l.Close() | ||||
| 			h.RLock() | ||||
| 			for _, subs := range h.subscribers { | ||||
| 				for _, sub := range subs { | ||||
| 					_ = h.r.Deregister(sub.svc) | ||||
| 				} | ||||
| 			} | ||||
| 			h.RUnlock() | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||||
| 	if req.Method != "POST" { | ||||
| 		err := merr.BadRequest("go.micro.broker", "Method not allowed") | ||||
| 		http.Error(w, err.Error(), http.StatusMethodNotAllowed) | ||||
| 		return | ||||
| 	} | ||||
| 	defer req.Body.Close() | ||||
|  | ||||
| 	req.ParseForm() | ||||
|  | ||||
| 	b, err := ioutil.ReadAll(req.Body) | ||||
| 	if err != nil { | ||||
| 		errr := merr.InternalServerError("go.micro.broker", "Error reading request body: %v", err) | ||||
| 		w.WriteHeader(http.StatusInternalServerError) | ||||
| 		w.Write([]byte(errr.Error())) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	var msg *broker.Message | ||||
| 	if err = h.opts.Codec.Unmarshal(b, &msg); err != nil { | ||||
| 		errr := merr.InternalServerError("go.micro.broker", "Error parsing request body: %v", err) | ||||
| 		w.WriteHeader(http.StatusInternalServerError) | ||||
| 		w.Write([]byte(errr.Error())) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	topic := msg.Header["Micro-Topic"] | ||||
|  | ||||
| 	if len(topic) == 0 { | ||||
| 		errr := merr.InternalServerError("go.micro.broker", "Topic not found") | ||||
| 		w.WriteHeader(http.StatusInternalServerError) | ||||
| 		w.Write([]byte(errr.Error())) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	id := req.Form.Get("id") | ||||
|  | ||||
| 	//nolint:prealloc | ||||
| 	var subs []broker.Handler | ||||
|  | ||||
| 	h.RLock() | ||||
| 	for _, subscriber := range h.subscribers[topic] { | ||||
| 		if id != subscriber.id { | ||||
| 			continue | ||||
| 		} | ||||
| 		subs = append(subs, subscriber.fn) | ||||
| 	} | ||||
| 	h.RUnlock() | ||||
|  | ||||
| 	// execute the handler | ||||
| 	for _, fn := range subs { | ||||
| 		fn(msg) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) Address() string { | ||||
| 	h.RLock() | ||||
| 	defer h.RUnlock() | ||||
| 	return h.address | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) Connect() error { | ||||
| 	h.RLock() | ||||
| 	if h.running { | ||||
| 		h.RUnlock() | ||||
| 		return nil | ||||
| 	} | ||||
| 	h.RUnlock() | ||||
|  | ||||
| 	h.Lock() | ||||
| 	defer h.Unlock() | ||||
|  | ||||
| 	var l net.Listener | ||||
| 	var err error | ||||
|  | ||||
| 	if h.opts.Secure || h.opts.TLSConfig != nil { | ||||
| 		config := h.opts.TLSConfig | ||||
|  | ||||
| 		fn := func(addr string) (net.Listener, error) { | ||||
| 			if config == nil { | ||||
| 				hosts := []string{addr} | ||||
|  | ||||
| 				// check if its a valid host:port | ||||
| 				if host, _, err := net.SplitHostPort(addr); err == nil { | ||||
| 					if len(host) == 0 { | ||||
| 						hosts = maddr.IPs() | ||||
| 					} else { | ||||
| 						hosts = []string{host} | ||||
| 					} | ||||
| 				} | ||||
|  | ||||
| 				// generate a certificate | ||||
| 				cert, err := mls.Certificate(hosts...) | ||||
| 				if err != nil { | ||||
| 					return nil, err | ||||
| 				} | ||||
| 				config = &tls.Config{Certificates: []tls.Certificate{cert}} | ||||
| 			} | ||||
| 			return tls.Listen("tcp", addr, config) | ||||
| 		} | ||||
|  | ||||
| 		l, err = mnet.Listen(h.address, fn) | ||||
| 	} else { | ||||
| 		fn := func(addr string) (net.Listener, error) { | ||||
| 			return net.Listen("tcp", addr) | ||||
| 		} | ||||
|  | ||||
| 		l, err = mnet.Listen(h.address, fn) | ||||
| 	} | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	addr := h.address | ||||
| 	h.address = l.Addr().String() | ||||
|  | ||||
| 	go http.Serve(l, h.mux) | ||||
| 	go func() { | ||||
| 		h.run(l) | ||||
| 		h.Lock() | ||||
| 		h.opts.Addrs = []string{addr} | ||||
| 		h.address = addr | ||||
| 		h.Unlock() | ||||
| 	}() | ||||
|  | ||||
| 	// get registry | ||||
| 	reg := h.opts.Registry | ||||
| 	if reg == nil { | ||||
| 		reg = mdns.NewRegistry() | ||||
| 	} | ||||
| 	// set cache | ||||
| 	h.r = cache.New(reg) | ||||
|  | ||||
| 	// set running | ||||
| 	h.running = true | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) Disconnect() error { | ||||
| 	h.RLock() | ||||
| 	if !h.running { | ||||
| 		h.RUnlock() | ||||
| 		return nil | ||||
| 	} | ||||
| 	h.RUnlock() | ||||
|  | ||||
| 	h.Lock() | ||||
| 	defer h.Unlock() | ||||
|  | ||||
| 	// stop cache | ||||
| 	rc, ok := h.r.(cache.Cache) | ||||
| 	if ok { | ||||
| 		rc.Stop() | ||||
| 	} | ||||
|  | ||||
| 	// exit and return err | ||||
| 	ch := make(chan error) | ||||
| 	h.exit <- ch | ||||
| 	err := <-ch | ||||
|  | ||||
| 	// set not running | ||||
| 	h.running = false | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) Init(opts ...broker.Option) error { | ||||
| 	h.RLock() | ||||
| 	if h.running { | ||||
| 		h.RUnlock() | ||||
| 		return errors.New("cannot init while connected") | ||||
| 	} | ||||
| 	h.RUnlock() | ||||
|  | ||||
| 	h.Lock() | ||||
| 	defer h.Unlock() | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&h.opts) | ||||
| 	} | ||||
|  | ||||
| 	if len(h.opts.Addrs) > 0 && len(h.opts.Addrs[0]) > 0 { | ||||
| 		h.address = h.opts.Addrs[0] | ||||
| 	} | ||||
|  | ||||
| 	if len(h.id) == 0 { | ||||
| 		h.id = "go.micro.http.broker-" + uuid.New().String() | ||||
| 	} | ||||
|  | ||||
| 	// get registry | ||||
| 	reg := h.opts.Registry | ||||
| 	if reg == nil { | ||||
| 		reg = mdns.NewRegistry() | ||||
| 	} | ||||
|  | ||||
| 	// get cache | ||||
| 	if rc, ok := h.r.(cache.Cache); ok { | ||||
| 		rc.Stop() | ||||
| 	} | ||||
|  | ||||
| 	// set registry | ||||
| 	h.r = cache.New(reg) | ||||
|  | ||||
| 	// reconfigure tls config | ||||
| 	if c := h.opts.TLSConfig; c != nil { | ||||
| 		h.c = &http.Client{ | ||||
| 			Transport: newTransport(c), | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) Options() broker.Options { | ||||
| 	return h.opts | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error { | ||||
| 	// create the message first | ||||
| 	m := &broker.Message{ | ||||
| 		Header: make(map[string]string), | ||||
| 		Body:   msg.Body, | ||||
| 	} | ||||
|  | ||||
| 	for k, v := range msg.Header { | ||||
| 		m.Header[k] = v | ||||
| 	} | ||||
|  | ||||
| 	m.Header["Micro-Topic"] = topic | ||||
|  | ||||
| 	// encode the message | ||||
| 	b, err := h.opts.Codec.Marshal(m) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// save the message | ||||
| 	h.saveMessage(topic, b) | ||||
|  | ||||
| 	// now attempt to get the service | ||||
| 	h.RLock() | ||||
| 	s, err := h.r.GetService(serviceName) | ||||
| 	if err != nil { | ||||
| 		h.RUnlock() | ||||
| 		return err | ||||
| 	} | ||||
| 	h.RUnlock() | ||||
|  | ||||
| 	pub := func(node *registry.Node, t string, b []byte) error { | ||||
| 		scheme := "http" | ||||
|  | ||||
| 		// check if secure is added in metadata | ||||
| 		if node.Metadata["secure"] == "true" { | ||||
| 			scheme = "https" | ||||
| 		} | ||||
|  | ||||
| 		vals := url.Values{} | ||||
| 		vals.Add("id", node.Id) | ||||
|  | ||||
| 		uri := fmt.Sprintf("%s://%s%s?%s", scheme, node.Address, DefaultPath, vals.Encode()) | ||||
| 		r, err := h.c.Post(uri, "application/json", bytes.NewReader(b)) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		// discard response body | ||||
| 		io.Copy(ioutil.Discard, r.Body) | ||||
| 		r.Body.Close() | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	srv := func(s []*registry.Service, b []byte) { | ||||
| 		for _, service := range s { | ||||
| 			var nodes []*registry.Node | ||||
|  | ||||
| 			for _, node := range service.Nodes { | ||||
| 				// only use nodes tagged with broker http | ||||
| 				if node.Metadata["broker"] != "http" { | ||||
| 					continue | ||||
| 				} | ||||
|  | ||||
| 				// look for nodes for the topic | ||||
| 				if node.Metadata["topic"] != topic { | ||||
| 					continue | ||||
| 				} | ||||
|  | ||||
| 				nodes = append(nodes, node) | ||||
| 			} | ||||
|  | ||||
| 			// only process if we have nodes | ||||
| 			if len(nodes) == 0 { | ||||
| 				continue | ||||
| 			} | ||||
|  | ||||
| 			switch service.Version { | ||||
| 			// broadcast version means broadcast to all nodes | ||||
| 			case broadcastVersion: | ||||
| 				var success bool | ||||
|  | ||||
| 				// publish to all nodes | ||||
| 				for _, node := range nodes { | ||||
| 					// publish async | ||||
| 					if err := pub(node, topic, b); err == nil { | ||||
| 						success = true | ||||
| 					} | ||||
| 				} | ||||
|  | ||||
| 				// save if it failed to publish at least once | ||||
| 				if !success { | ||||
| 					h.saveMessage(topic, b) | ||||
| 				} | ||||
| 			default: | ||||
| 				// select node to publish to | ||||
| 				node := nodes[rand.Int()%len(nodes)] | ||||
|  | ||||
| 				// publish async to one node | ||||
| 				if err := pub(node, topic, b); err != nil { | ||||
| 					// if failed save it | ||||
| 					h.saveMessage(topic, b) | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// do the rest async | ||||
| 	go func() { | ||||
| 		// get a third of the backlog | ||||
| 		messages := h.getMessage(topic, 8) | ||||
| 		delay := (len(messages) > 1) | ||||
|  | ||||
| 		// publish all the messages | ||||
| 		for _, msg := range messages { | ||||
| 			// serialize here | ||||
| 			srv(s, msg) | ||||
|  | ||||
| 			// sending a backlog of messages | ||||
| 			if delay { | ||||
| 				time.Sleep(time.Millisecond * 100) | ||||
| 			} | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) { | ||||
| 	var err error | ||||
| 	var host, port string | ||||
| 	options := broker.NewSubscribeOptions(opts...) | ||||
|  | ||||
| 	// parse address for host, port | ||||
| 	host, port, err = net.SplitHostPort(h.Address()) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	addr, err := maddr.Extract(host) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	var secure bool | ||||
|  | ||||
| 	if h.opts.Secure || h.opts.TLSConfig != nil { | ||||
| 		secure = true | ||||
| 	} | ||||
|  | ||||
| 	// register service | ||||
| 	node := ®istry.Node{ | ||||
| 		Id:      topic + "-" + h.id, | ||||
| 		Address: mnet.HostPort(addr, port), | ||||
| 		Metadata: map[string]string{ | ||||
| 			"secure": fmt.Sprintf("%t", secure), | ||||
| 			"broker": "http", | ||||
| 			"topic":  topic, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	// check for queue group or broadcast queue | ||||
| 	version := options.Queue | ||||
| 	if len(version) == 0 { | ||||
| 		version = broadcastVersion | ||||
| 	} | ||||
|  | ||||
| 	service := ®istry.Service{ | ||||
| 		Name:    serviceName, | ||||
| 		Version: version, | ||||
| 		Nodes:   []*registry.Node{node}, | ||||
| 	} | ||||
|  | ||||
| 	// generate subscriber | ||||
| 	subscriber := &httpSubscriber{ | ||||
| 		opts:  options, | ||||
| 		hb:    h, | ||||
| 		id:    node.Id, | ||||
| 		topic: topic, | ||||
| 		fn:    handler, | ||||
| 		svc:   service, | ||||
| 	} | ||||
|  | ||||
| 	// subscribe now | ||||
| 	if err := h.subscribe(subscriber); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	// return the subscriber | ||||
| 	return subscriber, nil | ||||
| } | ||||
|  | ||||
| func (h *httpBroker) String() string { | ||||
| 	return "http" | ||||
| } | ||||
|  | ||||
| // NewBroker returns a new http broker | ||||
| func NewBroker(opts ...broker.Option) broker.Broker { | ||||
| 	return newHttpBroker(opts...) | ||||
| } | ||||
							
								
								
									
										377
									
								
								broker/http/http_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										377
									
								
								broker/http/http_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,377 @@ | ||||
| package http | ||||
|  | ||||
| import ( | ||||
| 	"sync" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/broker" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/asim/go-micro/v3/registry/memory" | ||||
| 	"github.com/google/uuid" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	// mock data | ||||
| 	testData = map[string][]*registry.Service{ | ||||
| 		"foo": { | ||||
| 			{ | ||||
| 				Name:    "foo", | ||||
| 				Version: "1.0.0", | ||||
| 				Nodes: []*registry.Node{ | ||||
| 					{ | ||||
| 						Id:      "foo-1.0.0-123", | ||||
| 						Address: "localhost:9999", | ||||
| 					}, | ||||
| 					{ | ||||
| 						Id:      "foo-1.0.0-321", | ||||
| 						Address: "localhost:9999", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			{ | ||||
| 				Name:    "foo", | ||||
| 				Version: "1.0.1", | ||||
| 				Nodes: []*registry.Node{ | ||||
| 					{ | ||||
| 						Id:      "foo-1.0.1-321", | ||||
| 						Address: "localhost:6666", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			{ | ||||
| 				Name:    "foo", | ||||
| 				Version: "1.0.3", | ||||
| 				Nodes: []*registry.Node{ | ||||
| 					{ | ||||
| 						Id:      "foo-1.0.3-345", | ||||
| 						Address: "localhost:8888", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| ) | ||||
|  | ||||
| func newTestRegistry() registry.Registry { | ||||
| 	return memory.NewRegistry(memory.Services(testData)) | ||||
| } | ||||
|  | ||||
| func sub(be *testing.B, c int) { | ||||
| 	be.StopTimer() | ||||
| 	m := newTestRegistry() | ||||
|  | ||||
| 	b := NewBroker(broker.Registry(m)) | ||||
| 	topic := uuid.New().String() | ||||
|  | ||||
| 	if err := b.Init(); err != nil { | ||||
| 		be.Fatalf("Unexpected init error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Connect(); err != nil { | ||||
| 		be.Fatalf("Unexpected connect error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	msg := &broker.Message{ | ||||
| 		Header: map[string]string{ | ||||
| 			"Content-Type": "application/json", | ||||
| 		}, | ||||
| 		Body: []byte(`{"message": "Hello World"}`), | ||||
| 	} | ||||
|  | ||||
| 	var subs []broker.Subscriber | ||||
| 	done := make(chan bool, c) | ||||
|  | ||||
| 	for i := 0; i < c; i++ { | ||||
| 		sub, err := b.Subscribe(topic, func(m *broker.Message) error { | ||||
| 			done <- true | ||||
|  | ||||
| 			if string(m.Body) != string(msg.Body) { | ||||
| 				be.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body)) | ||||
| 			} | ||||
|  | ||||
| 			return nil | ||||
| 		}, broker.Queue("shared")) | ||||
| 		if err != nil { | ||||
| 			be.Fatalf("Unexpected subscribe error: %v", err) | ||||
| 		} | ||||
| 		subs = append(subs, sub) | ||||
| 	} | ||||
|  | ||||
| 	for i := 0; i < be.N; i++ { | ||||
| 		be.StartTimer() | ||||
| 		if err := b.Publish(topic, msg); err != nil { | ||||
| 			be.Fatalf("Unexpected publish error: %v", err) | ||||
| 		} | ||||
| 		<-done | ||||
| 		be.StopTimer() | ||||
| 	} | ||||
|  | ||||
| 	for _, sub := range subs { | ||||
| 		sub.Unsubscribe() | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Disconnect(); err != nil { | ||||
| 		be.Fatalf("Unexpected disconnect error: %v", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func pub(be *testing.B, c int) { | ||||
| 	be.StopTimer() | ||||
| 	m := newTestRegistry() | ||||
| 	b := NewBroker(broker.Registry(m)) | ||||
| 	topic := uuid.New().String() | ||||
|  | ||||
| 	if err := b.Init(); err != nil { | ||||
| 		be.Fatalf("Unexpected init error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Connect(); err != nil { | ||||
| 		be.Fatalf("Unexpected connect error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	msg := &broker.Message{ | ||||
| 		Header: map[string]string{ | ||||
| 			"Content-Type": "application/json", | ||||
| 		}, | ||||
| 		Body: []byte(`{"message": "Hello World"}`), | ||||
| 	} | ||||
|  | ||||
| 	done := make(chan bool, c*4) | ||||
|  | ||||
| 	sub, err := b.Subscribe(topic, func(m *broker.Message) error { | ||||
| 		done <- true | ||||
| 		if string(m.Body) != string(msg.Body) { | ||||
| 			be.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body)) | ||||
| 		} | ||||
| 		return nil | ||||
| 	}, broker.Queue("shared")) | ||||
| 	if err != nil { | ||||
| 		be.Fatalf("Unexpected subscribe error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	var wg sync.WaitGroup | ||||
| 	ch := make(chan int, c*4) | ||||
| 	be.StartTimer() | ||||
|  | ||||
| 	for i := 0; i < c; i++ { | ||||
| 		go func() { | ||||
| 			for range ch { | ||||
| 				if err := b.Publish(topic, msg); err != nil { | ||||
| 					be.Fatalf("Unexpected publish error: %v", err) | ||||
| 				} | ||||
| 				select { | ||||
| 				case <-done: | ||||
| 				case <-time.After(time.Second): | ||||
| 				} | ||||
| 				wg.Done() | ||||
| 			} | ||||
| 		}() | ||||
| 	} | ||||
|  | ||||
| 	for i := 0; i < be.N; i++ { | ||||
| 		wg.Add(1) | ||||
| 		ch <- i | ||||
| 	} | ||||
|  | ||||
| 	wg.Wait() | ||||
| 	be.StopTimer() | ||||
| 	sub.Unsubscribe() | ||||
| 	close(ch) | ||||
| 	close(done) | ||||
|  | ||||
| 	if err := b.Disconnect(); err != nil { | ||||
| 		be.Fatalf("Unexpected disconnect error: %v", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBroker(t *testing.T) { | ||||
| 	m := newTestRegistry() | ||||
| 	b := NewBroker(broker.Registry(m)) | ||||
|  | ||||
| 	if err := b.Init(); err != nil { | ||||
| 		t.Fatalf("Unexpected init error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Connect(); err != nil { | ||||
| 		t.Fatalf("Unexpected connect error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	msg := &broker.Message{ | ||||
| 		Header: map[string]string{ | ||||
| 			"Content-Type": "application/json", | ||||
| 		}, | ||||
| 		Body: []byte(`{"message": "Hello World"}`), | ||||
| 	} | ||||
|  | ||||
| 	done := make(chan bool) | ||||
|  | ||||
| 	sub, err := b.Subscribe("test", func(m *broker.Message) error { | ||||
|  | ||||
| 		if string(m.Body) != string(msg.Body) { | ||||
| 			t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body)) | ||||
| 		} | ||||
|  | ||||
| 		close(done) | ||||
| 		return nil | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Unexpected subscribe error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Publish("test", msg); err != nil { | ||||
| 		t.Fatalf("Unexpected publish error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	<-done | ||||
| 	sub.Unsubscribe() | ||||
|  | ||||
| 	if err := b.Disconnect(); err != nil { | ||||
| 		t.Fatalf("Unexpected disconnect error: %v", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestConcurrentSubBroker(t *testing.T) { | ||||
| 	m := newTestRegistry() | ||||
| 	b := NewBroker(broker.Registry(m)) | ||||
|  | ||||
| 	if err := b.Init(); err != nil { | ||||
| 		t.Fatalf("Unexpected init error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Connect(); err != nil { | ||||
| 		t.Fatalf("Unexpected connect error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	msg := &broker.Message{ | ||||
| 		Header: map[string]string{ | ||||
| 			"Content-Type": "application/json", | ||||
| 		}, | ||||
| 		Body: []byte(`{"message": "Hello World"}`), | ||||
| 	} | ||||
|  | ||||
| 	var subs []broker.Subscriber | ||||
| 	var wg sync.WaitGroup | ||||
|  | ||||
| 	for i := 0; i < 10; i++ { | ||||
| 		sub, err := b.Subscribe("test", func(m *broker.Message) error { | ||||
| 			defer wg.Done() | ||||
|  | ||||
| 			if string(m.Body) != string(msg.Body) { | ||||
| 				t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body)) | ||||
| 			} | ||||
|  | ||||
| 			return nil | ||||
| 		}) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("Unexpected subscribe error: %v", err) | ||||
| 		} | ||||
|  | ||||
| 		wg.Add(1) | ||||
| 		subs = append(subs, sub) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Publish("test", msg); err != nil { | ||||
| 		t.Fatalf("Unexpected publish error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	wg.Wait() | ||||
|  | ||||
| 	for _, sub := range subs { | ||||
| 		sub.Unsubscribe() | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Disconnect(); err != nil { | ||||
| 		t.Fatalf("Unexpected disconnect error: %v", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestConcurrentPubBroker(t *testing.T) { | ||||
| 	m := newTestRegistry() | ||||
| 	b := NewBroker(broker.Registry(m)) | ||||
|  | ||||
| 	if err := b.Init(); err != nil { | ||||
| 		t.Fatalf("Unexpected init error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Connect(); err != nil { | ||||
| 		t.Fatalf("Unexpected connect error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	msg := &broker.Message{ | ||||
| 		Header: map[string]string{ | ||||
| 			"Content-Type": "application/json", | ||||
| 		}, | ||||
| 		Body: []byte(`{"message": "Hello World"}`), | ||||
| 	} | ||||
|  | ||||
| 	var wg sync.WaitGroup | ||||
|  | ||||
| 	sub, err := b.Subscribe("test", func(m *broker.Message) error { | ||||
| 		defer wg.Done() | ||||
|  | ||||
| 		if string(m.Body) != string(msg.Body) { | ||||
| 			t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body)) | ||||
| 		} | ||||
|  | ||||
| 		return nil | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Unexpected subscribe error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	for i := 0; i < 10; i++ { | ||||
| 		wg.Add(1) | ||||
|  | ||||
| 		if err := b.Publish("test", msg); err != nil { | ||||
| 			t.Fatalf("Unexpected publish error: %v", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	wg.Wait() | ||||
|  | ||||
| 	sub.Unsubscribe() | ||||
|  | ||||
| 	if err := b.Disconnect(); err != nil { | ||||
| 		t.Fatalf("Unexpected disconnect error: %v", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func BenchmarkSub1(b *testing.B) { | ||||
| 	sub(b, 1) | ||||
| } | ||||
| func BenchmarkSub8(b *testing.B) { | ||||
| 	sub(b, 8) | ||||
| } | ||||
|  | ||||
| func BenchmarkSub32(b *testing.B) { | ||||
| 	sub(b, 32) | ||||
| } | ||||
|  | ||||
| func BenchmarkSub64(b *testing.B) { | ||||
| 	sub(b, 64) | ||||
| } | ||||
|  | ||||
| func BenchmarkSub128(b *testing.B) { | ||||
| 	sub(b, 128) | ||||
| } | ||||
|  | ||||
| func BenchmarkPub1(b *testing.B) { | ||||
| 	pub(b, 1) | ||||
| } | ||||
|  | ||||
| func BenchmarkPub8(b *testing.B) { | ||||
| 	pub(b, 8) | ||||
| } | ||||
|  | ||||
| func BenchmarkPub32(b *testing.B) { | ||||
| 	pub(b, 32) | ||||
| } | ||||
|  | ||||
| func BenchmarkPub64(b *testing.B) { | ||||
| 	pub(b, 64) | ||||
| } | ||||
|  | ||||
| func BenchmarkPub128(b *testing.B) { | ||||
| 	pub(b, 128) | ||||
| } | ||||
							
								
								
									
										23
									
								
								broker/http/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								broker/http/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| package http | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"net/http" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/broker" | ||||
| ) | ||||
|  | ||||
| // Handle registers the handler for the given pattern. | ||||
| func Handle(pattern string, handler http.Handler) broker.Option { | ||||
| 	return func(o *broker.Options) { | ||||
| 		if o.Context == nil { | ||||
| 			o.Context = context.Background() | ||||
| 		} | ||||
| 		handlers, ok := o.Context.Value("http_handlers").(map[string]http.Handler) | ||||
| 		if !ok { | ||||
| 			handlers = make(map[string]http.Handler) | ||||
| 		} | ||||
| 		handlers[pattern] = handler | ||||
| 		o.Context = context.WithValue(o.Context, "http_handlers", handlers) | ||||
| 	} | ||||
| } | ||||
| @@ -1,105 +1,49 @@ | ||||
| package broker | ||||
| // Package memory provides a memory broker | ||||
| package memory | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"strings" | ||||
| 	"errors" | ||||
| 	"math/rand" | ||||
| 	"sync" | ||||
| 	"time" | ||||
|  | ||||
| 	"go.unistack.org/micro/v4/broker" | ||||
| 	"go.unistack.org/micro/v4/codec" | ||||
| 	"go.unistack.org/micro/v4/logger" | ||||
| 	"go.unistack.org/micro/v4/metadata" | ||||
| 	"go.unistack.org/micro/v4/options" | ||||
| 	maddr "go.unistack.org/micro/v4/util/addr" | ||||
| 	"go.unistack.org/micro/v4/util/id" | ||||
| 	mnet "go.unistack.org/micro/v4/util/net" | ||||
| 	"go.unistack.org/micro/v4/util/rand" | ||||
| 	"github.com/asim/go-micro/v3/broker" | ||||
| 	maddr "github.com/asim/go-micro/v3/util/addr" | ||||
| 	mnet "github.com/asim/go-micro/v3/util/net" | ||||
| 	"github.com/google/uuid" | ||||
| ) | ||||
|  | ||||
| type Broker struct { | ||||
| 	funcPublish   broker.FuncPublish | ||||
| 	funcSubscribe broker.FuncSubscribe | ||||
| 	subscribers   map[string][]*Subscriber | ||||
| 	addr          string | ||||
| 	opts          broker.Options | ||||
| 	mu            sync.RWMutex | ||||
| 	connected     bool | ||||
| type memoryBroker struct { | ||||
| 	opts broker.Options | ||||
|  | ||||
| 	addr string | ||||
| 	sync.RWMutex | ||||
| 	connected   bool | ||||
| 	Subscribers map[string][]*memorySubscriber | ||||
| } | ||||
|  | ||||
| type memoryMessage struct { | ||||
| 	c     codec.Codec | ||||
| 	topic string | ||||
| 	ctx   context.Context | ||||
| 	body  []byte | ||||
| 	hdr   metadata.Metadata | ||||
| 	opts  broker.MessageOptions | ||||
| } | ||||
|  | ||||
| func (m *memoryMessage) Ack() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *memoryMessage) Body() []byte { | ||||
| 	return m.body | ||||
| } | ||||
|  | ||||
| func (m *memoryMessage) Header() metadata.Metadata { | ||||
| 	return m.hdr | ||||
| } | ||||
|  | ||||
| func (m *memoryMessage) Context() context.Context { | ||||
| 	return m.ctx | ||||
| } | ||||
|  | ||||
| func (m *memoryMessage) Topic() string { | ||||
| 	return "" | ||||
| } | ||||
|  | ||||
| func (m *memoryMessage) Unmarshal(dst interface{}, opts ...codec.Option) error { | ||||
| 	return m.c.Unmarshal(m.body, dst) | ||||
| } | ||||
|  | ||||
| type Subscriber struct { | ||||
| 	ctx     context.Context | ||||
| 	exit    chan bool | ||||
| 	handler interface{} | ||||
| type memorySubscriber struct { | ||||
| 	id      string | ||||
| 	topic   string | ||||
| 	exit    chan bool | ||||
| 	handler broker.Handler | ||||
| 	opts    broker.SubscribeOptions | ||||
| } | ||||
|  | ||||
| func (b *Broker) newCodec(ct string) (codec.Codec, error) { | ||||
| 	if idx := strings.IndexRune(ct, ';'); idx >= 0 { | ||||
| 		ct = ct[:idx] | ||||
| 	} | ||||
| 	b.mu.RLock() | ||||
| 	c, ok := b.opts.Codecs[ct] | ||||
| 	b.mu.RUnlock() | ||||
| 	if ok { | ||||
| 		return c, nil | ||||
| 	} | ||||
| 	return nil, codec.ErrUnknownContentType | ||||
| func (m *memoryBroker) Options() broker.Options { | ||||
| 	return m.opts | ||||
| } | ||||
|  | ||||
| func (b *Broker) Options() broker.Options { | ||||
| 	return b.opts | ||||
| func (m *memoryBroker) Address() string { | ||||
| 	return m.addr | ||||
| } | ||||
|  | ||||
| func (b *Broker) Address() string { | ||||
| 	return b.addr | ||||
| } | ||||
| func (m *memoryBroker) Connect() error { | ||||
| 	m.Lock() | ||||
| 	defer m.Unlock() | ||||
|  | ||||
| func (b *Broker) Connect(ctx context.Context) error { | ||||
| 	select { | ||||
| 	case <-ctx.Done(): | ||||
| 		return ctx.Err() | ||||
| 	default: | ||||
| 	} | ||||
|  | ||||
| 	b.mu.Lock() | ||||
| 	defer b.mu.Unlock() | ||||
|  | ||||
| 	if b.connected { | ||||
| 	if m.connected { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| @@ -108,234 +52,132 @@ func (b *Broker) Connect(ctx context.Context) error { | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	var rng rand.Rand | ||||
| 	i := rng.Intn(20000) | ||||
| 	i := rand.Intn(20000) | ||||
| 	// set addr with port | ||||
| 	addr = mnet.HostPort(addr, 10000+i) | ||||
|  | ||||
| 	b.addr = addr | ||||
| 	b.connected = true | ||||
| 	m.addr = addr | ||||
| 	m.connected = true | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *Broker) Disconnect(ctx context.Context) error { | ||||
| 	select { | ||||
| 	case <-ctx.Done(): | ||||
| 		return ctx.Err() | ||||
| 	default: | ||||
| 	} | ||||
| func (m *memoryBroker) Disconnect() error { | ||||
| 	m.Lock() | ||||
| 	defer m.Unlock() | ||||
|  | ||||
| 	b.mu.Lock() | ||||
| 	defer b.mu.Unlock() | ||||
|  | ||||
| 	if !b.connected { | ||||
| 	if !m.connected { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	b.connected = false | ||||
| 	m.connected = false | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *Broker) Init(opts ...broker.Option) error { | ||||
| func (m *memoryBroker) Init(opts ...broker.Option) error { | ||||
| 	for _, o := range opts { | ||||
| 		o(&b.opts) | ||||
| 		o(&m.opts) | ||||
| 	} | ||||
|  | ||||
| 	b.funcPublish = b.fnPublish | ||||
| 	b.funcSubscribe = b.fnSubscribe | ||||
|  | ||||
| 	b.opts.Hooks.EachPrev(func(hook options.Hook) { | ||||
| 		switch h := hook.(type) { | ||||
| 		case broker.HookPublish: | ||||
| 			b.funcPublish = h(b.funcPublish) | ||||
| 		case broker.HookSubscribe: | ||||
| 			b.funcSubscribe = h(b.funcSubscribe) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *Broker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...broker.MessageOption) (broker.Message, error) { | ||||
| 	options := broker.NewMessageOptions(opts...) | ||||
| 	if options.ContentType == "" { | ||||
| 		options.ContentType = b.opts.ContentType | ||||
| 	} | ||||
| 	m := &memoryMessage{ctx: ctx, hdr: hdr, opts: options} | ||||
| 	c, err := b.newCodec(m.opts.ContentType) | ||||
| 	if err == nil { | ||||
| 		m.body, err = c.Marshal(body) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| func (m *memoryBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error { | ||||
| 	m.RLock() | ||||
| 	if !m.connected { | ||||
| 		m.RUnlock() | ||||
| 		return errors.New("not connected") | ||||
| 	} | ||||
|  | ||||
| 	return m, nil | ||||
| } | ||||
|  | ||||
| func (b *Broker) Publish(ctx context.Context, topic string, messages ...broker.Message) error { | ||||
| 	return b.funcPublish(ctx, topic, messages...) | ||||
| } | ||||
|  | ||||
| func (b *Broker) fnPublish(ctx context.Context, topic string, messages ...broker.Message) error { | ||||
| 	return b.publish(ctx, topic, messages...) | ||||
| } | ||||
|  | ||||
| func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error { | ||||
| 	b.mu.RLock() | ||||
| 	if !b.connected { | ||||
| 		b.mu.RUnlock() | ||||
| 		return broker.ErrNotConnected | ||||
| 	} | ||||
| 	b.mu.RUnlock() | ||||
|  | ||||
| 	select { | ||||
| 	case <-ctx.Done(): | ||||
| 		return ctx.Err() | ||||
| 	default: | ||||
| 	} | ||||
|  | ||||
| 	b.mu.RLock() | ||||
| 	subs, ok := b.subscribers[topic] | ||||
| 	b.mu.RUnlock() | ||||
| 	subs, ok := m.Subscribers[topic] | ||||
| 	m.RUnlock() | ||||
| 	if !ok { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	var err error | ||||
|  | ||||
| 	for _, sub := range subs { | ||||
| 		switch s := sub.handler.(type) { | ||||
| 		default: | ||||
| 			if b.opts.Logger.V(logger.ErrorLevel) { | ||||
| 				b.opts.Logger.Error(ctx, "broker  handler error", broker.ErrInvalidHandler) | ||||
| 			} | ||||
| 		case func(broker.Message) error: | ||||
| 			for _, message := range messages { | ||||
| 				msg, ok := message.(*memoryMessage) | ||||
| 				if !ok { | ||||
| 					if b.opts.Logger.V(logger.ErrorLevel) { | ||||
| 						b.opts.Logger.Error(ctx, "broker handler error", broker.ErrInvalidMessage) | ||||
| 					} | ||||
| 				} | ||||
| 				msg.topic = topic | ||||
| 				if err = s(msg); err == nil && sub.opts.AutoAck { | ||||
| 					err = msg.Ack() | ||||
| 				} | ||||
| 				if err != nil { | ||||
| 					if b.opts.Logger.V(logger.ErrorLevel) { | ||||
| 						b.opts.Logger.Error(ctx, "broker handler error", err) | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		case func([]broker.Message) error: | ||||
| 			if err = s(messages); err == nil && sub.opts.AutoAck { | ||||
| 				for _, message := range messages { | ||||
| 					err = message.Ack() | ||||
| 					if err != nil { | ||||
| 						if b.opts.Logger.V(logger.ErrorLevel) { | ||||
| 							b.opts.Logger.Error(ctx, "broker handler error", err) | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 		if err := sub.handler(msg); err != nil { | ||||
| 			if eh := sub.opts.ErrorHandler; eh != nil { | ||||
| 				eh(msg, err) | ||||
| 			} | ||||
| 			continue | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *Broker) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...broker.SubscribeOption) (broker.Subscriber, error) { | ||||
| 	return b.funcSubscribe(ctx, topic, handler, opts...) | ||||
| } | ||||
| func (m *memoryBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) { | ||||
| 	m.RLock() | ||||
| 	if !m.connected { | ||||
| 		m.RUnlock() | ||||
| 		return nil, errors.New("not connected") | ||||
| 	} | ||||
| 	m.RUnlock() | ||||
|  | ||||
| func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interface{}, opts ...broker.SubscribeOption) (broker.Subscriber, error) { | ||||
| 	if err := broker.IsValidHandler(handler); err != nil { | ||||
| 		return nil, err | ||||
| 	var options broker.SubscribeOptions | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	b.mu.RLock() | ||||
| 	if !b.connected { | ||||
| 		b.mu.RUnlock() | ||||
| 		return nil, broker.ErrNotConnected | ||||
| 	} | ||||
| 	b.mu.RUnlock() | ||||
|  | ||||
| 	sid, err := id.New() | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	options := broker.NewSubscribeOptions(opts...) | ||||
|  | ||||
| 	sub := &Subscriber{ | ||||
| 	sub := &memorySubscriber{ | ||||
| 		exit:    make(chan bool, 1), | ||||
| 		id:      sid, | ||||
| 		id:      uuid.New().String(), | ||||
| 		topic:   topic, | ||||
| 		handler: handler, | ||||
| 		opts:    options, | ||||
| 		ctx:     ctx, | ||||
| 	} | ||||
|  | ||||
| 	b.mu.Lock() | ||||
| 	b.subscribers[topic] = append(b.subscribers[topic], sub) | ||||
| 	b.mu.Unlock() | ||||
| 	m.Lock() | ||||
| 	m.Subscribers[topic] = append(m.Subscribers[topic], sub) | ||||
| 	m.Unlock() | ||||
|  | ||||
| 	go func() { | ||||
| 		<-sub.exit | ||||
| 		b.mu.Lock() | ||||
| 		newSubscribers := make([]*Subscriber, 0, len(b.subscribers)-1) | ||||
| 		for _, sb := range b.subscribers[topic] { | ||||
| 		m.Lock() | ||||
| 		var newSubscribers []*memorySubscriber | ||||
| 		for _, sb := range m.Subscribers[topic] { | ||||
| 			if sb.id == sub.id { | ||||
| 				continue | ||||
| 			} | ||||
| 			newSubscribers = append(newSubscribers, sb) | ||||
| 		} | ||||
| 		b.subscribers[topic] = newSubscribers | ||||
| 		b.mu.Unlock() | ||||
| 		m.Subscribers[topic] = newSubscribers | ||||
| 		m.Unlock() | ||||
| 	}() | ||||
|  | ||||
| 	return sub, nil | ||||
| } | ||||
|  | ||||
| func (b *Broker) String() string { | ||||
| func (m *memoryBroker) String() string { | ||||
| 	return "memory" | ||||
| } | ||||
|  | ||||
| func (b *Broker) Name() string { | ||||
| 	return b.opts.Name | ||||
| } | ||||
|  | ||||
| func (b *Broker) Live() bool { | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (b *Broker) Ready() bool { | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (b *Broker) Health() bool { | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (m *Subscriber) Options() broker.SubscribeOptions { | ||||
| func (m *memorySubscriber) Options() broker.SubscribeOptions { | ||||
| 	return m.opts | ||||
| } | ||||
|  | ||||
| func (m *Subscriber) Topic() string { | ||||
| func (m *memorySubscriber) Topic() string { | ||||
| 	return m.topic | ||||
| } | ||||
|  | ||||
| func (m *Subscriber) Unsubscribe(ctx context.Context) error { | ||||
| func (m *memorySubscriber) Unsubscribe() error { | ||||
| 	m.exit <- true | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // NewBroker return new memory broker | ||||
| func NewBroker(opts ...broker.Option) broker.Broker { | ||||
| 	return &Broker{ | ||||
| 		opts:        broker.NewOptions(opts...), | ||||
| 		subscribers: make(map[string][]*Subscriber), | ||||
| 	options := broker.Options{ | ||||
| 		Context: context.Background(), | ||||
| 	} | ||||
|  | ||||
| 	rand.Seed(time.Now().UnixNano()) | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	return &memoryBroker{ | ||||
| 		opts:        options, | ||||
| 		Subscribers: make(map[string][]*memorySubscriber), | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,74 +1,50 @@ | ||||
| package broker | ||||
| package memory | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
|  | ||||
| 	"go.uber.org/atomic" | ||||
| 	"go.unistack.org/micro/v4/broker" | ||||
| 	"go.unistack.org/micro/v4/codec" | ||||
| 	"go.unistack.org/micro/v4/metadata" | ||||
| 	"github.com/asim/go-micro/v3/broker" | ||||
| ) | ||||
|  | ||||
| type hldr struct { | ||||
| 	c atomic.Int64 | ||||
| } | ||||
|  | ||||
| func (h *hldr) Handler(m broker.Message) error { | ||||
| 	h.c.Add(1) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func TestMemoryBroker(t *testing.T) { | ||||
| 	b := NewBroker(broker.Codec("application/octet-stream", codec.NewCodec())) | ||||
| 	ctx := context.Background() | ||||
| 	b := NewBroker() | ||||
|  | ||||
| 	if err := b.Init(); err != nil { | ||||
| 		t.Fatalf("Unexpected init error %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Connect(ctx); err != nil { | ||||
| 	if err := b.Connect(); err != nil { | ||||
| 		t.Fatalf("Unexpected connect error %v", err) | ||||
| 	} | ||||
|  | ||||
| 	topic := "test" | ||||
| 	count := int64(10) | ||||
| 	count := 10 | ||||
|  | ||||
| 	h := &hldr{} | ||||
| 	fn := func(m *broker.Message) error { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	sub, err := b.Subscribe(ctx, topic, h.Handler) | ||||
| 	sub, err := b.Subscribe(topic, fn) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Unexpected error subscribing %v", err) | ||||
| 	} | ||||
|  | ||||
| 	for i := int64(0); i < count; i++ { | ||||
| 		message, err := b.NewMessage(ctx, | ||||
| 			metadata.Pairs( | ||||
| 				"foo", "bar", | ||||
| 				"id", fmt.Sprintf("%d", i), | ||||
| 			), | ||||
| 			[]byte(`"hello world"`), | ||||
| 			broker.MessageContentType("application/octet-stream"), | ||||
| 		) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 	for i := 0; i < count; i++ { | ||||
| 		message := &broker.Message{ | ||||
| 			Header: map[string]string{ | ||||
| 				"foo": "bar", | ||||
| 				"id":  fmt.Sprintf("%d", i), | ||||
| 			}, | ||||
| 			Body: []byte(`hello world`), | ||||
| 		} | ||||
|  | ||||
| 		if err := b.Publish(ctx, topic, message); err != nil { | ||||
| 			t.Fatalf("Unexpected error publishing %d err: %v", i, err) | ||||
| 		if err := b.Publish(topic, message); err != nil { | ||||
| 			t.Fatalf("Unexpected error publishing %d", i) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if err := sub.Unsubscribe(ctx); err != nil { | ||||
| 	if err := sub.Unsubscribe(); err != nil { | ||||
| 		t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Disconnect(ctx); err != nil { | ||||
| 	if err := b.Disconnect(); err != nil { | ||||
| 		t.Fatalf("Unexpected connect error %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if h.c.Load() != count { | ||||
| 		t.Fatal("invalid messages count received") | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										179
									
								
								broker/noop.go
									
									
									
									
									
								
							
							
						
						
									
										179
									
								
								broker/noop.go
									
									
									
									
									
								
							| @@ -1,179 +0,0 @@ | ||||
| package broker | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"strings" | ||||
| 	"sync" | ||||
|  | ||||
| 	"go.unistack.org/micro/v4/codec" | ||||
| 	"go.unistack.org/micro/v4/metadata" | ||||
| 	"go.unistack.org/micro/v4/options" | ||||
| ) | ||||
|  | ||||
| type NoopBroker struct { | ||||
| 	funcPublish   FuncPublish | ||||
| 	funcSubscribe FuncSubscribe | ||||
| 	opts          Options | ||||
| 	mu            sync.RWMutex | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) newCodec(ct string) (codec.Codec, error) { | ||||
| 	if idx := strings.IndexRune(ct, ';'); idx >= 0 { | ||||
| 		ct = ct[:idx] | ||||
| 	} | ||||
| 	b.mu.RLock() | ||||
| 	c, ok := b.opts.Codecs[ct] | ||||
| 	b.mu.RUnlock() | ||||
| 	if ok { | ||||
| 		return c, nil | ||||
| 	} | ||||
| 	return nil, codec.ErrUnknownContentType | ||||
| } | ||||
|  | ||||
| func NewBroker(opts ...Option) *NoopBroker { | ||||
| 	b := &NoopBroker{opts: NewOptions(opts...)} | ||||
| 	b.funcPublish = b.fnPublish | ||||
| 	b.funcSubscribe = b.fnSubscribe | ||||
|  | ||||
| 	return b | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Health() bool { | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Live() bool { | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Ready() bool { | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Name() string { | ||||
| 	return b.opts.Name | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) String() string { | ||||
| 	return "noop" | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Options() Options { | ||||
| 	return b.opts | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Init(opts ...Option) error { | ||||
| 	for _, opt := range opts { | ||||
| 		opt(&b.opts) | ||||
| 	} | ||||
|  | ||||
| 	b.funcPublish = b.fnPublish | ||||
| 	b.funcSubscribe = b.fnSubscribe | ||||
|  | ||||
| 	b.opts.Hooks.EachPrev(func(hook options.Hook) { | ||||
| 		switch h := hook.(type) { | ||||
| 		case HookPublish: | ||||
| 			b.funcPublish = h(b.funcPublish) | ||||
| 		case HookSubscribe: | ||||
| 			b.funcSubscribe = h(b.funcSubscribe) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Connect(_ context.Context) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Disconnect(_ context.Context) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Address() string { | ||||
| 	return strings.Join(b.opts.Addrs, ",") | ||||
| } | ||||
|  | ||||
| type noopMessage struct { | ||||
| 	c    codec.Codec | ||||
| 	ctx  context.Context | ||||
| 	body []byte | ||||
| 	hdr  metadata.Metadata | ||||
| 	opts MessageOptions | ||||
| } | ||||
|  | ||||
| func (m *noopMessage) Ack() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *noopMessage) Body() []byte { | ||||
| 	return m.body | ||||
| } | ||||
|  | ||||
| func (m *noopMessage) Header() metadata.Metadata { | ||||
| 	return m.hdr | ||||
| } | ||||
|  | ||||
| func (m *noopMessage) Context() context.Context { | ||||
| 	return m.ctx | ||||
| } | ||||
|  | ||||
| func (m *noopMessage) Topic() string { | ||||
| 	return "" | ||||
| } | ||||
|  | ||||
| func (m *noopMessage) Unmarshal(dst interface{}, opts ...codec.Option) error { | ||||
| 	return m.c.Unmarshal(m.body, dst) | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...MessageOption) (Message, error) { | ||||
| 	options := NewMessageOptions(opts...) | ||||
| 	if options.ContentType == "" { | ||||
| 		options.ContentType = b.opts.ContentType | ||||
| 	} | ||||
| 	m := &noopMessage{ctx: ctx, hdr: hdr, opts: options} | ||||
| 	c, err := b.newCodec(m.opts.ContentType) | ||||
| 	if err == nil { | ||||
| 		m.body, err = c.Marshal(body) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return m, nil | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) fnPublish(_ context.Context, _ string, _ ...Message) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Publish(ctx context.Context, topic string, msg ...Message) error { | ||||
| 	return b.funcPublish(ctx, topic, msg...) | ||||
| } | ||||
|  | ||||
| type NoopSubscriber struct { | ||||
| 	ctx     context.Context | ||||
| 	topic   string | ||||
| 	handler interface{} | ||||
| 	opts    SubscribeOptions | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) fnSubscribe(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error) { | ||||
| 	return &NoopSubscriber{ctx: ctx, topic: topic, opts: NewSubscribeOptions(opts...), handler: handler}, nil | ||||
| } | ||||
|  | ||||
| func (b *NoopBroker) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error) { | ||||
| 	return b.funcSubscribe(ctx, topic, handler, opts...) | ||||
| } | ||||
|  | ||||
| func (s *NoopSubscriber) Options() SubscribeOptions { | ||||
| 	return s.opts | ||||
| } | ||||
|  | ||||
| func (s *NoopSubscriber) Topic() string { | ||||
| 	return s.topic | ||||
| } | ||||
|  | ||||
| func (s *NoopSubscriber) Unsubscribe(_ context.Context) error { | ||||
| 	return nil | ||||
| } | ||||
| @@ -1,35 +0,0 @@ | ||||
| package broker | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| type testHook struct { | ||||
| 	f bool | ||||
| } | ||||
|  | ||||
| func (t *testHook) Publish1(fn FuncPublish) FuncPublish { | ||||
| 	return func(ctx context.Context, topic string, messages ...Message) error { | ||||
| 		t.f = true | ||||
| 		return fn(ctx, topic, messages...) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestNoopHook(t *testing.T) { | ||||
| 	h := &testHook{} | ||||
|  | ||||
| 	b := NewBroker(Hooks(HookPublish(h.Publish1))) | ||||
|  | ||||
| 	if err := b.Init(); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	if err := b.Publish(context.TODO(), "", nil); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	if !h.f { | ||||
| 		t.Fatal("hook not works") | ||||
| 	} | ||||
| } | ||||
| @@ -3,146 +3,65 @@ package broker | ||||
| import ( | ||||
| 	"context" | ||||
| 	"crypto/tls" | ||||
| 	"time" | ||||
|  | ||||
| 	"go.unistack.org/micro/v4/codec" | ||||
| 	"go.unistack.org/micro/v4/logger" | ||||
| 	"go.unistack.org/micro/v4/meter" | ||||
| 	"go.unistack.org/micro/v4/options" | ||||
| 	"go.unistack.org/micro/v4/register" | ||||
| 	"go.unistack.org/micro/v4/sync" | ||||
| 	"go.unistack.org/micro/v4/tracer" | ||||
| 	"github.com/asim/go-micro/v3/codec" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| ) | ||||
|  | ||||
| // Options struct | ||||
| type Options struct { | ||||
| 	// Name holds the broker name | ||||
| 	Name string | ||||
| 	Addrs  []string | ||||
| 	Secure bool | ||||
| 	Codec  codec.Marshaler | ||||
|  | ||||
| 	// Tracer used for tracing | ||||
| 	Tracer tracer.Tracer | ||||
| 	// Register can be used for clustering | ||||
| 	Register register.Register | ||||
| 	// Codecs holds the codecs for marshal/unmarshal based on content-type | ||||
| 	Codecs map[string]codec.Codec | ||||
| 	// Logger used for logging | ||||
| 	Logger logger.Logger | ||||
| 	// Meter used for metrics | ||||
| 	Meter meter.Meter | ||||
| 	// Context holds external options | ||||
| 	Context context.Context | ||||
|  | ||||
| 	// Wait waits for a collection of goroutines to finish | ||||
| 	Wait *sync.WaitGroup | ||||
| 	// TLSConfig holds tls.TLSConfig options | ||||
| 	TLSConfig *tls.Config | ||||
|  | ||||
| 	// Addrs holds the broker address | ||||
| 	Addrs []string | ||||
| 	// Hooks can be run before broker Publish/BatchPublish and | ||||
| 	// Subscribe/BatchSubscribe methods | ||||
| 	Hooks options.Hooks | ||||
|  | ||||
| 	// GracefulTimeout contains time to wait to finish in flight requests | ||||
| 	GracefulTimeout time.Duration | ||||
|  | ||||
| 	// ContentType will be used if no content-type set when creating message | ||||
| 	ContentType string | ||||
| 	// Registry used for clustering | ||||
| 	Registry registry.Registry | ||||
| 	// Other options for implementations of the interface | ||||
| 	// can be stored in a context | ||||
| 	Context context.Context | ||||
| } | ||||
|  | ||||
| // NewOptions create new Options | ||||
| func NewOptions(opts ...Option) Options { | ||||
| 	options := Options{ | ||||
| 		Register:        register.DefaultRegister, | ||||
| 		Logger:          logger.DefaultLogger, | ||||
| 		Context:         context.Background(), | ||||
| 		Meter:           meter.DefaultMeter, | ||||
| 		Codecs:          make(map[string]codec.Codec), | ||||
| 		Tracer:          tracer.DefaultTracer, | ||||
| 		GracefulTimeout: DefaultGracefulTimeout, | ||||
| 		ContentType:     DefaultContentType, | ||||
| 	} | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	return options | ||||
| type PublishOptions struct { | ||||
| 	// Other options for implementations of the interface | ||||
| 	// can be stored in a context | ||||
| 	Context context.Context | ||||
| } | ||||
|  | ||||
| // DefaultContentType is the default content-type if not specified | ||||
| var DefaultContentType = "" | ||||
| type SubscribeOptions struct { | ||||
| 	// Handler executed when errors occur processing messages | ||||
| 	ErrorHandler ErrorHandler | ||||
|  | ||||
| // Context sets the context option | ||||
| func Context(ctx context.Context) Option { | ||||
| 	return func(o *Options) { | ||||
| 	// Subscribers with the same queue name | ||||
| 	// will create a shared subscription where each | ||||
| 	// receives a subset of messages. | ||||
| 	Queue string | ||||
|  | ||||
| 	// Other options for implementations of the interface | ||||
| 	// can be stored in a context | ||||
| 	Context context.Context | ||||
| } | ||||
|  | ||||
| type Option func(*Options) | ||||
|  | ||||
| type PublishOption func(*PublishOptions) | ||||
|  | ||||
| // PublishContext set context | ||||
| func PublishContext(ctx context.Context) PublishOption { | ||||
| 	return func(o *PublishOptions) { | ||||
| 		o.Context = ctx | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // ContentType used by default if not specified | ||||
| func ContentType(ct string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.ContentType = ct | ||||
| 	} | ||||
| } | ||||
| type SubscribeOption func(*SubscribeOptions) | ||||
|  | ||||
| // MessageOptions struct | ||||
| type MessageOptions struct { | ||||
| 	// ContentType for message body | ||||
| 	ContentType string | ||||
| 	// BodyOnly flag says the message contains raw body bytes and don't need | ||||
| 	// codec Marshal method | ||||
| 	BodyOnly bool | ||||
| 	// Context holds custom options | ||||
| 	Context context.Context | ||||
| } | ||||
| func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions { | ||||
| 	opt := SubscribeOptions{} | ||||
|  | ||||
| // NewMessageOptions creates MessageOptions struct | ||||
| func NewMessageOptions(opts ...MessageOption) MessageOptions { | ||||
| 	options := MessageOptions{ | ||||
| 		Context: context.Background(), | ||||
| 	} | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 		o(&opt) | ||||
| 	} | ||||
| 	return options | ||||
| } | ||||
|  | ||||
| // SubscribeOptions struct | ||||
| type SubscribeOptions struct { | ||||
| 	// Context holds external options | ||||
| 	Context context.Context | ||||
| 	// Group holds consumer group | ||||
| 	Group string | ||||
| 	// AutoAck flag specifies auto ack of incoming message when no error happens | ||||
| 	AutoAck bool | ||||
| 	// BodyOnly flag specifies that message contains only body bytes without header | ||||
| 	BodyOnly bool | ||||
| 	// BatchSize flag specifies max batch size | ||||
| 	BatchSize int | ||||
| 	// BatchWait flag specifies max wait time for batch filling | ||||
| 	BatchWait time.Duration | ||||
| } | ||||
|  | ||||
| // Option func | ||||
| type Option func(*Options) | ||||
|  | ||||
| // MessageOption func | ||||
| type MessageOption func(*MessageOptions) | ||||
|  | ||||
| // MessageContentType sets message content-type that used to Marshal | ||||
| func MessageContentType(ct string) MessageOption { | ||||
| 	return func(o *MessageOptions) { | ||||
| 		o.ContentType = ct | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // MessageBodyOnly publish only body of the message | ||||
| func MessageBodyOnly(b bool) MessageOption { | ||||
| 	return func(o *MessageOptions) { | ||||
| 		o.BodyOnly = b | ||||
| 	} | ||||
| 	return opt | ||||
| } | ||||
|  | ||||
| // Addrs sets the host addresses to be used by the broker | ||||
| @@ -152,116 +71,52 @@ func Addrs(addrs ...string) Option { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Codec sets the codec used for encoding/decoding messages | ||||
| func Codec(ct string, c codec.Codec) Option { | ||||
| // Codec sets the codec used for encoding/decoding used where | ||||
| // a broker does not support headers | ||||
| func Codec(c codec.Marshaler) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Codecs[ct] = c | ||||
| 		o.Codec = c | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SubscribeGroup sets the name of the queue to share messages on | ||||
| func SubscribeGroup(name string) SubscribeOption { | ||||
| // ErrorHandler will catch all broker errors that cant be handled | ||||
| // in normal way, for example Codec errors | ||||
| func HandleError(h ErrorHandler) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		o.Group = name | ||||
| 		o.ErrorHandler = h | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Register sets register option | ||||
| func Register(r register.Register) Option { | ||||
| // Queue sets the name of the queue to share messages on | ||||
| func Queue(name string) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		o.Queue = name | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func Registry(r registry.Registry) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Register = r | ||||
| 		o.Registry = r | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TLSConfig sets the TLS Config | ||||
| // Secure communication with the broker | ||||
| func Secure(b bool) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Secure = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Specify TLS Config | ||||
| func TLSConfig(t *tls.Config) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.TLSConfig = t | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Logger sets the logger | ||||
| func Logger(l logger.Logger) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Logger = l | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Tracer to be used for tracing | ||||
| func Tracer(t tracer.Tracer) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Tracer = t | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Meter sets the meter | ||||
| func Meter(m meter.Meter) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Meter = m | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Name sets the name | ||||
| func Name(n string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Name = n | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Hooks sets hook runs before action | ||||
| func Hooks(h ...options.Hook) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Hooks = append(o.Hooks, h...) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SubscribeContext set context | ||||
| func SubscribeContext(ctx context.Context) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		o.Context = ctx | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SubscribeAutoAck contol auto acking of messages | ||||
| // after they have been handled. | ||||
| func SubscribeAutoAck(b bool) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		o.AutoAck = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SubscribeBodyOnly consumes only body of the message | ||||
| func SubscribeBodyOnly(b bool) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		o.BodyOnly = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SubscribeBatchSize specifies max batch size | ||||
| func SubscribeBatchSize(n int) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		o.BatchSize = n | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SubscribeBatchWait specifies max batch wait time | ||||
| func SubscribeBatchWait(td time.Duration) SubscribeOption { | ||||
| 	return func(o *SubscribeOptions) { | ||||
| 		o.BatchWait = td | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SubscribeOption func | ||||
| type SubscribeOption func(*SubscribeOptions) | ||||
|  | ||||
| // NewSubscribeOptions creates new SubscribeOptions | ||||
| func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions { | ||||
| 	options := SubscribeOptions{ | ||||
| 		AutoAck: true, | ||||
| 		Context: context.Background(), | ||||
| 	} | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
| 	return options | ||||
| } | ||||
|   | ||||
| @@ -1,14 +0,0 @@ | ||||
| package broker | ||||
|  | ||||
| // IsValidHandler func signature | ||||
| func IsValidHandler(sub interface{}) error { | ||||
| 	switch sub.(type) { | ||||
| 	default: | ||||
| 		return ErrInvalidHandler | ||||
| 	case func(Message) error: | ||||
| 		break | ||||
| 	case func([]Message) error: | ||||
| 		break | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										36
									
								
								build/build.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								build/build.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| // Package build is for building source into a package | ||||
| package build | ||||
|  | ||||
| // Build is an interface for building packages | ||||
| type Build interface { | ||||
| 	// Package builds a package | ||||
| 	Package(*Source) (*Package, error) | ||||
| 	// Remove removes the package | ||||
| 	Remove(*Package) error | ||||
| 	// Implementation of build | ||||
| 	String() string | ||||
| } | ||||
|  | ||||
| // Source is the source of a build | ||||
| type Source struct { | ||||
| 	// Name of the source | ||||
| 	Name string | ||||
| 	// Path to the source if local | ||||
| 	Path string | ||||
| 	// Language is the language of code | ||||
| 	Language string | ||||
| 	// Location of the source | ||||
| 	Repository string | ||||
| } | ||||
|  | ||||
| // Package is packaged format for source | ||||
| type Package struct { | ||||
| 	// Name of the package | ||||
| 	Name string | ||||
| 	// Location of the package | ||||
| 	Path string | ||||
| 	// Type of package e.g tarball, binary, docker | ||||
| 	Type string | ||||
| 	// Source of the package | ||||
| 	Source *Source | ||||
| } | ||||
							
								
								
									
										76
									
								
								build/golang/golang.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								build/golang/golang.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,76 @@ | ||||
| // Package golang is a go package manager | ||||
| package golang | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"path/filepath" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/build" | ||||
| ) | ||||
|  | ||||
| type goBuild struct { | ||||
| 	Options build.Options | ||||
| 	Cmd     string | ||||
| 	Path    string | ||||
| } | ||||
|  | ||||
| // whichGo locates the go command | ||||
| func whichGo() string { | ||||
| 	// check GOROOT | ||||
| 	if gr := os.Getenv("GOROOT"); len(gr) > 0 { | ||||
| 		return filepath.Join(gr, "bin", "go") | ||||
| 	} | ||||
|  | ||||
| 	// check path | ||||
| 	for _, p := range filepath.SplitList(os.Getenv("PATH")) { | ||||
| 		bin := filepath.Join(p, "go") | ||||
| 		if _, err := os.Stat(bin); err == nil { | ||||
| 			return bin | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// best effort | ||||
| 	return "go" | ||||
| } | ||||
|  | ||||
| func (g *goBuild) Package(src *build.Source) (*build.Package, error) { | ||||
| 	name := src.Name | ||||
| 	binary := filepath.Join(g.Path, name) | ||||
| 	source := src.Path | ||||
|  | ||||
| 	cmd := exec.Command(g.Cmd, "build", "-o", binary, source) | ||||
| 	if err := cmd.Run(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return &build.Package{ | ||||
| 		Name:   name, | ||||
| 		Path:   binary, | ||||
| 		Type:   g.String(), | ||||
| 		Source: src, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (g *goBuild) Remove(b *build.Package) error { | ||||
| 	binary := filepath.Join(b.Path, b.Name) | ||||
| 	return os.Remove(binary) | ||||
| } | ||||
|  | ||||
| func (g *goBuild) String() string { | ||||
| 	return "golang" | ||||
| } | ||||
|  | ||||
| func NewBuild(opts ...build.Option) build.Build { | ||||
| 	options := build.Options{ | ||||
| 		Path: os.TempDir(), | ||||
| 	} | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
| 	return &goBuild{ | ||||
| 		Options: options, | ||||
| 		Cmd:     whichGo(), | ||||
| 		Path:    options.Path, | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										15
									
								
								build/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								build/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| package build | ||||
|  | ||||
| type Options struct { | ||||
| 	// local path to download source | ||||
| 	Path string | ||||
| } | ||||
|  | ||||
| type Option func(o *Options) | ||||
|  | ||||
| // Local path for repository | ||||
| func Path(p string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Path = p | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										46
									
								
								build/tar/tar.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								build/tar/tar.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| // Package tar basically tarballs source code | ||||
| package tar | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/build" | ||||
| ) | ||||
|  | ||||
| type tarBuild struct{} | ||||
|  | ||||
| func (t *tarBuild) Package(src *build.Source) (*build.Package, error) { | ||||
| 	name := src.Name | ||||
| 	pkg := name + ".tar.gz" | ||||
| 	// path to the tarball | ||||
| 	path := filepath.Join(os.TempDir(), src.Path, pkg) | ||||
|  | ||||
| 	// create a temp directory | ||||
| 	if err := os.MkdirAll(filepath.Join(os.TempDir(), src.Path), 0755); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	if err := Compress(src.Path, path); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return &build.Package{ | ||||
| 		Name:   name, | ||||
| 		Path:   path, | ||||
| 		Type:   t.String(), | ||||
| 		Source: src, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (t *tarBuild) Remove(b *build.Package) error { | ||||
| 	return os.Remove(b.Path) | ||||
| } | ||||
|  | ||||
| func (t *tarBuild) String() string { | ||||
| 	return "tar.gz" | ||||
| } | ||||
|  | ||||
| func NewBuild(opts ...build.Option) build.Build { | ||||
| 	return new(tarBuild) | ||||
| } | ||||
							
								
								
									
										92
									
								
								build/tar/util.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								build/tar/util.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,92 @@ | ||||
| package tar | ||||
|  | ||||
| import ( | ||||
| 	"archive/tar" | ||||
| 	"bytes" | ||||
| 	"compress/gzip" | ||||
| 	"io" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| func Compress(source, dest string) error { | ||||
| 	// tar + gzip | ||||
| 	var buf bytes.Buffer | ||||
| 	_ = compress(source, &buf) | ||||
|  | ||||
| 	// write the .tar.gzip | ||||
| 	fileToWrite, err := os.OpenFile(dest, os.O_CREATE|os.O_RDWR, 0666) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	_, err = io.Copy(fileToWrite, &buf) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func compress(src string, buf io.Writer) error { | ||||
| 	// tar > gzip > buf | ||||
| 	zr := gzip.NewWriter(buf) | ||||
| 	tw := tar.NewWriter(zr) | ||||
|  | ||||
| 	// walk through every file in the folder | ||||
| 	filepath.Walk(src, func(file string, fi os.FileInfo, err error) error { | ||||
|  | ||||
| 		// generate tar header | ||||
| 		header, err := tar.FileInfoHeader(fi, file) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		// must provide real name | ||||
| 		// (see https://golang.org/src/archive/tar/common.go?#L626) | ||||
|  | ||||
| 		srcWithSlash := src | ||||
| 		if !strings.HasSuffix(src, string(filepath.Separator)) { | ||||
| 			srcWithSlash = src + string(filepath.Separator) | ||||
| 		} | ||||
| 		header.Name = strings.ReplaceAll(file, srcWithSlash, "") | ||||
| 		if header.Name == src || len(strings.TrimSpace(header.Name)) == 0 { | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		// @todo This is a quick hack to speed up whole repo uploads | ||||
| 		// https://github.com/micro/micro/pull/956 | ||||
| 		if !fi.IsDir() && !strings.HasSuffix(header.Name, ".go") && | ||||
| 			!strings.HasSuffix(header.Name, ".mod") && | ||||
| 			!strings.HasSuffix(header.Name, ".sum") { | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		// write header | ||||
| 		if err := tw.WriteHeader(header); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if fi.IsDir() { | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		// if not a dir, write file content | ||||
|  | ||||
| 		data, err := os.Open(file) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if _, err := io.Copy(tw, data); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		return nil | ||||
| 	}) | ||||
|  | ||||
| 	// produce tar | ||||
| 	if err := tw.Close(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	// produce gzip | ||||
| 	if err := zr.Close(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	// | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										29
									
								
								cache/cache.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								cache/cache.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| // Package cache is a caching interface | ||||
| package cache | ||||
|  | ||||
| // Cache is an interface for caching | ||||
| type Cache interface { | ||||
| 	// Initialise options | ||||
| 	Init(...Option) error | ||||
| 	// Get a value | ||||
| 	Get(key string) (interface{}, error) | ||||
| 	// Set a value | ||||
| 	Set(key string, val interface{}) error | ||||
| 	// Delete a value | ||||
| 	Delete(key string) error | ||||
| 	// Name of the implementation | ||||
| 	String() string | ||||
| } | ||||
|  | ||||
| type Options struct { | ||||
| 	Nodes []string | ||||
| } | ||||
|  | ||||
| type Option func(o *Options) | ||||
|  | ||||
| // Nodes sets the nodes for the cache | ||||
| func Nodes(v ...string) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Nodes = v | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										56
									
								
								cache/memory/memory.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								cache/memory/memory.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| // Package memory is an in memory cache | ||||
| package memory | ||||
|  | ||||
| import ( | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/cache" | ||||
| 	"github.com/asim/go-micro/v3/errors" | ||||
| ) | ||||
|  | ||||
| type memoryCache struct { | ||||
| 	// TODO: use a decent caching library | ||||
| 	sync.RWMutex | ||||
| 	values map[string]interface{} | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Init(opts ...cache.Option) error { | ||||
| 	// TODO: implement | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Get(key string) (interface{}, error) { | ||||
| 	m.RLock() | ||||
| 	defer m.RUnlock() | ||||
|  | ||||
| 	v, ok := m.values[key] | ||||
| 	if !ok { | ||||
| 		return nil, errors.NotFound("go.micro.cache", key+" not found") | ||||
| 	} | ||||
|  | ||||
| 	return v, nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Set(key string, val interface{}) error { | ||||
| 	m.Lock() | ||||
| 	m.values[key] = val | ||||
| 	m.Unlock() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) Delete(key string) error { | ||||
| 	m.Lock() | ||||
| 	delete(m.values, key) | ||||
| 	m.Unlock() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *memoryCache) String() string { | ||||
| 	return "memory" | ||||
| } | ||||
|  | ||||
| func NewCache(opts ...cache.Option) cache.Cache { | ||||
| 	return &memoryCache{ | ||||
| 		values: make(map[string]interface{}), | ||||
| 	} | ||||
| } | ||||
| @@ -2,29 +2,13 @@ package client | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"math" | ||||
| 	"time" | ||||
|  | ||||
| 	"go.unistack.org/micro/v4/util/backoff" | ||||
| 	"github.com/asim/go-micro/v3/util/backoff" | ||||
| ) | ||||
|  | ||||
| // BackoffFunc is the backoff call func | ||||
| type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error) | ||||
|  | ||||
| // BackoffExp using exponential backoff func | ||||
| func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, error) { | ||||
| func exponentialBackoff(ctx context.Context, req Request, attempts int) (time.Duration, error) { | ||||
| 	return backoff.Do(attempts), nil | ||||
| } | ||||
|  | ||||
| // BackoffInterval specifies randomization interval for backoff func | ||||
| func BackoffInterval(minTime time.Duration, maxTime time.Duration) BackoffFunc { | ||||
| 	return func(_ context.Context, _ Request, attempts int) (time.Duration, error) { | ||||
| 		td := time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100 | ||||
| 		if td < minTime { | ||||
| 			return minTime, nil | ||||
| 		} else if td > maxTime { | ||||
| 			return maxTime, nil | ||||
| 		} | ||||
| 		return td, nil | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -4,9 +4,11 @@ import ( | ||||
| 	"context" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/codec" | ||||
| ) | ||||
|  | ||||
| func TestBackoffExp(t *testing.T) { | ||||
| func TestBackoff(t *testing.T) { | ||||
| 	results := []time.Duration{ | ||||
| 		0 * time.Second, | ||||
| 		100 * time.Millisecond, | ||||
| @@ -22,7 +24,7 @@ func TestBackoffExp(t *testing.T) { | ||||
| 	} | ||||
|  | ||||
| 	for i := 0; i < 5; i++ { | ||||
| 		d, err := BackoffExp(context.TODO(), r, i) | ||||
| 		d, err := exponentialBackoff(context.TODO(), r, i) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
| @@ -33,24 +35,62 @@ func TestBackoffExp(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBackoffInterval(t *testing.T) { | ||||
| 	minTime := 100 * time.Millisecond | ||||
| 	maxTime := 300 * time.Millisecond | ||||
| type testRequest struct { | ||||
| 	service     string | ||||
| 	method      string | ||||
| 	endpoint    string | ||||
| 	contentType string | ||||
| 	codec       codec.Codec | ||||
| 	body        interface{} | ||||
| 	opts        RequestOptions | ||||
| } | ||||
|  | ||||
| 	r := &testRequest{ | ||||
| 		service: "test", | ||||
| 		method:  "test", | ||||
| func newRequest(service, endpoint string, request interface{}, contentType string, reqOpts ...RequestOption) Request { | ||||
| 	var opts RequestOptions | ||||
|  | ||||
| 	for _, o := range reqOpts { | ||||
| 		o(&opts) | ||||
| 	} | ||||
|  | ||||
| 	fn := BackoffInterval(minTime, maxTime) | ||||
| 	for i := 0; i < 5; i++ { | ||||
| 		d, err := fn(context.TODO(), r, i) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
| 	// set the content-type specified | ||||
| 	if len(opts.ContentType) > 0 { | ||||
| 		contentType = opts.ContentType | ||||
| 	} | ||||
|  | ||||
| 		if d < minTime || d > maxTime { | ||||
| 			t.Fatalf("Expected %v < %v < %v", minTime, d, maxTime) | ||||
| 		} | ||||
| 	return &testRequest{ | ||||
| 		service:     service, | ||||
| 		method:      endpoint, | ||||
| 		endpoint:    endpoint, | ||||
| 		body:        request, | ||||
| 		contentType: contentType, | ||||
| 		opts:        opts, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (r *testRequest) ContentType() string { | ||||
| 	return r.contentType | ||||
| } | ||||
|  | ||||
| func (r *testRequest) Service() string { | ||||
| 	return r.service | ||||
| } | ||||
|  | ||||
| func (r *testRequest) Method() string { | ||||
| 	return r.method | ||||
| } | ||||
|  | ||||
| func (r *testRequest) Endpoint() string { | ||||
| 	return r.endpoint | ||||
| } | ||||
|  | ||||
| func (r *testRequest) Body() interface{} { | ||||
| 	return r.body | ||||
| } | ||||
|  | ||||
| func (r *testRequest) Codec() codec.Writer { | ||||
| 	return r.codec | ||||
| } | ||||
|  | ||||
| func (r *testRequest) Stream() bool { | ||||
| 	return r.opts.Stream | ||||
| } | ||||
|   | ||||
| @@ -5,47 +5,29 @@ import ( | ||||
| 	"context" | ||||
| 	"time" | ||||
|  | ||||
| 	"go.unistack.org/micro/v4/codec" | ||||
| 	"go.unistack.org/micro/v4/metadata" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	// DefaultClient is the global default client | ||||
| 	DefaultClient = NewClient() | ||||
| 	// DefaultContentType is the default content-type if not specified | ||||
| 	DefaultContentType = "" | ||||
| 	// DefaultBackoff is the default backoff function for retries (minimum 10 millisecond and maximum 5 second) | ||||
| 	DefaultBackoff = BackoffInterval(10*time.Millisecond, 5*time.Second) | ||||
| 	// DefaultRetry is the default check-for-retry function for retries | ||||
| 	DefaultRetry = RetryNever | ||||
| 	// DefaultRetries is the default number of times a request is tried | ||||
| 	DefaultRetries = 0 | ||||
| 	// DefaultRequestTimeout is the default request timeout | ||||
| 	DefaultRequestTimeout = time.Second * 5 | ||||
| 	// DefaultPoolSize sets the connection pool size | ||||
| 	DefaultPoolSize = 100 | ||||
| 	// DefaultPoolTTL sets the connection pool ttl | ||||
| 	DefaultPoolTTL = time.Minute | ||||
| 	"github.com/asim/go-micro/v3/codec" | ||||
| ) | ||||
|  | ||||
| // Client is the interface used to make requests to services. | ||||
| // It supports Request/Response via Transport and Publishing via the Broker. | ||||
| // It also supports bidirectional streaming of requests. | ||||
| type Client interface { | ||||
| 	Name() string | ||||
| 	Init(opts ...Option) error | ||||
| 	Init(...Option) error | ||||
| 	Options() Options | ||||
| 	NewRequest(service string, endpoint string, req interface{}, opts ...RequestOption) Request | ||||
| 	NewMessage(topic string, msg interface{}, opts ...MessageOption) Message | ||||
| 	NewRequest(service, endpoint string, req interface{}, reqOpts ...RequestOption) Request | ||||
| 	Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error | ||||
| 	Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) | ||||
| 	Publish(ctx context.Context, msg Message, opts ...PublishOption) error | ||||
| 	String() string | ||||
| } | ||||
|  | ||||
| type ( | ||||
| 	FuncCall   func(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error | ||||
| 	HookCall   func(next FuncCall) FuncCall | ||||
| 	FuncStream func(ctx context.Context, req Request, opts ...CallOption) (Stream, error) | ||||
| 	HookStream func(next FuncStream) FuncStream | ||||
| ) | ||||
| // Message is the interface for publishing asynchronously | ||||
| type Message interface { | ||||
| 	Topic() string | ||||
| 	Payload() interface{} | ||||
| 	ContentType() string | ||||
| } | ||||
|  | ||||
| // Request is the interface for a synchronous request used by Call or Stream | ||||
| type Request interface { | ||||
| @@ -60,7 +42,7 @@ type Request interface { | ||||
| 	// The unencoded request body | ||||
| 	Body() interface{} | ||||
| 	// Write to the encoded request writer. This is nil before a call is made | ||||
| 	Codec() codec.Codec | ||||
| 	Codec() codec.Writer | ||||
| 	// indicates whether the request will be a streaming one rather than unary | ||||
| 	Stream() bool | ||||
| } | ||||
| @@ -68,9 +50,9 @@ type Request interface { | ||||
| // Response is the response received from a service | ||||
| type Response interface { | ||||
| 	// Read the response | ||||
| 	Codec() codec.Codec | ||||
| 	// Header data | ||||
| 	Header() metadata.Metadata | ||||
| 	Codec() codec.Reader | ||||
| 	// read the header | ||||
| 	Header() map[string]string | ||||
| 	// Read the undecoded response | ||||
| 	Read() ([]byte, error) | ||||
| } | ||||
| @@ -84,19 +66,13 @@ type Stream interface { | ||||
| 	// The response read | ||||
| 	Response() Response | ||||
| 	// Send will encode and send a request | ||||
| 	Send(msg interface{}) error | ||||
| 	Send(interface{}) error | ||||
| 	// Recv will decode and read a response | ||||
| 	Recv(msg interface{}) error | ||||
| 	// SendMsg will encode and send a request | ||||
| 	SendMsg(msg interface{}) error | ||||
| 	// RecvMsg will decode and read a response | ||||
| 	RecvMsg(msg interface{}) error | ||||
| 	Recv(interface{}) error | ||||
| 	// Error returns the stream error | ||||
| 	Error() error | ||||
| 	// Close closes the stream | ||||
| 	Close() error | ||||
| 	// CloseSend closes the send direction of the stream | ||||
| 	CloseSend() error | ||||
| } | ||||
|  | ||||
| // Option used by the Client | ||||
| @@ -105,5 +81,26 @@ type Option func(*Options) | ||||
| // CallOption used by Call or Stream | ||||
| type CallOption func(*CallOptions) | ||||
|  | ||||
| // PublishOption used by Publish | ||||
| type PublishOption func(*PublishOptions) | ||||
|  | ||||
| // MessageOption used by NewMessage | ||||
| type MessageOption func(*MessageOptions) | ||||
|  | ||||
| // RequestOption used by NewRequest | ||||
| type RequestOption func(*RequestOptions) | ||||
|  | ||||
| var ( | ||||
| 	// DefaultBackoff is the default backoff function for retries | ||||
| 	DefaultBackoff = exponentialBackoff | ||||
| 	// DefaultRetry is the default check-for-retry function for retries | ||||
| 	DefaultRetry = RetryOnError | ||||
| 	// DefaultRetries is the default number of times a request is tried | ||||
| 	DefaultRetries = 1 | ||||
| 	// DefaultRequestTimeout is the default request timeout | ||||
| 	DefaultRequestTimeout = time.Second * 5 | ||||
| 	// DefaultPoolSize sets the connection pool size | ||||
| 	DefaultPoolSize = 100 | ||||
| 	// DefaultPoolTTL sets the connection pool ttl | ||||
| 	DefaultPoolTTL = time.Minute | ||||
| ) | ||||
|   | ||||
| @@ -1,23 +0,0 @@ | ||||
| package client | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| ) | ||||
|  | ||||
| type clientCallOptions struct { | ||||
| 	Client | ||||
| 	opts []CallOption | ||||
| } | ||||
|  | ||||
| func (s *clientCallOptions) Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error { | ||||
| 	return s.Client.Call(ctx, req, rsp, append(s.opts, opts...)...) | ||||
| } | ||||
|  | ||||
| func (s *clientCallOptions) Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) { | ||||
| 	return s.Client.Stream(ctx, req, append(s.opts, opts...)...) | ||||
| } | ||||
|  | ||||
| // NewClientCallOptions add CallOption to every call | ||||
| func NewClientCallOptions(c Client, opts ...CallOption) Client { | ||||
| 	return &clientCallOptions{c, opts} | ||||
| } | ||||
| @@ -1,53 +0,0 @@ | ||||
| package client | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| ) | ||||
|  | ||||
| type clientKey struct{} | ||||
|  | ||||
| // FromContext get client from context | ||||
| func FromContext(ctx context.Context) (Client, bool) { | ||||
| 	if ctx == nil { | ||||
| 		return nil, false | ||||
| 	} | ||||
| 	c, ok := ctx.Value(clientKey{}).(Client) | ||||
| 	return c, ok | ||||
| } | ||||
|  | ||||
| // MustContext get client from context | ||||
| func MustContext(ctx context.Context) Client { | ||||
| 	c, ok := FromContext(ctx) | ||||
| 	if !ok { | ||||
| 		panic("missing client") | ||||
| 	} | ||||
| 	return c | ||||
| } | ||||
|  | ||||
| // NewContext put client in context | ||||
| func NewContext(ctx context.Context, c Client) context.Context { | ||||
| 	if ctx == nil { | ||||
| 		ctx = context.Background() | ||||
| 	} | ||||
| 	return context.WithValue(ctx, clientKey{}, c) | ||||
| } | ||||
|  | ||||
| // SetCallOption returns a function to setup a context with given value | ||||
| func SetCallOption(k, v interface{}) CallOption { | ||||
| 	return func(o *CallOptions) { | ||||
| 		if o.Context == nil { | ||||
| 			o.Context = context.Background() | ||||
| 		} | ||||
| 		o.Context = context.WithValue(o.Context, k, v) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SetOption returns a function to setup a context with given value | ||||
| func SetOption(k, v interface{}) Option { | ||||
| 	return func(o *Options) { | ||||
| 		if o.Context == nil { | ||||
| 			o.Context = context.Background() | ||||
| 		} | ||||
| 		o.Context = context.WithValue(o.Context, k, v) | ||||
| 	} | ||||
| } | ||||
| @@ -1,62 +0,0 @@ | ||||
| package client | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestFromContext(t *testing.T) { | ||||
| 	ctx := context.WithValue(context.TODO(), clientKey{}, NewClient()) | ||||
|  | ||||
| 	c, ok := FromContext(ctx) | ||||
| 	if c == nil || !ok { | ||||
| 		t.Fatal("FromContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestFromNilContext(t *testing.T) { | ||||
| 	// nolint: staticcheck | ||||
| 	c, ok := FromContext(nil) | ||||
| 	if ok || c != nil { | ||||
| 		t.Fatal("FromContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestNewContext(t *testing.T) { | ||||
| 	ctx := NewContext(context.TODO(), NewClient()) | ||||
| 	c, ok := FromContext(ctx) | ||||
| 	if c == nil || !ok { | ||||
| 		t.Fatal("NewContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestNewNilContext(t *testing.T) { | ||||
| 	// nolint: staticcheck | ||||
| 	ctx := NewContext(nil, NewClient()) | ||||
| 	c, ok := FromContext(ctx) | ||||
| 	if c == nil || !ok { | ||||
| 		t.Fatal("NewContext not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestSetCallOption(t *testing.T) { | ||||
| 	type key struct{} | ||||
| 	o := SetCallOption(key{}, "test") | ||||
| 	opts := &CallOptions{} | ||||
| 	o(opts) | ||||
|  | ||||
| 	if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { | ||||
| 		t.Fatal("SetCallOption not works") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestSetOption(t *testing.T) { | ||||
| 	type key struct{} | ||||
| 	o := SetOption(key{}, "test") | ||||
| 	opts := &Options{} | ||||
| 	o(opts) | ||||
|  | ||||
| 	if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { | ||||
| 		t.Fatal("SetOption not works") | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										25
									
								
								client/grpc/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								client/grpc/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| # GRPC Client | ||||
|  | ||||
| The grpc client is a [micro.Client](https://godoc.org/github.com/asim/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/asim/go-micro" | ||||
| 	"github.com/micro/go-plugins/client/grpc" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	service := micro.NewService( | ||||
| 		micro.Name("greeter"), | ||||
| 		micro.Client(grpc.NewClient()), | ||||
| 	) | ||||
| } | ||||
| ``` | ||||
							
								
								
									
										206
									
								
								client/grpc/codec.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										206
									
								
								client/grpc/codec.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,206 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
|  | ||||
| 	b "bytes" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/codec" | ||||
| 	"github.com/asim/go-micro/v3/codec/bytes" | ||||
| 	"github.com/golang/protobuf/jsonpb" | ||||
| 	"github.com/golang/protobuf/proto" | ||||
| 	"github.com/oxtoacart/bpool" | ||||
| 	"google.golang.org/grpc" | ||||
| 	"google.golang.org/grpc/encoding" | ||||
| ) | ||||
|  | ||||
| type jsonCodec struct{} | ||||
| type protoCodec struct{} | ||||
| type bytesCodec struct{} | ||||
| type wrapCodec struct{ encoding.Codec } | ||||
|  | ||||
| var jsonpbMarshaler = &jsonpb.Marshaler{} | ||||
| var useNumber bool | ||||
|  | ||||
| // create buffer pool with 16 instances each preallocated with 256 bytes | ||||
| var bufferPool = bpool.NewSizedBufferPool(16, 256) | ||||
|  | ||||
| var ( | ||||
| 	defaultGRPCCodecs = map[string]encoding.Codec{ | ||||
| 		"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{}, | ||||
| 	} | ||||
| ) | ||||
|  | ||||
| // UseNumber fix unmarshal Number(8234567890123456789) to interface(8.234567890123457e+18) | ||||
| func UseNumber() { | ||||
| 	useNumber = true | ||||
| } | ||||
|  | ||||
| func (w wrapCodec) String() string { | ||||
| 	return w.Codec.Name() | ||||
| } | ||||
|  | ||||
| func (w wrapCodec) Marshal(v interface{}) ([]byte, error) { | ||||
| 	b, ok := v.(*bytes.Frame) | ||||
| 	if ok { | ||||
| 		return b.Data, nil | ||||
| 	} | ||||
| 	return w.Codec.Marshal(v) | ||||
| } | ||||
|  | ||||
| func (w wrapCodec) Unmarshal(data []byte, v interface{}) error { | ||||
| 	b, ok := v.(*bytes.Frame) | ||||
| 	if ok { | ||||
| 		b.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) | ||||
| 	} | ||||
| 	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 { | ||||
| 	m, ok := v.(proto.Message) | ||||
| 	if !ok { | ||||
| 		return fmt.Errorf("failed to unmarshal: %v is not type of proto.Message", v) | ||||
| 	} | ||||
| 	return proto.Unmarshal(data, m) | ||||
| } | ||||
|  | ||||
| func (protoCodec) Name() string { | ||||
| 	return "proto" | ||||
| } | ||||
|  | ||||
| func (bytesCodec) Marshal(v interface{}) ([]byte, error) { | ||||
| 	b, ok := v.(*[]byte) | ||||
| 	if !ok { | ||||
| 		return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte", v) | ||||
| 	} | ||||
| 	return *b, nil | ||||
| } | ||||
|  | ||||
| func (bytesCodec) Unmarshal(data []byte, v interface{}) error { | ||||
| 	b, ok := v.(*[]byte) | ||||
| 	if !ok { | ||||
| 		return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte", v) | ||||
| 	} | ||||
| 	*b = data | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (bytesCodec) Name() string { | ||||
| 	return "bytes" | ||||
| } | ||||
|  | ||||
| func (jsonCodec) Marshal(v interface{}) ([]byte, error) { | ||||
| 	if b, ok := v.(*bytes.Frame); ok { | ||||
| 		return b.Data, nil | ||||
| 	} | ||||
|  | ||||
| 	if pb, ok := v.(proto.Message); ok { | ||||
| 		buf := bufferPool.Get() | ||||
| 		defer bufferPool.Put(buf) | ||||
| 		if err := jsonpbMarshaler.Marshal(buf, pb); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		return buf.Bytes(), nil | ||||
| 	} | ||||
|  | ||||
| 	return json.Marshal(v) | ||||
| } | ||||
|  | ||||
| func (jsonCodec) Unmarshal(data []byte, v interface{}) error { | ||||
| 	if len(data) == 0 { | ||||
| 		return nil | ||||
| 	} | ||||
| 	if b, ok := v.(*bytes.Frame); ok { | ||||
| 		b.Data = data | ||||
| 		return nil | ||||
| 	} | ||||
| 	if pb, ok := v.(proto.Message); ok { | ||||
| 		return jsonpb.Unmarshal(b.NewReader(data), pb) | ||||
| 	} | ||||
|  | ||||
| 	dec := json.NewDecoder(b.NewReader(data)) | ||||
| 	if useNumber { | ||||
| 		dec.UseNumber() | ||||
| 	} | ||||
| 	return dec.Decode(v) | ||||
| } | ||||
|  | ||||
| func (jsonCodec) Name() string { | ||||
| 	return "json" | ||||
| } | ||||
|  | ||||
| type grpcCodec struct { | ||||
| 	// headers | ||||
| 	id       string | ||||
| 	target   string | ||||
| 	method   string | ||||
| 	endpoint string | ||||
|  | ||||
| 	s grpc.ClientStream | ||||
| 	c encoding.Codec | ||||
| } | ||||
|  | ||||
| func (g *grpcCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error { | ||||
| 	md, err := g.s.Header() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	if m == nil { | ||||
| 		m = new(codec.Message) | ||||
| 	} | ||||
| 	if m.Header == nil { | ||||
| 		m.Header = make(map[string]string, len(md)) | ||||
| 	} | ||||
| 	for k, v := range md { | ||||
| 		m.Header[k] = strings.Join(v, ",") | ||||
| 	} | ||||
| 	m.Id = g.id | ||||
| 	m.Target = g.target | ||||
| 	m.Method = g.method | ||||
| 	m.Endpoint = g.endpoint | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (g *grpcCodec) ReadBody(v interface{}) error { | ||||
| 	if f, ok := v.(*bytes.Frame); ok { | ||||
| 		return g.s.RecvMsg(f) | ||||
| 	} | ||||
| 	return g.s.RecvMsg(v) | ||||
| } | ||||
|  | ||||
| func (g *grpcCodec) Write(m *codec.Message, v interface{}) error { | ||||
| 	// if we don't have a body | ||||
| 	if v != nil { | ||||
| 		return g.s.SendMsg(v) | ||||
| 	} | ||||
| 	// write the body using the framing codec | ||||
| 	return g.s.SendMsg(&bytes.Frame{Data: m.Body}) | ||||
| } | ||||
|  | ||||
| func (g *grpcCodec) Close() error { | ||||
| 	return g.s.CloseSend() | ||||
| } | ||||
|  | ||||
| func (g *grpcCodec) String() string { | ||||
| 	return g.c.Name() | ||||
| } | ||||
							
								
								
									
										39
									
								
								client/grpc/error.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								client/grpc/error.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"github.com/asim/go-micro/v3/errors" | ||||
| 	"google.golang.org/grpc/status" | ||||
| ) | ||||
|  | ||||
| func microError(err error) error { | ||||
| 	// no error | ||||
| 	switch err { | ||||
| 	case nil: | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	if verr, ok := err.(*errors.Error); ok { | ||||
| 		return verr | ||||
| 	} | ||||
|  | ||||
| 	// grpc error | ||||
| 	s, ok := status.FromError(err) | ||||
| 	if !ok { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// return first error from details | ||||
| 	if details := s.Details(); len(details) > 0 { | ||||
| 		if verr, ok := details[0].(error); ok { | ||||
| 			return microError(verr) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// try to decode micro *errors.Error | ||||
| 	if e := errors.Parse(s.Message()); e.Code > 0 { | ||||
| 		return e // actually a micro error | ||||
| 	} | ||||
|  | ||||
| 	// fallback | ||||
| 	return errors.InternalServerError("go.micro.client", s.Message()) | ||||
| } | ||||
							
								
								
									
										728
									
								
								client/grpc/grpc.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										728
									
								
								client/grpc/grpc.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,728 @@ | ||||
| // Package grpc provides a gRPC client | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"crypto/tls" | ||||
| 	"fmt" | ||||
| 	"net" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 	"sync/atomic" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/broker" | ||||
| 	"github.com/asim/go-micro/v3/client" | ||||
| 	raw "github.com/asim/go-micro/v3/codec/bytes" | ||||
| 	"github.com/asim/go-micro/v3/errors" | ||||
| 	"github.com/asim/go-micro/v3/metadata" | ||||
|  | ||||
| 	"google.golang.org/grpc" | ||||
| 	"google.golang.org/grpc/credentials" | ||||
| 	"google.golang.org/grpc/encoding" | ||||
| 	gmetadata "google.golang.org/grpc/metadata" | ||||
| ) | ||||
|  | ||||
| type grpcClient struct { | ||||
| 	opts client.Options | ||||
| 	pool *pool | ||||
| 	once atomic.Value | ||||
| } | ||||
|  | ||||
| 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 | ||||
| func (g *grpcClient) secure(addr string) grpc.DialOption { | ||||
| 	// first we check if theres'a  tls config | ||||
| 	if g.opts.Context != nil { | ||||
| 		if v := g.opts.Context.Value(tlsAuth{}); v != nil { | ||||
| 			tls := v.(*tls.Config) | ||||
| 			creds := credentials.NewTLS(tls) | ||||
| 			// return tls config if it exists | ||||
| 			return grpc.WithTransportCredentials(creds) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// default config | ||||
| 	tlsConfig := &tls.Config{} | ||||
| 	defaultCreds := grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)) | ||||
|  | ||||
| 	// check if the address is prepended with https | ||||
| 	if strings.HasPrefix(addr, "https://") { | ||||
| 		return defaultCreds | ||||
| 	} | ||||
|  | ||||
| 	// if no port is specified or port is 443 default to tls | ||||
| 	_, port, err := net.SplitHostPort(addr) | ||||
| 	// assuming with no port its going to be secured | ||||
| 	if port == "443" { | ||||
| 		return defaultCreds | ||||
| 	} else if err != nil && strings.Contains(err.Error(), "missing port in address") { | ||||
| 		return defaultCreds | ||||
| 	} | ||||
|  | ||||
| 	// other fallback to insecure | ||||
| 	return grpc.WithInsecure() | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error { | ||||
| 	var header map[string]string | ||||
|  | ||||
| 	header = make(map[string]string) | ||||
| 	if md, ok := metadata.FromContext(ctx); ok { | ||||
| 		header = make(map[string]string, len(md)) | ||||
| 		for k, v := range md { | ||||
| 			header[strings.ToLower(k)] = v | ||||
| 		} | ||||
| 	} else { | ||||
| 		header = make(map[string]string) | ||||
| 	} | ||||
|  | ||||
| 	// set timeout in nanoseconds | ||||
| 	header["timeout"] = fmt.Sprintf("%d", opts.RequestTimeout) | ||||
| 	// set the content type for the request | ||||
| 	header["x-content-type"] = req.ContentType() | ||||
|  | ||||
| 	md := gmetadata.New(header) | ||||
| 	ctx = gmetadata.NewOutgoingContext(ctx, md) | ||||
|  | ||||
| 	cf, err := g.newGRPCCodec(req.ContentType()) | ||||
| 	if err != nil { | ||||
| 		return errors.InternalServerError("go.micro.client", err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	maxRecvMsgSize := g.maxRecvMsgSizeValue() | ||||
| 	maxSendMsgSize := g.maxSendMsgSizeValue() | ||||
|  | ||||
| 	var grr error | ||||
|  | ||||
| 	grpcDialOptions := []grpc.DialOption{ | ||||
| 		grpc.WithTimeout(opts.DialTimeout), | ||||
| 		g.secure(addr), | ||||
| 		grpc.WithDefaultCallOptions( | ||||
| 			grpc.MaxCallRecvMsgSize(maxRecvMsgSize), | ||||
| 			grpc.MaxCallSendMsgSize(maxSendMsgSize), | ||||
| 		), | ||||
| 	} | ||||
|  | ||||
| 	if opts := g.getGrpcDialOptions(); opts != nil { | ||||
| 		grpcDialOptions = append(grpcDialOptions, opts...) | ||||
| 	} | ||||
|  | ||||
| 	cc, err := g.pool.getConn(addr, grpcDialOptions...) | ||||
| 	if err != nil { | ||||
| 		return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err)) | ||||
| 	} | ||||
| 	defer func() { | ||||
| 		// defer execution of release | ||||
| 		g.pool.release(addr, cc, grr) | ||||
| 	}() | ||||
|  | ||||
| 	ch := make(chan error, 1) | ||||
|  | ||||
| 	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...) | ||||
| 		ch <- microError(err) | ||||
| 	}() | ||||
|  | ||||
| 	select { | ||||
| 	case err := <-ch: | ||||
| 		grr = err | ||||
| 	case <-ctx.Done(): | ||||
| 		grr = errors.Timeout("go.micro.client", "%v", ctx.Err()) | ||||
| 	} | ||||
|  | ||||
| 	return grr | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error { | ||||
| 	var header map[string]string | ||||
|  | ||||
| 	if md, ok := metadata.FromContext(ctx); ok { | ||||
| 		header = make(map[string]string, len(md)) | ||||
| 		for k, v := range md { | ||||
| 			header[k] = v | ||||
| 		} | ||||
| 	} else { | ||||
| 		header = make(map[string]string) | ||||
| 	} | ||||
|  | ||||
| 	// set timeout in nanoseconds | ||||
| 	if opts.StreamTimeout > time.Duration(0) { | ||||
| 		header["timeout"] = fmt.Sprintf("%d", opts.StreamTimeout) | ||||
| 	} | ||||
| 	// set the content type for the request | ||||
| 	header["x-content-type"] = req.ContentType() | ||||
|  | ||||
| 	md := gmetadata.New(header) | ||||
| 	ctx = gmetadata.NewOutgoingContext(ctx, md) | ||||
|  | ||||
| 	cf, err := g.newGRPCCodec(req.ContentType()) | ||||
| 	if err != nil { | ||||
| 		return errors.InternalServerError("go.micro.client", err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	wc := wrapCodec{cf} | ||||
|  | ||||
| 	grpcDialOptions := []grpc.DialOption{ | ||||
| 		grpc.WithTimeout(opts.DialTimeout), | ||||
| 		g.secure(addr), | ||||
| 	} | ||||
|  | ||||
| 	if opts := g.getGrpcDialOptions(); opts != nil { | ||||
| 		grpcDialOptions = append(grpcDialOptions, opts...) | ||||
| 	} | ||||
|  | ||||
| 	cc, err := g.pool.getConn(addr, grpcDialOptions...) | ||||
| 	if err != nil { | ||||
| 		return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err)) | ||||
| 	} | ||||
|  | ||||
| 	desc := &grpc.StreamDesc{ | ||||
| 		StreamName:    req.Service() + req.Endpoint(), | ||||
| 		ClientStreams: true, | ||||
| 		ServerStreams: true, | ||||
| 	} | ||||
|  | ||||
| 	grpcCallOptions := []grpc.CallOption{ | ||||
| 		grpc.ForceCodec(wc), | ||||
| 		grpc.CallContentSubtype(cf.Name()), | ||||
| 	} | ||||
| 	if opts := g.getGrpcCallOptions(); opts != nil { | ||||
| 		grpcCallOptions = append(grpcCallOptions, opts...) | ||||
| 	} | ||||
|  | ||||
| 	var cancel context.CancelFunc | ||||
| 	ctx, cancel = context.WithCancel(ctx) | ||||
|  | ||||
| 	st, err := cc.NewStream(ctx, desc, methodToGRPC(req.Service(), req.Endpoint()), grpcCallOptions...) | ||||
| 	if err != nil { | ||||
| 		// we need to cleanup as we dialled and created a context | ||||
| 		// cancel the context | ||||
| 		cancel() | ||||
| 		// release the connection | ||||
| 		g.pool.release(addr, cc, err) | ||||
| 		// now return the error | ||||
| 		return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error creating stream: %v", err)) | ||||
| 	} | ||||
|  | ||||
| 	codec := &grpcCodec{ | ||||
| 		s: st, | ||||
| 		c: wc, | ||||
| 	} | ||||
|  | ||||
| 	// set request codec | ||||
| 	if r, ok := req.(*grpcRequest); ok { | ||||
| 		r.codec = codec | ||||
| 	} | ||||
|  | ||||
| 	// setup the stream response | ||||
| 	stream := &grpcStream{ | ||||
| 		ClientStream: st, | ||||
| 		context:      ctx, | ||||
| 		request:      req, | ||||
| 		response: &response{ | ||||
| 			conn:   cc, | ||||
| 			stream: st, | ||||
| 			codec:  cf, | ||||
| 			gcodec: codec, | ||||
| 		}, | ||||
| 		conn: cc, | ||||
| 		close: func(err error) { | ||||
| 			// cancel the context if an error occured | ||||
| 			if err != nil { | ||||
| 				cancel() | ||||
| 			} | ||||
|  | ||||
| 			// defer execution of release | ||||
| 			g.pool.release(addr, cc, err) | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	// set the stream as the response | ||||
| 	val := reflect.ValueOf(rsp).Elem() | ||||
| 	val.Set(reflect.ValueOf(stream).Elem()) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) poolMaxStreams() int { | ||||
| 	if g.opts.Context == nil { | ||||
| 		return DefaultPoolMaxStreams | ||||
| 	} | ||||
| 	v := g.opts.Context.Value(poolMaxStreams{}) | ||||
| 	if v == nil { | ||||
| 		return DefaultPoolMaxStreams | ||||
| 	} | ||||
| 	return v.(int) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) poolMaxIdle() int { | ||||
| 	if g.opts.Context == nil { | ||||
| 		return DefaultPoolMaxIdle | ||||
| 	} | ||||
| 	v := g.opts.Context.Value(poolMaxIdle{}) | ||||
| 	if v == nil { | ||||
| 		return DefaultPoolMaxIdle | ||||
| 	} | ||||
| 	return v.(int) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) maxRecvMsgSizeValue() int { | ||||
| 	if g.opts.Context == nil { | ||||
| 		return DefaultMaxRecvMsgSize | ||||
| 	} | ||||
| 	v := g.opts.Context.Value(maxRecvMsgSizeKey{}) | ||||
| 	if v == nil { | ||||
| 		return DefaultMaxRecvMsgSize | ||||
| 	} | ||||
| 	return v.(int) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) maxSendMsgSizeValue() int { | ||||
| 	if g.opts.Context == nil { | ||||
| 		return DefaultMaxSendMsgSize | ||||
| 	} | ||||
| 	v := g.opts.Context.Value(maxSendMsgSizeKey{}) | ||||
| 	if v == nil { | ||||
| 		return DefaultMaxSendMsgSize | ||||
| 	} | ||||
| 	return v.(int) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) newGRPCCodec(contentType string) (encoding.Codec, error) { | ||||
| 	codecs := make(map[string]encoding.Codec) | ||||
| 	if g.opts.Context != nil { | ||||
| 		if v := g.opts.Context.Value(codecsKey{}); v != nil { | ||||
| 			codecs = v.(map[string]encoding.Codec) | ||||
| 		} | ||||
| 	} | ||||
| 	if c, ok := codecs[contentType]; ok { | ||||
| 		return wrapCodec{c}, nil | ||||
| 	} | ||||
| 	if c, ok := defaultGRPCCodecs[contentType]; ok { | ||||
| 		return wrapCodec{c}, nil | ||||
| 	} | ||||
| 	return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) Init(opts ...client.Option) error { | ||||
| 	size := g.opts.PoolSize | ||||
| 	ttl := g.opts.PoolTTL | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&g.opts) | ||||
| 	} | ||||
|  | ||||
| 	// update pool configuration if the options changed | ||||
| 	if size != g.opts.PoolSize || ttl != g.opts.PoolTTL { | ||||
| 		g.pool.Lock() | ||||
| 		g.pool.size = g.opts.PoolSize | ||||
| 		g.pool.ttl = int64(g.opts.PoolTTL.Seconds()) | ||||
| 		g.pool.Unlock() | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) Options() client.Options { | ||||
| 	return g.opts | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message { | ||||
| 	return newGRPCEvent(topic, msg, g.opts.ContentType, opts...) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) NewRequest(service, method string, req interface{}, reqOpts ...client.RequestOption) client.Request { | ||||
| 	return newGRPCRequest(service, method, req, g.opts.ContentType, reqOpts...) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { | ||||
| 	if req == nil { | ||||
| 		return errors.InternalServerError("go.micro.client", "req is nil") | ||||
| 	} else if rsp == nil { | ||||
| 		return errors.InternalServerError("go.micro.client", "rsp is nil") | ||||
| 	} | ||||
| 	// make a copy of call opts | ||||
| 	callOpts := g.opts.CallOptions | ||||
| 	for _, opt := range opts { | ||||
| 		opt(&callOpts) | ||||
| 	} | ||||
|  | ||||
| 	// check if we already have a deadline | ||||
| 	d, ok := ctx.Deadline() | ||||
| 	if !ok { | ||||
| 		// no deadline so we create a new one | ||||
| 		var cancel context.CancelFunc | ||||
| 		ctx, cancel = context.WithTimeout(ctx, callOpts.RequestTimeout) | ||||
| 		defer cancel() | ||||
| 	} else { | ||||
| 		// got a deadline so no need to setup context | ||||
| 		// but we need to set the timeout we pass along | ||||
| 		opt := client.WithRequestTimeout(time.Until(d)) | ||||
| 		opt(&callOpts) | ||||
| 	} | ||||
|  | ||||
| 	// should we noop right here? | ||||
| 	select { | ||||
| 	case <-ctx.Done(): | ||||
| 		return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408) | ||||
| 	default: | ||||
| 	} | ||||
|  | ||||
| 	// make copy of call method | ||||
| 	gcall := g.call | ||||
|  | ||||
| 	// wrap the call in reverse | ||||
| 	for i := len(callOpts.CallWrappers); i > 0; i-- { | ||||
| 		gcall = callOpts.CallWrappers[i-1](gcall) | ||||
| 	} | ||||
|  | ||||
| 	// use the router passed as a call option, or fallback to the rpc clients router | ||||
| 	if callOpts.Router == nil { | ||||
| 		callOpts.Router = g.opts.Router | ||||
| 	} | ||||
|  | ||||
| 	if callOpts.Selector == nil { | ||||
| 		callOpts.Selector = g.opts.Selector | ||||
| 	} | ||||
|  | ||||
| 	// inject proxy address | ||||
| 	// TODO: don't even bother using Lookup/Select in this case | ||||
| 	if len(g.opts.Proxy) > 0 { | ||||
| 		callOpts.Address = []string{g.opts.Proxy} | ||||
| 	} | ||||
|  | ||||
| 	// 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 backoff first. Someone may want an initial start delay | ||||
| 		t, err := callOpts.Backoff(ctx, req, i) | ||||
| 		if err != nil { | ||||
| 			return errors.InternalServerError("go.micro.client", err.Error()) | ||||
| 		} | ||||
|  | ||||
| 		// only sleep if greater than 0 | ||||
| 		if t.Seconds() > 0 { | ||||
| 			time.Sleep(t) | ||||
| 		} | ||||
|  | ||||
| 		// get the next node | ||||
| 		node := next() | ||||
|  | ||||
| 		// make the call | ||||
| 		err = gcall(ctx, node, req, rsp, callOpts) | ||||
|  | ||||
| 		// record the result of the call to inform future routing decisions | ||||
| 		g.opts.Selector.Record(node, err) | ||||
|  | ||||
| 		// try and transform the error to a go-micro error | ||||
| 		if verr, ok := err.(*errors.Error); ok { | ||||
| 			return verr | ||||
| 		} | ||||
|  | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ch := make(chan error, callOpts.Retries+1) | ||||
| 	var gerr error | ||||
|  | ||||
| 	for i := 0; i <= callOpts.Retries; i++ { | ||||
| 		go func(i int) { | ||||
| 			ch <- call(i) | ||||
| 		}(i) | ||||
|  | ||||
| 		select { | ||||
| 		case <-ctx.Done(): | ||||
| 			return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408) | ||||
| 		case err := <-ch: | ||||
| 			// if the call succeeded lets bail early | ||||
| 			if err == nil { | ||||
| 				return nil | ||||
| 			} | ||||
|  | ||||
| 			retry, rerr := callOpts.Retry(ctx, req, i, err) | ||||
| 			if rerr != nil { | ||||
| 				return rerr | ||||
| 			} | ||||
|  | ||||
| 			if !retry { | ||||
| 				return err | ||||
| 			} | ||||
|  | ||||
| 			gerr = err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return gerr | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { | ||||
| 	// make a copy of call opts | ||||
| 	callOpts := g.opts.CallOptions | ||||
| 	for _, opt := range opts { | ||||
| 		opt(&callOpts) | ||||
| 	} | ||||
|  | ||||
| 	// #200 - streams shouldn't have a request timeout set on the context | ||||
|  | ||||
| 	// should we noop right here? | ||||
| 	select { | ||||
| 	case <-ctx.Done(): | ||||
| 		return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408) | ||||
| 	default: | ||||
| 	} | ||||
|  | ||||
| 	// make a copy of stream | ||||
| 	gstream := g.stream | ||||
|  | ||||
| 	// wrap the call in reverse | ||||
| 	for i := len(callOpts.CallWrappers); i > 0; i-- { | ||||
| 		gstream = callOpts.CallWrappers[i-1](gstream) | ||||
| 	} | ||||
|  | ||||
| 	// use the router passed as a call option, or fallback to the rpc clients router | ||||
| 	if callOpts.Router == nil { | ||||
| 		callOpts.Router = g.opts.Router | ||||
| 	} | ||||
|  | ||||
| 	if callOpts.Selector == nil { | ||||
| 		callOpts.Selector = g.opts.Selector | ||||
| 	} | ||||
|  | ||||
| 	// inject proxy address | ||||
| 	// TODO: don't even bother using Lookup/Select in this case | ||||
| 	if len(g.opts.Proxy) > 0 { | ||||
| 		callOpts.Address = []string{g.opts.Proxy} | ||||
| 	} | ||||
|  | ||||
| 	// 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 backoff first. Someone may want an initial start delay | ||||
| 		t, err := callOpts.Backoff(ctx, req, i) | ||||
| 		if err != nil { | ||||
| 			return nil, errors.InternalServerError("go.micro.client", err.Error()) | ||||
| 		} | ||||
|  | ||||
| 		// only sleep if greater than 0 | ||||
| 		if t.Seconds() > 0 { | ||||
| 			time.Sleep(t) | ||||
| 		} | ||||
|  | ||||
| 		// get the next node | ||||
| 		node := next() | ||||
|  | ||||
| 		// make the call | ||||
| 		stream := &grpcStream{} | ||||
| 		err = g.stream(ctx, node, req, stream, callOpts) | ||||
|  | ||||
| 		// record the result of the call to inform future routing decisions | ||||
| 		g.opts.Selector.Record(node, err) | ||||
|  | ||||
| 		// try and transform the error to a go-micro error | ||||
| 		if verr, ok := err.(*errors.Error); ok { | ||||
| 			return nil, verr | ||||
| 		} | ||||
|  | ||||
| 		g.opts.Selector.Record(node, err) | ||||
| 		return stream, err | ||||
| 	} | ||||
|  | ||||
| 	type response struct { | ||||
| 		stream client.Stream | ||||
| 		err    error | ||||
| 	} | ||||
|  | ||||
| 	ch := make(chan response, callOpts.Retries+1) | ||||
| 	var grr error | ||||
|  | ||||
| 	for i := 0; i <= callOpts.Retries; i++ { | ||||
| 		go func(i int) { | ||||
| 			s, err := call(i) | ||||
| 			ch <- response{s, err} | ||||
| 		}(i) | ||||
|  | ||||
| 		select { | ||||
| 		case <-ctx.Done(): | ||||
| 			return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408) | ||||
| 		case rsp := <-ch: | ||||
| 			// if the call succeeded lets bail early | ||||
| 			if rsp.err == nil { | ||||
| 				return rsp.stream, nil | ||||
| 			} | ||||
|  | ||||
| 			retry, rerr := callOpts.Retry(ctx, req, i, grr) | ||||
| 			if rerr != nil { | ||||
| 				return nil, rerr | ||||
| 			} | ||||
|  | ||||
| 			if !retry { | ||||
| 				return nil, rsp.err | ||||
| 			} | ||||
|  | ||||
| 			grr = rsp.err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil, grr | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error { | ||||
| 	var options client.PublishOptions | ||||
| 	var body []byte | ||||
|  | ||||
| 	// fail early on connect error | ||||
| 	if !g.once.Load().(bool) { | ||||
| 		if err := g.opts.Broker.Connect(); err != nil { | ||||
| 			return errors.InternalServerError("go.micro.client", err.Error()) | ||||
| 		} | ||||
| 		g.once.Store(true) | ||||
| 	} | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	md, ok := metadata.FromContext(ctx) | ||||
| 	if !ok { | ||||
| 		md = make(map[string]string) | ||||
| 	} | ||||
| 	md["Content-Type"] = p.ContentType() | ||||
| 	md["Micro-Topic"] = p.Topic() | ||||
|  | ||||
| 	// passed in raw data | ||||
| 	if d, ok := p.Payload().(*raw.Frame); ok { | ||||
| 		body = d.Data | ||||
| 	} else { | ||||
| 		// use codec for payload | ||||
| 		cf, err := g.newGRPCCodec(p.ContentType()) | ||||
| 		if err != nil { | ||||
| 			return errors.InternalServerError("go.micro.client", err.Error()) | ||||
| 		} | ||||
| 		// set the body | ||||
| 		b, err := cf.Marshal(p.Payload()) | ||||
| 		if err != nil { | ||||
| 			return errors.InternalServerError("go.micro.client", err.Error()) | ||||
| 		} | ||||
| 		body = b | ||||
| 	} | ||||
|  | ||||
| 	topic := p.Topic() | ||||
|  | ||||
| 	// get the exchange | ||||
| 	if len(options.Exchange) > 0 { | ||||
| 		topic = options.Exchange | ||||
| 	} | ||||
|  | ||||
| 	return g.opts.Broker.Publish(topic, &broker.Message{ | ||||
| 		Header: md, | ||||
| 		Body:   body, | ||||
| 	}, broker.PublishContext(options.Context)) | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) String() string { | ||||
| 	return "grpc" | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) getGrpcDialOptions() []grpc.DialOption { | ||||
| 	if g.opts.CallOptions.Context == nil { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	v := g.opts.CallOptions.Context.Value(grpcDialOptions{}) | ||||
|  | ||||
| 	if v == nil { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	opts, ok := v.([]grpc.DialOption) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	return opts | ||||
| } | ||||
|  | ||||
| func (g *grpcClient) getGrpcCallOptions() []grpc.CallOption { | ||||
| 	if g.opts.CallOptions.Context == nil { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	v := g.opts.CallOptions.Context.Value(grpcCallOptions{}) | ||||
|  | ||||
| 	if v == nil { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	opts, ok := v.([]grpc.CallOption) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	return opts | ||||
| } | ||||
|  | ||||
| func newClient(opts ...client.Option) client.Client { | ||||
| 	options := client.NewOptions() | ||||
| 	// default content type for grpc | ||||
| 	options.ContentType = "application/grpc+proto" | ||||
|  | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| 	} | ||||
|  | ||||
| 	rc := &grpcClient{ | ||||
| 		opts: options, | ||||
| 	} | ||||
| 	rc.once.Store(false) | ||||
|  | ||||
| 	rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams()) | ||||
|  | ||||
| 	c := client.Client(rc) | ||||
|  | ||||
| 	// wrap in reverse | ||||
| 	for i := len(options.Wrappers); i > 0; i-- { | ||||
| 		c = options.Wrappers[i-1](c) | ||||
| 	} | ||||
|  | ||||
| 	return c | ||||
| } | ||||
|  | ||||
| func NewClient(opts ...client.Option) client.Client { | ||||
| 	return newClient(opts...) | ||||
| } | ||||
							
								
								
									
										218
									
								
								client/grpc/grpc_pool.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										218
									
								
								client/grpc/grpc_pool.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,218 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"sync" | ||||
| 	"time" | ||||
|  | ||||
| 	"google.golang.org/grpc" | ||||
| 	"google.golang.org/grpc/connectivity" | ||||
| ) | ||||
|  | ||||
| type pool struct { | ||||
| 	size int | ||||
| 	ttl  int64 | ||||
|  | ||||
| 	//  max streams on a *poolConn | ||||
| 	maxStreams int | ||||
| 	//  max idle conns | ||||
| 	maxIdle int | ||||
|  | ||||
| 	sync.Mutex | ||||
| 	conns map[string]*streamsPool | ||||
| } | ||||
|  | ||||
| type streamsPool struct { | ||||
| 	//  head of list | ||||
| 	head *poolConn | ||||
| 	//  busy conns list | ||||
| 	busy *poolConn | ||||
| 	//  the siza of list | ||||
| 	count int | ||||
| 	//  idle conn | ||||
| 	idle int | ||||
| } | ||||
|  | ||||
| type poolConn struct { | ||||
| 	//  grpc conn | ||||
| 	*grpc.ClientConn | ||||
| 	err  error | ||||
| 	addr string | ||||
|  | ||||
| 	//  pool and streams pool | ||||
| 	pool    *pool | ||||
| 	sp      *streamsPool | ||||
| 	streams int | ||||
| 	created int64 | ||||
|  | ||||
| 	//  list | ||||
| 	pre  *poolConn | ||||
| 	next *poolConn | ||||
| 	in   bool | ||||
| } | ||||
|  | ||||
| func newPool(size int, ttl time.Duration, idle int, ms int) *pool { | ||||
| 	if ms <= 0 { | ||||
| 		ms = 1 | ||||
| 	} | ||||
| 	if idle < 0 { | ||||
| 		idle = 0 | ||||
| 	} | ||||
| 	return &pool{ | ||||
| 		size:       size, | ||||
| 		ttl:        int64(ttl.Seconds()), | ||||
| 		maxStreams: ms, | ||||
| 		maxIdle:    idle, | ||||
| 		conns:      make(map[string]*streamsPool), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error) { | ||||
| 	now := time.Now().Unix() | ||||
| 	p.Lock() | ||||
| 	sp, ok := p.conns[addr] | ||||
| 	if !ok { | ||||
| 		sp = &streamsPool{head: &poolConn{}, busy: &poolConn{}, count: 0, idle: 0} | ||||
| 		p.conns[addr] = sp | ||||
| 	} | ||||
| 	//  while we have conns check streams and then return one | ||||
| 	//  otherwise we'll create a new conn | ||||
| 	conn := sp.head.next | ||||
| 	for conn != nil { | ||||
| 		//  check conn state | ||||
| 		// https://github.com/grpc/grpc/blob/master/doc/connectivity-semantics-and-api.md | ||||
| 		switch conn.GetState() { | ||||
| 		case connectivity.Connecting: | ||||
| 			conn = conn.next | ||||
| 			continue | ||||
| 		case connectivity.Shutdown: | ||||
| 			next := conn.next | ||||
| 			if conn.streams == 0 { | ||||
| 				removeConn(conn) | ||||
| 				sp.idle-- | ||||
| 			} | ||||
| 			conn = next | ||||
| 			continue | ||||
| 		case connectivity.TransientFailure: | ||||
| 			next := conn.next | ||||
| 			if conn.streams == 0 { | ||||
| 				removeConn(conn) | ||||
| 				conn.ClientConn.Close() | ||||
| 				sp.idle-- | ||||
| 			} | ||||
| 			conn = next | ||||
| 			continue | ||||
| 		case connectivity.Ready: | ||||
| 		case connectivity.Idle: | ||||
| 		} | ||||
| 		//  a old conn | ||||
| 		if now-conn.created > p.ttl { | ||||
| 			next := conn.next | ||||
| 			if conn.streams == 0 { | ||||
| 				removeConn(conn) | ||||
| 				conn.ClientConn.Close() | ||||
| 				sp.idle-- | ||||
| 			} | ||||
| 			conn = next | ||||
| 			continue | ||||
| 		} | ||||
| 		//  a busy conn | ||||
| 		if conn.streams >= p.maxStreams { | ||||
| 			next := conn.next | ||||
| 			removeConn(conn) | ||||
| 			addConnAfter(conn, sp.busy) | ||||
| 			conn = next | ||||
| 			continue | ||||
| 		} | ||||
| 		//  a idle conn | ||||
| 		if conn.streams == 0 { | ||||
| 			sp.idle-- | ||||
| 		} | ||||
| 		//  a good conn | ||||
| 		conn.streams++ | ||||
| 		p.Unlock() | ||||
| 		return conn, nil | ||||
| 	} | ||||
| 	p.Unlock() | ||||
|  | ||||
| 	//  create new conn | ||||
| 	cc, err := grpc.Dial(addr, opts...) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	conn = &poolConn{cc, nil, addr, p, sp, 1, time.Now().Unix(), nil, nil, false} | ||||
|  | ||||
| 	//  add conn to streams pool | ||||
| 	p.Lock() | ||||
| 	if sp.count < p.size { | ||||
| 		addConnAfter(conn, sp.head) | ||||
| 	} | ||||
| 	p.Unlock() | ||||
|  | ||||
| 	return conn, nil | ||||
| } | ||||
|  | ||||
| func (p *pool) release(addr string, conn *poolConn, err error) { | ||||
| 	p.Lock() | ||||
| 	p, sp, created := conn.pool, conn.sp, conn.created | ||||
| 	//  try to add conn | ||||
| 	if !conn.in && sp.count < p.size { | ||||
| 		addConnAfter(conn, sp.head) | ||||
| 	} | ||||
| 	if !conn.in { | ||||
| 		p.Unlock() | ||||
| 		conn.ClientConn.Close() | ||||
| 		return | ||||
| 	} | ||||
| 	//  a busy conn | ||||
| 	if conn.streams >= p.maxStreams { | ||||
| 		removeConn(conn) | ||||
| 		addConnAfter(conn, sp.head) | ||||
| 	} | ||||
| 	conn.streams-- | ||||
| 	//  if streams == 0, we can do something | ||||
| 	if conn.streams == 0 { | ||||
| 		//  1. it has errored | ||||
| 		//  2. too many idle conn or | ||||
| 		//  3. conn is too old | ||||
| 		now := time.Now().Unix() | ||||
| 		if err != nil || sp.idle >= p.maxIdle || now-created > p.ttl { | ||||
| 			removeConn(conn) | ||||
| 			p.Unlock() | ||||
| 			conn.ClientConn.Close() | ||||
| 			return | ||||
| 		} | ||||
| 		sp.idle++ | ||||
| 	} | ||||
| 	p.Unlock() | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func (conn *poolConn) Close() { | ||||
| 	conn.pool.release(conn.addr, conn, conn.err) | ||||
| } | ||||
|  | ||||
| func removeConn(conn *poolConn) { | ||||
| 	if conn.pre != nil { | ||||
| 		conn.pre.next = conn.next | ||||
| 	} | ||||
| 	if conn.next != nil { | ||||
| 		conn.next.pre = conn.pre | ||||
| 	} | ||||
| 	conn.pre = nil | ||||
| 	conn.next = nil | ||||
| 	conn.in = false | ||||
| 	conn.sp.count-- | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func addConnAfter(conn *poolConn, after *poolConn) { | ||||
| 	conn.next = after.next | ||||
| 	conn.pre = after | ||||
| 	if after.next != nil { | ||||
| 		after.next.pre = conn | ||||
| 	} | ||||
| 	after.next = conn | ||||
| 	conn.in = true | ||||
| 	conn.sp.count++ | ||||
| 	return | ||||
| } | ||||
							
								
								
									
										64
									
								
								client/grpc/grpc_pool_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								client/grpc/grpc_pool_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"net" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"google.golang.org/grpc" | ||||
| 	pgrpc "google.golang.org/grpc" | ||||
| 	pb "google.golang.org/grpc/examples/helloworld/helloworld" | ||||
| ) | ||||
|  | ||||
| func testPool(t *testing.T, size int, ttl time.Duration, idle int, ms int) { | ||||
| 	// setup server | ||||
| 	l, err := net.Listen("tcp", ":0") | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("failed to listen: %v", err) | ||||
| 	} | ||||
| 	defer l.Close() | ||||
|  | ||||
| 	s := pgrpc.NewServer() | ||||
| 	pb.RegisterGreeterServer(s, &greeterServer{}) | ||||
|  | ||||
| 	go s.Serve(l) | ||||
| 	defer s.Stop() | ||||
|  | ||||
| 	// zero pool | ||||
| 	p := newPool(size, ttl, idle, ms) | ||||
|  | ||||
| 	for i := 0; i < 10; i++ { | ||||
| 		// get a conn | ||||
| 		cc, err := p.getConn(l.Addr().String(), grpc.WithInsecure()) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
|  | ||||
| 		rsp := pb.HelloReply{} | ||||
|  | ||||
| 		err = cc.Invoke(context.TODO(), "/helloworld.Greeter/SayHello", &pb.HelloRequest{Name: "John"}, &rsp) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
|  | ||||
| 		if rsp.Message != "Hello John" { | ||||
| 			t.Fatalf("Got unexpected response %v", rsp.Message) | ||||
| 		} | ||||
|  | ||||
| 		// release the conn | ||||
| 		p.release(l.Addr().String(), cc, nil) | ||||
|  | ||||
| 		p.Lock() | ||||
| 		if i := p.conns[l.Addr().String()].count; i > size { | ||||
| 			p.Unlock() | ||||
| 			t.Fatalf("pool size %d is greater than expected %d", i, size) | ||||
| 		} | ||||
| 		p.Unlock() | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestGRPCPool(t *testing.T) { | ||||
| 	testPool(t, 0, time.Minute, 10, 2) | ||||
| 	testPool(t, 2, time.Minute, 10, 1) | ||||
| } | ||||
							
								
								
									
										108
									
								
								client/grpc/grpc_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								client/grpc/grpc_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,108 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"net" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/asim/go-micro/v3/client" | ||||
| 	"github.com/asim/go-micro/v3/errors" | ||||
| 	"github.com/asim/go-micro/v3/registry" | ||||
| 	"github.com/asim/go-micro/v3/registry/memory" | ||||
| 	"github.com/asim/go-micro/v3/router" | ||||
| 	regRouter "github.com/asim/go-micro/v3/router/registry" | ||||
| 	pgrpc "google.golang.org/grpc" | ||||
| 	pb "google.golang.org/grpc/examples/helloworld/helloworld" | ||||
| ) | ||||
|  | ||||
| // server is used to implement helloworld.GreeterServer. | ||||
| type greeterServer struct{} | ||||
|  | ||||
| // SayHello implements helloworld.GreeterServer | ||||
| func (g *greeterServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { | ||||
| 	if in.Name == "Error" { | ||||
| 		return nil, &errors.Error{Id: "1", Code: 99, Detail: "detail"} | ||||
| 	} | ||||
| 	return &pb.HelloReply{Message: "Hello " + in.Name}, nil | ||||
| } | ||||
|  | ||||
| func TestGRPCClient(t *testing.T) { | ||||
| 	l, err := net.Listen("tcp", ":0") | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("failed to listen: %v", err) | ||||
| 	} | ||||
| 	defer l.Close() | ||||
|  | ||||
| 	s := pgrpc.NewServer() | ||||
| 	pb.RegisterGreeterServer(s, &greeterServer{}) | ||||
|  | ||||
| 	go s.Serve(l) | ||||
| 	defer s.Stop() | ||||
|  | ||||
| 	// create mock registry | ||||
| 	r := memory.NewRegistry() | ||||
|  | ||||
| 	// register service | ||||
| 	r.Register(®istry.Service{ | ||||
| 		Name:    "helloworld", | ||||
| 		Version: "test", | ||||
| 		Nodes: []*registry.Node{ | ||||
| 			{ | ||||
| 				Id:      "test-1", | ||||
| 				Address: l.Addr().String(), | ||||
| 				Metadata: map[string]string{ | ||||
| 					"protocol": "grpc", | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}) | ||||
|  | ||||
| 	// create router | ||||
| 	rtr := regRouter.NewRouter(router.Registry(r)) | ||||
|  | ||||
| 	// create client | ||||
| 	c := NewClient(client.Router(rtr)) | ||||
|  | ||||
| 	testMethods := []string{ | ||||
| 		"/helloworld.Greeter/SayHello", | ||||
| 		"Greeter.SayHello", | ||||
| 	} | ||||
|  | ||||
| 	for _, method := range testMethods { | ||||
| 		req := c.NewRequest("helloworld", method, &pb.HelloRequest{ | ||||
| 			Name: "John", | ||||
| 		}) | ||||
|  | ||||
| 		rsp := pb.HelloReply{} | ||||
|  | ||||
| 		err = c.Call(context.TODO(), req, &rsp) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
|  | ||||
| 		if rsp.Message != "Hello John" { | ||||
| 			t.Fatalf("Got unexpected response %v", rsp.Message) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	req := c.NewRequest("helloworld", "/helloworld.Greeter/SayHello", &pb.HelloRequest{ | ||||
| 		Name: "Error", | ||||
| 	}) | ||||
|  | ||||
| 	rsp := pb.HelloReply{} | ||||
|  | ||||
| 	err = c.Call(context.TODO(), req, &rsp) | ||||
| 	if err == nil { | ||||
| 		t.Fatal("nil error received") | ||||
| 	} | ||||
|  | ||||
| 	verr, ok := err.(*errors.Error) | ||||
| 	if !ok { | ||||
| 		t.Fatalf("invalid error received %#+v\n", err) | ||||
| 	} | ||||
|  | ||||
| 	if verr.Code != 99 && verr.Id != "1" && verr.Detail != "detail" { | ||||
| 		t.Fatalf("invalid error received %#+v\n", verr) | ||||
| 	} | ||||
|  | ||||
| } | ||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user