Compare commits
	
		
			135 Commits
		
	
	
		
			v3.3.0
			...
			b2d89018b8
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| b2d89018b8 | |||
| 0981f89f60 | |||
| 332fe5f4d4 | |||
| 757fe0245b | |||
| 27eccc1ed2 | |||
| 7c641fa8ac | |||
| 24c9f20196 | |||
| 953b5b0021 | |||
| 87e2e2b947 | |||
| 256e61a437 | |||
| f9cdd41c94 | |||
| ecad15fe17 | |||
| fa3d18b353 | |||
| 2f3951773f | |||
| b263e14032 | |||
| 518cc1db73 | |||
| 4484cd34ec | |||
| 7bceeee6bf | |||
| aed9512b93 | |||
| de72a10973 | |||
| 62c2de51d4 | |||
| 741b2310ec | |||
| 1e8a44b088 | |||
| 2245314c2f | |||
| db770c3fe7 | |||
| 7ae302d438 | |||
| 655be440e7 | |||
| 8e516544f5 | |||
| 97e30b0122 | |||
| c9ce8498d2 | |||
| 7e2590b43c | |||
|  | e3db9c85dc | ||
| de6c875f91 | |||
|  | 3884de33f2 | ||
| 9cbcfeb9c6 | |||
|  | 011a9b2534 | ||
| f8dd344259 | |||
|  | bfd0536f06 | ||
| f95c953cad | |||
|  | 538f066be0 | ||
| 69d321121b | |||
|  | 05831ca789 | ||
| 70831b68df | |||
|  | cccd131b77 | ||
| 4ca9195091 | |||
|  | ae1d0d1c11 | ||
| fcab95bd8a | |||
| ede2cf695c | |||
|  | 755d601c94 | ||
| 164969611a | |||
|  | e0d526588b | ||
|  | 990d87c946 | ||
| 1e3f33dab8 | |||
|  | 28ba5d2c25 | ||
| 62973372d6 | |||
|  | 7b820e6b02 | ||
| abab4e5428 | |||
|  | 1d72797e95 | ||
| 627e3fdb94 | |||
|  | 1ad6cbfb2e | ||
| 5ff5b6e018 | |||
|  | 0b9be86b6b | ||
| a23f2dead7 | |||
|  | c76675c4d3 | ||
|  | 64e8a8a6bd | ||
| 3532091dc7 | |||
|  | 2cda4e9af7 | ||
|  | c182715e3e | ||
|  | 4c3c2f880d | ||
| 3c726fb85e | |||
| 95ac858c36 | |||
| 25b637a199 | |||
| edb352fd48 | |||
|  | 1e78c57dc5 | ||
|  | fedc7ad843 | ||
| 9d4daf95f6 | |||
| cd2235c34f | |||
|  | 5356120625 | ||
| da1b9deee3 | |||
| f737bf2006 | |||
|  | 939d7e68e6 | ||
|  | d59535d393 | ||
| bf975b41f5 | |||
|  | 6756cc6dc7 | ||
|  | d24f68dfb3 | ||
|  | a539930570 | ||
| 11b8399aaa | |||
|  | 9b1cf8c16d | ||
| b99fa3cdba | |||
|  | dd79f332eb | ||
| 2b90494bb7 | |||
|  | 4879c3cfe1 | ||
| d49d9099ab | |||
|  | ef830f1b40 | ||
| 5fb6e40b62 | |||
|  | 0bf2804377 | ||
| 7fa664b87d | |||
|  | 793fb77411 | ||
| f7ae8c923a | |||
|  | 61f9eb9806 | ||
| fb727ebbef | |||
|  | 56c894ad60 | ||
| 836096de98 | |||
| 80f1207140 | |||
| ba586a6502 | |||
|  | b653b2779f | ||
| b463dc1718 | |||
|  | ab0ac77d07 | ||
| c891537ebf | |||
|  | d1629ab145 | ||
| 8beabbff96 | |||
|  | 7301df6d94 | ||
| 1e8dd60cf3 | |||
|  | c3facb7d7f | ||
| f02a18ef8f | |||
|  | e241daf8e5 | ||
| bab263a273 | |||
|  | 9f4783b41b | ||
| df4a459726 | |||
|  | 5cbfea90d6 | ||
| 68395b8008 | |||
|  | 691d5ea7b3 | ||
| 26d83959c2 | |||
| 5ea9bb0e7a | |||
| 764e5dc1d7 | |||
|  | 973ba35807 | ||
| f38d49c30b | |||
|  | 19bff8ae4b | ||
|  | 66c0a66322 | ||
| ee6d7b67a7 | |||
| 712d8d5867 | |||
| 8438b0f8eb | |||
|  | d64573130d | ||
| 0392d48433 | |||
| c89919e100 | 
							
								
								
									
										19
									
								
								.github/dependabot.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								.github/dependabot.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | # To get started with Dependabot version updates, you'll need to specify which | ||||||
|  | # package ecosystems to update and where the package manifests are located. | ||||||
|  | # Please see the documentation for all configuration options: | ||||||
|  | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||||||
|  |  | ||||||
|  | version: 2 | ||||||
|  | updates: | ||||||
|  |  | ||||||
|  |   # Maintain dependencies for GitHub Actions | ||||||
|  |   - package-ecosystem: "github-actions" | ||||||
|  |     directory: "/" | ||||||
|  |     schedule: | ||||||
|  |       interval: "daily" | ||||||
|  |  | ||||||
|  |   # Maintain dependencies for Golang | ||||||
|  |   - package-ecosystem: "gomod" | ||||||
|  |     directory: "/" | ||||||
|  |     schedule: | ||||||
|  |       interval: "daily" | ||||||
							
								
								
									
										20
									
								
								.github/workflows/autoapprove.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								.github/workflows/autoapprove.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | name: "autoapprove" | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   pull_request_target: | ||||||
|  |     types: [assigned, opened, synchronize, reopened] | ||||||
|  |  | ||||||
|  | permissions: | ||||||
|  |   pull-requests: write | ||||||
|  |   contents: write | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   autoapprove: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     steps: | ||||||
|  |       - name: approve | ||||||
|  |         uses: hmarr/auto-approve-action@v3 | ||||||
|  |         if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]' | ||||||
|  |         id: approve | ||||||
|  |         with: | ||||||
|  |           github-token: ${{ secrets.GITHUB_TOKEN }} | ||||||
							
								
								
									
										21
									
								
								.github/workflows/automerge.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								.github/workflows/automerge.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | |||||||
|  | name: "automerge" | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   pull_request_target: | ||||||
|  |     types: [assigned, opened, synchronize, reopened] | ||||||
|  |  | ||||||
|  | permissions: | ||||||
|  |   pull-requests: write | ||||||
|  |   contents: write | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   automerge: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     if: github.actor == 'vtolstov' | ||||||
|  |     steps: | ||||||
|  |       - name: merge | ||||||
|  |         id: merge | ||||||
|  |         run: gh pr merge --auto --merge "$PR_URL" | ||||||
|  |         env: | ||||||
|  |           PR_URL: ${{github.event.pull_request.html_url}} | ||||||
|  |           GITHUB_TOKEN: ${{secrets.TOKEN}} | ||||||
							
								
								
									
										13
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										13
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							| @@ -3,19 +3,20 @@ on: | |||||||
|  push: |  push: | ||||||
|     branches: |     branches: | ||||||
|     - master |     - master | ||||||
|  |     - v3 | ||||||
| jobs: | jobs: | ||||||
|   test: |   test: | ||||||
|     name: test |     name: test | ||||||
|     runs-on: ubuntu-latest |     runs-on: ubuntu-latest | ||||||
|     steps: |     steps: | ||||||
|     - name: setup |     - name: setup | ||||||
|       uses: actions/setup-go@v2 |       uses: actions/setup-go@v3 | ||||||
|       with: |       with: | ||||||
|         go-version: 1.16 |         go-version: 1.17 | ||||||
|     - name: checkout |     - name: checkout | ||||||
|       uses: actions/checkout@v2 |       uses: actions/checkout@v3 | ||||||
|     - name: cache |     - name: cache | ||||||
|       uses: actions/cache@v2 |       uses: actions/cache@v3 | ||||||
|       with: |       with: | ||||||
|         path: ~/go/pkg/mod |         path: ~/go/pkg/mod | ||||||
|         key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |         key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||||||
| @@ -31,9 +32,9 @@ jobs: | |||||||
|     runs-on: ubuntu-latest |     runs-on: ubuntu-latest | ||||||
|     steps: |     steps: | ||||||
|       - name: checkout |       - name: checkout | ||||||
|         uses: actions/checkout@v2 |         uses: actions/checkout@v3 | ||||||
|       - name: lint |       - name: lint | ||||||
|         uses: golangci/golangci-lint-action@v2 |         uses: golangci/golangci-lint-action@v3.4.0 | ||||||
|         continue-on-error: true |         continue-on-error: true | ||||||
|         with: |         with: | ||||||
|           # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. |           # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. | ||||||
|   | |||||||
							
								
								
									
										78
									
								
								.github/workflows/codeql-analysis.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								.github/workflows/codeql-analysis.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,78 @@ | |||||||
|  | # For most projects, this workflow file will not need changing; you simply need | ||||||
|  | # to commit it to your repository. | ||||||
|  | # | ||||||
|  | # You may wish to alter this file to override the set of languages analyzed, | ||||||
|  | # or to provide custom queries or build logic. | ||||||
|  | # | ||||||
|  | # ******** NOTE ******** | ||||||
|  | # We have attempted to detect the languages in your repository. Please check | ||||||
|  | # the `language` matrix defined below to confirm you have the correct set of | ||||||
|  | # supported CodeQL languages. | ||||||
|  | # | ||||||
|  | name: "codeql" | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   workflow_run: | ||||||
|  |     workflows: ["prbuild"] | ||||||
|  |     types: | ||||||
|  |       - completed | ||||||
|  |   push: | ||||||
|  |     branches: [ master, v3 ] | ||||||
|  |   pull_request: | ||||||
|  |     # The branches below must be a subset of the branches above | ||||||
|  |     branches: [ master, v3 ] | ||||||
|  |   schedule: | ||||||
|  |     - cron: '34 1 * * 0' | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   analyze: | ||||||
|  |     name: analyze | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     permissions: | ||||||
|  |       actions: read | ||||||
|  |       contents: read | ||||||
|  |       security-events: write | ||||||
|  |  | ||||||
|  |     strategy: | ||||||
|  |       fail-fast: false | ||||||
|  |       matrix: | ||||||
|  |         language: [ 'go' ] | ||||||
|  |         # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] | ||||||
|  |         # Learn more: | ||||||
|  |         # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed | ||||||
|  |  | ||||||
|  |     steps: | ||||||
|  |     - name: checkout | ||||||
|  |       uses: actions/checkout@v3 | ||||||
|  |     - name: setup | ||||||
|  |       uses: actions/setup-go@v3 | ||||||
|  |       with: | ||||||
|  |         go-version: 1.17 | ||||||
|  |     # Initializes the CodeQL tools for scanning. | ||||||
|  |     - name: init | ||||||
|  |       uses: github/codeql-action/init@v2 | ||||||
|  |       with: | ||||||
|  |         languages: ${{ matrix.language }} | ||||||
|  |         # If you wish to specify custom queries, you can do so here or in a config file. | ||||||
|  |         # By default, queries listed here will override any specified in a config file. | ||||||
|  |         # Prefix the list here with "+" to use these queries and those in the config file. | ||||||
|  |         # queries: ./path/to/local/query, your-org/your-repo/queries@main | ||||||
|  |  | ||||||
|  |     # Autobuild attempts to build any compiled languages  (C/C++, C#, or Java). | ||||||
|  |     # If this step fails, then you should remove it and run the build manually (see below) | ||||||
|  |     - name: autobuild | ||||||
|  |       uses: github/codeql-action/autobuild@v2 | ||||||
|  |  | ||||||
|  |     # ℹ️ Command-line programs to run using the OS shell. | ||||||
|  |     # 📚 https://git.io/JvXDl | ||||||
|  |  | ||||||
|  |     # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines | ||||||
|  |     #    and modify them (or add more) to build your code if your project | ||||||
|  |     #    uses a compiled language | ||||||
|  |  | ||||||
|  |     #- run: | | ||||||
|  |     #   make bootstrap | ||||||
|  |     #   make release | ||||||
|  |  | ||||||
|  |     - name: analyze | ||||||
|  |       uses: github/codeql-action/analyze@v2 | ||||||
							
								
								
									
										27
									
								
								.github/workflows/dependabot-automerge.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								.github/workflows/dependabot-automerge.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | name: "dependabot-automerge" | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   pull_request_target: | ||||||
|  |     types: [assigned, opened, synchronize, reopened] | ||||||
|  |  | ||||||
|  | permissions: | ||||||
|  |   pull-requests: write | ||||||
|  |   contents: write | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   automerge: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     if: github.actor == 'dependabot[bot]' | ||||||
|  |     steps: | ||||||
|  |       - name: metadata | ||||||
|  |         id: metadata | ||||||
|  |         uses: dependabot/fetch-metadata@v1.3.6 | ||||||
|  |         with: | ||||||
|  |           github-token: "${{ secrets.TOKEN }}" | ||||||
|  |       - name: merge | ||||||
|  |         id: merge | ||||||
|  |         if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}} | ||||||
|  |         run: gh pr merge --auto --merge "$PR_URL" | ||||||
|  |         env: | ||||||
|  |           PR_URL: ${{github.event.pull_request.html_url}} | ||||||
|  |           GITHUB_TOKEN: ${{secrets.TOKEN}} | ||||||
							
								
								
									
										13
									
								
								.github/workflows/pr.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										13
									
								
								.github/workflows/pr.yml
									
									
									
									
										vendored
									
									
								
							| @@ -3,19 +3,20 @@ on: | |||||||
|   pull_request: |   pull_request: | ||||||
|     branches: |     branches: | ||||||
|     - master |     - master | ||||||
|  |     - v3 | ||||||
| jobs: | jobs: | ||||||
|   test: |   test: | ||||||
|     name: test |     name: test | ||||||
|     runs-on: ubuntu-latest |     runs-on: ubuntu-latest | ||||||
|     steps: |     steps: | ||||||
|     - name: setup |     - name: setup | ||||||
|       uses: actions/setup-go@v2 |       uses: actions/setup-go@v3 | ||||||
|       with: |       with: | ||||||
|         go-version: 1.16 |         go-version: 1.17 | ||||||
|     - name: checkout |     - name: checkout | ||||||
|       uses: actions/checkout@v2 |       uses: actions/checkout@v3 | ||||||
|     - name: cache |     - name: cache | ||||||
|       uses: actions/cache@v2 |       uses: actions/cache@v3 | ||||||
|       with: |       with: | ||||||
|         path: ~/go/pkg/mod |         path: ~/go/pkg/mod | ||||||
|         key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |         key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||||||
| @@ -31,9 +32,9 @@ jobs: | |||||||
|     runs-on: ubuntu-latest |     runs-on: ubuntu-latest | ||||||
|     steps: |     steps: | ||||||
|       - name: checkout |       - name: checkout | ||||||
|         uses: actions/checkout@v2 |         uses: actions/checkout@v3 | ||||||
|       - name: lint |       - name: lint | ||||||
|         uses: golangci/golangci-lint-action@v2 |         uses: golangci/golangci-lint-action@v3.4.0 | ||||||
|         continue-on-error: true |         continue-on-error: true | ||||||
|         with: |         with: | ||||||
|           # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. |           # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | .idea | ||||||
							
								
								
									
										19
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								go.mod
									
									
									
									
									
								
							| @@ -1,8 +1,19 @@ | |||||||
| module github.com/unistack-org/micro-store-redis/v3 | module go.unistack.org/micro-store-redis/v3 | ||||||
|  |  | ||||||
| go 1.16 | go 1.22 | ||||||
|  |  | ||||||
|  | toolchain go1.22.4 | ||||||
|  |  | ||||||
| require ( | require ( | ||||||
| 	github.com/go-redis/redis/v8 v8.8.2 | 	github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 | ||||||
| 	github.com/unistack-org/micro/v3 v3.3.17 | 	github.com/redis/go-redis/v9 v9.7.0 | ||||||
|  | 	go.unistack.org/micro/v3 v3.10.105 | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | require ( | ||||||
|  | 	github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||||||
|  | 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect | ||||||
|  | 	github.com/google/go-cmp v0.6.0 // indirect | ||||||
|  | 	go.unistack.org/micro-proto/v3 v3.4.1 // indirect | ||||||
|  | 	google.golang.org/protobuf v1.35.2 // indirect | ||||||
| ) | ) | ||||||
|   | |||||||
							
								
								
									
										127
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										127
									
								
								go.sum
									
									
									
									
									
								
							| @@ -1,111 +1,20 @@ | |||||||
| github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= | github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= | ||||||
| github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= | github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= | ||||||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= | ||||||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= | ||||||
| github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= | github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= | ||||||
|  | github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= | ||||||
| github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= | github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= | ||||||
| github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= | github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= | ||||||
| github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg= | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||||||
| github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||||||
| github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= | github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 h1:BIx9TNZH/Jsr4l1i7VVxnV0JPiwYj8qyrHyuL0fGZrk= | ||||||
| github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= | github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0/go.mod h1:eTg/YQtGYAZD5r3DlGlJptJ45AHA+/G+2NPn30PKzik= | ||||||
| github.com/go-redis/redis/v8 v8.8.2 h1:O/NcHqobw7SEptA0yA6up6spZVFtwE06SXM8rgLtsP8= | github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E= | ||||||
| github.com/go-redis/redis/v8 v8.8.2/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqWMnCV1iP5Y= | github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw= | ||||||
| github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= | ||||||
| github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= | go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= | ||||||
| github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= | go.unistack.org/micro/v3 v3.10.105 h1:JYNV0d+fnR7Hy8d4/sjr+25DbSNqq1Z7IPeDDdB+f1I= | ||||||
| github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= | go.unistack.org/micro/v3 v3.10.105/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g= | ||||||
| github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= | google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= | ||||||
| github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= | google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= | ||||||
| github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= |  | ||||||
| github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= |  | ||||||
| github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= |  | ||||||
| github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= |  | ||||||
| github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= |  | ||||||
| github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= |  | ||||||
| github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= |  | ||||||
| github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= |  | ||||||
| github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= |  | ||||||
| github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= |  | ||||||
| github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= |  | ||||||
| github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= |  | ||||||
| github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= |  | ||||||
| github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4= |  | ||||||
| github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg= |  | ||||||
| github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= |  | ||||||
| github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= |  | ||||||
| github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ= |  | ||||||
| github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= |  | ||||||
| github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= |  | ||||||
| github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= |  | ||||||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |  | ||||||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |  | ||||||
| github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= |  | ||||||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |  | ||||||
| github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= |  | ||||||
| github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |  | ||||||
| github.com/unistack-org/micro/v3 v3.3.17 h1:WcyS7InP0DlS/JpRQGLh5sG6VstkdHJbgpMp+gmHmwg= |  | ||||||
| github.com/unistack-org/micro/v3 v3.3.17/go.mod h1:022EOEZZ789hZY3yB5ZSMXU6jLiadBgcNB/cpediV3c= |  | ||||||
| github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= |  | ||||||
| go.opentelemetry.io/otel v0.19.0 h1:Lenfy7QHRXPZVsw/12CWpxX6d/JkrX8wrx2vO8G80Ng= |  | ||||||
| go.opentelemetry.io/otel v0.19.0/go.mod h1:j9bF567N9EfomkSidSfmMwIwIBuP37AMAIzVW85OxSg= |  | ||||||
| go.opentelemetry.io/otel/metric v0.19.0 h1:dtZ1Ju44gkJkYvo+3qGqVXmf88tc+a42edOywypengg= |  | ||||||
| go.opentelemetry.io/otel/metric v0.19.0/go.mod h1:8f9fglJPRnXuskQmKpnad31lcLJ2VmNNqIsx/uIwBSc= |  | ||||||
| go.opentelemetry.io/otel/oteltest v0.19.0 h1:YVfA0ByROYqTwOxqHVZYZExzEpfZor+MU1rU+ip2v9Q= |  | ||||||
| go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoTKFexE/PJ/nSO7IA= |  | ||||||
| go.opentelemetry.io/otel/trace v0.19.0 h1:1ucYlenXIDA1OlHVLDZKX0ObXV5RLaq06DtUKz5e5zc= |  | ||||||
| go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg= |  | ||||||
| golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= |  | ||||||
| golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= |  | ||||||
| golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= |  | ||||||
| golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= |  | ||||||
| golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= |  | ||||||
| golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= |  | ||||||
| golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= |  | ||||||
| golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= |  | ||||||
| golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= |  | ||||||
| golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= |  | ||||||
| golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 h1:0PC75Fz/kyMGhL0e1QnypqK2kQMqKt9csD1GnMJR+Zk= |  | ||||||
| golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= |  | ||||||
| golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |  | ||||||
| golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |  | ||||||
| golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |  | ||||||
| golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |  | ||||||
| golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |  | ||||||
| golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c= |  | ||||||
| golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |  | ||||||
| golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= |  | ||||||
| golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |  | ||||||
| golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= |  | ||||||
| golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= |  | ||||||
| golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= |  | ||||||
| golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |  | ||||||
| golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= |  | ||||||
| golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= |  | ||||||
| golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |  | ||||||
| golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |  | ||||||
| golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |  | ||||||
| golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |  | ||||||
| google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= |  | ||||||
| google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= |  | ||||||
| google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= |  | ||||||
| google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= |  | ||||||
| google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= |  | ||||||
| google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= |  | ||||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |  | ||||||
| gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= |  | ||||||
| gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= |  | ||||||
| gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= |  | ||||||
| gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |  | ||||||
| gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= |  | ||||||
| gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |  | ||||||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= |  | ||||||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |  | ||||||
|   | |||||||
							
								
								
									
										57
									
								
								options.go
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								options.go
									
									
									
									
									
								
							| @@ -1,18 +1,67 @@ | |||||||
| package redis | package redis | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"github.com/go-redis/redis/v8" | 	goredis "github.com/redis/go-redis/v9" | ||||||
| 	"github.com/unistack-org/micro/v3/store" | 	"go.unistack.org/micro/v3/logger" | ||||||
|  | 	"go.unistack.org/micro/v3/meter" | ||||||
|  | 	"go.unistack.org/micro/v3/store" | ||||||
|  | 	"go.unistack.org/micro/v3/tracer" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type configKey struct{} | type configKey struct{} | ||||||
|  |  | ||||||
| func Config(c *redis.Options) store.Option { | func Config(c *goredis.Options) store.Option { | ||||||
| 	return store.SetOption(configKey{}, c) | 	return store.SetOption(configKey{}, c) | ||||||
| } | } | ||||||
|  |  | ||||||
| type clusterConfigKey struct{} | type clusterConfigKey struct{} | ||||||
|  |  | ||||||
| func ClusterConfig(c *redis.ClusterOptions) store.Option { | func ClusterConfig(c *goredis.ClusterOptions) store.Option { | ||||||
| 	return store.SetOption(clusterConfigKey{}, c) | 	return store.SetOption(clusterConfigKey{}, c) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type universalConfigKey struct{} | ||||||
|  |  | ||||||
|  | func UniversalConfig(c *goredis.UniversalOptions) store.Option { | ||||||
|  | 	return store.SetOption(universalConfigKey{}, c) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	labelHost = "redis_host" | ||||||
|  | 	labelName = "redis_name" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Options struct holds wrapper options | ||||||
|  | type Options struct { | ||||||
|  | 	Logger    logger.Logger | ||||||
|  | 	Meter     meter.Meter | ||||||
|  | 	Tracer    tracer.Tracer | ||||||
|  | 	RedisHost string | ||||||
|  | 	RedisName string | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Option func signature | ||||||
|  | type Option func(*Options) | ||||||
|  |  | ||||||
|  | // NewOptions create new Options struct from provided option slice | ||||||
|  | func NewOptions(opts ...Option) Options { | ||||||
|  | 	options := Options{ | ||||||
|  | 		Logger: logger.DefaultLogger, | ||||||
|  | 		Meter:  meter.DefaultMeter, | ||||||
|  | 		Tracer: tracer.DefaultTracer, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, o := range opts { | ||||||
|  | 		o(&options) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	options.Meter = options.Meter.Clone( | ||||||
|  | 		meter.Labels( | ||||||
|  | 			labelHost, options.RedisHost, | ||||||
|  | 			labelName, options.RedisName), | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	options.Logger = options.Logger.Clone(logger.WithAddCallerSkipCount(1)) | ||||||
|  |  | ||||||
|  | 	return options | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										836
									
								
								redis.go
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										836
									
								
								redis.go
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							| @@ -2,191 +2,781 @@ package redis | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"reflect" | ||||||
|  | 	"strings" | ||||||
|  | 	"sync/atomic" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/go-redis/redis/v8" | 	goredis "github.com/redis/go-redis/v9" | ||||||
| 	"github.com/unistack-org/micro/v3/store" | 	"go.unistack.org/micro/v3/semconv" | ||||||
|  | 	"go.unistack.org/micro/v3/store" | ||||||
|  | 	pool "go.unistack.org/micro/v3/util/xpool" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type rkv struct { | var ( | ||||||
| 	opts store.Options | 	DefaultPathSeparator = "/" | ||||||
| 	cli  redisClient |  | ||||||
|  | 	DefaultUniversalOptions = &goredis.UniversalOptions{ | ||||||
|  | 		Username:        "", | ||||||
|  | 		Password:        "", // no password set | ||||||
|  | 		MaxRetries:      2, | ||||||
|  | 		MaxRetryBackoff: 256 * time.Millisecond, | ||||||
|  | 		DialTimeout:     1 * time.Second, | ||||||
|  | 		ReadTimeout:     1 * time.Second, | ||||||
|  | 		WriteTimeout:    1 * time.Second, | ||||||
|  | 		PoolTimeout:     1 * time.Second, | ||||||
|  | 		MinIdleConns:    10, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	DefaultClusterOptions = &goredis.ClusterOptions{ | ||||||
|  | 		Username:        "", | ||||||
|  | 		Password:        "", // no password set | ||||||
|  | 		MaxRetries:      2, | ||||||
|  | 		MaxRetryBackoff: 256 * time.Millisecond, | ||||||
|  | 		DialTimeout:     1 * time.Second, | ||||||
|  | 		ReadTimeout:     1 * time.Second, | ||||||
|  | 		WriteTimeout:    1 * time.Second, | ||||||
|  | 		PoolTimeout:     1 * time.Second, | ||||||
|  | 		MinIdleConns:    10, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	DefaultOptions = &goredis.Options{ | ||||||
|  | 		Username:        "", | ||||||
|  | 		Password:        "", // no password set | ||||||
|  | 		DB:              0,  // use default DB | ||||||
|  | 		MaxRetries:      2, | ||||||
|  | 		MaxRetryBackoff: 256 * time.Millisecond, | ||||||
|  | 		DialTimeout:     1 * time.Second, | ||||||
|  | 		ReadTimeout:     1 * time.Second, | ||||||
|  | 		WriteTimeout:    1 * time.Second, | ||||||
|  | 		PoolTimeout:     1 * time.Second, | ||||||
|  | 		MinIdleConns:    10, | ||||||
|  | 	} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type Store struct { | ||||||
|  | 	opts        store.Options | ||||||
|  | 	cli         goredis.UniversalClient | ||||||
|  | 	done        chan struct{} | ||||||
|  | 	pool        *pool.StringsPool | ||||||
|  | 	isConnected atomic.Int32 | ||||||
| } | } | ||||||
|  |  | ||||||
| type redisClient interface { | func (r *Store) Connect(ctx context.Context) error { | ||||||
| 	Get(ctx context.Context, key string) *redis.StringCmd | 	if r.cli == nil { | ||||||
| 	Del(ctx context.Context, keys ...string) *redis.IntCmd | 		return store.ErrNotConnected | ||||||
| 	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd | 	} | ||||||
| 	Keys(ctx context.Context, pattern string) *redis.StringSliceCmd | 	if r.opts.LazyConnect { | ||||||
| 	Exists(ctx context.Context, keys ...string) *redis.IntCmd | 		return nil | ||||||
| 	Close() error | 	} | ||||||
|  | 	return r.connect(ctx) | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) Connect(ctx context.Context) error { | func (r *Store) Init(opts ...store.Option) error { | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (r *rkv) Init(opts ...store.Option) error { |  | ||||||
| 	for _, o := range opts { | 	for _, o := range opts { | ||||||
| 		o(&r.opts) | 		o(&r.opts) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.configure() | 	err := r.configure() | ||||||
| } |  | ||||||
|  |  | ||||||
| func (r *rkv) Disconnect(ctx context.Context) error { |  | ||||||
| 	return r.cli.Close() |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (r *rkv) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error { |  | ||||||
| 	//options := store.NewReadOptions(opts...) |  | ||||||
| 	//if len(options.Table) == 0 { |  | ||||||
| 	//	options.Table = r.opts.Table |  | ||||||
| 	//} |  | ||||||
| 	rkey := fmt.Sprintf("%s%s", r.opts.Table, key) |  | ||||||
| 	st, err := r.cli.Exists(ctx, rkey).Result() |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	if st == 0 { |  | ||||||
| 		return store.ErrNotFound | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) Client() *goredis.Client { | ||||||
|  | 	if c, ok := r.cli.(*goredis.Client); ok { | ||||||
|  | 		return c | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error { | func (r *Store) UniversalClient() goredis.UniversalClient { | ||||||
| 	options := store.NewReadOptions(opts...) | 	return r.cli | ||||||
| 	if len(options.Table) == 0 { | } | ||||||
| 		options.Table = r.opts.Table |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	rkey := fmt.Sprintf("%s%s", options.Table, key) | func (r *Store) ClusterClient() *goredis.ClusterClient { | ||||||
| 	buf, err := r.cli.Get(ctx, rkey).Bytes() | 	if c, ok := r.cli.(*goredis.ClusterClient); ok { | ||||||
| 	if err != nil && err == redis.Nil { | 		return c | ||||||
| 		return store.ErrNotFound | 	} | ||||||
| 	} else if err != nil { | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) Disconnect(ctx context.Context) error { | ||||||
|  | 	var err error | ||||||
|  | 	select { | ||||||
|  | 	case <-r.done: | ||||||
| 		return err | 		return err | ||||||
| 	} | 	default: | ||||||
| 	if buf == nil { | 		if r.cli != nil { | ||||||
| 		return store.ErrNotFound | 			if err = r.cli.Close(); err != nil { | ||||||
| 	} | 				r.isConnected.Store(0) | ||||||
| 	/* |  | ||||||
| 			d, err := r.Client.TTL(rkey).Result() |  | ||||||
| 			if err != nil { |  | ||||||
| 				return nil, err |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			records = append(records, &store.Record{ |  | ||||||
| 				Key:    key, |  | ||||||
| 				Value:  val, |  | ||||||
| 				Expiry: d, |  | ||||||
| 			}) |  | ||||||
| 		} | 		} | ||||||
| 	*/ | 		close(r.done) | ||||||
| 	return r.opts.Codec.Unmarshal(buf, val) | 		return err | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error { | func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error { | ||||||
| 	options := store.NewDeleteOptions(opts...) | 	if err := r.connect(ctx); err != nil { | ||||||
| 	if len(options.Table) == 0 { |  | ||||||
| 		options.Table = r.opts.Table |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	rkey := fmt.Sprintf("%s%s", options.Table, key) |  | ||||||
| 	return r.cli.Del(ctx, rkey).Err() |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error { |  | ||||||
| 	options := store.NewWriteOptions(opts...) |  | ||||||
| 	if len(options.Table) == 0 { |  | ||||||
| 		options.Table = r.opts.Table |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	rkey := fmt.Sprintf("%s%s", options.Table, key) |  | ||||||
| 	buf, err := r.opts.Codec.Marshal(val) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return r.cli.Set(ctx, rkey, buf, options.TTL).Err() | 	b := r.pool.Get() | ||||||
|  | 	defer r.pool.Put(b) | ||||||
|  | 	options := store.NewExistsOptions(opts...) | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key) | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
|  | 	ts := time.Now() | ||||||
|  | 	val, err := r.cli.Exists(ctx, rkey).Result() | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil || (err == nil && val == 0) { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) List(ctx context.Context, opts ...store.ListOption) ([]string, error) { | func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error { | ||||||
|  | 	if err := r.connect(ctx); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	b := r.pool.Get() | ||||||
|  | 	defer r.pool.Put(b) | ||||||
|  |  | ||||||
|  | 	options := store.NewReadOptions(opts...) | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key) | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
|  | 	ts := time.Now() | ||||||
|  | 	buf, err := r.cli.Get(ctx, rkey).Bytes() | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil || (err == nil && buf == nil) { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	switch b := val.(type) { | ||||||
|  | 	case *[]byte: | ||||||
|  | 		*b = buf | ||||||
|  | 	case *string: | ||||||
|  | 		*b = string(buf) | ||||||
|  | 	default: | ||||||
|  | 		if err = r.opts.Codec.Unmarshal(buf, val); err != nil { | ||||||
|  | 			setSpanError(ctx, err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error { | ||||||
|  | 	if err := r.connect(ctx); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	options := store.NewReadOptions(opts...) | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var rkeys []string | ||||||
|  | 	var pools []*strings.Builder | ||||||
|  | 	if r.opts.Namespace != "" || options.Namespace != "" { | ||||||
|  | 		rkeys = make([]string, len(keys)) | ||||||
|  | 		pools = make([]*strings.Builder, len(keys)) | ||||||
|  | 		for idx, key := range keys { | ||||||
|  | 			b := r.pool.Get() | ||||||
|  | 			pools[idx] = b | ||||||
|  | 			rkeys[idx] = r.getKey(b, r.opts.Namespace, options.Namespace, key) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
|  | 	ts := time.Now() | ||||||
|  | 	var rvals []interface{} | ||||||
|  | 	var err error | ||||||
|  | 	if r.opts.Namespace != "" || options.Namespace != "" { | ||||||
|  | 		rvals, err = r.cli.MGet(ctx, rkeys...).Result() | ||||||
|  | 		for idx := range pools { | ||||||
|  | 			r.pool.Put(pools[idx]) | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 		rvals, err = r.cli.MGet(ctx, keys...).Result() | ||||||
|  | 	} | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil || (len(rvals) == 0) { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	vv := reflect.ValueOf(vals) | ||||||
|  | 	vt := reflect.TypeOf(vals) | ||||||
|  | 	switch vv.Kind() { | ||||||
|  | 	case reflect.Ptr: | ||||||
|  | 		vv = reflect.Indirect(vv) | ||||||
|  | 		vt = vt.Elem() | ||||||
|  | 	} | ||||||
|  | 	if vv.Kind() != reflect.Slice { | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} | ||||||
|  | 	nvv := reflect.MakeSlice(vt, len(rvals), len(rvals)) | ||||||
|  | 	vt = vt.Elem() | ||||||
|  | 	for idx := 0; idx < len(rvals); idx++ { | ||||||
|  | 		if rvals[idx] == nil { | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		itm := nvv.Index(idx) | ||||||
|  | 		var buf []byte | ||||||
|  | 		switch b := rvals[idx].(type) { | ||||||
|  | 		case []byte: | ||||||
|  | 			buf = b | ||||||
|  | 		case string: | ||||||
|  | 			buf = []byte(b) | ||||||
|  | 		} | ||||||
|  | 		// special case for raw data | ||||||
|  | 		if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 { | ||||||
|  | 			itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf))) | ||||||
|  | 			itm.SetBytes(buf) | ||||||
|  | 			continue | ||||||
|  | 		} else if vt.Kind() == reflect.String { | ||||||
|  | 			itm.SetString(string(buf)) | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		itm.Set(reflect.New(vt.Elem())) | ||||||
|  | 		if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil { | ||||||
|  | 			setSpanError(ctx, err) | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	vv.Set(nvv) | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error { | ||||||
|  | 	if err := r.connect(ctx); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	options := store.NewDeleteOptions(opts...) | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var rkeys []string | ||||||
|  | 	var pools []*strings.Builder | ||||||
|  | 	if r.opts.Namespace != "" || options.Namespace != "" { | ||||||
|  | 		rkeys = make([]string, len(keys)) | ||||||
|  | 		pools = make([]*strings.Builder, len(keys)) | ||||||
|  | 		for idx, key := range keys { | ||||||
|  | 			b := r.pool.Get() | ||||||
|  | 			pools[idx] = b | ||||||
|  | 			rkeys[idx] = r.getKey(b, r.opts.Namespace, options.Namespace, key) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
|  | 	ts := time.Now() | ||||||
|  | 	var err error | ||||||
|  | 	if r.opts.Namespace != "" || options.Namespace != "" { | ||||||
|  | 		err = r.cli.Del(ctx, rkeys...).Err() | ||||||
|  | 		for idx := range pools { | ||||||
|  | 			r.pool.Put(pools[idx]) | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 		err = r.cli.Del(ctx, keys...).Err() | ||||||
|  | 	} | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error { | ||||||
|  | 	if err := r.connect(ctx); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	b := r.pool.Get() | ||||||
|  | 	defer r.pool.Put(b) | ||||||
|  |  | ||||||
|  | 	options := store.NewDeleteOptions(opts...) | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
|  | 	ts := time.Now() | ||||||
|  | 	err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err() | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error { | ||||||
|  | 	if err := r.connect(ctx); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	options := store.NewWriteOptions(opts...) | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	kvs := make([]string, 0, len(keys)*2) | ||||||
|  | 	pools := make([]*strings.Builder, len(keys)) | ||||||
|  | 	for idx, key := range keys { | ||||||
|  | 		b := r.pool.Get() | ||||||
|  | 		pools[idx] = b | ||||||
|  | 		kvs = append(kvs, r.getKey(b, r.opts.Namespace, options.Namespace, key)) | ||||||
|  |  | ||||||
|  | 		switch vt := vals[idx].(type) { | ||||||
|  | 		case string: | ||||||
|  | 			kvs = append(kvs, vt) | ||||||
|  | 		case []byte: | ||||||
|  | 			kvs = append(kvs, string(vt)) | ||||||
|  | 		default: | ||||||
|  | 			buf, err := r.opts.Codec.Marshal(vt) | ||||||
|  | 			if err != nil { | ||||||
|  | 				return err | ||||||
|  | 			} | ||||||
|  | 			kvs = append(kvs, string(buf)) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
|  |  | ||||||
|  | 	pipeliner := func(pipe goredis.Pipeliner) error { | ||||||
|  | 		for idx := 0; idx < len(kvs); idx += 2 { | ||||||
|  | 			if _, err := pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result(); err != nil { | ||||||
|  | 				setSpanError(ctx, err) | ||||||
|  | 				return err | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	ts := time.Now() | ||||||
|  | 	cmds, err := r.cli.Pipelined(ctx, pipeliner) | ||||||
|  | 	for idx := range pools { | ||||||
|  | 		r.pool.Put(pools[idx]) | ||||||
|  | 	} | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, cmd := range cmds { | ||||||
|  | 		if err = cmd.Err(); err != nil { | ||||||
|  | 			if err == goredis.Nil { | ||||||
|  | 				r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 				return store.ErrNotFound | ||||||
|  | 			} | ||||||
|  | 			setSpanError(ctx, err) | ||||||
|  | 			r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error { | ||||||
|  | 	if err := r.connect(ctx); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	b := r.pool.Get() | ||||||
|  | 	defer r.pool.Put(b) | ||||||
|  |  | ||||||
|  | 	options := store.NewWriteOptions(opts...) | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key) | ||||||
|  |  | ||||||
|  | 	var buf []byte | ||||||
|  | 	switch vt := val.(type) { | ||||||
|  | 	case string: | ||||||
|  | 		buf = []byte(vt) | ||||||
|  | 	case []byte: | ||||||
|  | 		buf = vt | ||||||
|  | 	default: | ||||||
|  | 		var err error | ||||||
|  | 		buf, err = r.opts.Codec.Marshal(val) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
|  | 	ts := time.Now() | ||||||
|  | 	err := r.cli.Set(ctx, rkey, buf, options.TTL).Err() | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) { | ||||||
|  | 	if err := r.connect(ctx); err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	b := r.pool.Get() | ||||||
|  | 	defer r.pool.Put(b) | ||||||
|  |  | ||||||
| 	options := store.NewListOptions(opts...) | 	options := store.NewListOptions(opts...) | ||||||
| 	if len(options.Table) == 0 { | 	if len(options.Namespace) == 0 { | ||||||
| 		options.Table = r.opts.Table | 		options.Namespace = r.opts.Namespace | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	rkey := r.getKey(b, options.Namespace, "", options.Prefix+"*") | ||||||
|  | 	if options.Suffix != "" { | ||||||
|  | 		rkey += options.Suffix | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	timeout := r.opts.Timeout | ||||||
|  | 	if options.Timeout > 0 { | ||||||
|  | 		timeout = options.Timeout | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if timeout > 0 { | ||||||
|  | 		var cancel context.CancelFunc | ||||||
|  | 		ctx, cancel = context.WithTimeout(ctx, timeout) | ||||||
|  | 		defer cancel() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// TODO: add support for prefix/suffix/limit | 	// TODO: add support for prefix/suffix/limit | ||||||
| 	keys, err := r.cli.Keys(ctx, "*").Result() | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() | ||||||
| 	if err != nil { | 	ts := time.Now() | ||||||
|  | 	var keys []string | ||||||
|  | 	var err error | ||||||
|  |  | ||||||
|  | 	if c, ok := r.cli.(*goredis.ClusterClient); ok { | ||||||
|  | 		err = c.ForEachMaster(ctx, func(nctx context.Context, cli *goredis.Client) error { | ||||||
|  | 			nkeys, nerr := cli.Keys(nctx, rkey).Result() | ||||||
|  | 			if nerr != nil { | ||||||
|  | 				return nerr | ||||||
|  | 			} | ||||||
|  | 			keys = append(keys, nkeys...) | ||||||
|  | 			return nil | ||||||
|  | 		}) | ||||||
|  | 	} else { | ||||||
|  | 		keys, err = r.cli.Keys(ctx, rkey).Result() | ||||||
|  | 	} | ||||||
|  | 	te := time.Since(ts) | ||||||
|  | 	setSpanError(ctx, err) | ||||||
|  |  | ||||||
|  | 	r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() | ||||||
|  | 	r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) | ||||||
|  | 	if err == goredis.Nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() | ||||||
|  | 		return nil, store.ErrNotFound | ||||||
|  | 	} else if err == nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() | ||||||
|  | 	} else if err != nil { | ||||||
|  | 		r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	prefix := r.opts.Namespace | ||||||
|  | 	if options.Namespace != "" { | ||||||
|  | 		prefix = options.Namespace | ||||||
|  | 	} | ||||||
|  | 	if prefix == "" { | ||||||
|  | 		return keys, nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for idx, key := range keys { | ||||||
|  | 		keys[idx] = strings.TrimPrefix(key, prefix) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return keys, nil | 	return keys, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) Options() store.Options { | func (r *Store) Options() store.Options { | ||||||
| 	return r.opts | 	return r.opts | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) Name() string { | func (r *Store) Name() string { | ||||||
| 	return r.opts.Name | 	return r.opts.Name | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) String() string { | func (r *Store) String() string { | ||||||
| 	return "redis" | 	return "redis" | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewStore(opts ...store.Option) store.Store { | func NewStore(opts ...store.Option) *Store { | ||||||
| 	return &rkv{opts: store.NewOptions(opts...)} | 	return &Store{done: make(chan struct{}), opts: store.NewOptions(opts...)} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *rkv) configure() error { | func (r *Store) configure() error { | ||||||
| 	var redisOptions *redis.Options | 	if r.cli != nil && r.opts.Context == nil { | ||||||
| 	var redisClusterOptions *redis.ClusterOptions | 		return nil | ||||||
| 	var err error |  | ||||||
|  |  | ||||||
| 	nodes := r.opts.Nodes |  | ||||||
|  |  | ||||||
| 	if len(nodes) == 0 { |  | ||||||
| 		nodes = []string{"redis://127.0.0.1:6379"} |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	universalOptions := DefaultUniversalOptions | ||||||
|  |  | ||||||
| 	if r.opts.Context != nil { | 	if r.opts.Context != nil { | ||||||
| 		if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok { | 		if o, ok := r.opts.Context.Value(configKey{}).(*goredis.Options); ok { | ||||||
| 			redisOptions = c | 			universalOptions.Addrs = []string{o.Addr} | ||||||
| 		} | 			universalOptions.Dialer = o.Dialer | ||||||
|  | 			universalOptions.OnConnect = o.OnConnect | ||||||
|  | 			universalOptions.Username = o.Username | ||||||
|  | 			universalOptions.Password = o.Password | ||||||
|  |  | ||||||
| 		if c, ok := r.opts.Context.Value(clusterConfigKey{}).(*redis.ClusterOptions); ok { | 			universalOptions.MaxRetries = o.MaxRetries | ||||||
| 			redisClusterOptions = c | 			universalOptions.MinRetryBackoff = o.MinRetryBackoff | ||||||
| 		} | 			universalOptions.MaxRetryBackoff = o.MaxRetryBackoff | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if redisOptions != nil && redisClusterOptions != nil { | 			universalOptions.DialTimeout = o.DialTimeout | ||||||
| 		return fmt.Errorf("must specify only one option Config or ClusterConfig") | 			universalOptions.ReadTimeout = o.ReadTimeout | ||||||
| 	} | 			universalOptions.WriteTimeout = o.WriteTimeout | ||||||
|  | 			universalOptions.ContextTimeoutEnabled = o.ContextTimeoutEnabled | ||||||
|  |  | ||||||
| 	if redisOptions == nil && redisClusterOptions == nil && len(nodes) == 1 { | 			universalOptions.PoolFIFO = o.PoolFIFO | ||||||
| 		redisOptions, err = redis.ParseURL(nodes[0]) |  | ||||||
| 		if err != nil { | 			universalOptions.PoolSize = o.PoolSize | ||||||
| 			//Backwards compatibility | 			universalOptions.PoolTimeout = o.PoolTimeout | ||||||
| 			redisOptions = &redis.Options{ | 			universalOptions.MinIdleConns = o.MinIdleConns | ||||||
| 				Addr:     nodes[0], | 			universalOptions.MaxIdleConns = o.MaxIdleConns | ||||||
| 				Password: "", // no password set | 			universalOptions.ConnMaxIdleTime = o.ConnMaxIdleTime | ||||||
| 				DB:       0,  // use default DB | 			universalOptions.ConnMaxLifetime = o.ConnMaxLifetime | ||||||
|  |  | ||||||
|  | 			if r.opts.TLSConfig != nil { | ||||||
|  | 				universalOptions.TLSConfig = r.opts.TLSConfig | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if o, ok := r.opts.Context.Value(clusterConfigKey{}).(*goredis.ClusterOptions); ok { | ||||||
|  | 			universalOptions.Addrs = o.Addrs | ||||||
|  | 			universalOptions.Dialer = o.Dialer | ||||||
|  | 			universalOptions.OnConnect = o.OnConnect | ||||||
|  | 			universalOptions.Username = o.Username | ||||||
|  | 			universalOptions.Password = o.Password | ||||||
|  |  | ||||||
|  | 			universalOptions.MaxRedirects = o.MaxRedirects | ||||||
|  | 			universalOptions.ReadOnly = o.ReadOnly | ||||||
|  | 			universalOptions.RouteByLatency = o.RouteByLatency | ||||||
|  | 			universalOptions.RouteRandomly = o.RouteRandomly | ||||||
|  |  | ||||||
|  | 			universalOptions.MaxRetries = o.MaxRetries | ||||||
|  | 			universalOptions.MinRetryBackoff = o.MinRetryBackoff | ||||||
|  | 			universalOptions.MaxRetryBackoff = o.MaxRetryBackoff | ||||||
|  |  | ||||||
|  | 			universalOptions.DialTimeout = o.DialTimeout | ||||||
|  | 			universalOptions.ReadTimeout = o.ReadTimeout | ||||||
|  | 			universalOptions.WriteTimeout = o.WriteTimeout | ||||||
|  | 			universalOptions.ContextTimeoutEnabled = o.ContextTimeoutEnabled | ||||||
|  |  | ||||||
|  | 			universalOptions.PoolFIFO = o.PoolFIFO | ||||||
|  |  | ||||||
|  | 			universalOptions.PoolSize = o.PoolSize | ||||||
|  | 			universalOptions.PoolTimeout = o.PoolTimeout | ||||||
|  | 			universalOptions.MinIdleConns = o.MinIdleConns | ||||||
|  | 			universalOptions.MaxIdleConns = o.MaxIdleConns | ||||||
|  | 			universalOptions.ConnMaxIdleTime = o.ConnMaxIdleTime | ||||||
|  | 			universalOptions.ConnMaxLifetime = o.ConnMaxLifetime | ||||||
|  | 			if r.opts.TLSConfig != nil { | ||||||
|  | 				universalOptions.TLSConfig = r.opts.TLSConfig | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if o, ok := r.opts.Context.Value(universalConfigKey{}).(*goredis.UniversalOptions); ok { | ||||||
|  | 			universalOptions = o | ||||||
|  | 			if r.opts.TLSConfig != nil { | ||||||
|  | 				universalOptions.TLSConfig = r.opts.TLSConfig | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} else if redisOptions == nil && redisClusterOptions == nil && len(nodes) > 1 { |  | ||||||
| 		redisClusterOptions = &redis.ClusterOptions{Addrs: nodes} |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if redisOptions != nil { | 	if len(r.opts.Addrs) > 0 { | ||||||
| 		r.cli = redis.NewClient(redisOptions) | 		universalOptions.Addrs = r.opts.Addrs | ||||||
| 	} else if redisClusterOptions != nil { | 	} else { | ||||||
| 		r.cli = redis.NewClusterClient(redisClusterOptions) | 		universalOptions.Addrs = []string{"127.0.0.1:6379"} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	r.cli = goredis.NewUniversalClient(universalOptions) | ||||||
|  | 	setTracing(r.cli, r.opts.Tracer) | ||||||
|  |  | ||||||
|  | 	r.pool = pool.NewStringsPool(50) | ||||||
|  |  | ||||||
|  | 	r.statsMeter() | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace string, key string) string { | ||||||
|  | 	if opNamespace == "" { | ||||||
|  | 		opNamespace = mainNamespace | ||||||
|  | 	} | ||||||
|  | 	if opNamespace != "" { | ||||||
|  | 		b.WriteString(opNamespace) | ||||||
|  | 		b.WriteString(DefaultPathSeparator) | ||||||
|  | 	} | ||||||
|  | 	b.WriteString(key) | ||||||
|  | 	return b.String() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) connect(ctx context.Context) (err error) { | ||||||
|  | 	if r.isConnected.Load() == 0 { | ||||||
|  | 		if err = r.cli.Ping(ctx).Err(); err != nil { | ||||||
|  | 			setSpanError(ctx, err) | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	r.isConnected.Store(1) | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										199
									
								
								redis_test.go
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										199
									
								
								redis_test.go
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							| @@ -1,19 +1,48 @@ | |||||||
| package redis | package redis | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"bytes" | ||||||
| 	"context" | 	"context" | ||||||
| 	"os" | 	"os" | ||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/go-redis/redis/v8" | 	goredis "github.com/redis/go-redis/v9" | ||||||
| 	"github.com/unistack-org/micro/v3/store" | 	"go.unistack.org/micro/v3/store" | ||||||
|  | 	"go.unistack.org/micro/v3/tracer" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | func TestKeepTTL(t *testing.T) { | ||||||
|  | 	ctx := context.Background() | ||||||
|  |  | ||||||
|  | 	if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { | ||||||
|  | 		t.Skip() | ||||||
|  | 	} | ||||||
|  | 	r := NewStore(store.Addrs(os.Getenv("STORE_NODES"))) | ||||||
|  |  | ||||||
|  | 	if err := r.Init(); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	if err := r.Connect(ctx); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	key := "key" | ||||||
|  | 	err := r.Write(ctx, key, "val1", store.WriteTTL(15*time.Second)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Write error: %v", err) | ||||||
|  | 	} | ||||||
|  | 	time.Sleep(3 * time.Second) | ||||||
|  | 	err = r.Write(ctx, key, "val2", store.WriteTTL(-1)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Write error: %v", err) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| func Test_rkv_configure(t *testing.T) { | func Test_rkv_configure(t *testing.T) { | ||||||
| 	type fields struct { | 	type fields struct { | ||||||
| 		options store.Options | 		options store.Options | ||||||
| 		Client  *redis.Client | 		Client  goredis.UniversalClient | ||||||
| 	} | 	} | ||||||
| 	type wantValues struct { | 	type wantValues struct { | ||||||
| 		username string | 		username string | ||||||
| @@ -27,44 +56,54 @@ func Test_rkv_configure(t *testing.T) { | |||||||
| 		wantErr bool | 		wantErr bool | ||||||
| 		want    wantValues | 		want    wantValues | ||||||
| 	}{ | 	}{ | ||||||
| 		{name: "No Url", fields: fields{options: store.Options{}, Client: nil}, | 		{ | ||||||
|  | 			name: "No Url", fields: fields{options: store.Options{}, Client: nil}, | ||||||
| 			wantErr: false, want: wantValues{ | 			wantErr: false, want: wantValues{ | ||||||
| 				username: "", | 				username: "", | ||||||
| 				password: "", | 				password: "", | ||||||
| 				address:  "127.0.0.1:6379", | 				address:  "127.0.0.1:6379", | ||||||
| 			}}, | 			}, | ||||||
| 		{name: "legacy Url", fields: fields{options: store.Options{Nodes: []string{"127.0.0.1:6379"}}, Client: nil}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "legacy Url", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"127.0.0.1:6379"}}, Client: nil}, | ||||||
| 			wantErr: false, want: wantValues{ | 			wantErr: false, want: wantValues{ | ||||||
| 				username: "", | 				username: "", | ||||||
| 				password: "", | 				password: "", | ||||||
| 				address:  "127.0.0.1:6379", | 				address:  "127.0.0.1:6379", | ||||||
| 			}}, | 			}, | ||||||
| 		{name: "New Url", fields: fields{options: store.Options{Nodes: []string{"redis://127.0.0.1:6379"}}, Client: nil}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "New Url", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://127.0.0.1:6379"}}, Client: nil}, | ||||||
| 			wantErr: false, want: wantValues{ | 			wantErr: false, want: wantValues{ | ||||||
| 				username: "", | 				username: "", | ||||||
| 				password: "", | 				password: "", | ||||||
| 				address:  "127.0.0.1:6379", | 				address:  "127.0.0.1:6379", | ||||||
| 			}}, | 			}, | ||||||
| 		{name: "Url with Pwd", fields: fields{options: store.Options{Nodes: []string{"redis://:password@redis:6379"}}, Client: nil}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "Url with Pwd", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://:password@redis:6379"}}, Client: nil}, | ||||||
| 			wantErr: false, want: wantValues{ | 			wantErr: false, want: wantValues{ | ||||||
| 				username: "", | 				username: "", | ||||||
| 				password: "password", | 				password: "password", | ||||||
| 				address:  "redis:6379", | 				address:  "redis:6379", | ||||||
| 			}}, | 			}, | ||||||
| 		{name: "Url with username and Pwd", fields: fields{options: store.Options{Nodes: []string{"redis://username:password@redis:6379"}}, Client: nil}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "Url with username and Pwd", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://username:password@redis:6379"}}, Client: nil}, | ||||||
| 			wantErr: false, want: wantValues{ | 			wantErr: false, want: wantValues{ | ||||||
| 				username: "username", | 				username: "username", | ||||||
| 				password: "password", | 				password: "password", | ||||||
| 				address:  "redis:6379", | 				address:  "redis:6379", | ||||||
| 			}}, | 			}, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| 	for _, tt := range tests { | 	for _, tt := range tests { | ||||||
| 		t.Run(tt.name, func(t *testing.T) { | 		t.Run(tt.name, func(t *testing.T) { | ||||||
| 			r := &rkv{ | 			rc := &Store{ | ||||||
| 				opts: tt.fields.options, | 				opts: tt.fields.options, | ||||||
| 				cli:  tt.fields.Client, | 				cli:  tt.fields.Client, | ||||||
| 			} | 			} | ||||||
| 			err := r.configure() | 			err := rc.configure() | ||||||
| 			if (err != nil) != tt.wantErr { | 			if (err != nil) != tt.wantErr { | ||||||
| 				t.Errorf("configure() error = %v, wantErr %v", err, tt.wantErr) | 				t.Errorf("configure() error = %v, wantErr %v", err, tt.wantErr) | ||||||
| 				return | 				return | ||||||
| @@ -79,33 +118,143 @@ func Test_Store(t *testing.T) { | |||||||
| 	if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { | 	if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { | ||||||
| 		t.Skip() | 		t.Skip() | ||||||
| 	} | 	} | ||||||
| 	r := new(rkv) | 	r := NewStore(store.Addrs(os.Getenv("STORE_NODES"))) | ||||||
|  |  | ||||||
| 	//r.options = store.Options{Nodes: []string{"redis://:password@127.0.0.1:6379"}} | 	if err := r.Init(); err != nil { | ||||||
| 	//r.options = store.Options{Nodes: []string{"127.0.0.1:6379"}} | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
| 	r.opts = store.NewOptions(store.Nodes(os.Getenv("STORE_NODES"))) | 	if err := r.Connect(ctx); err != nil { | ||||||
|  |  | ||||||
| 	if err := r.configure(); err != nil { |  | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	key := "myTest" | 	key := "myTest" | ||||||
|  | 	tval := []byte("myValue") | ||||||
| 	var val []byte | 	var val []byte | ||||||
| 	err := r.Write(ctx, key, []byte("myValue"), store.WriteTTL(2*time.Minute)) | 	err := r.Write(ctx, key, tval, store.WriteTTL(2*time.Minute)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("Write error: %v", err) | 		t.Fatalf("Write error: %v", err) | ||||||
| 	} | 	} | ||||||
| 	err = r.Read(ctx, key, val) | 	err = r.Read(ctx, key, &val) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("Read error: %v\n", err) | 		t.Fatalf("Read error: %v\n", err) | ||||||
|  | 	} else if !bytes.Equal(val, tval) { | ||||||
|  | 		t.Fatalf("read err: data not eq") | ||||||
| 	} | 	} | ||||||
|  | 	keys, err := r.List(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("List error: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	_ = keys | ||||||
| 	err = r.Delete(ctx, key) | 	err = r.Delete(ctx, key) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("Delete error: %v\n", err) | 		t.Fatalf("Delete error: %v\n", err) | ||||||
| 	} | 	} | ||||||
| 	_, err = r.List(ctx) | 	// t.Logf("%v", keys) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func Test_MRead(t *testing.T) { | ||||||
|  | 	ctx := context.Background() | ||||||
|  | 	var err error | ||||||
|  | 	if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { | ||||||
|  | 		t.Skip() | ||||||
|  | 	} | ||||||
|  | 	r := NewStore(store.Addrs(os.Getenv("STORE_NODES"))) | ||||||
|  |  | ||||||
|  | 	if err = r.Init(); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	if err = r.Connect(ctx); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	key1 := "myTest1" | ||||||
|  | 	key2 := "myTest2" | ||||||
|  | 	tval1 := []byte("myValue1") | ||||||
|  | 	tval2 := []byte("myValue2") | ||||||
|  | 	var vals [][]byte | ||||||
|  | 	err = r.Write(ctx, key1, tval1, store.WriteTTL(2*time.Minute)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Write error: %v", err) | ||||||
|  | 	} | ||||||
|  | 	err = r.Write(ctx, key2, tval2, store.WriteTTL(2*time.Minute)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Write error: %v", err) | ||||||
|  | 	} | ||||||
|  | 	err = r.MRead(ctx, []string{key1, key2}, &vals) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Read error: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	// t.Logf("%s", vals) | ||||||
|  | 	_ = vals | ||||||
|  | 	keys, err := r.List(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("List error: %v\n", err) | 		t.Fatalf("List error: %v\n", err) | ||||||
| 	} | 	} | ||||||
|  | 	_ = keys | ||||||
|  | 	// t.Logf("%v", keys) | ||||||
|  | 	err = r.Delete(ctx, key1) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Delete error: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	err = r.Delete(ctx, key2) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Delete error: %v\n", err) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | func Test_MReadCodec(t *testing.T) { | ||||||
|  | 	type mytype struct { | ||||||
|  | 		Key string `json:"name"` | ||||||
|  | 		Val string `json:"val"` | ||||||
|  | 	} | ||||||
|  | 	ctx := context.Background() | ||||||
|  | 	var err error | ||||||
|  | 	if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { | ||||||
|  | 		t.Skip() | ||||||
|  | 	} | ||||||
|  | 	r := NewStore(store.Nodes(os.Getenv("STORE_NODES")), store.Codec(jsoncodec.NewCodec())) | ||||||
|  |  | ||||||
|  | 	if err = r.Init(); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	if err = r.Connect(ctx); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	key1 := "myTest1" | ||||||
|  | 	key2 := "myTest2" | ||||||
|  | 	key3 := "myTest3" | ||||||
|  | 	tval1 := &mytype{Key: "key1", Val: "val1"} | ||||||
|  | 	tval2 := &mytype{Key: "key2", Val: "val2"} | ||||||
|  | 	var vals []*mytype | ||||||
|  | 	err = r.Write(ctx, key1, tval1, store.WriteTTL(2*time.Minute)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Write error: %v", err) | ||||||
|  | 	} | ||||||
|  | 	err = r.Write(ctx, key2, tval2, store.WriteTTL(2*time.Minute)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Write error: %v", err) | ||||||
|  | 	} | ||||||
|  | 	err = r.MRead(ctx, []string{key1, key3, key2}, &vals) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Read error: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	if vals[0].Key != "key1" || vals[1] != nil || vals[2].Key != "key2" { | ||||||
|  | 		t.Fatalf("read err: struct not filled") | ||||||
|  | 	} | ||||||
|  | 	keys, err := r.List(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("List error: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	_ = keys | ||||||
|  | 	err = r.Delete(ctx, key1) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Delete error: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	err = r.Delete(ctx, key2) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Delete error: %v\n", err) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | */ | ||||||
|   | |||||||
							
								
								
									
										49
									
								
								stats.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								stats.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | |||||||
|  | package redis | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
|  | 	goredis "github.com/redis/go-redis/v9" | ||||||
|  | 	"go.unistack.org/micro/v3/meter" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	PoolHitsTotal        = "pool_hits_total" | ||||||
|  | 	PoolMissesTotal      = "pool_misses_total" | ||||||
|  | 	PoolTimeoutTotal     = "pool_timeout_total" | ||||||
|  | 	PoolConnTotalCurrent = "pool_conn_total_current" | ||||||
|  | 	PoolConnIdleCurrent  = "pool_conn_idle_current" | ||||||
|  | 	PoolConnStaleTotal   = "pool_conn_stale_total" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type Statser interface { | ||||||
|  | 	PoolStats() *goredis.PoolStats | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Store) statsMeter() { | ||||||
|  | 	var st Statser | ||||||
|  |  | ||||||
|  | 	if r.cli != nil { | ||||||
|  | 		st = r.cli | ||||||
|  | 	} else { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	go func() { | ||||||
|  | 		ticker := time.NewTicker(meter.DefaultMeterStatsInterval) | ||||||
|  | 		defer ticker.Stop() | ||||||
|  |  | ||||||
|  | 		for _ = range ticker.C { | ||||||
|  | 			if st == nil { | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			stats := st.PoolStats() | ||||||
|  | 			r.opts.Meter.Counter(PoolHitsTotal).Set(uint64(stats.Hits)) | ||||||
|  | 			r.opts.Meter.Counter(PoolMissesTotal).Set(uint64(stats.Misses)) | ||||||
|  | 			r.opts.Meter.Counter(PoolTimeoutTotal).Set(uint64(stats.Timeouts)) | ||||||
|  | 			r.opts.Meter.Counter(PoolConnTotalCurrent).Set(uint64(stats.TotalConns)) | ||||||
|  | 			r.opts.Meter.Counter(PoolConnIdleCurrent).Set(uint64(stats.IdleConns)) | ||||||
|  | 			r.opts.Meter.Counter(PoolConnStaleTotal).Set(uint64(stats.StaleConns)) | ||||||
|  | 		} | ||||||
|  | 	}() | ||||||
|  | } | ||||||
							
								
								
									
										128
									
								
								tracer.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								tracer.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,128 @@ | |||||||
|  | package redis | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"fmt" | ||||||
|  | 	"net" | ||||||
|  | 	"strconv" | ||||||
|  |  | ||||||
|  | 	rediscmd "github.com/redis/go-redis/extra/rediscmd/v9" | ||||||
|  | 	goredis "github.com/redis/go-redis/v9" | ||||||
|  | 	"go.unistack.org/micro/v3/tracer" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func setTracing(rdb goredis.UniversalClient, tr tracer.Tracer, opts ...tracer.SpanOption) { | ||||||
|  | 	switch rdb := rdb.(type) { | ||||||
|  | 	case *goredis.Client: | ||||||
|  | 		opt := rdb.Options() | ||||||
|  | 		connString := formatDBConnString(opt.Network, opt.Addr) | ||||||
|  | 		rdb.AddHook(newTracingHook(connString, tr)) | ||||||
|  | 	case *goredis.ClusterClient: | ||||||
|  | 		rdb.OnNewNode(func(rdb *goredis.Client) { | ||||||
|  | 			opt := rdb.Options() | ||||||
|  | 			connString := formatDBConnString(opt.Network, opt.Addr) | ||||||
|  | 			rdb.AddHook(newTracingHook(connString, tr)) | ||||||
|  | 		}) | ||||||
|  | 	case *goredis.Ring: | ||||||
|  | 		rdb.OnNewNode(func(rdb *goredis.Client) { | ||||||
|  | 			opt := rdb.Options() | ||||||
|  | 			connString := formatDBConnString(opt.Network, opt.Addr) | ||||||
|  | 			rdb.AddHook(newTracingHook(connString, tr)) | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type tracingHook struct { | ||||||
|  | 	tr   tracer.Tracer | ||||||
|  | 	opts []tracer.SpanOption | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var _ goredis.Hook = (*tracingHook)(nil) | ||||||
|  |  | ||||||
|  | func newTracingHook(connString string, tr tracer.Tracer, opts ...tracer.SpanOption) *tracingHook { | ||||||
|  | 	opts = append(opts, tracer.WithSpanKind(tracer.SpanKindClient)) | ||||||
|  | 	if connString != "" { | ||||||
|  | 		opts = append(opts, tracer.WithSpanLabels("db.connection_string", connString)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return &tracingHook{ | ||||||
|  | 		tr:   tr, | ||||||
|  | 		opts: opts, | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (h *tracingHook) DialHook(hook goredis.DialHook) goredis.DialHook { | ||||||
|  | 	return func(ctx context.Context, network, addr string) (net.Conn, error) { | ||||||
|  | 		/* | ||||||
|  | 			_, span := h.tr.Start(ctx, "goredis.dial", h.opts...) | ||||||
|  | 			defer span.Finish() | ||||||
|  | 		*/ | ||||||
|  | 		conn, err := hook(ctx, network, addr) | ||||||
|  | 		// recordError(span, err) | ||||||
|  |  | ||||||
|  | 		return conn, err | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (h *tracingHook) ProcessHook(hook goredis.ProcessHook) goredis.ProcessHook { | ||||||
|  | 	return func(ctx context.Context, cmd goredis.Cmder) error { | ||||||
|  | 		cmdString := rediscmd.CmdString(cmd) | ||||||
|  | 		var err error | ||||||
|  |  | ||||||
|  | 		switch cmdString { | ||||||
|  | 		case "cluster slots": | ||||||
|  | 			break | ||||||
|  | 		default: | ||||||
|  | 			_, span := h.tr.Start(ctx, "goredis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...) | ||||||
|  | 			defer func() { | ||||||
|  | 				recordError(span, err) | ||||||
|  | 				span.Finish() | ||||||
|  | 			}() | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		err = hook(ctx, cmd) | ||||||
|  |  | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (h *tracingHook) ProcessPipelineHook(hook goredis.ProcessPipelineHook) goredis.ProcessPipelineHook { | ||||||
|  | 	return func(ctx context.Context, cmds []goredis.Cmder) error { | ||||||
|  | 		_, cmdsString := rediscmd.CmdsString(cmds) | ||||||
|  |  | ||||||
|  | 		opts := append(h.opts, tracer.WithSpanLabels( | ||||||
|  | 			"db.goredis.num_cmd", strconv.Itoa(len(cmds)), | ||||||
|  | 			"db.statement", cmdsString, | ||||||
|  | 		)) | ||||||
|  |  | ||||||
|  | 		_, span := h.tr.Start(ctx, "goredis.process_pipeline", opts...) | ||||||
|  | 		defer span.Finish() | ||||||
|  |  | ||||||
|  | 		err := hook(ctx, cmds) | ||||||
|  | 		recordError(span, err) | ||||||
|  |  | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func setSpanError(ctx context.Context, err error) { | ||||||
|  | 	if err == nil || err == goredis.Nil { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	if sp, ok := tracer.SpanFromContext(ctx); !ok && sp != nil { | ||||||
|  | 		sp.SetStatus(tracer.SpanStatusError, err.Error()) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func recordError(span tracer.Span, err error) { | ||||||
|  | 	if err != nil && err != goredis.Nil { | ||||||
|  | 		span.SetStatus(tracer.SpanStatusError, err.Error()) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func formatDBConnString(network, addr string) string { | ||||||
|  | 	if network == "tcp" { | ||||||
|  | 		network = "redis" | ||||||
|  | 	} | ||||||
|  | 	return fmt.Sprintf("%s://%s", network, addr) | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user