Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
518cc1db73 | |||
4484cd34ec | |||
7bceeee6bf | |||
aed9512b93 | |||
de72a10973 | |||
62c2de51d4 | |||
741b2310ec | |||
1e8a44b088 | |||
2245314c2f | |||
db770c3fe7 |
@@ -1,29 +0,0 @@
|
|||||||
name: lint
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
lint:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup deps
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run lint
|
|
||||||
uses: https://github.com/golangci/golangci-lint-action@v6
|
|
||||||
with:
|
|
||||||
version: 'latest'
|
|
@@ -1,34 +0,0 @@
|
|||||||
name: test
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup deps
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
@@ -1,53 +0,0 @@
|
|||||||
name: test
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, reopened, synchronize]
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
- v4
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
filter: 'blob:none'
|
|
||||||
- name: checkout tests
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: master
|
|
||||||
filter: 'blob:none'
|
|
||||||
repository: unistack-org/micro-tests
|
|
||||||
path: micro-tests
|
|
||||||
- name: setup go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
cache-dependency-path: "**/*.sum"
|
|
||||||
go-version: 'stable'
|
|
||||||
- name: setup go work
|
|
||||||
env:
|
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
|
||||||
run: |
|
|
||||||
go work init
|
|
||||||
go work use .
|
|
||||||
go work use micro-tests
|
|
||||||
- name: setup deps
|
|
||||||
env:
|
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
|
||||||
run: go get -v ./...
|
|
||||||
- name: run tests
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
|
||||||
run: |
|
|
||||||
cd micro-tests
|
|
||||||
go test -mod readonly -v ./... || true
|
|
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}}
|
47
.github/workflows/build.yml
vendored
Normal file
47
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
name: build
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: setup
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: 1.17
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: cache
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: ${{ runner.os }}-go-
|
||||||
|
- name: deps
|
||||||
|
run: go get -v -t -d ./...
|
||||||
|
- name: test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
||||||
|
lint:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: lint
|
||||||
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.30
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
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}}
|
47
.github/workflows/pr.yml
vendored
Normal file
47
.github/workflows/pr.yml
vendored
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
name: prbuild
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: setup
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: 1.17
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: cache
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: ${{ runner.os }}-go-
|
||||||
|
- name: deps
|
||||||
|
run: go get -v -t -d ./...
|
||||||
|
- name: test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
||||||
|
lint:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: lint
|
||||||
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.30
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
@@ -1,5 +0,0 @@
|
|||||||
run:
|
|
||||||
concurrency: 8
|
|
||||||
deadline: 5m
|
|
||||||
issues-exit-code: 1
|
|
||||||
tests: true
|
|
12
go.mod
12
go.mod
@@ -1,14 +1,16 @@
|
|||||||
module go.unistack.org/micro-store-redis/v4
|
module go.unistack.org/micro-store-redis/v3
|
||||||
|
|
||||||
go 1.20
|
go 1.21
|
||||||
|
|
||||||
|
toolchain go1.22.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/redis/go-redis/v9 v9.2.1
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.5.3
|
||||||
go.unistack.org/micro/v4 v4.0.7
|
github.com/redis/go-redis/v9 v9.5.3
|
||||||
|
go.unistack.org/micro/v3 v3.10.80
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
|
||||||
)
|
)
|
||||||
|
14
go.sum
14
go.sum
@@ -1,12 +1,14 @@
|
|||||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.2.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/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.5.3 h1:1/BDligzCa40GTllkDnY3Y5DTHuKCONbB2JcRyIfl20=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.5.3/go.mod h1:3dZmcLn3Qw6FLlWASn1g4y+YO9ycEFUOM+bhBmzLVKQ=
|
||||||
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU=
|
||||||
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||||
go.unistack.org/micro/v4 v4.0.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
|
go.unistack.org/micro/v3 v3.10.80 h1:A0zWNoM9MOcMg9gdFFgVkgbT3uSYVIINhuvumX9nP2o=
|
||||||
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
go.unistack.org/micro/v3 v3.10.80/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||||
|
75
options.go
75
options.go
@@ -1,12 +1,79 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v4/options"
|
"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 universalConfigKey struct{}
|
type configKey struct{}
|
||||||
|
|
||||||
func UniversalConfig(c *redis.UniversalOptions) options.Option {
|
func Config(c *redis.Options) store.Option {
|
||||||
return options.ContextOption(universalConfigKey{}, c)
|
return store.SetOption(configKey{}, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
type clusterConfigKey struct{}
|
||||||
|
|
||||||
|
func ClusterConfig(c *redis.ClusterOptions) store.Option {
|
||||||
|
return store.SetOption(clusterConfigKey{}, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultMeterStatsInterval holds default stats interval
|
||||||
|
DefaultMeterStatsInterval = 5 * time.Second
|
||||||
|
// DefaultMeterMetricPrefix holds default metric prefix
|
||||||
|
DefaultMeterMetricPrefix = "micro_store_"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Options struct holds wrapper options
|
||||||
|
type Options struct {
|
||||||
|
Logger logger.Logger
|
||||||
|
Meter meter.Meter
|
||||||
|
Tracer tracer.Tracer
|
||||||
|
MeterMetricPrefix string
|
||||||
|
MeterStatsInterval time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
MeterStatsInterval: DefaultMeterStatsInterval,
|
||||||
|
MeterMetricPrefix: DefaultMeterMetricPrefix,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
options.Meter = options.Meter.Clone(
|
||||||
|
meter.MetricPrefix(options.MeterMetricPrefix),
|
||||||
|
)
|
||||||
|
|
||||||
|
options.Logger = options.Logger.Clone(logger.WithCallerSkipCount(1))
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
// MetricInterval specifies stats interval for *sql.DB
|
||||||
|
func MetricInterval(td time.Duration) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.MeterStatsInterval = td
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MetricPrefix specifies prefix for each metric
|
||||||
|
func MetricPrefix(pref string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.MeterMetricPrefix = pref
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
667
redis.go
667
redis.go
@@ -7,115 +7,245 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
redis "github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v4/options"
|
"go.unistack.org/micro/v3/semconv"
|
||||||
"go.unistack.org/micro/v4/store"
|
"go.unistack.org/micro/v3/store"
|
||||||
|
pool "go.unistack.org/micro/v3/util/xpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultPathSeparator = "/"
|
||||||
|
|
||||||
|
DefaultClusterOptions = &redis.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 = &redis.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 {
|
type Store struct {
|
||||||
opts store.Options
|
opts store.Options
|
||||||
cli redis.UniversalClient
|
cli *wrappedClient
|
||||||
|
done chan struct{}
|
||||||
|
pool pool.Pool[*strings.Builder]
|
||||||
|
}
|
||||||
|
|
||||||
|
type wrappedClient struct {
|
||||||
|
*redis.Client
|
||||||
|
*redis.ClusterClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Connect(ctx context.Context) error {
|
func (r *Store) Connect(ctx context.Context) error {
|
||||||
return r.cli.Ping(ctx).Err()
|
var err error
|
||||||
}
|
if r.cli.Client != nil {
|
||||||
|
err = r.cli.Client.Ping(ctx).Err()
|
||||||
func (r *Store) Init(opts ...options.Option) error {
|
|
||||||
return r.configure(opts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Store) Redis() redis.UniversalClient {
|
|
||||||
return r.cli
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Store) Disconnect(ctx context.Context) error {
|
|
||||||
return r.cli.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Store) Exists(ctx context.Context, key string, opts ...options.Option) error {
|
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
||||||
defer cancel()
|
|
||||||
}
|
}
|
||||||
options := store.NewExistsOptions(opts...)
|
err = r.cli.ClusterClient.Ping(ctx).Err()
|
||||||
if len(options.Namespace) == 0 {
|
setSpanError(ctx, err)
|
||||||
options.Namespace = r.opts.Namespace
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Init(opts ...store.Option) error {
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&r.opts)
|
||||||
}
|
}
|
||||||
if options.Namespace != "" {
|
|
||||||
key = fmt.Sprintf("%s%s", r.opts.Namespace, key)
|
err := r.configure()
|
||||||
}
|
|
||||||
val, err := r.cli.Exists(ctx, key).Result()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if val == 0 {
|
|
||||||
return store.ErrNotFound
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Client() *redis.Client {
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
return r.cli.Client
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
|
func (r *Store) ClusterClient() *redis.ClusterClient {
|
||||||
if r.opts.Timeout > 0 {
|
if r.cli.ClusterClient != nil {
|
||||||
var cancel context.CancelFunc
|
return r.cli.ClusterClient
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
||||||
defer cancel()
|
|
||||||
}
|
}
|
||||||
options := store.NewReadOptions(opts...)
|
return nil
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
|
||||||
}
|
|
||||||
if options.Namespace != "" {
|
|
||||||
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := r.cli.Get(ctx, key).Bytes()
|
|
||||||
if err != nil && err == redis.Nil {
|
|
||||||
return store.ErrNotFound
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if buf == nil {
|
|
||||||
return store.ErrNotFound
|
|
||||||
}
|
|
||||||
return r.opts.Codec.Unmarshal(buf, val)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...options.Option) error {
|
func (r *Store) Disconnect(ctx context.Context) error {
|
||||||
if len(keys) == 1 {
|
var err error
|
||||||
vt := reflect.ValueOf(vals)
|
select {
|
||||||
if vt.Kind() == reflect.Ptr {
|
case <-r.done:
|
||||||
vt = reflect.Indirect(vt)
|
return err
|
||||||
return r.Read(ctx, keys[0], vt.Index(0).Interface(), opts...)
|
default:
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
err = r.cli.Client.Close()
|
||||||
|
} else if r.cli.ClusterClient != nil {
|
||||||
|
err = r.cli.ClusterClient.Close()
|
||||||
}
|
}
|
||||||
|
close(r.done)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if r.opts.Timeout > 0 {
|
}
|
||||||
|
|
||||||
|
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
||||||
|
options := store.NewExistsOptions(opts...)
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
options := store.NewReadOptions(opts...)
|
|
||||||
rkeys := make([]string, 0, len(keys))
|
rkey := r.getKey(r.opts.Namespace, options.Namespace, key)
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
|
ts := time.Now()
|
||||||
|
var err error
|
||||||
|
var val int64
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
val, err = r.cli.Client.Exists(ctx, rkey).Result()
|
||||||
|
} else {
|
||||||
|
val, err = r.cli.ClusterClient.Exists(ctx, rkey).Result()
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
setSpanError(ctx, err)
|
||||||
if options.Namespace != "" {
|
te := time.Since(ts)
|
||||||
rkeys = append(rkeys, fmt.Sprintf("%s%s", options.Namespace, key))
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
||||||
} else {
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
||||||
rkeys = append(rkeys, key)
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
if err == redis.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 *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
||||||
|
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(r.opts.Namespace, options.Namespace, key)
|
||||||
|
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
|
ts := time.Now()
|
||||||
|
var buf []byte
|
||||||
|
var err error
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
buf, err = r.cli.Client.Get(ctx, rkey).Bytes()
|
||||||
|
} else {
|
||||||
|
buf, err = r.cli.ClusterClient.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 == redis.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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rvals, err := r.cli.MGet(ctx, rkeys...).Result()
|
return err
|
||||||
if err != nil && err == redis.Nil {
|
}
|
||||||
return store.ErrNotFound
|
|
||||||
} else if err != nil {
|
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
|
||||||
return err
|
options := store.NewReadOptions(opts...)
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
}
|
}
|
||||||
if len(rvals) == 0 {
|
|
||||||
|
if timeout > 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
||||||
|
for idx, key := range keys {
|
||||||
|
keys[idx] = r.getKey(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.cli.Client != nil {
|
||||||
|
rvals, err = r.cli.Client.MGet(ctx, keys...).Result()
|
||||||
|
} else {
|
||||||
|
rvals, err = r.cli.ClusterClient.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 == redis.Nil || (len(rvals) == 0) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
||||||
return store.ErrNotFound
|
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)
|
vv := reflect.ValueOf(vals)
|
||||||
@@ -146,77 +276,128 @@ func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts
|
|||||||
// special case for raw data
|
// special case for raw data
|
||||||
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
||||||
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
||||||
} else {
|
itm.SetBytes(buf)
|
||||||
itm.Set(reflect.New(vt.Elem()))
|
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 {
|
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
||||||
|
setSpanError(ctx, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vv.Set(nvv)
|
vv.Set(nvv)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...options.Option) error {
|
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
||||||
if len(keys) == 1 {
|
options := store.NewDeleteOptions(opts...)
|
||||||
return r.Delete(ctx, keys[0], opts...)
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
}
|
}
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
options := store.NewDeleteOptions(opts...)
|
|
||||||
if len(options.Namespace) == 0 {
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
||||||
options.Namespace = r.opts.Namespace
|
for idx, key := range keys {
|
||||||
|
keys[idx] = r.getKey(r.opts.Namespace, options.Namespace, key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if options.Namespace == "" {
|
|
||||||
return r.cli.Del(ctx, keys...).Err()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
|
ts := time.Now()
|
||||||
|
var err error
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
err = r.cli.Client.Del(ctx, keys...).Err()
|
||||||
|
} else {
|
||||||
|
err = r.cli.ClusterClient.Del(ctx, keys...).Err()
|
||||||
}
|
}
|
||||||
for idx := range keys {
|
setSpanError(ctx, err)
|
||||||
keys[idx] = options.Namespace + keys[idx]
|
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 == redis.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 r.cli.Del(ctx, keys...).Err()
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Delete(ctx context.Context, key string, opts ...options.Option) error {
|
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
||||||
if r.opts.Timeout > 0 {
|
options := store.NewDeleteOptions(opts...)
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
options := store.NewDeleteOptions(opts...)
|
|
||||||
if len(options.Namespace) == 0 {
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
options.Namespace = r.opts.Namespace
|
ts := time.Now()
|
||||||
|
var err error
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
err = r.cli.Client.Del(ctx, r.getKey(r.opts.Namespace, options.Namespace, key)).Err()
|
||||||
|
} else {
|
||||||
|
err = r.cli.ClusterClient.Del(ctx, r.getKey(r.opts.Namespace, options.Namespace, key)).Err()
|
||||||
}
|
}
|
||||||
if options.Namespace == "" {
|
setSpanError(ctx, err)
|
||||||
return r.cli.Del(ctx, key).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 == redis.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 r.cli.Del(ctx, fmt.Sprintf("%s%s", options.Namespace, key)).Err()
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...options.Option) error {
|
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
||||||
if len(keys) == 1 {
|
|
||||||
return r.Write(ctx, keys[0], vals[0], opts...)
|
|
||||||
}
|
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
||||||
defer cancel()
|
|
||||||
}
|
|
||||||
options := store.NewWriteOptions(opts...)
|
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)
|
kvs := make([]string, 0, len(keys)*2)
|
||||||
|
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
|
||||||
}
|
|
||||||
|
|
||||||
for idx, key := range keys {
|
for idx, key := range keys {
|
||||||
if options.Namespace != "" {
|
kvs = append(kvs, r.getKey(r.opts.Namespace, options.Namespace, key))
|
||||||
kvs = append(kvs, fmt.Sprintf("%s%s", options.Namespace, key))
|
|
||||||
} else {
|
|
||||||
kvs = append(kvs, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch vt := vals[idx].(type) {
|
switch vt := vals[idx].(type) {
|
||||||
case string:
|
case string:
|
||||||
@@ -232,21 +413,50 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmds, err := r.cli.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
var err error
|
ts := time.Now()
|
||||||
|
|
||||||
|
pipeliner := func(pipe redis.Pipeliner) error {
|
||||||
for idx := 0; idx < len(kvs); idx += 2 {
|
for idx := 0; idx < len(kvs); idx += 2 {
|
||||||
if _, err = pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result(); err != nil {
|
if _, err := pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result(); err != nil {
|
||||||
|
setSpanError(ctx, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
}
|
||||||
if err != nil {
|
|
||||||
|
var err error
|
||||||
|
var cmds []redis.Cmder
|
||||||
|
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
cmds, err = r.cli.Client.Pipelined(ctx, pipeliner)
|
||||||
|
} else {
|
||||||
|
cmds, err = r.cli.ClusterClient.Pipelined(ctx, pipeliner)
|
||||||
|
}
|
||||||
|
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 == redis.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
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
if err = cmd.Err(); err != nil {
|
if err = cmd.Err(); err != nil {
|
||||||
|
if err == redis.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 err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -254,17 +464,21 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
|
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
||||||
if r.opts.Timeout > 0 {
|
options := store.NewWriteOptions(opts...)
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
options := store.NewWriteOptions(opts...)
|
rkey := r.getKey(r.opts.Namespace, options.Namespace, key)
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
|
||||||
}
|
|
||||||
|
|
||||||
var buf []byte
|
var buf []byte
|
||||||
switch vt := val.(type) {
|
switch vt := val.(type) {
|
||||||
@@ -280,42 +494,100 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.Namespace != "" {
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
ts := time.Now()
|
||||||
|
var err error
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
err = r.cli.Client.Set(ctx, rkey, buf, options.TTL).Err()
|
||||||
|
} else {
|
||||||
|
err = r.cli.ClusterClient.Set(ctx, rkey, buf, options.TTL).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 == redis.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 r.cli.Set(ctx, key, buf, options.TTL).Err()
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) List(ctx context.Context, opts ...options.Option) ([]string, error) {
|
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
||||||
options := store.NewListOptions(opts...)
|
options := store.NewListOptions(opts...)
|
||||||
if len(options.Namespace) == 0 {
|
if len(options.Namespace) == 0 {
|
||||||
options.Namespace = r.opts.Namespace
|
options.Namespace = r.opts.Namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
rkey := fmt.Sprintf("%s%s*", options.Namespace, options.Prefix)
|
rkey := r.getKey(options.Namespace, "", options.Prefix+"*")
|
||||||
if options.Suffix != "" {
|
if options.Suffix != "" {
|
||||||
rkey += options.Suffix
|
rkey += options.Suffix
|
||||||
}
|
}
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add support for prefix/suffix/limit
|
// TODO: add support for prefix/suffix/limit
|
||||||
keys, err := r.cli.Keys(ctx, rkey).Result()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
if err != nil {
|
ts := time.Now()
|
||||||
|
var keys []string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
keys, err = r.cli.Client.Keys(ctx, rkey).Result()
|
||||||
|
} else {
|
||||||
|
err = r.cli.ClusterClient.ForEachMaster(ctx, func(nctx context.Context, cli *redis.Client) error {
|
||||||
|
nkeys, nerr := cli.Keys(nctx, rkey).Result()
|
||||||
|
if nerr != nil {
|
||||||
|
return nerr
|
||||||
|
}
|
||||||
|
keys = append(keys, nkeys...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
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 == redis.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
|
||||||
}
|
}
|
||||||
if options.Namespace == "" {
|
|
||||||
|
prefix := r.opts.Namespace
|
||||||
|
if options.Namespace != "" {
|
||||||
|
prefix = options.Namespace
|
||||||
|
}
|
||||||
|
if prefix == "" {
|
||||||
return keys, nil
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
nkeys := make([]string, 0, len(keys))
|
for idx, key := range keys {
|
||||||
for _, key := range keys {
|
keys[idx] = strings.TrimPrefix(key, prefix)
|
||||||
nkeys = append(nkeys, strings.TrimPrefix(key, options.Namespace))
|
|
||||||
}
|
}
|
||||||
return nkeys, nil
|
|
||||||
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Options() store.Options {
|
func (r *Store) Options() store.Options {
|
||||||
@@ -330,60 +602,91 @@ func (r *Store) String() string {
|
|||||||
return "redis"
|
return "redis"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStore(opts ...options.Option) *Store {
|
func NewStore(opts ...store.Option) *Store {
|
||||||
return &Store{opts: store.NewOptions(opts...)}
|
return &Store{done: make(chan struct{}), opts: store.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) configure(opts ...options.Option) error {
|
func (r *Store) configure() error {
|
||||||
if r.cli != nil && len(opts) == 0 {
|
var redisOptions *redis.Options
|
||||||
|
var redisClusterOptions *redis.ClusterOptions
|
||||||
|
var err error
|
||||||
|
|
||||||
|
nodes := r.opts.Addrs
|
||||||
|
|
||||||
|
if len(nodes) == 0 {
|
||||||
|
nodes = []string{"redis://127.0.0.1:6379"}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.cli != nil && r.opts.Context == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var redisUniversalOptions *redis.UniversalOptions
|
|
||||||
var err error
|
|
||||||
|
|
||||||
for _, o := range opts {
|
|
||||||
if err = o(r.opts); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.opts.Context != nil {
|
if r.opts.Context != nil {
|
||||||
if c, ok := r.opts.Context.Value(universalConfigKey{}).(*redis.UniversalOptions); ok {
|
if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok {
|
||||||
redisUniversalOptions = c
|
redisOptions = c
|
||||||
if r.opts.TLSConfig != nil {
|
if r.opts.TLSConfig != nil {
|
||||||
redisUniversalOptions.TLSConfig = r.opts.TLSConfig
|
redisOptions.TLSConfig = r.opts.TLSConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c, ok := r.opts.Context.Value(clusterConfigKey{}).(*redis.ClusterOptions); ok {
|
||||||
|
redisClusterOptions = c
|
||||||
|
if r.opts.TLSConfig != nil {
|
||||||
|
redisClusterOptions.TLSConfig = r.opts.TLSConfig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if redisUniversalOptions == nil && r.cli != nil {
|
if redisOptions != nil && redisClusterOptions != nil {
|
||||||
|
return fmt.Errorf("must specify only one option Config or ClusterConfig")
|
||||||
|
}
|
||||||
|
|
||||||
|
if redisOptions == nil && redisClusterOptions == nil && r.cli != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if redisUniversalOptions == nil {
|
if redisOptions == nil && redisClusterOptions == nil && len(nodes) == 1 {
|
||||||
redisUniversalOptions = &redis.UniversalOptions{
|
redisOptions, err = redis.ParseURL(nodes[0])
|
||||||
Username: "",
|
if err != nil {
|
||||||
Password: "", // no password set
|
redisOptions = DefaultOptions
|
||||||
DB: 0, // use default DB
|
redisOptions.Addr = r.opts.Addrs[0]
|
||||||
MaxRetries: 2,
|
redisOptions.TLSConfig = r.opts.TLSConfig
|
||||||
MaxRetryBackoff: 256 * time.Millisecond,
|
|
||||||
DialTimeout: 1 * time.Second,
|
|
||||||
ReadTimeout: 1 * time.Second,
|
|
||||||
WriteTimeout: 1 * time.Second,
|
|
||||||
PoolTimeout: 1 * time.Second,
|
|
||||||
MinIdleConns: 10,
|
|
||||||
TLSConfig: r.opts.TLSConfig,
|
|
||||||
}
|
}
|
||||||
|
} else if redisOptions == nil && redisClusterOptions == nil && len(nodes) > 1 {
|
||||||
|
redisClusterOptions = DefaultClusterOptions
|
||||||
|
redisClusterOptions.Addrs = r.opts.Addrs
|
||||||
|
redisClusterOptions.TLSConfig = r.opts.TLSConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(r.opts.Address) > 0 {
|
if redisOptions != nil {
|
||||||
redisUniversalOptions.Addrs = r.opts.Address
|
c := redis.NewClient(redisOptions)
|
||||||
} else if len(redisUniversalOptions.Addrs) == 0 {
|
setTracing(c, r.opts.Tracer)
|
||||||
redisUniversalOptions.Addrs = []string{"redis://127.0.0.1:6379"}
|
r.cli = &wrappedClient{Client: c}
|
||||||
|
} else if redisClusterOptions != nil {
|
||||||
|
c := redis.NewClusterClient(redisClusterOptions)
|
||||||
|
setTracing(c, r.opts.Tracer)
|
||||||
|
r.cli = &wrappedClient{ClusterClient: c}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.cli = redis.NewUniversalClient(redisUniversalOptions)
|
r.pool = pool.NewPool(func() *strings.Builder { return &strings.Builder{} })
|
||||||
|
|
||||||
|
r.statsMeter()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Store) getKey(mainNamespace string, opNamespace string, key string) string {
|
||||||
|
b := r.pool.Get()
|
||||||
|
defer r.pool.Put(b)
|
||||||
|
b.Reset()
|
||||||
|
|
||||||
|
if opNamespace == "" {
|
||||||
|
opNamespace = mainNamespace
|
||||||
|
}
|
||||||
|
if opNamespace != "" {
|
||||||
|
b.WriteString(opNamespace)
|
||||||
|
b.WriteString(DefaultPathSeparator)
|
||||||
|
}
|
||||||
|
b.WriteString(key)
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
@@ -7,15 +7,41 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"go.unistack.org/micro/v3/store"
|
||||||
"go.unistack.org/micro/v4/options"
|
"go.unistack.org/micro/v3/tracer"
|
||||||
"go.unistack.org/micro/v4/store"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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 *wrappedClient
|
||||||
}
|
}
|
||||||
type wantValues struct {
|
type wantValues struct {
|
||||||
username string
|
username string
|
||||||
@@ -38,7 +64,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "legacy Url", fields: fields{options: store.Options{Address: []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: "",
|
||||||
@@ -46,7 +72,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "New Url", fields: fields{options: store.Options{Address: []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: "",
|
||||||
@@ -54,7 +80,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with Pwd", fields: fields{options: store.Options{Address: []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",
|
||||||
@@ -62,7 +88,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with username and Pwd", fields: fields{options: store.Options{Address: []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",
|
||||||
@@ -91,7 +117,7 @@ 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 := NewStore(options.Address(os.Getenv("STORE_NODES")))
|
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
if err := r.Init(); err != nil {
|
if err := r.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -131,7 +157,7 @@ func Test_MRead(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 := NewStore(options.Address(os.Getenv("STORE_NODES")))
|
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
if err = r.Init(); err != nil {
|
if err = r.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
57
stats.go
Normal file
57
stats.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
meterRequestTotal = "request_total"
|
||||||
|
meterRequestLatencyMicroseconds = "latency_microseconds"
|
||||||
|
meterRequestDurationSeconds = "request_duration_seconds"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Statser interface {
|
||||||
|
PoolStats() *redis.PoolStats
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) statsMeter() {
|
||||||
|
var st Statser
|
||||||
|
|
||||||
|
if r.cli.Client != nil {
|
||||||
|
st = r.cli.Client
|
||||||
|
} else if r.cli.ClusterClient != nil {
|
||||||
|
st = r.cli.ClusterClient
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(DefaultMeterStatsInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
121
tracer.go
Normal file
121
tracer.go
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/extra/rediscmd/v9"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"go.unistack.org/micro/v3/tracer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setTracing(rdb redis.UniversalClient, tr tracer.Tracer, opts ...tracer.SpanOption) {
|
||||||
|
switch rdb := rdb.(type) {
|
||||||
|
case *redis.Client:
|
||||||
|
opt := rdb.Options()
|
||||||
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
|
case *redis.ClusterClient:
|
||||||
|
rdb.AddHook(newTracingHook("", tr, opts...))
|
||||||
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
||||||
|
opt := rdb.Options()
|
||||||
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
|
})
|
||||||
|
case *redis.Ring:
|
||||||
|
rdb.AddHook(newTracingHook("", tr, opts...))
|
||||||
|
rdb.OnNewNode(func(rdb *redis.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 _ redis.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 redis.DialHook) redis.DialHook {
|
||||||
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
_, span := h.tr.Start(ctx, "redis.dial", h.opts...)
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
conn, err := hook(ctx, network, addr)
|
||||||
|
recordError(span, err)
|
||||||
|
|
||||||
|
return conn, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
||||||
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
||||||
|
cmdString := rediscmd.CmdString(cmd)
|
||||||
|
|
||||||
|
_, span := h.tr.Start(ctx, "redis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
err := hook(ctx, cmd)
|
||||||
|
recordError(span, err)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *tracingHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
||||||
|
return func(ctx context.Context, cmds []redis.Cmder) error {
|
||||||
|
_, cmdsString := rediscmd.CmdsString(cmds)
|
||||||
|
|
||||||
|
opts := append(h.opts, tracer.WithSpanLabels(
|
||||||
|
"db.redis.num_cmd", strconv.Itoa(len(cmds)),
|
||||||
|
"db.statement", cmdsString,
|
||||||
|
))
|
||||||
|
|
||||||
|
_, span := h.tr.Start(ctx, "redis.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 == redis.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 != redis.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