Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
3cd07fafaa | |||
5f5ce49407 | |||
552a30fb6e | |||
cbf00a0981 | |||
7fc5a26347 | |||
24f9f41de4 | |||
77fe9cbef6 | |||
c59902201d | |||
0981f89f60 | |||
332fe5f4d4 | |||
757fe0245b | |||
27eccc1ed2 | |||
7c641fa8ac |
@ -4,41 +4,31 @@ 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@v3
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 'stable'
|
||||||
- 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.
|
19
.github/dependabot.yml
vendored
19
.github/dependabot.yml
vendored
@ -1,19 +0,0 @@
|
|||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
|
|
||||||
# Maintain dependencies for GitHub Actions
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
47
.github/workflows/build.yml
vendored
47
.github/workflows/build.yml
vendored
@ -1,47 +0,0 @@
|
|||||||
name: build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
78
.github/workflows/codeql-analysis.yml
vendored
78
.github/workflows/codeql-analysis.yml
vendored
@ -1,78 +0,0 @@
|
|||||||
# For most projects, this workflow file will not need changing; you simply need
|
|
||||||
# to commit it to your repository.
|
|
||||||
#
|
|
||||||
# You may wish to alter this file to override the set of languages analyzed,
|
|
||||||
# or to provide custom queries or build logic.
|
|
||||||
#
|
|
||||||
# ******** NOTE ********
|
|
||||||
# We have attempted to detect the languages in your repository. Please check
|
|
||||||
# the `language` matrix defined below to confirm you have the correct set of
|
|
||||||
# supported CodeQL languages.
|
|
||||||
#
|
|
||||||
name: "codeql"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
||||||
# Learn more:
|
|
||||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: init
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 https://git.io/JvXDl
|
|
||||||
|
|
||||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
||||||
# and modify them (or add more) to build your code if your project
|
|
||||||
# uses a compiled language
|
|
||||||
|
|
||||||
#- run: |
|
|
||||||
# make bootstrap
|
|
||||||
# make release
|
|
||||||
|
|
||||||
- name: analyze
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
27
.github/workflows/dependabot-automerge.yml
vendored
27
.github/workflows/dependabot-automerge.yml
vendored
@ -1,27 +0,0 @@
|
|||||||
name: "dependabot-automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- name: metadata
|
|
||||||
id: metadata
|
|
||||||
uses: dependabot/fetch-metadata@v1.3.6
|
|
||||||
with:
|
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
86
event.go
Normal file
86
event.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
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)
|
||||||
|
}
|
8
go.mod
8
go.mod
@ -5,9 +5,9 @@ go 1.22
|
|||||||
toolchain go1.22.4
|
toolchain go1.22.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0
|
||||||
github.com/redis/go-redis/v9 v9.6.1
|
github.com/redis/go-redis/v9 v9.7.0
|
||||||
go.unistack.org/micro/v3 v3.10.96
|
go.unistack.org/micro/v3 v3.10.108
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -15,5 +15,5 @@ require (
|
|||||||
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/google/go-cmp v0.6.0 // indirect
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
||||||
google.golang.org/protobuf v1.35.1 // indirect
|
google.golang.org/protobuf v1.35.2 // indirect
|
||||||
)
|
)
|
||||||
|
20
go.sum
20
go.sum
@ -1,24 +1,20 @@
|
|||||||
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
|
|
||||||
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/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
|
||||||
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/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 h1:EaDatTxkdHG+U3Bk4EUr+DZ7fOGwTfezUiUJMaIcaho=
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 h1:BIx9TNZH/Jsr4l1i7VVxnV0JPiwYj8qyrHyuL0fGZrk=
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5/go.mod h1:fyalQWdtzDBECAQFBJuQe5bzQ02jGd5Qcbgb97Flm7U=
|
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.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
|
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
||||||
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
|
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
|
||||||
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
|
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
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-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
go.unistack.org/micro/v3 v3.10.96 h1:njukb0DlXOJspX5mLSwcP7+cVSg/8Fy+6++mYkOpY2A=
|
go.unistack.org/micro/v3 v3.10.108 h1:3L7SkilMVLtH8y3pQIPtr3jjQYrf0AMv1oAkoL3nFkE=
|
||||||
go.unistack.org/micro/v3 v3.10.96/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
go.unistack.org/micro/v3 v3.10.108/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
|
@ -61,7 +61,7 @@ func NewOptions(opts ...Option) Options {
|
|||||||
labelName, options.RedisName),
|
labelName, options.RedisName),
|
||||||
)
|
)
|
||||||
|
|
||||||
options.Logger = options.Logger.Clone(logger.WithCallerSkipCount(1))
|
options.Logger = options.Logger.Clone(logger.WithAddCallerSkipCount(1))
|
||||||
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
309
redis.go
309
redis.go
@ -2,20 +2,25 @@ package redis
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
goredis "github.com/redis/go-redis/v9"
|
goredis "github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v3/semconv"
|
"go.unistack.org/micro/v3/semconv"
|
||||||
"go.unistack.org/micro/v3/store"
|
"go.unistack.org/micro/v3/store"
|
||||||
|
"go.unistack.org/micro/v3/util/id"
|
||||||
pool "go.unistack.org/micro/v3/util/xpool"
|
pool "go.unistack.org/micro/v3/util/xpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultPathSeparator = "/"
|
_ store.Store = (*Store)(nil)
|
||||||
|
_ store.Event = (*event)(nil)
|
||||||
DefaultUniversalOptions = &goredis.UniversalOptions{
|
sendEventTime = 10 * time.Millisecond
|
||||||
|
DefaultUniversalOptions = &goredis.UniversalOptions{
|
||||||
Username: "",
|
Username: "",
|
||||||
Password: "", // no password set
|
Password: "", // no password set
|
||||||
MaxRetries: 2,
|
MaxRetries: 2,
|
||||||
@ -54,19 +59,42 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
opts store.Options
|
cli goredis.UniversalClient
|
||||||
cli goredis.UniversalClient
|
pool *pool.StringsPool
|
||||||
done chan struct{}
|
connected *atomic.Uint32
|
||||||
pool *pool.StringsPool
|
opts store.Options
|
||||||
|
watchers map[string]*watcher
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Live() bool {
|
||||||
|
return r.connected.Load() == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Ready() bool {
|
||||||
|
return r.connected.Load() == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Health() bool {
|
||||||
|
return r.connected.Load() == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Connect(ctx context.Context) error {
|
func (r *Store) Connect(ctx context.Context) error {
|
||||||
|
if r.connected.Load() == 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if r.cli == nil {
|
if r.cli == nil {
|
||||||
return store.ErrNotConnected
|
return store.ErrNotConnected
|
||||||
}
|
}
|
||||||
err := r.cli.Ping(ctx).Err()
|
if r.opts.LazyConnect {
|
||||||
setSpanError(ctx, err)
|
return nil
|
||||||
return err
|
}
|
||||||
|
if err := r.cli.Ping(ctx).Err(); err != nil {
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r.connected.Store(1)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Init(opts ...store.Option) error {
|
func (r *Store) Init(opts ...store.Option) error {
|
||||||
@ -101,23 +129,26 @@ func (r *Store) ClusterClient() *goredis.ClusterClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Disconnect(ctx context.Context) error {
|
func (r *Store) Disconnect(ctx context.Context) error {
|
||||||
var err error
|
if r.connected.Load() == 0 {
|
||||||
select {
|
return nil
|
||||||
case <-r.done:
|
|
||||||
return err
|
|
||||||
default:
|
|
||||||
if r.cli != nil {
|
|
||||||
err = r.cli.Close()
|
|
||||||
}
|
|
||||||
close(r.done)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.cli != nil {
|
||||||
|
if err := r.cli.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.connected.Store(1)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
||||||
b := r.pool.Get()
|
b := r.pool.Get()
|
||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
options := store.NewExistsOptions(opts...)
|
options := store.NewExistsOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "exists")
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
if options.Timeout > 0 {
|
if options.Timeout > 0 {
|
||||||
@ -132,21 +163,22 @@ func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOpti
|
|||||||
|
|
||||||
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
val, err := r.cli.Exists(ctx, rkey).Result()
|
val, err := r.cli.Exists(ctx, rkey).Result()
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).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())
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
if err == goredis.Nil || (err == nil && val == 0) {
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
if errors.Is(err, goredis.Nil) || (err == nil && val == 0) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
} else if err != nil {
|
} else {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,6 +190,8 @@ func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...s
|
|||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
options := store.NewReadOptions(opts...)
|
options := store.NewReadOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "read")
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
if options.Timeout > 0 {
|
if options.Timeout > 0 {
|
||||||
@ -172,21 +206,21 @@ func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...s
|
|||||||
|
|
||||||
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
buf, err := r.cli.Get(ctx, rkey).Bytes()
|
buf, err := r.cli.Get(ctx, rkey).Bytes()
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
if err == goredis.Nil || (err == nil && buf == nil) {
|
if errors.Is(err, goredis.Nil) || (err == nil && buf == nil) {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,6 +339,8 @@ func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts
|
|||||||
|
|
||||||
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
||||||
options := store.NewDeleteOptions(opts...)
|
options := store.NewDeleteOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "delete")
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
if options.Timeout > 0 {
|
if options.Timeout > 0 {
|
||||||
@ -329,7 +365,7 @@ func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.Delete
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
var err error
|
var err error
|
||||||
if r.opts.Namespace != "" || options.Namespace != "" {
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
||||||
@ -342,16 +378,16 @@ func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.Delete
|
|||||||
}
|
}
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
if err == goredis.Nil {
|
if err == goredis.Nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -363,6 +399,8 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOpti
|
|||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
options := store.NewDeleteOptions(opts...)
|
options := store.NewDeleteOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "delete")
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
if options.Timeout > 0 {
|
if options.Timeout > 0 {
|
||||||
@ -375,21 +413,22 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOpti
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err()
|
err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err()
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
if err == goredis.Nil {
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
if errors.Is(err, goredis.Nil) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -398,6 +437,8 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOpti
|
|||||||
|
|
||||||
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
||||||
options := store.NewWriteOptions(opts...)
|
options := store.NewWriteOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "write")
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
if options.Timeout > 0 {
|
if options.Timeout > 0 {
|
||||||
@ -431,7 +472,7 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
|
|
||||||
pipeliner := func(pipe goredis.Pipeliner) error {
|
pipeliner := func(pipe goredis.Pipeliner) error {
|
||||||
for idx := 0; idx < len(kvs); idx += 2 {
|
for idx := 0; idx < len(kvs); idx += 2 {
|
||||||
@ -451,27 +492,27 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
if err == goredis.Nil {
|
if err == goredis.Nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "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 == goredis.Nil {
|
if err == goredis.Nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
}
|
}
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -484,6 +525,8 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
|||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
options := store.NewWriteOptions(opts...)
|
options := store.NewWriteOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "write")
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
if options.Timeout > 0 {
|
if options.Timeout > 0 {
|
||||||
@ -512,22 +555,22 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
err := r.cli.Set(ctx, rkey, buf, options.TTL).Err()
|
err := r.cli.Set(ctx, rkey, buf, options.TTL).Err()
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
if err == goredis.Nil {
|
if errors.Is(err, goredis.Nil) {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
} else if err != nil {
|
} else {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -539,6 +582,9 @@ func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, e
|
|||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
options := store.NewListOptions(opts...)
|
options := store.NewListOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "list")
|
||||||
|
|
||||||
if len(options.Namespace) == 0 {
|
if len(options.Namespace) == 0 {
|
||||||
options.Namespace = r.opts.Namespace
|
options.Namespace = r.opts.Namespace
|
||||||
}
|
}
|
||||||
@ -560,7 +606,7 @@ func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add support for prefix/suffix/limit
|
// TODO: add support for prefix/suffix/limit
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
var keys []string
|
var keys []string
|
||||||
var err error
|
var err error
|
||||||
@ -580,16 +626,16 @@ func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, e
|
|||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
setSpanError(ctx, err)
|
setSpanError(ctx, err)
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
if err == goredis.Nil {
|
if err == goredis.Nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
return nil, store.ErrNotFound
|
return nil, store.ErrNotFound
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "git")...).Inc()
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -621,16 +667,21 @@ func (r *Store) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewStore(opts ...store.Option) *Store {
|
func NewStore(opts ...store.Option) *Store {
|
||||||
return &Store{done: make(chan struct{}), opts: store.NewOptions(opts...)}
|
b := atomic.Uint32{}
|
||||||
|
return &Store{
|
||||||
|
opts: store.NewOptions(opts...),
|
||||||
|
connected: &b,
|
||||||
|
watchers: make(map[string]*watcher),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) configure() error {
|
func (r *Store) configure() error {
|
||||||
var universalOptions *goredis.UniversalOptions
|
|
||||||
|
|
||||||
if r.cli != nil && r.opts.Context == nil {
|
if r.cli != nil && r.opts.Context == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
universalOptions := DefaultUniversalOptions
|
||||||
|
|
||||||
if r.opts.Context != nil {
|
if r.opts.Context != nil {
|
||||||
if o, ok := r.opts.Context.Value(configKey{}).(*goredis.Options); ok {
|
if o, ok := r.opts.Context.Value(configKey{}).(*goredis.Options); ok {
|
||||||
universalOptions.Addrs = []string{o.Addr}
|
universalOptions.Addrs = []string{o.Addr}
|
||||||
@ -704,10 +755,6 @@ func (r *Store) configure() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if universalOptions == nil {
|
|
||||||
universalOptions = DefaultUniversalOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(r.opts.Addrs) > 0 {
|
if len(r.opts.Addrs) > 0 {
|
||||||
universalOptions.Addrs = r.opts.Addrs
|
universalOptions.Addrs = r.opts.Addrs
|
||||||
} else {
|
} else {
|
||||||
@ -716,6 +763,7 @@ func (r *Store) configure() error {
|
|||||||
|
|
||||||
r.cli = goredis.NewUniversalClient(universalOptions)
|
r.cli = goredis.NewUniversalClient(universalOptions)
|
||||||
setTracing(r.cli, r.opts.Tracer)
|
setTracing(r.cli, r.opts.Tracer)
|
||||||
|
r.cli.AddHook(newEventHook(r))
|
||||||
|
|
||||||
r.pool = pool.NewStringsPool(50)
|
r.pool = pool.NewStringsPool(50)
|
||||||
|
|
||||||
@ -730,8 +778,99 @@ func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace str
|
|||||||
}
|
}
|
||||||
if opNamespace != "" {
|
if opNamespace != "" {
|
||||||
b.WriteString(opNamespace)
|
b.WriteString(opNamespace)
|
||||||
b.WriteString(DefaultPathSeparator)
|
b.WriteString(r.opts.Separator)
|
||||||
}
|
}
|
||||||
b.WriteString(key)
|
b.WriteString(key)
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Store) Watch(ctx context.Context, opts ...store.WatchOption) (store.Watcher, error) {
|
||||||
|
id, err := id.New()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
wo, err := store.NewWatchOptions(opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// construct the watcher
|
||||||
|
w := &watcher{
|
||||||
|
exit: make(chan bool),
|
||||||
|
ch: make(chan store.Event),
|
||||||
|
id: id,
|
||||||
|
opts: wo,
|
||||||
|
}
|
||||||
|
|
||||||
|
r.mu.Lock()
|
||||||
|
r.watchers[w.id] = w
|
||||||
|
r.mu.Unlock()
|
||||||
|
|
||||||
|
return w, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) sendEvent(e store.Event) {
|
||||||
|
r.mu.RLock()
|
||||||
|
watchers := make([]*watcher, 0, len(r.watchers))
|
||||||
|
for _, w := range r.watchers {
|
||||||
|
watchers = append(watchers, w)
|
||||||
|
}
|
||||||
|
r.mu.RUnlock()
|
||||||
|
for _, w := range watchers {
|
||||||
|
select {
|
||||||
|
case <-w.exit:
|
||||||
|
r.mu.Lock()
|
||||||
|
delete(r.watchers, w.id)
|
||||||
|
r.mu.Unlock()
|
||||||
|
default:
|
||||||
|
select {
|
||||||
|
case w.ch <- e:
|
||||||
|
case <-time.After(sendEventTime):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type watcher struct {
|
||||||
|
ch chan store.Event
|
||||||
|
exit chan bool
|
||||||
|
opts store.WatchOptions
|
||||||
|
id string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watcher) Next() (store.Event, error) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case e := <-w.ch:
|
||||||
|
return e, nil
|
||||||
|
case <-w.exit:
|
||||||
|
return nil, store.ErrWatcherStopped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watcher) Stop() {
|
||||||
|
select {
|
||||||
|
case <-w.exit:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
close(w.exit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type event struct {
|
||||||
|
ts time.Time
|
||||||
|
t store.EventType
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *event) Error() error {
|
||||||
|
return e.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *event) Timestamp() time.Time {
|
||||||
|
return e.ts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *event) Type() store.EventType {
|
||||||
|
return e.t
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -12,6 +13,27 @@ import (
|
|||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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) {
|
func TestKeepTTL(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
@ -20,7 +42,7 @@ func TestKeepTTL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
if err := r.Init(); err != nil {
|
if err := r.Init(store.LazyConnect(true)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.Connect(ctx); err != nil {
|
if err := r.Connect(ctx); err != nil {
|
||||||
@ -99,9 +121,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{}
|
||||||
rc := &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 := rc.configure()
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
|
23
stats.go
23
stats.go
@ -33,20 +33,17 @@ func (r *Store) statsMeter() {
|
|||||||
ticker := time.NewTicker(meter.DefaultMeterStatsInterval)
|
ticker := time.NewTicker(meter.DefaultMeterStatsInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for _ = range ticker.C {
|
||||||
select {
|
if st == nil {
|
||||||
case <-ticker.C:
|
return
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ func (h *tracingHook) ProcessHook(hook goredis.ProcessHook) goredis.ProcessHook
|
|||||||
case "cluster slots":
|
case "cluster slots":
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
_, span := h.tr.Start(ctx, "goredis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
_, span := h.tr.Start(ctx, "sdk.database", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
||||||
defer func() {
|
defer func() {
|
||||||
recordError(span, err)
|
recordError(span, err)
|
||||||
span.Finish()
|
span.Finish()
|
||||||
@ -91,11 +91,11 @@ func (h *tracingHook) ProcessPipelineHook(hook goredis.ProcessPipelineHook) gore
|
|||||||
_, cmdsString := rediscmd.CmdsString(cmds)
|
_, cmdsString := rediscmd.CmdsString(cmds)
|
||||||
|
|
||||||
opts := append(h.opts, tracer.WithSpanLabels(
|
opts := append(h.opts, tracer.WithSpanLabels(
|
||||||
"db.goredis.num_cmd", strconv.Itoa(len(cmds)),
|
"db.database.num_cmd", strconv.Itoa(len(cmds)),
|
||||||
"db.statement", cmdsString,
|
"db.statement", cmdsString,
|
||||||
))
|
))
|
||||||
|
|
||||||
_, span := h.tr.Start(ctx, "goredis.process_pipeline", opts...)
|
_, span := h.tr.Start(ctx, "sdk.database", opts...)
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
err := hook(ctx, cmds)
|
err := hook(ctx, cmds)
|
||||||
|
Loading…
Reference in New Issue
Block a user