Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
4459bfa1a0 | |||
b90361e48a |
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"
|
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}}
|
@@ -4,31 +4,41 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
- v3
|
- v3
|
||||||
- v4
|
|
||||||
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@v5
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 'stable'
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
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
|
- name: deps
|
||||||
run: go get -v -t -d ./...
|
run: go get -v -t -d ./...
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v6
|
|
||||||
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: latest
|
|
||||||
- name: test
|
- name: test
|
||||||
env:
|
env:
|
||||||
INTEGRATION_TESTS: yes
|
INTEGRATION_TESTS: yes
|
||||||
run: go test -mod readonly -v ./...
|
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
|
# Optional: working directory, useful for monorepos
|
||||||
# working-directory: somedir
|
# working-directory: somedir
|
||||||
# Optional: golangci-lint command line arguments.
|
# Optional: golangci-lint command line arguments.
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
.idea
|
|
86
event.go
86
event.go
@@ -1,86 +0,0 @@
|
|||||||
package redis
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
goredis "github.com/redis/go-redis/v9"
|
|
||||||
"go.unistack.org/micro/v3/store"
|
|
||||||
)
|
|
||||||
|
|
||||||
type eventHook struct {
|
|
||||||
s *Store
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ goredis.Hook = (*eventHook)(nil)
|
|
||||||
|
|
||||||
func newEventHook(s *Store) *eventHook {
|
|
||||||
return &eventHook{s: s}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *eventHook) DialHook(hook goredis.DialHook) goredis.DialHook {
|
|
||||||
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
||||||
conn, err := hook(ctx, network, addr)
|
|
||||||
if err != nil {
|
|
||||||
if !isRedisError(err) {
|
|
||||||
if h.s.connected.CompareAndSwap(1, 0) {
|
|
||||||
h.s.sendEvent(&event{ts: time.Now(), err: err, t: store.EventTypeDisconnect})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
h.s.connected.Store(1)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if h.s.connected.CompareAndSwap(0, 1) {
|
|
||||||
h.s.sendEvent(&event{ts: time.Now(), err: err, t: store.EventTypeConnect})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return conn, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *eventHook) ProcessHook(hook goredis.ProcessHook) goredis.ProcessHook {
|
|
||||||
return func(ctx context.Context, cmd goredis.Cmder) error {
|
|
||||||
err := hook(ctx, cmd)
|
|
||||||
if err != nil {
|
|
||||||
if !isRedisError(err) {
|
|
||||||
if h.s.connected.CompareAndSwap(1, 0) {
|
|
||||||
h.s.sendEvent(&event{ts: time.Now(), err: err, t: store.EventTypeDisconnect})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
h.s.connected.Store(1)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if h.s.connected.CompareAndSwap(0, 1) {
|
|
||||||
h.s.sendEvent(&event{ts: time.Now(), err: err, t: store.EventTypeConnect})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *eventHook) ProcessPipelineHook(hook goredis.ProcessPipelineHook) goredis.ProcessPipelineHook {
|
|
||||||
return func(ctx context.Context, cmds []goredis.Cmder) error {
|
|
||||||
err := hook(ctx, cmds)
|
|
||||||
if err != nil {
|
|
||||||
if !isRedisError(err) {
|
|
||||||
if h.s.connected.CompareAndSwap(1, 0) {
|
|
||||||
h.s.sendEvent(&event{ts: time.Now(), err: err, t: store.EventTypeDisconnect})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
h.s.connected.Store(1)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if h.s.connected.CompareAndSwap(0, 1) {
|
|
||||||
h.s.sendEvent(&event{ts: time.Now(), err: err, t: store.EventTypeConnect})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isRedisError(err error) bool {
|
|
||||||
var rerr goredis.Error
|
|
||||||
return errors.As(err, &rerr)
|
|
||||||
}
|
|
17
go.mod
17
go.mod
@@ -1,19 +1,14 @@
|
|||||||
module go.unistack.org/micro-store-redis/v3
|
module go.unistack.org/micro-store-redis/v4
|
||||||
|
|
||||||
go 1.22
|
go 1.20
|
||||||
|
|
||||||
toolchain go1.22.4
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0
|
github.com/redis/go-redis/v9 v9.2.1
|
||||||
github.com/redis/go-redis/v9 v9.7.0
|
go.unistack.org/micro/v4 v4.0.7
|
||||||
go.unistack.org/micro/v3 v3.10.108
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cespare/xxhash/v2 v2.3.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/google/go-cmp v0.6.0 // indirect
|
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
|
||||||
google.golang.org/protobuf v1.35.2 // indirect
|
|
||||||
)
|
)
|
||||||
|
24
go.sum
24
go.sum
@@ -1,20 +1,12 @@
|
|||||||
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.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 h1:BIx9TNZH/Jsr4l1i7VVxnV0JPiwYj8qyrHyuL0fGZrk=
|
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0/go.mod h1:eTg/YQtGYAZD5r3DlGlJptJ45AHA+/G+2NPn30PKzik=
|
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||||
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
go.unistack.org/micro/v4 v4.0.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
|
||||||
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
|
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
|
||||||
go.unistack.org/micro/v3 v3.10.108 h1:3L7SkilMVLtH8y3pQIPtr3jjQYrf0AMv1oAkoL3nFkE=
|
|
||||||
go.unistack.org/micro/v3 v3.10.108/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
|
||||||
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
|
||||||
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
|
||||||
|
61
options.go
61
options.go
@@ -1,67 +1,18 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
goredis "github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v4/options"
|
||||||
"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 *goredis.Options) store.Option {
|
func Config(c *redis.Options) options.Option {
|
||||||
return store.SetOption(configKey{}, c)
|
return options.ContextOption(configKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
type clusterConfigKey struct{}
|
type clusterConfigKey struct{}
|
||||||
|
|
||||||
func ClusterConfig(c *goredis.ClusterOptions) store.Option {
|
func ClusterConfig(c *redis.ClusterOptions) options.Option {
|
||||||
return store.SetOption(clusterConfigKey{}, c)
|
return options.ContextOption(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
|
|
||||||
}
|
}
|
||||||
|
@@ -4,67 +4,18 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
goredis "github.com/redis/go-redis/v9"
|
"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 TestLazyConnect(t *testing.T) {
|
|
||||||
t.Skip("skipping test for manual check")
|
|
||||||
ctx := context.Background()
|
|
||||||
var err error
|
|
||||||
|
|
||||||
r := NewStore()
|
|
||||||
|
|
||||||
if err = r.Init(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err = r.Connect(ctx); err != nil {
|
|
||||||
t.Logf("connect failed %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
if err = r.Write(ctx, "mykey", "myval"); err != nil {
|
|
||||||
t.Logf("failed to write %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(store.LazyConnect(true)); 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 goredis.UniversalClient
|
Client *redis.Client
|
||||||
}
|
}
|
||||||
type wantValues struct {
|
type wantValues struct {
|
||||||
username string
|
username string
|
||||||
@@ -87,7 +38,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "legacy Url", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"127.0.0.1:6379"}}, Client: nil},
|
name: "legacy Url", fields: fields{options: store.Options{Address: []string{"127.0.0.1:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
@@ -95,7 +46,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "New Url", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://127.0.0.1:6379"}}, Client: nil},
|
name: "New Url", fields: fields{options: store.Options{Address: []string{"redis://127.0.0.1:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
@@ -103,7 +54,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with Pwd", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://:password@redis:6379"}}, Client: nil},
|
name: "Url with Pwd", fields: fields{options: store.Options{Address: []string{"redis://:password@redis:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "password",
|
password: "password",
|
||||||
@@ -111,7 +62,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with username and Pwd", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://username:password@redis:6379"}}, Client: nil},
|
name: "Url with username and Pwd", fields: fields{options: store.Options{Address: []string{"redis://username:password@redis:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "username",
|
username: "username",
|
||||||
password: "password",
|
password: "password",
|
||||||
@@ -121,13 +72,11 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
b := atomic.Uint32{}
|
r := &Store{
|
||||||
rc := &Store{
|
|
||||||
opts: tt.fields.options,
|
opts: tt.fields.options,
|
||||||
cli: tt.fields.Client,
|
cli: tt.fields.Client,
|
||||||
connected: &b,
|
|
||||||
}
|
}
|
||||||
err := rc.configure()
|
err := r.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
|
||||||
@@ -142,7 +91,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(store.Addrs(os.Getenv("STORE_NODES")))
|
r := NewStore(options.Address(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
if err := r.Init(); err != nil {
|
if err := r.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -182,7 +131,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(store.Addrs(os.Getenv("STORE_NODES")))
|
r := NewStore(options.Address(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
if err = r.Init(); err != nil {
|
if err = r.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
49
stats.go
49
stats.go
@@ -1,49 +0,0 @@
|
|||||||
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
128
tracer.go
@@ -1,128 +0,0 @@
|
|||||||
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, "sdk.database", 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.database.num_cmd", strconv.Itoa(len(cmds)),
|
|
||||||
"db.statement", cmdsString,
|
|
||||||
))
|
|
||||||
|
|
||||||
_, span := h.tr.Start(ctx, "sdk.database", 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