mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-24 07:08:18 +08:00
Compare commits
164 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a75dce465 | ||
|
|
801f1adf71 | ||
|
|
f76b976262 | ||
|
|
a49f9060c2 | ||
|
|
ebe28882eb | ||
|
|
fdc57d07d7 | ||
|
|
ef22042f4d | ||
|
|
944193ce25 | ||
|
|
dcfc9b79f1 | ||
|
|
b7052854bb | ||
|
|
4729a16142 | ||
|
|
3604659027 | ||
|
|
9f7f94b673 | ||
|
|
0b3629b636 | ||
|
|
a644ec7edd | ||
|
|
9941055eaa | ||
|
|
10fd9131a1 | ||
|
|
90828a0d4a | ||
|
|
b1c3c21c81 | ||
|
|
97a8b3ade5 | ||
|
|
95a5f64493 | ||
|
|
20e659749a | ||
|
|
94708cc78f | ||
|
|
06fafd2153 | ||
|
|
79de932646 | ||
|
|
b562e940e7 | ||
|
|
69068cdaf0 | ||
|
|
f25788ebea | ||
|
|
1293c4321b | ||
|
|
e3e08a7396 | ||
|
|
4b071f4c33 | ||
|
|
81831b60a9 | ||
|
|
1677a4dceb | ||
|
|
dac3600b53 | ||
|
|
3db64c7d47 | ||
|
|
7eb6aae949 | ||
|
|
07128213d6 | ||
|
|
9504d30049 | ||
|
|
ce73b9a85c | ||
|
|
4d2a146733 | ||
|
|
46e236fef7 | ||
|
|
06e4914e41 | ||
|
|
9cadab2684 | ||
|
|
7fe2492009 | ||
|
|
22bdf0bbd5 | ||
|
|
c92a2d1b77 | ||
|
|
b21162d638 | ||
|
|
7c9ef3ca67 | ||
|
|
bbadbe0175 | ||
|
|
f9beab1095 | ||
|
|
de5c59aad3 | ||
|
|
36d3765c5c | ||
|
|
d326e6f813 | ||
|
|
ea52fe2e0d | ||
|
|
05a5de7c6d | ||
|
|
d4c9fd2aff | ||
|
|
776673d57d | ||
|
|
1b87f5e30d | ||
|
|
bc47959384 | ||
|
|
9f6d926455 | ||
|
|
f7a4e3a19e | ||
|
|
a515a3c735 | ||
|
|
6f6f1ae21f | ||
|
|
10f94ffcc2 | ||
|
|
f068062b13 | ||
|
|
799c118d95 | ||
|
|
74cc6b55e8 | ||
|
|
fc59aec2e7 | ||
|
|
7868667b4f | ||
|
|
773b59106b | ||
|
|
97f8667b71 | ||
|
|
b51339b69b | ||
|
|
38a73d7fbe | ||
|
|
e50689beed | ||
|
|
1bc138bd34 | ||
|
|
4b9066eda6 | ||
|
|
0c66e041b5 | ||
|
|
aa2be0163a | ||
|
|
ada2941e87 | ||
|
|
59c0013cd1 | ||
|
|
05737f6519 | ||
|
|
4f6a900fd4 | ||
|
|
63cfe60f1a | ||
|
|
e7acadb15d | ||
|
|
111e626a73 | ||
|
|
1a6d7b3ef6 | ||
|
|
2e1e4f3574 | ||
|
|
22d0a2120a | ||
|
|
68e15360c2 | ||
|
|
1b344a8851 | ||
|
|
d640544a40 | ||
|
|
e6aa6fc361 | ||
|
|
4c927624b0 | ||
|
|
0ea92b7280 | ||
|
|
2cde970c9e | ||
|
|
5061158bd6 | ||
|
|
9138056c01 | ||
|
|
0b1884b6bd | ||
|
|
1f6688e5c1 | ||
|
|
ae7f1aabdd | ||
|
|
b8664be2bb | ||
|
|
6e16a9647e | ||
|
|
bb0e76be47 | ||
|
|
27a20e1ed3 | ||
|
|
cbbbee0ace | ||
|
|
e9650d547b | ||
|
|
60160f56b8 | ||
|
|
05c2f313c7 | ||
|
|
f2a0f78288 | ||
|
|
3e96994b7b | ||
|
|
66c2a28e66 | ||
|
|
9672071b5d | ||
|
|
9581e8445a | ||
|
|
6ec8bc6655 | ||
|
|
d935c83a54 | ||
|
|
590d784800 | ||
|
|
784276b360 | ||
|
|
da80662b0f | ||
|
|
cfda972d50 | ||
|
|
6078bf1a04 | ||
|
|
ce638d26d9 | ||
|
|
422f401153 | ||
|
|
dfeef5e497 | ||
|
|
8c72136631 | ||
|
|
9d6c8f67f5 | ||
|
|
f70805ee60 | ||
|
|
a1466e1707 | ||
|
|
1b477bbef9 | ||
|
|
813625d995 | ||
|
|
15a2802f12 | ||
|
|
5d00dfb962 | ||
|
|
d9620bb072 | ||
|
|
d978563523 | ||
|
|
fb6d7e2fd2 | ||
|
|
2d60f0c65a | ||
|
|
5d4ae201d0 | ||
|
|
05007c86bb | ||
|
|
93584c6ca6 | ||
|
|
22bb7e95fd | ||
|
|
bebf6322ff | ||
|
|
36678f9023 | ||
|
|
90cdd61efc | ||
|
|
28166dedd6 | ||
|
|
0316b6e10e | ||
|
|
4cb68a034a | ||
|
|
847a396f1c | ||
|
|
c1babdf8b2 | ||
|
|
040c9e0954 | ||
|
|
1c85d39add | ||
|
|
4cd065f4f4 | ||
|
|
b9c97678bc | ||
|
|
5208def65a | ||
|
|
3b96dc1598 | ||
|
|
fa3f1bc19c | ||
|
|
8ed22eafdd | ||
|
|
05dd6bd743 | ||
|
|
9af1a42386 | ||
|
|
f3645e420e | ||
|
|
62abac0b7e | ||
|
|
6357e27418 | ||
|
|
1568c3be0e | ||
|
|
27e773fa1f | ||
|
|
d8e17be33e | ||
|
|
da5770ee2b |
29
.github/workflows/go.yml
vendored
29
.github/workflows/go.yml
vendored
@@ -11,15 +11,17 @@ jobs:
|
|||||||
name: Linux
|
name: Linux
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Set up Go 1.x
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: ^1.15
|
|
||||||
id: go
|
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: ^1.16
|
||||||
|
check-latest: true
|
||||||
|
cache: true
|
||||||
|
id: go
|
||||||
|
|
||||||
- name: Get dependencies
|
- name: Get dependencies
|
||||||
run: |
|
run: |
|
||||||
go get -v -t -d ./...
|
go get -v -t -d ./...
|
||||||
@@ -34,20 +36,23 @@ jobs:
|
|||||||
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
|
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
|
||||||
|
|
||||||
- name: Codecov
|
- name: Codecov
|
||||||
uses: codecov/codecov-action@v2
|
uses: codecov/codecov-action@v3
|
||||||
|
|
||||||
test-win:
|
test-win:
|
||||||
name: Windows
|
name: Windows
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Set up Go 1.x
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: ^1.15
|
|
||||||
|
|
||||||
- name: Checkout codebase
|
- name: Checkout codebase
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
# use 1.16 to guarantee Go 1.16 compatibility
|
||||||
|
go-version: 1.16
|
||||||
|
check-latest: true
|
||||||
|
cache: true
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
go mod verify
|
go mod verify
|
||||||
|
|||||||
2
.github/workflows/issue-translator.yml
vendored
2
.github/workflows/issue-translator.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: tomsun28/issues-translate-action@v2.6
|
- uses: usthe/issues-translate-action@v2.7
|
||||||
with:
|
with:
|
||||||
IS_MODIFY_TITLE: true
|
IS_MODIFY_TITLE: true
|
||||||
# not require, default false, . Decide whether to modify the issue title
|
# not require, default false, . Decide whether to modify the issue title
|
||||||
|
|||||||
6
.github/workflows/issues.yml
vendored
6
.github/workflows/issues.yml
vendored
@@ -7,10 +7,10 @@ jobs:
|
|||||||
close-issues:
|
close-issues:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/stale@v3
|
- uses: actions/stale@v6
|
||||||
with:
|
with:
|
||||||
days-before-issue-stale: 30
|
days-before-issue-stale: 365
|
||||||
days-before-issue-close: 14
|
days-before-issue-close: 90
|
||||||
stale-issue-label: "stale"
|
stale-issue-label: "stale"
|
||||||
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
|
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
|
||||||
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
|
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
|
||||||
|
|||||||
2
.github/workflows/release.yaml
vendored
2
.github/workflows/release.yaml
vendored
@@ -25,4 +25,4 @@ jobs:
|
|||||||
goversion: "https://dl.google.com/go/go1.17.5.linux-amd64.tar.gz"
|
goversion: "https://dl.google.com/go/go1.17.5.linux-amd64.tar.gz"
|
||||||
project_path: "tools/goctl"
|
project_path: "tools/goctl"
|
||||||
binary_name: "goctl"
|
binary_name: "goctl"
|
||||||
extra_files: tools/goctl/goctl.md
|
extra_files: tools/goctl/readme.md tools/goctl/readme-cn.md
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -22,6 +22,7 @@ go.work.sum
|
|||||||
|
|
||||||
# gitlab ci
|
# gitlab ci
|
||||||
.cache
|
.cache
|
||||||
|
.golangci.yml
|
||||||
|
|
||||||
# vim auto backup file
|
# vim auto backup file
|
||||||
*~
|
*~
|
||||||
|
|||||||
@@ -20,16 +20,16 @@ func (b noOpBreaker) Do(req func() error) error {
|
|||||||
return req()
|
return req()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b noOpBreaker) DoWithAcceptable(req func() error, acceptable Acceptable) error {
|
func (b noOpBreaker) DoWithAcceptable(req func() error, _ Acceptable) error {
|
||||||
return req()
|
return req()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b noOpBreaker) DoWithFallback(req func() error, fallback func(err error) error) error {
|
func (b noOpBreaker) DoWithFallback(req func() error, _ func(err error) error) error {
|
||||||
return req()
|
return req()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error,
|
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, _ func(err error) error,
|
||||||
acceptable Acceptable) error {
|
_ Acceptable) error {
|
||||||
return req()
|
return req()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,5 +38,5 @@ type nopPromise struct{}
|
|||||||
func (p nopPromise) Accept() {
|
func (p nopPromise) Accept() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p nopPromise) Reject(reason string) {
|
func (p nopPromise) Reject(_ string) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -48,7 +48,7 @@ type (
|
|||||||
|
|
||||||
// NewRsaDecrypter returns a RsaDecrypter with the given file.
|
// NewRsaDecrypter returns a RsaDecrypter with the given file.
|
||||||
func NewRsaDecrypter(file string) (RsaDecrypter, error) {
|
func NewRsaDecrypter(file string) (RsaDecrypter, error) {
|
||||||
content, err := ioutil.ReadFile(file)
|
content, err := os.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,24 @@ func (m *SafeMap) Get(key interface{}) (interface{}, bool) {
|
|||||||
return val, ok
|
return val, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Range calls f sequentially for each key and value present in the map.
|
||||||
|
// If f returns false, range stops the iteration.
|
||||||
|
func (m *SafeMap) Range(f func(key, val interface{}) bool) {
|
||||||
|
m.lock.RLock()
|
||||||
|
defer m.lock.RUnlock()
|
||||||
|
|
||||||
|
for k, v := range m.dirtyOld {
|
||||||
|
if !f(k, v) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k, v := range m.dirtyNew {
|
||||||
|
if !f(k, v) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets the value into m with the given key.
|
// Set sets the value into m with the given key.
|
||||||
func (m *SafeMap) Set(key, value interface{}) {
|
func (m *SafeMap) Set(key, value interface{}) {
|
||||||
m.lock.Lock()
|
m.lock.Lock()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package collection
|
package collection
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -107,3 +108,42 @@ func testSafeMapWithParameters(t *testing.T, size, exception int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSafeMap_Range(t *testing.T) {
|
||||||
|
const (
|
||||||
|
size = 100000
|
||||||
|
exception1 = 5
|
||||||
|
exception2 = 500
|
||||||
|
)
|
||||||
|
|
||||||
|
m := NewSafeMap()
|
||||||
|
newMap := NewSafeMap()
|
||||||
|
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
m.Set(i, i)
|
||||||
|
}
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
if i%exception1 == 0 {
|
||||||
|
m.Del(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := size; i < size<<1; i++ {
|
||||||
|
m.Set(i, i)
|
||||||
|
}
|
||||||
|
for i := size; i < size<<1; i++ {
|
||||||
|
if i%exception2 != 0 {
|
||||||
|
m.Del(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int32
|
||||||
|
m.Range(func(k, v interface{}) bool {
|
||||||
|
atomic.AddInt32(&count, 1)
|
||||||
|
newMap.Set(k, v)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
assert.Equal(t, int(atomic.LoadInt32(&count)), m.Size())
|
||||||
|
assert.Equal(t, m.dirtyNew, newMap.dirtyNew)
|
||||||
|
assert.Equal(t, m.dirtyOld, newMap.dirtyOld)
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func NewSet() *Set {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUnmanagedSet returns a unmanaged Set, which can put values with different types.
|
// NewUnmanagedSet returns an unmanaged Set, which can put values with different types.
|
||||||
func NewUnmanagedSet() *Set {
|
func NewUnmanagedSet() *Set {
|
||||||
return &Set{
|
return &Set{
|
||||||
data: make(map[interface{}]lang.PlaceholderType),
|
data: make(map[interface{}]lang.PlaceholderType),
|
||||||
|
|||||||
@@ -2,15 +2,18 @@ package conf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/jsonx"
|
||||||
"github.com/zeromicro/go-zero/core/mapping"
|
"github.com/zeromicro/go-zero/core/mapping"
|
||||||
|
"github.com/zeromicro/go-zero/internal/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const distanceBetweenUpperAndLower = 32
|
||||||
|
|
||||||
var loaders = map[string]func([]byte, interface{}) error{
|
var loaders = map[string]func([]byte, interface{}) error{
|
||||||
".json": LoadFromJsonBytes,
|
".json": LoadFromJsonBytes,
|
||||||
".toml": LoadFromTomlBytes,
|
".toml": LoadFromTomlBytes,
|
||||||
@@ -20,7 +23,7 @@ var loaders = map[string]func([]byte, interface{}) error{
|
|||||||
|
|
||||||
// Load loads config into v from file, .json, .yaml and .yml are acceptable.
|
// Load loads config into v from file, .json, .yaml and .yml are acceptable.
|
||||||
func Load(file string, v interface{}, opts ...Option) error {
|
func Load(file string, v interface{}, opts ...Option) error {
|
||||||
content, err := ioutil.ReadFile(file)
|
content, err := os.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -50,7 +53,12 @@ func LoadConfig(file string, v interface{}, opts ...Option) error {
|
|||||||
|
|
||||||
// LoadFromJsonBytes loads config into v from content json bytes.
|
// LoadFromJsonBytes loads config into v from content json bytes.
|
||||||
func LoadFromJsonBytes(content []byte, v interface{}) error {
|
func LoadFromJsonBytes(content []byte, v interface{}) error {
|
||||||
return mapping.UnmarshalJsonBytes(content, v)
|
var m map[string]interface{}
|
||||||
|
if err := jsonx.Unmarshal(content, &m); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapping.UnmarshalJsonMap(toCamelCaseKeyMap(m), v, mapping.WithCanonicalKeyFunc(toCamelCase))
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfigFromJsonBytes loads config into v from content json bytes.
|
// LoadConfigFromJsonBytes loads config into v from content json bytes.
|
||||||
@@ -61,12 +69,22 @@ func LoadConfigFromJsonBytes(content []byte, v interface{}) error {
|
|||||||
|
|
||||||
// LoadFromTomlBytes loads config into v from content toml bytes.
|
// LoadFromTomlBytes loads config into v from content toml bytes.
|
||||||
func LoadFromTomlBytes(content []byte, v interface{}) error {
|
func LoadFromTomlBytes(content []byte, v interface{}) error {
|
||||||
return mapping.UnmarshalTomlBytes(content, v)
|
b, err := encoding.TomlToJson(content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return LoadFromJsonBytes(b, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadFromYamlBytes loads config into v from content yaml bytes.
|
// LoadFromYamlBytes loads config into v from content yaml bytes.
|
||||||
func LoadFromYamlBytes(content []byte, v interface{}) error {
|
func LoadFromYamlBytes(content []byte, v interface{}) error {
|
||||||
return mapping.UnmarshalYamlBytes(content, v)
|
b, err := encoding.YamlToJson(content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return LoadFromJsonBytes(b, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfigFromYamlBytes loads config into v from content yaml bytes.
|
// LoadConfigFromYamlBytes loads config into v from content yaml bytes.
|
||||||
@@ -81,3 +99,66 @@ func MustLoad(path string, v interface{}, opts ...Option) {
|
|||||||
log.Fatalf("error: config file %s, %s", path, err.Error())
|
log.Fatalf("error: config file %s, %s", path, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toCamelCase(s string) string {
|
||||||
|
var buf strings.Builder
|
||||||
|
buf.Grow(len(s))
|
||||||
|
var capNext bool
|
||||||
|
boundary := true
|
||||||
|
for _, v := range s {
|
||||||
|
isCap := v >= 'A' && v <= 'Z'
|
||||||
|
isLow := v >= 'a' && v <= 'z'
|
||||||
|
if boundary && (isCap || isLow) {
|
||||||
|
if capNext {
|
||||||
|
if isLow {
|
||||||
|
v -= distanceBetweenUpperAndLower
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if isCap {
|
||||||
|
v += distanceBetweenUpperAndLower
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boundary = false
|
||||||
|
}
|
||||||
|
if isCap || isLow {
|
||||||
|
buf.WriteRune(v)
|
||||||
|
capNext = false
|
||||||
|
} else if v == ' ' || v == '\t' {
|
||||||
|
buf.WriteRune(v)
|
||||||
|
capNext = false
|
||||||
|
boundary = true
|
||||||
|
} else if v == '_' {
|
||||||
|
capNext = true
|
||||||
|
boundary = true
|
||||||
|
} else {
|
||||||
|
buf.WriteRune(v)
|
||||||
|
capNext = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func toCamelCaseInterface(v interface{}) interface{} {
|
||||||
|
switch vv := v.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
return toCamelCaseKeyMap(vv)
|
||||||
|
case []interface{}:
|
||||||
|
var arr []interface{}
|
||||||
|
for _, vvv := range vv {
|
||||||
|
arr = append(arr, toCamelCaseInterface(vvv))
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
default:
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func toCamelCaseKeyMap(m map[string]interface{}) map[string]interface{} {
|
||||||
|
res := make(map[string]interface{})
|
||||||
|
for k, v := range m {
|
||||||
|
res[toCamelCase(k)] = toCamelCaseInterface(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -57,6 +56,22 @@ func TestConfigJson(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadFromJsonBytesArray(t *testing.T) {
|
||||||
|
input := []byte(`{"users": [{"name": "foo"}, {"Name": "bar"}]}`)
|
||||||
|
var val struct {
|
||||||
|
Users []struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, LoadFromJsonBytes(input, &val))
|
||||||
|
var expect []string
|
||||||
|
for _, user := range val.Users {
|
||||||
|
expect = append(expect, user.Name)
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, []string{"foo", "bar"}, expect)
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigToml(t *testing.T) {
|
func TestConfigToml(t *testing.T) {
|
||||||
text := `a = "foo"
|
text := `a = "foo"
|
||||||
b = 1
|
b = 1
|
||||||
@@ -82,6 +97,65 @@ d = "abcd!@#$112"
|
|||||||
assert.Equal(t, "abcd!@#$112", val.D)
|
assert.Equal(t, "abcd!@#$112", val.D)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigJsonCanonical(t *testing.T) {
|
||||||
|
text := []byte(`{"a": "foo", "B": "bar"}`)
|
||||||
|
|
||||||
|
var val1 struct {
|
||||||
|
A string `json:"a"`
|
||||||
|
B string `json:"b"`
|
||||||
|
}
|
||||||
|
var val2 struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
}
|
||||||
|
assert.NoError(t, LoadFromJsonBytes(text, &val1))
|
||||||
|
assert.Equal(t, "foo", val1.A)
|
||||||
|
assert.Equal(t, "bar", val1.B)
|
||||||
|
assert.NoError(t, LoadFromJsonBytes(text, &val2))
|
||||||
|
assert.Equal(t, "foo", val2.A)
|
||||||
|
assert.Equal(t, "bar", val2.B)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConfigTomlCanonical(t *testing.T) {
|
||||||
|
text := []byte(`a = "foo"
|
||||||
|
B = "bar"`)
|
||||||
|
|
||||||
|
var val1 struct {
|
||||||
|
A string `json:"a"`
|
||||||
|
B string `json:"b"`
|
||||||
|
}
|
||||||
|
var val2 struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
}
|
||||||
|
assert.NoError(t, LoadFromTomlBytes(text, &val1))
|
||||||
|
assert.Equal(t, "foo", val1.A)
|
||||||
|
assert.Equal(t, "bar", val1.B)
|
||||||
|
assert.NoError(t, LoadFromTomlBytes(text, &val2))
|
||||||
|
assert.Equal(t, "foo", val2.A)
|
||||||
|
assert.Equal(t, "bar", val2.B)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConfigYamlCanonical(t *testing.T) {
|
||||||
|
text := []byte(`a: foo
|
||||||
|
B: bar`)
|
||||||
|
|
||||||
|
var val1 struct {
|
||||||
|
A string `json:"a"`
|
||||||
|
B string `json:"b"`
|
||||||
|
}
|
||||||
|
var val2 struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
}
|
||||||
|
assert.NoError(t, LoadFromYamlBytes(text, &val1))
|
||||||
|
assert.Equal(t, "foo", val1.A)
|
||||||
|
assert.Equal(t, "bar", val1.B)
|
||||||
|
assert.NoError(t, LoadFromYamlBytes(text, &val2))
|
||||||
|
assert.Equal(t, "foo", val2.A)
|
||||||
|
assert.Equal(t, "bar", val2.B)
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigTomlEnv(t *testing.T) {
|
func TestConfigTomlEnv(t *testing.T) {
|
||||||
text := `a = "foo"
|
text := `a = "foo"
|
||||||
b = 1
|
b = 1
|
||||||
@@ -106,7 +180,6 @@ d = "abcd!@#112"
|
|||||||
assert.Equal(t, 1, val.B)
|
assert.Equal(t, 1, val.B)
|
||||||
assert.Equal(t, "2", val.C)
|
assert.Equal(t, "2", val.C)
|
||||||
assert.Equal(t, "abcd!@#112", val.D)
|
assert.Equal(t, "abcd!@#112", val.D)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigJsonEnv(t *testing.T) {
|
func TestConfigJsonEnv(t *testing.T) {
|
||||||
@@ -145,13 +218,123 @@ func TestConfigJsonEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToCamelCase(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: "",
|
||||||
|
expect: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "A",
|
||||||
|
expect: "a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "a",
|
||||||
|
expect: "a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "hello_world",
|
||||||
|
expect: "helloWorld",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello_world",
|
||||||
|
expect: "helloWorld",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "hello_World",
|
||||||
|
expect: "helloWorld",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "helloWorld",
|
||||||
|
expect: "helloWorld",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "HelloWorld",
|
||||||
|
expect: "helloWorld",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "hello World",
|
||||||
|
expect: "hello world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello World",
|
||||||
|
expect: "hello world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello World",
|
||||||
|
expect: "hello world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello World foo_bar",
|
||||||
|
expect: "hello world fooBar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello World foo_Bar",
|
||||||
|
expect: "hello world fooBar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello World Foo_bar",
|
||||||
|
expect: "hello world fooBar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Hello World Foo_Bar",
|
||||||
|
expect: "hello world fooBar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "你好 World Foo_Bar",
|
||||||
|
expect: "你好 world fooBar",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.input, func(t *testing.T) {
|
||||||
|
assert.Equal(t, test.expect, toCamelCase(test.input))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadFromJsonBytesError(t *testing.T) {
|
||||||
|
var val struct{}
|
||||||
|
assert.Error(t, LoadFromJsonBytes([]byte(`hello`), &val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadFromTomlBytesError(t *testing.T) {
|
||||||
|
var val struct{}
|
||||||
|
assert.Error(t, LoadFromTomlBytes([]byte(`hello`), &val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadFromYamlBytesError(t *testing.T) {
|
||||||
|
var val struct{}
|
||||||
|
assert.Error(t, LoadFromYamlBytes([]byte(`':hello`), &val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadFromYamlBytes(t *testing.T) {
|
||||||
|
input := []byte(`layer1:
|
||||||
|
layer2:
|
||||||
|
layer3: foo`)
|
||||||
|
var val struct {
|
||||||
|
Layer1 struct {
|
||||||
|
Layer2 struct {
|
||||||
|
Layer3 string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, LoadFromYamlBytes(input, &val))
|
||||||
|
assert.Equal(t, "foo", val.Layer1.Layer2.Layer3)
|
||||||
|
}
|
||||||
|
|
||||||
func createTempFile(ext, text string) (string, error) {
|
func createTempFile(ext, text string) (string, error) {
|
||||||
tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext)
|
tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ioutil.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
if err := os.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package internal
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"io/ioutil"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ func AddTLS(endpoints []string, certFile, certKeyFile, caFile string, insecureSk
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
caData, err := ioutil.ReadFile(caFile)
|
caData, err := os.ReadFile(caFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ func (c *cluster) handleWatchEvents(key string, events []*clientv3.Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cluster) load(cli EtcdClient, key string) {
|
func (c *cluster) load(cli EtcdClient, key string) int64 {
|
||||||
var resp *clientv3.GetResponse
|
var resp *clientv3.GetResponse
|
||||||
for {
|
for {
|
||||||
var err error
|
var err error
|
||||||
@@ -232,6 +232,8 @@ func (c *cluster) load(cli EtcdClient, key string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.handleChanges(key, kvs)
|
c.handleChanges(key, kvs)
|
||||||
|
|
||||||
|
return resp.Header.Revision
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cluster) monitor(key string, l UpdateListener) error {
|
func (c *cluster) monitor(key string, l UpdateListener) error {
|
||||||
@@ -244,9 +246,9 @@ func (c *cluster) monitor(key string, l UpdateListener) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.load(cli, key)
|
rev := c.load(cli, key)
|
||||||
c.watchGroup.Run(func() {
|
c.watchGroup.Run(func() {
|
||||||
c.watch(cli, key)
|
c.watch(cli, key, rev)
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -278,22 +280,29 @@ func (c *cluster) reload(cli EtcdClient) {
|
|||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
k := key
|
k := key
|
||||||
c.watchGroup.Run(func() {
|
c.watchGroup.Run(func() {
|
||||||
c.load(cli, k)
|
rev := c.load(cli, k)
|
||||||
c.watch(cli, k)
|
c.watch(cli, k, rev)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cluster) watch(cli EtcdClient, key string) {
|
func (c *cluster) watch(cli EtcdClient, key string, rev int64) {
|
||||||
for {
|
for {
|
||||||
if c.watchStream(cli, key) {
|
if c.watchStream(cli, key, rev) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cluster) watchStream(cli EtcdClient, key string) bool {
|
func (c *cluster) watchStream(cli EtcdClient, key string, rev int64) bool {
|
||||||
rch := cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix())
|
var rch clientv3.WatchChan
|
||||||
|
if rev != 0 {
|
||||||
|
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix(),
|
||||||
|
clientv3.WithRev(rev+1))
|
||||||
|
} else {
|
||||||
|
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix())
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case wresp, ok := <-rch:
|
case wresp, ok := <-rch:
|
||||||
@@ -334,6 +343,7 @@ func DialClient(endpoints []string) (EtcdClient, error) {
|
|||||||
DialKeepAliveTime: dialKeepAliveTime,
|
DialKeepAliveTime: dialKeepAliveTime,
|
||||||
DialKeepAliveTimeout: DialTimeout,
|
DialKeepAliveTimeout: DialTimeout,
|
||||||
RejectOldCluster: true,
|
RejectOldCluster: true,
|
||||||
|
PermitWithoutStream: true,
|
||||||
}
|
}
|
||||||
if account, ok := GetAccount(endpoints); ok {
|
if account, ok := GetAccount(endpoints); ok {
|
||||||
cfg.Username = account.User
|
cfg.Username = account.User
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/lang"
|
"github.com/zeromicro/go-zero/core/lang"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"github.com/zeromicro/go-zero/core/stringx"
|
"github.com/zeromicro/go-zero/core/stringx"
|
||||||
|
"go.etcd.io/etcd/api/v3/etcdserverpb"
|
||||||
"go.etcd.io/etcd/api/v3/mvccpb"
|
"go.etcd.io/etcd/api/v3/mvccpb"
|
||||||
clientv3 "go.etcd.io/etcd/client/v3"
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
)
|
)
|
||||||
@@ -112,6 +113,7 @@ func TestCluster_Load(t *testing.T) {
|
|||||||
restore := setMockClient(cli)
|
restore := setMockClient(cli)
|
||||||
defer restore()
|
defer restore()
|
||||||
cli.EXPECT().Get(gomock.Any(), "any/", gomock.Any()).Return(&clientv3.GetResponse{
|
cli.EXPECT().Get(gomock.Any(), "any/", gomock.Any()).Return(&clientv3.GetResponse{
|
||||||
|
Header: &etcdserverpb.ResponseHeader{},
|
||||||
Kvs: []*mvccpb.KeyValue{
|
Kvs: []*mvccpb.KeyValue{
|
||||||
{
|
{
|
||||||
Key: []byte("hello"),
|
Key: []byte("hello"),
|
||||||
@@ -168,7 +170,7 @@ func TestCluster_Watch(t *testing.T) {
|
|||||||
listener.EXPECT().OnDelete(gomock.Any()).Do(func(_ interface{}) {
|
listener.EXPECT().OnDelete(gomock.Any()).Do(func(_ interface{}) {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}).MaxTimes(1)
|
}).MaxTimes(1)
|
||||||
go c.watch(cli, "any")
|
go c.watch(cli, "any", 0)
|
||||||
ch <- clientv3.WatchResponse{
|
ch <- clientv3.WatchResponse{
|
||||||
Events: []*clientv3.Event{
|
Events: []*clientv3.Event{
|
||||||
{
|
{
|
||||||
@@ -212,7 +214,7 @@ func TestClusterWatch_RespFailures(t *testing.T) {
|
|||||||
ch <- resp
|
ch <- resp
|
||||||
close(c.done)
|
close(c.done)
|
||||||
}()
|
}()
|
||||||
c.watch(cli, "any")
|
c.watch(cli, "any", 0)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -232,7 +234,7 @@ func TestClusterWatch_CloseChan(t *testing.T) {
|
|||||||
close(ch)
|
close(ch)
|
||||||
close(c.done)
|
close(c.done)
|
||||||
}()
|
}()
|
||||||
c.watch(cli, "any")
|
c.watch(cli, "any", 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValueOnlyContext(t *testing.T) {
|
func TestValueOnlyContext(t *testing.T) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ type (
|
|||||||
// SubOption defines the method to customize a Subscriber.
|
// SubOption defines the method to customize a Subscriber.
|
||||||
SubOption func(sub *Subscriber)
|
SubOption func(sub *Subscriber)
|
||||||
|
|
||||||
// A Subscriber is used to subscribe the given key on a etcd cluster.
|
// A Subscriber is used to subscribe the given key on an etcd cluster.
|
||||||
Subscriber struct {
|
Subscriber struct {
|
||||||
endpoints []string
|
endpoints []string
|
||||||
exclusive bool
|
exclusive bool
|
||||||
|
|||||||
15
core/fs/files_test.go
Normal file
15
core/fs/files_test.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCloseOnExec(t *testing.T) {
|
||||||
|
file := os.NewFile(0, os.DevNull)
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
CloseOnExec(file)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/hash"
|
"github.com/zeromicro/go-zero/core/hash"
|
||||||
@@ -12,12 +11,12 @@ import (
|
|||||||
// The file is kept as open, the caller should close the file handle,
|
// The file is kept as open, the caller should close the file handle,
|
||||||
// and remove the file by name.
|
// and remove the file by name.
|
||||||
func TempFileWithText(text string) (*os.File, error) {
|
func TempFileWithText(text string) (*os.File, error) {
|
||||||
tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(text)))
|
tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ioutil.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
if err := os.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ func TestTempFileWithText(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.Remove(f.Name())
|
defer os.Remove(f.Name())
|
||||||
|
|
||||||
bs, err := ioutil.ReadAll(f)
|
bs, err := io.ReadAll(f)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
if len(bs) != 4 {
|
if len(bs) != 4 {
|
||||||
t.Error("TempFileWithText returned wrong file size")
|
t.Error("TempFileWithText returned wrong file size")
|
||||||
@@ -41,7 +41,7 @@ func TestTempFilenameWithText(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.Remove(f)
|
defer os.Remove(f)
|
||||||
|
|
||||||
bs, err := ioutil.ReadFile(f)
|
bs, err := os.ReadFile(f)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
if len(bs) != 4 {
|
if len(bs) != 4 {
|
||||||
t.Error("TempFilenameWithText returned wrong file size")
|
t.Error("TempFilenameWithText returned wrong file size")
|
||||||
|
|||||||
@@ -328,7 +328,7 @@ func (s Stream) Parallel(fn ParallelFunc, opts ...Option) {
|
|||||||
}, opts...).Done()
|
}, opts...).Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce is a utility method to let the caller deal with the underlying channel.
|
// Reduce is an utility method to let the caller deal with the underlying channel.
|
||||||
func (s Stream) Reduce(fn ReduceFunc) (interface{}, error) {
|
func (s Stream) Reduce(fn ReduceFunc) (interface{}, error) {
|
||||||
return fn(s.source)
|
return fn(s.source)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package fx
|
package fx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -238,7 +238,7 @@ func TestLast(t *testing.T) {
|
|||||||
|
|
||||||
func TestMap(t *testing.T) {
|
func TestMap(t *testing.T) {
|
||||||
runCheckedTest(t, func(t *testing.T) {
|
runCheckedTest(t, func(t *testing.T) {
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(io.Discard)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
mapper MapFunc
|
mapper MapFunc
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/lang"
|
"github.com/zeromicro/go-zero/core/lang"
|
||||||
"github.com/zeromicro/go-zero/core/mapping"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -183,5 +182,5 @@ func innerRepr(node interface{}) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func repr(node interface{}) string {
|
func repr(node interface{}) string {
|
||||||
return mapping.Repr(node)
|
return lang.Repr(node)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ func (nopCloser) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NopCloser returns a io.WriteCloser that does nothing on calling Close.
|
// NopCloser returns an io.WriteCloser that does nothing on calling Close.
|
||||||
func NopCloser(w io.Writer) io.WriteCloser {
|
func NopCloser(w io.Writer) io.WriteCloser {
|
||||||
return nopCloser{w}
|
return nopCloser{w}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -26,7 +25,7 @@ type (
|
|||||||
func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) {
|
func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
tee := io.TeeReader(reader, &buf)
|
tee := io.TeeReader(reader, &buf)
|
||||||
return ioutil.NopCloser(tee), ioutil.NopCloser(&buf)
|
return io.NopCloser(tee), io.NopCloser(&buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeepSpace customizes the reading functions to keep leading and tailing spaces.
|
// KeepSpace customizes the reading functions to keep leading and tailing spaces.
|
||||||
@@ -54,7 +53,7 @@ func ReadBytes(reader io.Reader, buf []byte) error {
|
|||||||
|
|
||||||
// ReadText reads content from the given file with leading and tailing spaces trimmed.
|
// ReadText reads content from the given file with leading and tailing spaces trimmed.
|
||||||
func ReadText(filename string) (string, error) {
|
func ReadText(filename string) (string, error) {
|
||||||
content, err := ioutil.ReadFile(filename)
|
content, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package iox
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -97,10 +96,10 @@ func TestReadTextLines(t *testing.T) {
|
|||||||
|
|
||||||
func TestDupReadCloser(t *testing.T) {
|
func TestDupReadCloser(t *testing.T) {
|
||||||
input := "hello"
|
input := "hello"
|
||||||
reader := ioutil.NopCloser(bytes.NewBufferString(input))
|
reader := io.NopCloser(bytes.NewBufferString(input))
|
||||||
r1, r2 := DupReadCloser(reader)
|
r1, r2 := DupReadCloser(reader)
|
||||||
verify := func(r io.Reader) {
|
verify := func(r io.Reader) {
|
||||||
output, err := ioutil.ReadAll(r)
|
output, err := io.ReadAll(r)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, input, string(output))
|
assert.Equal(t, input, string(output))
|
||||||
}
|
}
|
||||||
@@ -110,7 +109,7 @@ func TestDupReadCloser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReadBytes(t *testing.T) {
|
func TestReadBytes(t *testing.T) {
|
||||||
reader := ioutil.NopCloser(bytes.NewBufferString("helloworld"))
|
reader := io.NopCloser(bytes.NewBufferString("helloworld"))
|
||||||
buf := make([]byte, 5)
|
buf := make([]byte, 5)
|
||||||
err := ReadBytes(reader, buf)
|
err := ReadBytes(reader, buf)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@@ -118,7 +117,7 @@ func TestReadBytes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReadBytesNotEnough(t *testing.T) {
|
func TestReadBytesNotEnough(t *testing.T) {
|
||||||
reader := ioutil.NopCloser(bytes.NewBufferString("hell"))
|
reader := io.NopCloser(bytes.NewBufferString("hell"))
|
||||||
buf := make([]byte, 5)
|
buf := make([]byte, 5)
|
||||||
err := ReadBytes(reader, buf)
|
err := ReadBytes(reader, buf)
|
||||||
assert.Equal(t, io.EOF, err)
|
assert.Equal(t, io.EOF, err)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package iox
|
package iox
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -13,7 +12,7 @@ func TestCountLines(t *testing.T) {
|
|||||||
2
|
2
|
||||||
3
|
3
|
||||||
4`
|
4`
|
||||||
file, err := ioutil.TempFile(os.TempDir(), "test-")
|
file, err := os.CreateTemp(os.TempDir(), "test-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
package lang
|
package lang
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
// Placeholder is a placeholder object that can be used globally.
|
// Placeholder is a placeholder object that can be used globally.
|
||||||
var Placeholder PlaceholderType
|
var Placeholder PlaceholderType
|
||||||
|
|
||||||
@@ -9,3 +15,64 @@ type (
|
|||||||
// PlaceholderType represents a placeholder type.
|
// PlaceholderType represents a placeholder type.
|
||||||
PlaceholderType = struct{}
|
PlaceholderType = struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Repr returns the string representation of v.
|
||||||
|
func Repr(v interface{}) string {
|
||||||
|
if v == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// if func (v *Type) String() string, we can't use Elem()
|
||||||
|
switch vt := v.(type) {
|
||||||
|
case fmt.Stringer:
|
||||||
|
return vt.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
val := reflect.ValueOf(v)
|
||||||
|
if val.Kind() == reflect.Ptr && !val.IsNil() {
|
||||||
|
val = val.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
return reprOfValue(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reprOfValue(val reflect.Value) string {
|
||||||
|
switch vt := val.Interface().(type) {
|
||||||
|
case bool:
|
||||||
|
return strconv.FormatBool(vt)
|
||||||
|
case error:
|
||||||
|
return vt.Error()
|
||||||
|
case float32:
|
||||||
|
return strconv.FormatFloat(float64(vt), 'f', -1, 32)
|
||||||
|
case float64:
|
||||||
|
return strconv.FormatFloat(vt, 'f', -1, 64)
|
||||||
|
case fmt.Stringer:
|
||||||
|
return vt.String()
|
||||||
|
case int:
|
||||||
|
return strconv.Itoa(vt)
|
||||||
|
case int8:
|
||||||
|
return strconv.Itoa(int(vt))
|
||||||
|
case int16:
|
||||||
|
return strconv.Itoa(int(vt))
|
||||||
|
case int32:
|
||||||
|
return strconv.Itoa(int(vt))
|
||||||
|
case int64:
|
||||||
|
return strconv.FormatInt(vt, 10)
|
||||||
|
case string:
|
||||||
|
return vt
|
||||||
|
case uint:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint8:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint16:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint32:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint64:
|
||||||
|
return strconv.FormatUint(vt, 10)
|
||||||
|
case []byte:
|
||||||
|
return string(vt)
|
||||||
|
default:
|
||||||
|
return fmt.Sprint(val.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
131
core/lang/lang_test.go
Normal file
131
core/lang/lang_test.go
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
package lang
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRepr(t *testing.T) {
|
||||||
|
var (
|
||||||
|
f32 float32 = 1.1
|
||||||
|
f64 = 2.2
|
||||||
|
i8 int8 = 1
|
||||||
|
i16 int16 = 2
|
||||||
|
i32 int32 = 3
|
||||||
|
i64 int64 = 4
|
||||||
|
u8 uint8 = 5
|
||||||
|
u16 uint16 = 6
|
||||||
|
u32 uint32 = 7
|
||||||
|
u64 uint64 = 8
|
||||||
|
)
|
||||||
|
tests := []struct {
|
||||||
|
v interface{}
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
nil,
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
mockStringable{},
|
||||||
|
"mocked",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
new(mockStringable),
|
||||||
|
"mocked",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
newMockPtr(),
|
||||||
|
"mockptr",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
&mockOpacity{
|
||||||
|
val: 1,
|
||||||
|
},
|
||||||
|
"{1}",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
true,
|
||||||
|
"true",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
false,
|
||||||
|
"false",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f32,
|
||||||
|
"1.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
f64,
|
||||||
|
"2.2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
i8,
|
||||||
|
"1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
i16,
|
||||||
|
"2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
i32,
|
||||||
|
"3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
i64,
|
||||||
|
"4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
u8,
|
||||||
|
"5",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
u16,
|
||||||
|
"6",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
u32,
|
||||||
|
"7",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
u64,
|
||||||
|
"8",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]byte(`abcd`),
|
||||||
|
"abcd",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
mockOpacity{val: 1},
|
||||||
|
"{1}",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.expect, func(t *testing.T) {
|
||||||
|
assert.Equal(t, test.expect, Repr(test.v))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockStringable struct{}
|
||||||
|
|
||||||
|
func (m mockStringable) String() string {
|
||||||
|
return "mocked"
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockPtr struct{}
|
||||||
|
|
||||||
|
func newMockPtr() *mockPtr {
|
||||||
|
return new(mockPtr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockPtr) String() string {
|
||||||
|
return "mockptr"
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockOpacity struct {
|
||||||
|
val int
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package limit
|
package limit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -58,8 +60,8 @@ type TokenLimiter struct {
|
|||||||
timestampKey string
|
timestampKey string
|
||||||
rescueLock sync.Mutex
|
rescueLock sync.Mutex
|
||||||
redisAlive uint32
|
redisAlive uint32
|
||||||
rescueLimiter *xrate.Limiter
|
|
||||||
monitorStarted bool
|
monitorStarted bool
|
||||||
|
rescueLimiter *xrate.Limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTokenLimiter returns a new TokenLimiter that allows events up to rate and permits
|
// NewTokenLimiter returns a new TokenLimiter that allows events up to rate and permits
|
||||||
@@ -84,19 +86,31 @@ func (lim *TokenLimiter) Allow() bool {
|
|||||||
return lim.AllowN(time.Now(), 1)
|
return lim.AllowN(time.Now(), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AllowCtx is shorthand for AllowNCtx(ctx,time.Now(), 1) with incoming context.
|
||||||
|
func (lim *TokenLimiter) AllowCtx(ctx context.Context) bool {
|
||||||
|
return lim.AllowNCtx(ctx, time.Now(), 1)
|
||||||
|
}
|
||||||
|
|
||||||
// AllowN reports whether n events may happen at time now.
|
// AllowN reports whether n events may happen at time now.
|
||||||
// Use this method if you intend to drop / skip events that exceed the rate.
|
// Use this method if you intend to drop / skip events that exceed the rate.
|
||||||
// Otherwise, use Reserve or Wait.
|
// Otherwise, use Reserve or Wait.
|
||||||
func (lim *TokenLimiter) AllowN(now time.Time, n int) bool {
|
func (lim *TokenLimiter) AllowN(now time.Time, n int) bool {
|
||||||
return lim.reserveN(now, n)
|
return lim.reserveN(context.Background(), now, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lim *TokenLimiter) reserveN(now time.Time, n int) bool {
|
// AllowNCtx reports whether n events may happen at time now with incoming context.
|
||||||
|
// Use this method if you intend to drop / skip events that exceed the rate.
|
||||||
|
// Otherwise, use Reserve or Wait.
|
||||||
|
func (lim *TokenLimiter) AllowNCtx(ctx context.Context, now time.Time, n int) bool {
|
||||||
|
return lim.reserveN(ctx, now, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lim *TokenLimiter) reserveN(ctx context.Context, now time.Time, n int) bool {
|
||||||
if atomic.LoadUint32(&lim.redisAlive) == 0 {
|
if atomic.LoadUint32(&lim.redisAlive) == 0 {
|
||||||
return lim.rescueLimiter.AllowN(now, n)
|
return lim.rescueLimiter.AllowN(now, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := lim.store.Eval(
|
resp, err := lim.store.EvalCtx(ctx,
|
||||||
script,
|
script,
|
||||||
[]string{
|
[]string{
|
||||||
lim.tokenKey,
|
lim.tokenKey,
|
||||||
@@ -113,6 +127,10 @@ func (lim *TokenLimiter) reserveN(now time.Time, n int) bool {
|
|||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
||||||
|
logx.Errorf("fail to use rate limiter: %s", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Errorf("fail to use rate limiter: %s, use in-process limiter for rescue", err)
|
logx.Errorf("fail to use rate limiter: %s, use in-process limiter for rescue", err)
|
||||||
lim.startMonitor()
|
lim.startMonitor()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package limit
|
package limit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -15,6 +16,30 @@ func init() {
|
|||||||
logx.Disable()
|
logx.Disable()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTokenLimit_WithCtx(t *testing.T) {
|
||||||
|
s, err := miniredis.Run()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
const (
|
||||||
|
total = 100
|
||||||
|
rate = 5
|
||||||
|
burst = 10
|
||||||
|
)
|
||||||
|
l := NewTokenLimiter(rate, burst, redis.New(s.Addr()), "tokenlimit")
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
ok := l.AllowCtx(ctx)
|
||||||
|
assert.True(t, ok)
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
for i := 0; i < total; i++ {
|
||||||
|
ok := l.AllowCtx(ctx)
|
||||||
|
assert.False(t, ok)
|
||||||
|
assert.False(t, l.monitorStarted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTokenLimit_Rescue(t *testing.T) {
|
func TestTokenLimit_Rescue(t *testing.T) {
|
||||||
s, err := miniredis.Run()
|
s, err := miniredis.Run()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
defaultBuckets = 50
|
defaultBuckets = 50
|
||||||
defaultWindow = time.Second * 5
|
defaultWindow = time.Second * 5
|
||||||
// using 1000m notation, 900m is like 80%, keep it as var for unit test
|
// using 1000m notation, 900m is like 90%, keep it as var for unit test
|
||||||
defaultCpuThreshold = 900
|
defaultCpuThreshold = 900
|
||||||
defaultMinRt = float64(time.Second / time.Millisecond)
|
defaultMinRt = float64(time.Second / time.Millisecond)
|
||||||
// moving average hyperparameter beta for calculating requests on the fly
|
// moving average hyperparameter beta for calculating requests on the fly
|
||||||
@@ -70,7 +70,7 @@ type (
|
|||||||
flying int64
|
flying int64
|
||||||
avgFlying float64
|
avgFlying float64
|
||||||
avgFlyingLock syncx.SpinLock
|
avgFlyingLock syncx.SpinLock
|
||||||
dropTime *syncx.AtomicDuration
|
overloadTime *syncx.AtomicDuration
|
||||||
droppedRecently *syncx.AtomicBool
|
droppedRecently *syncx.AtomicBool
|
||||||
passCounter *collection.RollingWindow
|
passCounter *collection.RollingWindow
|
||||||
rtCounter *collection.RollingWindow
|
rtCounter *collection.RollingWindow
|
||||||
@@ -106,7 +106,7 @@ func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
|
|||||||
return &adaptiveShedder{
|
return &adaptiveShedder{
|
||||||
cpuThreshold: options.cpuThreshold,
|
cpuThreshold: options.cpuThreshold,
|
||||||
windows: int64(time.Second / bucketDuration),
|
windows: int64(time.Second / bucketDuration),
|
||||||
dropTime: syncx.NewAtomicDuration(),
|
overloadTime: syncx.NewAtomicDuration(),
|
||||||
droppedRecently: syncx.NewAtomicBool(),
|
droppedRecently: syncx.NewAtomicBool(),
|
||||||
passCounter: collection.NewRollingWindow(options.buckets, bucketDuration,
|
passCounter: collection.NewRollingWindow(options.buckets, bucketDuration,
|
||||||
collection.IgnoreCurrentBucket()),
|
collection.IgnoreCurrentBucket()),
|
||||||
@@ -118,7 +118,6 @@ func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
|
|||||||
// Allow implements Shedder.Allow.
|
// Allow implements Shedder.Allow.
|
||||||
func (as *adaptiveShedder) Allow() (Promise, error) {
|
func (as *adaptiveShedder) Allow() (Promise, error) {
|
||||||
if as.shouldDrop() {
|
if as.shouldDrop() {
|
||||||
as.dropTime.Set(timex.Now())
|
|
||||||
as.droppedRecently.Set(true)
|
as.droppedRecently.Set(true)
|
||||||
|
|
||||||
return nil, ErrServiceOverloaded
|
return nil, ErrServiceOverloaded
|
||||||
@@ -215,21 +214,26 @@ func (as *adaptiveShedder) stillHot() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
dropTime := as.dropTime.Load()
|
overloadTime := as.overloadTime.Load()
|
||||||
if dropTime == 0 {
|
if overloadTime == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
hot := timex.Since(dropTime) < coolOffDuration
|
if timex.Since(overloadTime) < coolOffDuration {
|
||||||
if !hot {
|
return true
|
||||||
as.droppedRecently.Set(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return hot
|
as.droppedRecently.Set(false)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *adaptiveShedder) systemOverloaded() bool {
|
func (as *adaptiveShedder) systemOverloaded() bool {
|
||||||
return systemOverloadChecker(as.cpuThreshold)
|
if !systemOverloadChecker(as.cpuThreshold) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
as.overloadTime.Set(timex.Now())
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithBuckets customizes the Shedder with given number of buckets.
|
// WithBuckets customizes the Shedder with given number of buckets.
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/mathx"
|
"github.com/zeromicro/go-zero/core/mathx"
|
||||||
"github.com/zeromicro/go-zero/core/stat"
|
"github.com/zeromicro/go-zero/core/stat"
|
||||||
"github.com/zeromicro/go-zero/core/syncx"
|
"github.com/zeromicro/go-zero/core/syncx"
|
||||||
|
"github.com/zeromicro/go-zero/core/timex"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -136,7 +137,7 @@ func TestAdaptiveShedderShouldDrop(t *testing.T) {
|
|||||||
passCounter: passCounter,
|
passCounter: passCounter,
|
||||||
rtCounter: rtCounter,
|
rtCounter: rtCounter,
|
||||||
windows: buckets,
|
windows: buckets,
|
||||||
dropTime: syncx.NewAtomicDuration(),
|
overloadTime: syncx.NewAtomicDuration(),
|
||||||
droppedRecently: syncx.NewAtomicBool(),
|
droppedRecently: syncx.NewAtomicBool(),
|
||||||
}
|
}
|
||||||
// cpu >= 800, inflight < maxPass
|
// cpu >= 800, inflight < maxPass
|
||||||
@@ -190,12 +191,15 @@ func TestAdaptiveShedderStillHot(t *testing.T) {
|
|||||||
passCounter: passCounter,
|
passCounter: passCounter,
|
||||||
rtCounter: rtCounter,
|
rtCounter: rtCounter,
|
||||||
windows: buckets,
|
windows: buckets,
|
||||||
dropTime: syncx.NewAtomicDuration(),
|
overloadTime: syncx.NewAtomicDuration(),
|
||||||
droppedRecently: syncx.ForAtomicBool(true),
|
droppedRecently: syncx.ForAtomicBool(true),
|
||||||
}
|
}
|
||||||
assert.False(t, shedder.stillHot())
|
assert.False(t, shedder.stillHot())
|
||||||
shedder.dropTime.Set(-coolOffDuration * 2)
|
shedder.overloadTime.Set(-coolOffDuration * 2)
|
||||||
assert.False(t, shedder.stillHot())
|
assert.False(t, shedder.stillHot())
|
||||||
|
shedder.droppedRecently.Set(true)
|
||||||
|
shedder.overloadTime.Set(timex.Now())
|
||||||
|
assert.True(t, shedder.stillHot())
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAdaptiveShedder_Allow(b *testing.B) {
|
func BenchmarkAdaptiveShedder_Allow(b *testing.B) {
|
||||||
|
|||||||
122
core/logc/logs.go
Normal file
122
core/logc/logs.go
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package logc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
LogConf = logx.LogConf
|
||||||
|
LogField = logx.LogField
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddGlobalFields adds global fields.
|
||||||
|
func AddGlobalFields(fields ...LogField) {
|
||||||
|
logx.AddGlobalFields(fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alert alerts v in alert level, and the message is written to error log.
|
||||||
|
func Alert(_ context.Context, v string) {
|
||||||
|
logx.Alert(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the logging.
|
||||||
|
func Close() error {
|
||||||
|
return logx.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error writes v into error log.
|
||||||
|
func Error(ctx context.Context, v ...interface{}) {
|
||||||
|
getLogger(ctx).Error(v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf writes v with format into error log.
|
||||||
|
func Errorf(ctx context.Context, format string, v ...interface{}) {
|
||||||
|
getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorv writes v into error log with json content.
|
||||||
|
// No call stack attached, because not elegant to pack the messages.
|
||||||
|
func Errorv(ctx context.Context, v interface{}) {
|
||||||
|
getLogger(ctx).Errorv(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorw writes msg along with fields into error log.
|
||||||
|
func Errorw(ctx context.Context, msg string, fields ...LogField) {
|
||||||
|
getLogger(ctx).Errorw(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Field returns a LogField for the given key and value.
|
||||||
|
func Field(key string, value interface{}) LogField {
|
||||||
|
return logx.Field(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info writes v into access log.
|
||||||
|
func Info(ctx context.Context, v ...interface{}) {
|
||||||
|
getLogger(ctx).Info(v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Infof writes v with format into access log.
|
||||||
|
func Infof(ctx context.Context, format string, v ...interface{}) {
|
||||||
|
getLogger(ctx).Infof(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Infov writes v into access log with json content.
|
||||||
|
func Infov(ctx context.Context, v interface{}) {
|
||||||
|
getLogger(ctx).Infov(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Infow writes msg along with fields into access log.
|
||||||
|
func Infow(ctx context.Context, msg string, fields ...LogField) {
|
||||||
|
getLogger(ctx).Infow(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must checks if err is nil, otherwise logs the error and exits.
|
||||||
|
func Must(err error) {
|
||||||
|
logx.Must(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustSetup sets up logging with given config c. It exits on error.
|
||||||
|
func MustSetup(c logx.LogConf) {
|
||||||
|
logx.MustSetup(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLevel sets the logging level. It can be used to suppress some logs.
|
||||||
|
func SetLevel(level uint32) {
|
||||||
|
logx.SetLevel(level)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetUp sets up the logx. If already set up, just return nil.
|
||||||
|
// we allow SetUp to be called multiple times, because for example
|
||||||
|
// we need to allow different service frameworks to initialize logx respectively.
|
||||||
|
// the same logic for SetUp
|
||||||
|
func SetUp(c LogConf) error {
|
||||||
|
return logx.SetUp(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slow writes v into slow log.
|
||||||
|
func Slow(ctx context.Context, v ...interface{}) {
|
||||||
|
getLogger(ctx).Slow(v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slowf writes v with format into slow log.
|
||||||
|
func Slowf(ctx context.Context, format string, v ...interface{}) {
|
||||||
|
getLogger(ctx).Slowf(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slowv writes v into slow log with json content.
|
||||||
|
func Slowv(ctx context.Context, v interface{}) {
|
||||||
|
getLogger(ctx).Slowv(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sloww writes msg along with fields into slow log.
|
||||||
|
func Sloww(ctx context.Context, msg string, fields ...LogField) {
|
||||||
|
getLogger(ctx).Sloww(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLogger returns the logx.Logger with the given ctx and correct caller.
|
||||||
|
func getLogger(ctx context.Context) logx.Logger {
|
||||||
|
return logx.WithContext(ctx).WithCallerSkip(1)
|
||||||
|
}
|
||||||
218
core/logc/logs_test.go
Normal file
218
core/logc/logs_test.go
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
package logc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAddGlobalFields(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
Info(context.Background(), "hello")
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
AddGlobalFields(Field("a", "1"), Field("b", "2"))
|
||||||
|
AddGlobalFields(Field("c", "3"))
|
||||||
|
Info(context.Background(), "world")
|
||||||
|
var m map[string]interface{}
|
||||||
|
assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
|
||||||
|
assert.Equal(t, "1", m["a"])
|
||||||
|
assert.Equal(t, "2", m["b"])
|
||||||
|
assert.Equal(t, "3", m["c"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAlert(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
Alert(context.Background(), "foo")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), "foo"), buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestError(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Error(context.Background(), "foo")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorf(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Errorf(context.Background(), "foo %s", "bar")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorv(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Errorv(context.Background(), "foo")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorw(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Errorw(context.Background(), "foo", Field("a", "b"))
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInfo(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Info(context.Background(), "foo")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInfof(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Infof(context.Background(), "foo %s", "bar")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInfov(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Infov(context.Background(), "foo")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInfow(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Infow(context.Background(), "foo", Field("a", "b"))
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMust(t *testing.T) {
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
Must(nil)
|
||||||
|
})
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
MustSetup(LogConf{})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMisc(t *testing.T) {
|
||||||
|
SetLevel(logx.DebugLevel)
|
||||||
|
assert.NoError(t, SetUp(LogConf{}))
|
||||||
|
assert.NoError(t, Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlow(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Slow(context.Background(), "foo")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlowf(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Slowf(context.Background(), "foo %s", "bar")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlowv(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Slowv(context.Background(), "foo")
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSloww(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
writer := logx.NewWriter(&buf)
|
||||||
|
old := logx.Reset()
|
||||||
|
logx.SetWriter(writer)
|
||||||
|
defer logx.SetWriter(old)
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
Sloww(context.Background(), "foo", Field("a", "b"))
|
||||||
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func getFileLine() (string, int) {
|
||||||
|
_, file, line, _ := runtime.Caller(1)
|
||||||
|
short := file
|
||||||
|
|
||||||
|
for i := len(file) - 1; i > 0; i-- {
|
||||||
|
if file[i] == '/' {
|
||||||
|
short = file[i+1:]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return short, line
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ type LogConf struct {
|
|||||||
Encoding string `json:",default=json,options=[json,plain]"`
|
Encoding string `json:",default=json,options=[json,plain]"`
|
||||||
TimeFormat string `json:",optional"`
|
TimeFormat string `json:",optional"`
|
||||||
Path string `json:",default=logs"`
|
Path string `json:",default=logs"`
|
||||||
Level string `json:",default=info,options=[info,error,severe]"`
|
Level string `json:",default=info,options=[debug,info,error,severe]"`
|
||||||
Compress bool `json:",optional"`
|
Compress bool `json:",optional"`
|
||||||
KeepDays int `json:",optional"`
|
KeepDays int `json:",optional"`
|
||||||
StackCooldownMillis int `json:",default=100"`
|
StackCooldownMillis int `json:",default=100"`
|
||||||
|
|||||||
@@ -1,145 +0,0 @@
|
|||||||
package logx
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/timex"
|
|
||||||
"go.opentelemetry.io/otel/trace"
|
|
||||||
)
|
|
||||||
|
|
||||||
// WithContext sets ctx to log, for keeping tracing information.
|
|
||||||
func WithContext(ctx context.Context) Logger {
|
|
||||||
return &contextLogger{
|
|
||||||
ctx: ctx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type contextLogger struct {
|
|
||||||
logEntry
|
|
||||||
ctx context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Error(v ...interface{}) {
|
|
||||||
l.err(fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Errorf(format string, v ...interface{}) {
|
|
||||||
l.err(fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Errorv(v interface{}) {
|
|
||||||
l.err(fmt.Sprint(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Errorw(msg string, fields ...LogField) {
|
|
||||||
l.err(msg, fields...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Info(v ...interface{}) {
|
|
||||||
l.info(fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Infof(format string, v ...interface{}) {
|
|
||||||
l.info(fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Infov(v interface{}) {
|
|
||||||
l.info(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Infow(msg string, fields ...LogField) {
|
|
||||||
l.info(msg, fields...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Slow(v ...interface{}) {
|
|
||||||
l.slow(fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Slowf(format string, v ...interface{}) {
|
|
||||||
l.slow(fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Slowv(v interface{}) {
|
|
||||||
l.slow(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) Sloww(msg string, fields ...LogField) {
|
|
||||||
l.slow(msg, fields...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) WithContext(ctx context.Context) Logger {
|
|
||||||
if ctx == nil {
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
|
||||||
l.ctx = ctx
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) WithDuration(duration time.Duration) Logger {
|
|
||||||
l.Duration = timex.ReprOfDuration(duration)
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) buildFields(fields ...LogField) []LogField {
|
|
||||||
if len(l.Duration) > 0 {
|
|
||||||
fields = append(fields, Field(durationKey, l.Duration))
|
|
||||||
}
|
|
||||||
|
|
||||||
traceID := traceIdFromContext(l.ctx)
|
|
||||||
if len(traceID) > 0 {
|
|
||||||
fields = append(fields, Field(traceKey, traceID))
|
|
||||||
}
|
|
||||||
|
|
||||||
spanID := spanIdFromContext(l.ctx)
|
|
||||||
if len(spanID) > 0 {
|
|
||||||
fields = append(fields, Field(spanKey, spanID))
|
|
||||||
}
|
|
||||||
|
|
||||||
val := l.ctx.Value(fieldsContextKey)
|
|
||||||
if val != nil {
|
|
||||||
if arr, ok := val.([]LogField); ok {
|
|
||||||
fields = append(fields, arr...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fields
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) err(v interface{}, fields ...LogField) {
|
|
||||||
if shallLog(ErrorLevel) {
|
|
||||||
getWriter().Error(v, l.buildFields(fields...)...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) info(v interface{}, fields ...LogField) {
|
|
||||||
if shallLog(InfoLevel) {
|
|
||||||
getWriter().Info(v, l.buildFields(fields...)...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *contextLogger) slow(v interface{}, fields ...LogField) {
|
|
||||||
if shallLog(ErrorLevel) {
|
|
||||||
getWriter().Slow(v, l.buildFields(fields...)...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func spanIdFromContext(ctx context.Context) string {
|
|
||||||
spanCtx := trace.SpanContextFromContext(ctx)
|
|
||||||
if spanCtx.HasSpanID() {
|
|
||||||
return spanCtx.SpanID().String()
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func traceIdFromContext(ctx context.Context) string {
|
|
||||||
spanCtx := trace.SpanContextFromContext(ctx)
|
|
||||||
if spanCtx.HasTraceID() {
|
|
||||||
return spanCtx.TraceID().String()
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
package logx
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/timex"
|
|
||||||
)
|
|
||||||
|
|
||||||
// WithDuration returns a Logger which logs the given duration.
|
|
||||||
func WithDuration(d time.Duration) Logger {
|
|
||||||
return &durationLogger{
|
|
||||||
Duration: timex.ReprOfDuration(d),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type durationLogger logEntry
|
|
||||||
|
|
||||||
func (l *durationLogger) Error(v ...interface{}) {
|
|
||||||
l.err(fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Errorf(format string, v ...interface{}) {
|
|
||||||
l.err(fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Errorv(v interface{}) {
|
|
||||||
l.err(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Errorw(msg string, fields ...LogField) {
|
|
||||||
l.err(msg, fields...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Info(v ...interface{}) {
|
|
||||||
l.info(fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Infof(format string, v ...interface{}) {
|
|
||||||
l.info(fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Infov(v interface{}) {
|
|
||||||
l.info(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Infow(msg string, fields ...LogField) {
|
|
||||||
l.info(msg, fields...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Slow(v ...interface{}) {
|
|
||||||
l.slow(fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Slowf(format string, v ...interface{}) {
|
|
||||||
l.slow(fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Slowv(v interface{}) {
|
|
||||||
l.slow(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) Sloww(msg string, fields ...LogField) {
|
|
||||||
l.slow(msg, fields...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) WithContext(ctx context.Context) Logger {
|
|
||||||
return &contextLogger{
|
|
||||||
ctx: ctx,
|
|
||||||
logEntry: logEntry{
|
|
||||||
Duration: l.Duration,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) WithDuration(duration time.Duration) Logger {
|
|
||||||
l.Duration = timex.ReprOfDuration(duration)
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) err(v interface{}, fields ...LogField) {
|
|
||||||
if shallLog(ErrorLevel) {
|
|
||||||
fields = append(fields, Field(durationKey, l.Duration))
|
|
||||||
getWriter().Error(v, fields...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) info(v interface{}, fields ...LogField) {
|
|
||||||
if shallLog(InfoLevel) {
|
|
||||||
fields = append(fields, Field(durationKey, l.Duration))
|
|
||||||
getWriter().Info(v, fields...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *durationLogger) slow(v interface{}, fields ...LogField) {
|
|
||||||
if shallLog(ErrorLevel) {
|
|
||||||
fields = append(fields, Field(durationKey, l.Duration))
|
|
||||||
getWriter().Slow(v, fields...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,161 +0,0 @@
|
|||||||
package logx
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"go.opentelemetry.io/otel"
|
|
||||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWithDurationError(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Error("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationErrorf(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Errorf("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationErrorv(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Errorv("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationErrorw(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Errorw("foo", Field("foo", "bar"))
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "bar"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationInfo(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Info("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationInfoConsole(t *testing.T) {
|
|
||||||
old := atomic.LoadUint32(&encoding)
|
|
||||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
|
||||||
defer func() {
|
|
||||||
atomic.StoreUint32(&encoding, old)
|
|
||||||
}()
|
|
||||||
|
|
||||||
w := new(mockWriter)
|
|
||||||
o := writer.Swap(w)
|
|
||||||
defer writer.Store(o)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Info("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "ms"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationInfof(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Infof("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationInfov(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Infov("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationInfow(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Infow("foo", Field("foo", "bar"))
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "bar"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationWithContextInfow(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
otp := otel.GetTracerProvider()
|
|
||||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
|
||||||
otel.SetTracerProvider(tp)
|
|
||||||
defer otel.SetTracerProvider(otp)
|
|
||||||
|
|
||||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
|
||||||
WithDuration(time.Second).WithContext(ctx).Infow("foo", Field("foo", "bar"))
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "bar"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "trace"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "span"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationSlow(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).Slow("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationSlowf(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).WithDuration(time.Hour).Slowf("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationSlowv(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).WithDuration(time.Hour).Slowv("foo")
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDurationSloww(t *testing.T) {
|
|
||||||
w := new(mockWriter)
|
|
||||||
old := writer.Swap(w)
|
|
||||||
defer writer.Store(old)
|
|
||||||
|
|
||||||
WithDuration(time.Second).WithDuration(time.Hour).Sloww("foo", Field("foo", "bar"))
|
|
||||||
assert.True(t, strings.Contains(w.String(), "duration"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
|
||||||
assert.True(t, strings.Contains(w.String(), "bar"), w.String())
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,48 @@
|
|||||||
package logx
|
package logx
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
)
|
||||||
|
|
||||||
var fieldsContextKey contextKey
|
var (
|
||||||
|
fieldsContextKey contextKey
|
||||||
|
globalFields atomic.Value
|
||||||
|
globalFieldsLock sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
type contextKey struct{}
|
type contextKey struct{}
|
||||||
|
|
||||||
// WithFields returns a new context with the given fields.
|
// AddGlobalFields adds global fields.
|
||||||
func WithFields(ctx context.Context, fields ...LogField) context.Context {
|
func AddGlobalFields(fields ...LogField) {
|
||||||
|
globalFieldsLock.Lock()
|
||||||
|
defer globalFieldsLock.Unlock()
|
||||||
|
|
||||||
|
old := globalFields.Load()
|
||||||
|
if old == nil {
|
||||||
|
globalFields.Store(append([]LogField(nil), fields...))
|
||||||
|
} else {
|
||||||
|
globalFields.Store(append(old.([]LogField), fields...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContextWithFields returns a new context with the given fields.
|
||||||
|
func ContextWithFields(ctx context.Context, fields ...LogField) context.Context {
|
||||||
if val := ctx.Value(fieldsContextKey); val != nil {
|
if val := ctx.Value(fieldsContextKey); val != nil {
|
||||||
if arr, ok := val.([]LogField); ok {
|
if arr, ok := val.([]LogField); ok {
|
||||||
return context.WithValue(ctx, fieldsContextKey, append(arr, fields...))
|
allFields := make([]LogField, 0, len(arr)+len(fields))
|
||||||
|
allFields = append(allFields, arr...)
|
||||||
|
allFields = append(allFields, fields...)
|
||||||
|
return context.WithValue(ctx, fieldsContextKey, allFields)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return context.WithValue(ctx, fieldsContextKey, fields)
|
return context.WithValue(ctx, fieldsContextKey, fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithFields returns a new logger with the given fields.
|
||||||
|
// deprecated: use ContextWithFields instead.
|
||||||
|
func WithFields(ctx context.Context, fields ...LogField) context.Context {
|
||||||
|
return ContextWithFields(ctx, fields...)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,46 @@
|
|||||||
package logx
|
package logx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestAddGlobalFields(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
writer := NewWriter(&buf)
|
||||||
|
old := Reset()
|
||||||
|
SetWriter(writer)
|
||||||
|
defer SetWriter(old)
|
||||||
|
|
||||||
|
Info("hello")
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
AddGlobalFields(Field("a", "1"), Field("b", "2"))
|
||||||
|
AddGlobalFields(Field("c", "3"))
|
||||||
|
Info("world")
|
||||||
|
var m map[string]interface{}
|
||||||
|
assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
|
||||||
|
assert.Equal(t, "1", m["a"])
|
||||||
|
assert.Equal(t, "2", m["b"])
|
||||||
|
assert.Equal(t, "3", m["c"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextWithFields(t *testing.T) {
|
||||||
|
ctx := ContextWithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||||||
|
vals := ctx.Value(fieldsContextKey)
|
||||||
|
assert.NotNil(t, vals)
|
||||||
|
fields, ok := vals.([]LogField)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWithFields(t *testing.T) {
|
func TestWithFields(t *testing.T) {
|
||||||
ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2))
|
ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||||||
vals := ctx.Value(fieldsContextKey)
|
vals := ctx.Value(fieldsContextKey)
|
||||||
@@ -19,8 +53,8 @@ func TestWithFields(t *testing.T) {
|
|||||||
func TestWithFieldsAppend(t *testing.T) {
|
func TestWithFieldsAppend(t *testing.T) {
|
||||||
var dummyKey struct{}
|
var dummyKey struct{}
|
||||||
ctx := context.WithValue(context.Background(), dummyKey, "dummy")
|
ctx := context.WithValue(context.Background(), dummyKey, "dummy")
|
||||||
ctx = WithFields(ctx, Field("a", 1), Field("b", 2))
|
ctx = ContextWithFields(ctx, Field("a", 1), Field("b", 2))
|
||||||
ctx = WithFields(ctx, Field("c", 3), Field("d", 4))
|
ctx = ContextWithFields(ctx, Field("c", 3), Field("d", 4))
|
||||||
vals := ctx.Value(fieldsContextKey)
|
vals := ctx.Value(fieldsContextKey)
|
||||||
assert.NotNil(t, vals)
|
assert.NotNil(t, vals)
|
||||||
fields, ok := vals.([]LogField)
|
fields, ok := vals.([]LogField)
|
||||||
@@ -33,3 +67,55 @@ func TestWithFieldsAppend(t *testing.T) {
|
|||||||
Field("d", 4),
|
Field("d", 4),
|
||||||
}, fields)
|
}, fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithFieldsAppendCopy(t *testing.T) {
|
||||||
|
const count = 10
|
||||||
|
ctx := context.Background()
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
ctx = ContextWithFields(ctx, Field(strconv.Itoa(i), 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
af := Field("foo", 1)
|
||||||
|
bf := Field("bar", 2)
|
||||||
|
ctxa := ContextWithFields(ctx, af)
|
||||||
|
ctxb := ContextWithFields(ctx, bf)
|
||||||
|
|
||||||
|
assert.EqualValues(t, af, ctxa.Value(fieldsContextKey).([]LogField)[count])
|
||||||
|
assert.EqualValues(t, bf, ctxb.Value(fieldsContextKey).([]LogField)[count])
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkAtomicValue(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var container atomic.Value
|
||||||
|
vals := []LogField{
|
||||||
|
Field("a", "b"),
|
||||||
|
Field("c", "d"),
|
||||||
|
Field("e", "f"),
|
||||||
|
}
|
||||||
|
container.Store(&vals)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
val := container.Load()
|
||||||
|
if val != nil {
|
||||||
|
_ = *val.(*[]LogField)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkRWMutex(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var lock sync.RWMutex
|
||||||
|
vals := []LogField{
|
||||||
|
Field("a", "b"),
|
||||||
|
Field("c", "d"),
|
||||||
|
Field("e", "f"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
lock.RLock()
|
||||||
|
_ = vals
|
||||||
|
lock.RUnlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,14 @@ import (
|
|||||||
|
|
||||||
// A Logger represents a logger.
|
// A Logger represents a logger.
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
|
// Debug logs a message at info level.
|
||||||
|
Debug(...interface{})
|
||||||
|
// Debugf logs a message at info level.
|
||||||
|
Debugf(string, ...interface{})
|
||||||
|
// Debugv logs a message at info level.
|
||||||
|
Debugv(interface{})
|
||||||
|
// Debugw logs a message at info level.
|
||||||
|
Debugw(string, ...LogField)
|
||||||
// Error logs a message at error level.
|
// Error logs a message at error level.
|
||||||
Error(...interface{})
|
Error(...interface{})
|
||||||
// Errorf logs a message at error level.
|
// Errorf logs a message at error level.
|
||||||
@@ -31,8 +39,12 @@ type Logger interface {
|
|||||||
Slowv(interface{})
|
Slowv(interface{})
|
||||||
// Sloww logs a message at slow level.
|
// Sloww logs a message at slow level.
|
||||||
Sloww(string, ...LogField)
|
Sloww(string, ...LogField)
|
||||||
|
// WithCallerSkip returns a new logger with the given caller skip.
|
||||||
|
WithCallerSkip(skip int) Logger
|
||||||
// WithContext returns a new logger with the given context.
|
// WithContext returns a new logger with the given context.
|
||||||
WithContext(context.Context) Logger
|
WithContext(ctx context.Context) Logger
|
||||||
// WithDuration returns a new logger with the given duration.
|
// WithDuration returns a new logger with the given duration.
|
||||||
WithDuration(time.Duration) Logger
|
WithDuration(d time.Duration) Logger
|
||||||
|
// WithFields returns a new logger with the given fields.
|
||||||
|
WithFields(fields ...LogField) Logger
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,14 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/sysx"
|
"github.com/zeromicro/go-zero/core/sysx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const callerDepth = 5
|
const callerDepth = 4
|
||||||
|
|
||||||
var (
|
var (
|
||||||
timeFormat = "2006-01-02T15:04:05.000Z07:00"
|
timeFormat = "2006-01-02T15:04:05.000Z07:00"
|
||||||
logLevel uint32
|
logLevel uint32
|
||||||
encoding uint32 = jsonEncodingType
|
encoding uint32 = jsonEncodingType
|
||||||
// use uint32 for atomic operations
|
// use uint32 for atomic operations
|
||||||
|
disableLog uint32
|
||||||
disableStat uint32
|
disableStat uint32
|
||||||
options logOptions
|
options logOptions
|
||||||
writer = new(atomicWriter)
|
writer = new(atomicWriter)
|
||||||
@@ -28,15 +29,16 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
logEntry struct {
|
// LogField is a key-value pair that will be added to the log entry.
|
||||||
Timestamp string `json:"@timestamp"`
|
LogField struct {
|
||||||
Level string `json:"level"`
|
Key string
|
||||||
Duration string `json:"duration,omitempty"`
|
Value interface{}
|
||||||
Caller string `json:"caller,omitempty"`
|
|
||||||
Content interface{} `json:"content"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logEntryWithFields map[string]interface{}
|
// LogOption defines the method to customize the logging.
|
||||||
|
LogOption func(options *logOptions)
|
||||||
|
|
||||||
|
logEntry map[string]interface{}
|
||||||
|
|
||||||
logOptions struct {
|
logOptions struct {
|
||||||
gzipEnabled bool
|
gzipEnabled bool
|
||||||
@@ -46,15 +48,6 @@ type (
|
|||||||
maxSize int
|
maxSize int
|
||||||
rotationRule string
|
rotationRule string
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogField is a key-value pair that will be added to the log entry.
|
|
||||||
LogField struct {
|
|
||||||
Key string
|
|
||||||
Value interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogOption defines the method to customize the logging.
|
|
||||||
LogOption func(options *logOptions)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Alert alerts v in alert level, and the message is written to error log.
|
// Alert alerts v in alert level, and the message is written to error log.
|
||||||
@@ -71,8 +64,29 @@ func Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debug writes v into access log.
|
||||||
|
func Debug(v ...interface{}) {
|
||||||
|
writeDebug(fmt.Sprint(v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugf writes v with format into access log.
|
||||||
|
func Debugf(format string, v ...interface{}) {
|
||||||
|
writeDebug(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugv writes v into access log with json content.
|
||||||
|
func Debugv(v interface{}) {
|
||||||
|
writeDebug(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugw writes msg along with fields into access log.
|
||||||
|
func Debugw(msg string, fields ...LogField) {
|
||||||
|
writeDebug(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
// Disable disables the logging.
|
// Disable disables the logging.
|
||||||
func Disable() {
|
func Disable() {
|
||||||
|
atomic.StoreUint32(&disableLog, 1)
|
||||||
writer.Store(nopWriter{})
|
writer.Store(nopWriter{})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,35 +97,35 @@ func DisableStat() {
|
|||||||
|
|
||||||
// Error writes v into error log.
|
// Error writes v into error log.
|
||||||
func Error(v ...interface{}) {
|
func Error(v ...interface{}) {
|
||||||
errorTextSync(fmt.Sprint(v...))
|
writeError(fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf writes v with format into error log.
|
// Errorf writes v with format into error log.
|
||||||
func Errorf(format string, v ...interface{}) {
|
func Errorf(format string, v ...interface{}) {
|
||||||
errorTextSync(fmt.Errorf(format, v...).Error())
|
writeError(fmt.Errorf(format, v...).Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorStack writes v along with call stack into error log.
|
// ErrorStack writes v along with call stack into error log.
|
||||||
func ErrorStack(v ...interface{}) {
|
func ErrorStack(v ...interface{}) {
|
||||||
// there is newline in stack string
|
// there is newline in stack string
|
||||||
stackSync(fmt.Sprint(v...))
|
writeStack(fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorStackf writes v along with call stack in format into error log.
|
// ErrorStackf writes v along with call stack in format into error log.
|
||||||
func ErrorStackf(format string, v ...interface{}) {
|
func ErrorStackf(format string, v ...interface{}) {
|
||||||
// there is newline in stack string
|
// there is newline in stack string
|
||||||
stackSync(fmt.Sprintf(format, v...))
|
writeStack(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorv writes v into error log with json content.
|
// Errorv writes v into error log with json content.
|
||||||
// No call stack attached, because not elegant to pack the messages.
|
// No call stack attached, because not elegant to pack the messages.
|
||||||
func Errorv(v interface{}) {
|
func Errorv(v interface{}) {
|
||||||
errorAnySync(v)
|
writeError(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorw writes msg along with fields into error log.
|
// Errorw writes msg along with fields into error log.
|
||||||
func Errorw(msg string, fields ...LogField) {
|
func Errorw(msg string, fields ...LogField) {
|
||||||
errorFieldsSync(msg, fields...)
|
writeError(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Field returns a LogField for the given key and value.
|
// Field returns a LogField for the given key and value.
|
||||||
@@ -154,22 +168,22 @@ func Field(key string, value interface{}) LogField {
|
|||||||
|
|
||||||
// Info writes v into access log.
|
// Info writes v into access log.
|
||||||
func Info(v ...interface{}) {
|
func Info(v ...interface{}) {
|
||||||
infoTextSync(fmt.Sprint(v...))
|
writeInfo(fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infof writes v with format into access log.
|
// Infof writes v with format into access log.
|
||||||
func Infof(format string, v ...interface{}) {
|
func Infof(format string, v ...interface{}) {
|
||||||
infoTextSync(fmt.Sprintf(format, v...))
|
writeInfo(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infov writes v into access log with json content.
|
// Infov writes v into access log with json content.
|
||||||
func Infov(v interface{}) {
|
func Infov(v interface{}) {
|
||||||
infoAnySync(v)
|
writeInfo(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infow writes msg along with fields into access log.
|
// Infow writes msg along with fields into access log.
|
||||||
func Infow(msg string, fields ...LogField) {
|
func Infow(msg string, fields ...LogField) {
|
||||||
infoFieldsSync(msg, fields...)
|
writeInfo(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must checks if err is nil, otherwise logs the error and exits.
|
// Must checks if err is nil, otherwise logs the error and exits.
|
||||||
@@ -201,7 +215,9 @@ func SetLevel(level uint32) {
|
|||||||
|
|
||||||
// SetWriter sets the logging writer. It can be used to customize the logging.
|
// SetWriter sets the logging writer. It can be used to customize the logging.
|
||||||
func SetWriter(w Writer) {
|
func SetWriter(w Writer) {
|
||||||
writer.Store(w)
|
if atomic.LoadUint32(&disableLog) == 0 {
|
||||||
|
writer.Store(w)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetUp sets up the logx. If already set up, just return nil.
|
// SetUp sets up the logx. If already set up, just return nil.
|
||||||
@@ -240,42 +256,42 @@ func SetUp(c LogConf) (err error) {
|
|||||||
|
|
||||||
// Severe writes v into severe log.
|
// Severe writes v into severe log.
|
||||||
func Severe(v ...interface{}) {
|
func Severe(v ...interface{}) {
|
||||||
severeSync(fmt.Sprint(v...))
|
writeSevere(fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Severef writes v with format into severe log.
|
// Severef writes v with format into severe log.
|
||||||
func Severef(format string, v ...interface{}) {
|
func Severef(format string, v ...interface{}) {
|
||||||
severeSync(fmt.Sprintf(format, v...))
|
writeSevere(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slow writes v into slow log.
|
// Slow writes v into slow log.
|
||||||
func Slow(v ...interface{}) {
|
func Slow(v ...interface{}) {
|
||||||
slowTextSync(fmt.Sprint(v...))
|
writeSlow(fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slowf writes v with format into slow log.
|
// Slowf writes v with format into slow log.
|
||||||
func Slowf(format string, v ...interface{}) {
|
func Slowf(format string, v ...interface{}) {
|
||||||
slowTextSync(fmt.Sprintf(format, v...))
|
writeSlow(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slowv writes v into slow log with json content.
|
// Slowv writes v into slow log with json content.
|
||||||
func Slowv(v interface{}) {
|
func Slowv(v interface{}) {
|
||||||
slowAnySync(v)
|
writeSlow(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sloww writes msg along with fields into slow log.
|
// Sloww writes msg along with fields into slow log.
|
||||||
func Sloww(msg string, fields ...LogField) {
|
func Sloww(msg string, fields ...LogField) {
|
||||||
slowFieldsSync(msg, fields...)
|
writeSlow(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stat writes v into stat log.
|
// Stat writes v into stat log.
|
||||||
func Stat(v ...interface{}) {
|
func Stat(v ...interface{}) {
|
||||||
statSync(fmt.Sprint(v...))
|
writeStat(fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Statf writes v with format into stat log.
|
// Statf writes v with format into stat log.
|
||||||
func Statf(format string, v ...interface{}) {
|
func Statf(format string, v ...interface{}) {
|
||||||
statSync(fmt.Sprintf(format, v...))
|
writeStat(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithCooldownMillis customizes logging on writing call stack interval.
|
// WithCooldownMillis customizes logging on writing call stack interval.
|
||||||
@@ -320,6 +336,10 @@ func WithRotation(r string) LogOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addCaller(fields ...LogField) []LogField {
|
||||||
|
return append(fields, Field(callerKey, getCaller(callerDepth)))
|
||||||
|
}
|
||||||
|
|
||||||
func createOutput(path string) (io.WriteCloser, error) {
|
func createOutput(path string) (io.WriteCloser, error) {
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return nil, ErrLogPathNotSet
|
return nil, ErrLogPathNotSet
|
||||||
@@ -335,29 +355,10 @@ func createOutput(path string) (io.WriteCloser, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorAnySync(v interface{}) {
|
|
||||||
if shallLog(ErrorLevel) {
|
|
||||||
getWriter().Error(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func errorFieldsSync(content string, fields ...LogField) {
|
|
||||||
if shallLog(ErrorLevel) {
|
|
||||||
getWriter().Error(content, fields...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func errorTextSync(msg string) {
|
|
||||||
if shallLog(ErrorLevel) {
|
|
||||||
getWriter().Error(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getWriter() Writer {
|
func getWriter() Writer {
|
||||||
w := writer.Load()
|
w := writer.Load()
|
||||||
if w == nil {
|
if w == nil {
|
||||||
w = newConsoleWriter()
|
w = writer.StoreIfNil(newConsoleWriter())
|
||||||
writer.Store(w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return w
|
return w
|
||||||
@@ -369,26 +370,10 @@ func handleOptions(opts []LogOption) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func infoAnySync(val interface{}) {
|
|
||||||
if shallLog(InfoLevel) {
|
|
||||||
getWriter().Info(val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func infoFieldsSync(content string, fields ...LogField) {
|
|
||||||
if shallLog(InfoLevel) {
|
|
||||||
getWriter().Info(content, fields...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func infoTextSync(msg string) {
|
|
||||||
if shallLog(InfoLevel) {
|
|
||||||
getWriter().Info(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupLogLevel(c LogConf) {
|
func setupLogLevel(c LogConf) {
|
||||||
switch c.Level {
|
switch c.Level {
|
||||||
|
case levelDebug:
|
||||||
|
SetLevel(DebugLevel)
|
||||||
case levelInfo:
|
case levelInfo:
|
||||||
SetLevel(InfoLevel)
|
SetLevel(InfoLevel)
|
||||||
case levelError:
|
case levelError:
|
||||||
@@ -421,12 +406,6 @@ func setupWithVolume(c LogConf) error {
|
|||||||
return setupWithFiles(c)
|
return setupWithFiles(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func severeSync(msg string) {
|
|
||||||
if shallLog(SevereLevel) {
|
|
||||||
getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func shallLog(level uint32) bool {
|
func shallLog(level uint32) bool {
|
||||||
return atomic.LoadUint32(&logLevel) <= level
|
return atomic.LoadUint32(&logLevel) <= level
|
||||||
}
|
}
|
||||||
@@ -435,32 +414,44 @@ func shallLogStat() bool {
|
|||||||
return atomic.LoadUint32(&disableStat) == 0
|
return atomic.LoadUint32(&disableStat) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func slowAnySync(v interface{}) {
|
func writeDebug(val interface{}, fields ...LogField) {
|
||||||
if shallLog(ErrorLevel) {
|
if shallLog(DebugLevel) {
|
||||||
getWriter().Slow(v)
|
getWriter().Debug(val, addCaller(fields...)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func slowFieldsSync(content string, fields ...LogField) {
|
func writeError(val interface{}, fields ...LogField) {
|
||||||
if shallLog(ErrorLevel) {
|
if shallLog(ErrorLevel) {
|
||||||
getWriter().Slow(content, fields...)
|
getWriter().Error(val, addCaller(fields...)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func slowTextSync(msg string) {
|
func writeInfo(val interface{}, fields ...LogField) {
|
||||||
if shallLog(ErrorLevel) {
|
if shallLog(InfoLevel) {
|
||||||
getWriter().Slow(msg)
|
getWriter().Info(val, addCaller(fields...)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stackSync(msg string) {
|
func writeSevere(msg string) {
|
||||||
|
if shallLog(SevereLevel) {
|
||||||
|
getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeSlow(val interface{}, fields ...LogField) {
|
||||||
|
if shallLog(ErrorLevel) {
|
||||||
|
getWriter().Slow(val, addCaller(fields...)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeStack(msg string) {
|
||||||
if shallLog(ErrorLevel) {
|
if shallLog(ErrorLevel) {
|
||||||
getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func statSync(msg string) {
|
func writeStat(msg string) {
|
||||||
if shallLogStat() && shallLog(InfoLevel) {
|
if shallLogStat() && shallLog(InfoLevel) {
|
||||||
getWriter().Stat(msg)
|
getWriter().Stat(msg, addCaller()...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -35,6 +35,12 @@ func (mw *mockWriter) Alert(v interface{}) {
|
|||||||
output(&mw.builder, levelAlert, v)
|
output(&mw.builder, levelAlert, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mw *mockWriter) Debug(v interface{}, fields ...LogField) {
|
||||||
|
mw.lock.Lock()
|
||||||
|
defer mw.lock.Unlock()
|
||||||
|
output(&mw.builder, levelDebug, v, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
func (mw *mockWriter) Error(v interface{}, fields ...LogField) {
|
func (mw *mockWriter) Error(v interface{}, fields ...LogField) {
|
||||||
mw.lock.Lock()
|
mw.lock.Lock()
|
||||||
defer mw.lock.Unlock()
|
defer mw.lock.Unlock()
|
||||||
@@ -212,6 +218,46 @@ func TestStructedLogAlert(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStructedLogDebug(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
defer writer.Store(old)
|
||||||
|
|
||||||
|
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||||
|
Debug(v...)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStructedLogDebugf(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
defer writer.Store(old)
|
||||||
|
|
||||||
|
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||||
|
Debugf(fmt.Sprint(v...))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStructedLogDebugv(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
defer writer.Store(old)
|
||||||
|
|
||||||
|
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||||
|
Debugv(fmt.Sprint(v...))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStructedLogDebugw(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
defer writer.Store(old)
|
||||||
|
|
||||||
|
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||||
|
Debugw(fmt.Sprint(v...), Field("foo", time.Second))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestStructedLogError(t *testing.T) {
|
func TestStructedLogError(t *testing.T) {
|
||||||
w := new(mockWriter)
|
w := new(mockWriter)
|
||||||
old := writer.Swap(w)
|
old := writer.Swap(w)
|
||||||
@@ -461,13 +507,13 @@ func TestStructedLogWithDuration(t *testing.T) {
|
|||||||
defer writer.Store(old)
|
defer writer.Store(old)
|
||||||
|
|
||||||
WithDuration(time.Second).Info(message)
|
WithDuration(time.Second).Info(message)
|
||||||
var entry logEntry
|
var entry map[string]interface{}
|
||||||
if err := json.Unmarshal([]byte(w.String()), &entry); err != nil {
|
if err := json.Unmarshal([]byte(w.String()), &entry); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, levelInfo, entry.Level)
|
assert.Equal(t, levelInfo, entry[levelKey])
|
||||||
assert.Equal(t, message, entry.Content)
|
assert.Equal(t, message, entry[contentKey])
|
||||||
assert.Equal(t, "1000.0ms", entry.Duration)
|
assert.Equal(t, "1000.0ms", entry[durationKey])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetLevel(t *testing.T) {
|
func TestSetLevel(t *testing.T) {
|
||||||
@@ -531,6 +577,7 @@ func TestSetup(t *testing.T) {
|
|||||||
MustSetup(LogConf{
|
MustSetup(LogConf{
|
||||||
ServiceName: "any",
|
ServiceName: "any",
|
||||||
Mode: "console",
|
Mode: "console",
|
||||||
|
TimeFormat: timeFormat,
|
||||||
})
|
})
|
||||||
MustSetup(LogConf{
|
MustSetup(LogConf{
|
||||||
ServiceName: "any",
|
ServiceName: "any",
|
||||||
@@ -553,13 +600,23 @@ func TestSetup(t *testing.T) {
|
|||||||
Encoding: plainEncoding,
|
Encoding: plainEncoding,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
defer os.RemoveAll("CD01CB7D-2705-4F3F-889E-86219BF56F10")
|
||||||
assert.NotNil(t, setupWithVolume(LogConf{}))
|
assert.NotNil(t, setupWithVolume(LogConf{}))
|
||||||
|
assert.Nil(t, setupWithVolume(LogConf{
|
||||||
|
ServiceName: "CD01CB7D-2705-4F3F-889E-86219BF56F10",
|
||||||
|
}))
|
||||||
|
assert.Nil(t, setupWithVolume(LogConf{
|
||||||
|
ServiceName: "CD01CB7D-2705-4F3F-889E-86219BF56F10",
|
||||||
|
Rotation: sizeRotationRule,
|
||||||
|
}))
|
||||||
assert.NotNil(t, setupWithFiles(LogConf{}))
|
assert.NotNil(t, setupWithFiles(LogConf{}))
|
||||||
assert.Nil(t, setupWithFiles(LogConf{
|
assert.Nil(t, setupWithFiles(LogConf{
|
||||||
ServiceName: "any",
|
ServiceName: "any",
|
||||||
Path: os.TempDir(),
|
Path: os.TempDir(),
|
||||||
Compress: true,
|
Compress: true,
|
||||||
KeepDays: 1,
|
KeepDays: 1,
|
||||||
|
MaxBackups: 3,
|
||||||
|
MaxSize: 1024 * 1024,
|
||||||
}))
|
}))
|
||||||
setupLogLevel(LogConf{
|
setupLogLevel(LogConf{
|
||||||
Level: levelInfo,
|
Level: levelInfo,
|
||||||
@@ -583,6 +640,8 @@ func TestDisable(t *testing.T) {
|
|||||||
var opt logOptions
|
var opt logOptions
|
||||||
WithKeepDays(1)(&opt)
|
WithKeepDays(1)(&opt)
|
||||||
WithGzip()(&opt)
|
WithGzip()(&opt)
|
||||||
|
WithMaxBackups(1)(&opt)
|
||||||
|
WithMaxSize(1024)(&opt)
|
||||||
assert.Nil(t, Close())
|
assert.Nil(t, Close())
|
||||||
assert.Nil(t, Close())
|
assert.Nil(t, Close())
|
||||||
}
|
}
|
||||||
@@ -599,6 +658,7 @@ func TestDisableStat(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSetWriter(t *testing.T) {
|
func TestSetWriter(t *testing.T) {
|
||||||
|
atomic.StoreUint32(&disableLog, 0)
|
||||||
Reset()
|
Reset()
|
||||||
SetWriter(nopWriter{})
|
SetWriter(nopWriter{})
|
||||||
assert.NotNil(t, writer.Load())
|
assert.NotNil(t, writer.Load())
|
||||||
@@ -648,7 +708,7 @@ func BenchmarkCopyByteSlice(b *testing.B) {
|
|||||||
buf = make([]byte, len(s))
|
buf = make([]byte, len(s))
|
||||||
copy(buf, s)
|
copy(buf, s)
|
||||||
}
|
}
|
||||||
fmt.Fprint(ioutil.Discard, buf)
|
fmt.Fprint(io.Discard, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkCopyOnWriteByteSlice(b *testing.B) {
|
func BenchmarkCopyOnWriteByteSlice(b *testing.B) {
|
||||||
@@ -657,7 +717,7 @@ func BenchmarkCopyOnWriteByteSlice(b *testing.B) {
|
|||||||
size := len(s)
|
size := len(s)
|
||||||
buf = s[:size:size]
|
buf = s[:size:size]
|
||||||
}
|
}
|
||||||
fmt.Fprint(ioutil.Discard, buf)
|
fmt.Fprint(io.Discard, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkCacheByteSlice(b *testing.B) {
|
func BenchmarkCacheByteSlice(b *testing.B) {
|
||||||
@@ -671,7 +731,7 @@ func BenchmarkCacheByteSlice(b *testing.B) {
|
|||||||
func BenchmarkLogs(b *testing.B) {
|
func BenchmarkLogs(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(io.Discard)
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
Info(i)
|
Info(i)
|
||||||
}
|
}
|
||||||
@@ -710,14 +770,16 @@ func put(b []byte) {
|
|||||||
func doTestStructedLog(t *testing.T, level string, w *mockWriter, write func(...interface{})) {
|
func doTestStructedLog(t *testing.T, level string, w *mockWriter, write func(...interface{})) {
|
||||||
const message = "hello there"
|
const message = "hello there"
|
||||||
write(message)
|
write(message)
|
||||||
var entry logEntry
|
|
||||||
|
var entry map[string]interface{}
|
||||||
if err := json.Unmarshal([]byte(w.String()), &entry); err != nil {
|
if err := json.Unmarshal([]byte(w.String()), &entry); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, level, entry.Level)
|
|
||||||
val, ok := entry.Content.(string)
|
assert.Equal(t, level, entry[levelKey])
|
||||||
|
val, ok := entry[contentKey]
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
assert.True(t, strings.Contains(val, message))
|
assert.True(t, strings.Contains(val.(string), message))
|
||||||
}
|
}
|
||||||
|
|
||||||
func doTestStructedLogConsole(t *testing.T, w *mockWriter, write func(...interface{})) {
|
func doTestStructedLogConsole(t *testing.T, w *mockWriter, write func(...interface{})) {
|
||||||
|
|||||||
181
core/logx/richlogger.go
Normal file
181
core/logx/richlogger.go
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
package logx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/timex"
|
||||||
|
"github.com/zeromicro/go-zero/internal/trace"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WithCallerSkip returns a Logger with given caller skip.
|
||||||
|
func WithCallerSkip(skip int) Logger {
|
||||||
|
if skip <= 0 {
|
||||||
|
return new(richLogger)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &richLogger{
|
||||||
|
callerSkip: skip,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithContext sets ctx to log, for keeping tracing information.
|
||||||
|
func WithContext(ctx context.Context) Logger {
|
||||||
|
return &richLogger{
|
||||||
|
ctx: ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDuration returns a Logger with given duration.
|
||||||
|
func WithDuration(d time.Duration) Logger {
|
||||||
|
return &richLogger{
|
||||||
|
fields: []LogField{Field(durationKey, timex.ReprOfDuration(d))},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type richLogger struct {
|
||||||
|
ctx context.Context
|
||||||
|
callerSkip int
|
||||||
|
fields []LogField
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Debug(v ...interface{}) {
|
||||||
|
l.debug(fmt.Sprint(v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Debugf(format string, v ...interface{}) {
|
||||||
|
l.debug(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Debugv(v interface{}) {
|
||||||
|
l.debug(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Debugw(msg string, fields ...LogField) {
|
||||||
|
l.debug(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Error(v ...interface{}) {
|
||||||
|
l.err(fmt.Sprint(v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Errorf(format string, v ...interface{}) {
|
||||||
|
l.err(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Errorv(v interface{}) {
|
||||||
|
l.err(fmt.Sprint(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Errorw(msg string, fields ...LogField) {
|
||||||
|
l.err(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Info(v ...interface{}) {
|
||||||
|
l.info(fmt.Sprint(v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Infof(format string, v ...interface{}) {
|
||||||
|
l.info(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Infov(v interface{}) {
|
||||||
|
l.info(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Infow(msg string, fields ...LogField) {
|
||||||
|
l.info(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Slow(v ...interface{}) {
|
||||||
|
l.slow(fmt.Sprint(v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Slowf(format string, v ...interface{}) {
|
||||||
|
l.slow(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Slowv(v interface{}) {
|
||||||
|
l.slow(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) Sloww(msg string, fields ...LogField) {
|
||||||
|
l.slow(msg, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) WithCallerSkip(skip int) Logger {
|
||||||
|
if skip <= 0 {
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
l.callerSkip = skip
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) WithContext(ctx context.Context) Logger {
|
||||||
|
l.ctx = ctx
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) WithDuration(duration time.Duration) Logger {
|
||||||
|
l.fields = append(l.fields, Field(durationKey, timex.ReprOfDuration(duration)))
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) WithFields(fields ...LogField) Logger {
|
||||||
|
l.fields = append(l.fields, fields...)
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) buildFields(fields ...LogField) []LogField {
|
||||||
|
fields = append(l.fields, fields...)
|
||||||
|
fields = append(fields, Field(callerKey, getCaller(callerDepth+l.callerSkip)))
|
||||||
|
|
||||||
|
if l.ctx == nil {
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|
||||||
|
traceID := trace.TraceIDFromContext(l.ctx)
|
||||||
|
if len(traceID) > 0 {
|
||||||
|
fields = append(fields, Field(traceKey, traceID))
|
||||||
|
}
|
||||||
|
|
||||||
|
spanID := trace.SpanIDFromContext(l.ctx)
|
||||||
|
if len(spanID) > 0 {
|
||||||
|
fields = append(fields, Field(spanKey, spanID))
|
||||||
|
}
|
||||||
|
|
||||||
|
val := l.ctx.Value(fieldsContextKey)
|
||||||
|
if val != nil {
|
||||||
|
if arr, ok := val.([]LogField); ok {
|
||||||
|
fields = append(fields, arr...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) debug(v interface{}, fields ...LogField) {
|
||||||
|
if shallLog(DebugLevel) {
|
||||||
|
getWriter().Debug(v, l.buildFields(fields...)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) err(v interface{}, fields ...LogField) {
|
||||||
|
if shallLog(ErrorLevel) {
|
||||||
|
getWriter().Error(v, l.buildFields(fields...)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) info(v interface{}, fields ...LogField) {
|
||||||
|
if shallLog(InfoLevel) {
|
||||||
|
getWriter().Info(v, l.buildFields(fields...)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *richLogger) slow(v interface{}, fields ...LogField) {
|
||||||
|
if shallLog(ErrorLevel) {
|
||||||
|
getWriter().Slow(v, l.buildFields(fields...)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package logx
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -36,6 +37,41 @@ func TestTraceLog(t *testing.T) {
|
|||||||
validate(t, w.String(), true, true)
|
validate(t, w.String(), true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTraceDebug(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
writer.lock.RLock()
|
||||||
|
defer func() {
|
||||||
|
writer.lock.RUnlock()
|
||||||
|
writer.Store(old)
|
||||||
|
}()
|
||||||
|
|
||||||
|
otp := otel.GetTracerProvider()
|
||||||
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||||
|
otel.SetTracerProvider(tp)
|
||||||
|
defer otel.SetTracerProvider(otp)
|
||||||
|
|
||||||
|
ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
|
l := WithContext(ctx)
|
||||||
|
SetLevel(DebugLevel)
|
||||||
|
l.WithDuration(time.Second).Debug(testlog)
|
||||||
|
assert.True(t, strings.Contains(w.String(), traceKey))
|
||||||
|
assert.True(t, strings.Contains(w.String(), spanKey))
|
||||||
|
w.Reset()
|
||||||
|
l.WithDuration(time.Second).Debugf(testlog)
|
||||||
|
validate(t, w.String(), true, true)
|
||||||
|
w.Reset()
|
||||||
|
l.WithDuration(time.Second).Debugv(testlog)
|
||||||
|
validate(t, w.String(), true, true)
|
||||||
|
w.Reset()
|
||||||
|
l.WithDuration(time.Second).Debugw(testlog, Field("foo", "bar"))
|
||||||
|
validate(t, w.String(), true, true)
|
||||||
|
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
||||||
|
assert.True(t, strings.Contains(w.String(), "bar"), w.String())
|
||||||
|
}
|
||||||
|
|
||||||
func TestTraceError(t *testing.T) {
|
func TestTraceError(t *testing.T) {
|
||||||
w := new(mockWriter)
|
w := new(mockWriter)
|
||||||
old := writer.Swap(w)
|
old := writer.Swap(w)
|
||||||
@@ -201,7 +237,7 @@ func TestLogWithFields(t *testing.T) {
|
|||||||
writer.Store(old)
|
writer.Store(old)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx := WithFields(context.Background(), Field("foo", "bar"))
|
ctx := ContextWithFields(context.Background(), Field("foo", "bar"))
|
||||||
l := WithContext(ctx)
|
l := WithContext(ctx)
|
||||||
SetLevel(InfoLevel)
|
SetLevel(InfoLevel)
|
||||||
l.Info(testlog)
|
l.Info(testlog)
|
||||||
@@ -211,6 +247,48 @@ func TestLogWithFields(t *testing.T) {
|
|||||||
assert.Equal(t, "bar", val.Foo)
|
assert.Equal(t, "bar", val.Foo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLogWithCallerSkip(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
writer.lock.RLock()
|
||||||
|
defer func() {
|
||||||
|
writer.lock.RUnlock()
|
||||||
|
writer.Store(old)
|
||||||
|
}()
|
||||||
|
|
||||||
|
l := WithCallerSkip(1).WithCallerSkip(0)
|
||||||
|
p := func(v string) {
|
||||||
|
l.Info(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, line := getFileLine()
|
||||||
|
p(testlog)
|
||||||
|
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
|
||||||
|
w.Reset()
|
||||||
|
l = WithCallerSkip(0).WithCallerSkip(1)
|
||||||
|
file, line = getFileLine()
|
||||||
|
p(testlog)
|
||||||
|
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerWithFields(t *testing.T) {
|
||||||
|
w := new(mockWriter)
|
||||||
|
old := writer.Swap(w)
|
||||||
|
writer.lock.RLock()
|
||||||
|
defer func() {
|
||||||
|
writer.lock.RUnlock()
|
||||||
|
writer.Store(old)
|
||||||
|
}()
|
||||||
|
|
||||||
|
l := WithContext(context.Background()).WithFields(Field("foo", "bar"))
|
||||||
|
l.Info(testlog)
|
||||||
|
|
||||||
|
var val mockValue
|
||||||
|
assert.Nil(t, json.Unmarshal([]byte(w.String()), &val))
|
||||||
|
assert.Equal(t, "bar", val.Foo)
|
||||||
|
}
|
||||||
|
|
||||||
func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
||||||
var val mockValue
|
var val mockValue
|
||||||
dec := json.NewDecoder(strings.NewReader(body))
|
dec := json.NewDecoder(strings.NewReader(body))
|
||||||
@@ -115,7 +115,9 @@ func (r *DailyRotateRule) OutdatedFiles() []string {
|
|||||||
|
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(dateFormat)
|
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(dateFormat)
|
||||||
fmt.Fprintf(&buf, "%s%s%s", r.filename, r.delimiter, boundary)
|
buf.WriteString(r.filename)
|
||||||
|
buf.WriteString(r.delimiter)
|
||||||
|
buf.WriteString(boundary)
|
||||||
if r.gzip {
|
if r.gzip {
|
||||||
buf.WriteString(gzipExt)
|
buf.WriteString(gzipExt)
|
||||||
}
|
}
|
||||||
@@ -282,7 +284,7 @@ func (l *RotateLogger) getBackupFilename() string {
|
|||||||
func (l *RotateLogger) init() error {
|
func (l *RotateLogger) init() error {
|
||||||
l.backup = l.rule.BackupFileName()
|
l.backup = l.rule.BackupFileName()
|
||||||
|
|
||||||
if _, err := os.Stat(l.filename); err != nil {
|
if fileInfo, err := os.Stat(l.filename); err != nil {
|
||||||
basePath := path.Dir(l.filename)
|
basePath := path.Dir(l.filename)
|
||||||
if _, err = os.Stat(basePath); err != nil {
|
if _, err = os.Stat(basePath); err != nil {
|
||||||
if err = os.MkdirAll(basePath, defaultDirMode); err != nil {
|
if err = os.MkdirAll(basePath, defaultDirMode); err != nil {
|
||||||
@@ -293,8 +295,11 @@ func (l *RotateLogger) init() error {
|
|||||||
if l.fp, err = os.Create(l.filename); err != nil {
|
if l.fp, err = os.Create(l.filename); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if l.fp, err = os.OpenFile(l.filename, os.O_APPEND|os.O_WRONLY, defaultFileMode); err != nil {
|
} else {
|
||||||
return err
|
if l.fp, err = os.OpenFile(l.filename, os.O_APPEND|os.O_WRONLY, defaultFileMode); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
l.currentSize = fileInfo.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.CloseOnExec(l.fp)
|
fs.CloseOnExec(l.fp)
|
||||||
|
|||||||
@@ -42,11 +42,18 @@ func captureOutput(f func()) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getContent(jsonStr string) string {
|
func getContent(jsonStr string) string {
|
||||||
var entry logEntry
|
var entry map[string]interface{}
|
||||||
json.Unmarshal([]byte(jsonStr), &entry)
|
json.Unmarshal([]byte(jsonStr), &entry)
|
||||||
val, ok := entry.Content.(string)
|
|
||||||
if ok {
|
val, ok := entry[contentKey]
|
||||||
return val
|
if !ok {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
|
str, ok := val.(string)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package logx
|
|||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// InfoLevel logs everything
|
// DebugLevel logs everything
|
||||||
InfoLevel uint32 = iota
|
DebugLevel uint32 = iota
|
||||||
|
// InfoLevel does not include debugs
|
||||||
|
InfoLevel
|
||||||
// ErrorLevel includes errors, slows, stacks
|
// ErrorLevel includes errors, slows, stacks
|
||||||
ErrorLevel
|
ErrorLevel
|
||||||
// SevereLevel only log severe messages
|
// SevereLevel only log severe messages
|
||||||
@@ -37,6 +39,7 @@ const (
|
|||||||
levelFatal = "fatal"
|
levelFatal = "fatal"
|
||||||
levelSlow = "slow"
|
levelSlow = "slow"
|
||||||
levelStat = "stat"
|
levelStat = "stat"
|
||||||
|
levelDebug = "debug"
|
||||||
|
|
||||||
backupFileDelimiter = "-"
|
backupFileDelimiter = "-"
|
||||||
flags = 0x0
|
flags = 0x0
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
package logx
|
package logx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
fatihcolor "github.com/fatih/color"
|
||||||
"github.com/zeromicro/go-zero/core/color"
|
"github.com/zeromicro/go-zero/core/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,6 +18,7 @@ type (
|
|||||||
Writer interface {
|
Writer interface {
|
||||||
Alert(v interface{})
|
Alert(v interface{})
|
||||||
Close() error
|
Close() error
|
||||||
|
Debug(v interface{}, fields ...LogField)
|
||||||
Error(v interface{}, fields ...LogField)
|
Error(v interface{}, fields ...LogField)
|
||||||
Info(v interface{}, fields ...LogField)
|
Info(v interface{}, fields ...LogField)
|
||||||
Severe(v interface{})
|
Severe(v interface{})
|
||||||
@@ -67,6 +68,17 @@ func (w *atomicWriter) Store(v Writer) {
|
|||||||
w.writer = v
|
w.writer = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *atomicWriter) StoreIfNil(v Writer) Writer {
|
||||||
|
w.lock.Lock()
|
||||||
|
defer w.lock.Unlock()
|
||||||
|
|
||||||
|
if w.writer == nil {
|
||||||
|
w.writer = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.writer
|
||||||
|
}
|
||||||
|
|
||||||
func (w *atomicWriter) Swap(v Writer) Writer {
|
func (w *atomicWriter) Swap(v Writer) Writer {
|
||||||
w.lock.Lock()
|
w.lock.Lock()
|
||||||
defer w.lock.Unlock()
|
defer w.lock.Unlock()
|
||||||
@@ -76,8 +88,8 @@ func (w *atomicWriter) Swap(v Writer) Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newConsoleWriter() Writer {
|
func newConsoleWriter() Writer {
|
||||||
outLog := newLogWriter(log.New(os.Stdout, "", flags))
|
outLog := newLogWriter(log.New(fatihcolor.Output, "", flags))
|
||||||
errLog := newLogWriter(log.New(os.Stderr, "", flags))
|
errLog := newLogWriter(log.New(fatihcolor.Error, "", flags))
|
||||||
return &concreteWriter{
|
return &concreteWriter{
|
||||||
infoLog: outLog,
|
infoLog: outLog,
|
||||||
errorLog: errLog,
|
errorLog: errLog,
|
||||||
@@ -183,6 +195,10 @@ func (w *concreteWriter) Close() error {
|
|||||||
return w.statLog.Close()
|
return w.statLog.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *concreteWriter) Debug(v interface{}, fields ...LogField) {
|
||||||
|
output(w.infoLog, levelDebug, v, fields...)
|
||||||
|
}
|
||||||
|
|
||||||
func (w *concreteWriter) Error(v interface{}, fields ...LogField) {
|
func (w *concreteWriter) Error(v interface{}, fields ...LogField) {
|
||||||
output(w.errorLog, levelError, v, fields...)
|
output(w.errorLog, levelError, v, fields...)
|
||||||
}
|
}
|
||||||
@@ -216,6 +232,9 @@ func (n nopWriter) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n nopWriter) Debug(_ interface{}, _ ...LogField) {
|
||||||
|
}
|
||||||
|
|
||||||
func (n nopWriter) Error(_ interface{}, _ ...LogField) {
|
func (n nopWriter) Error(_ interface{}, _ ...LogField) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,14 +263,23 @@ func buildFields(fields ...LogField) []string {
|
|||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func combineGlobalFields(fields []LogField) []LogField {
|
||||||
|
globals := globalFields.Load()
|
||||||
|
if globals == nil {
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|
||||||
|
return append(globals.([]LogField), fields...)
|
||||||
|
}
|
||||||
|
|
||||||
func output(writer io.Writer, level string, val interface{}, fields ...LogField) {
|
func output(writer io.Writer, level string, val interface{}, fields ...LogField) {
|
||||||
fields = append(fields, Field(callerKey, getCaller(callerDepth)))
|
fields = combineGlobalFields(fields)
|
||||||
|
|
||||||
switch atomic.LoadUint32(&encoding) {
|
switch atomic.LoadUint32(&encoding) {
|
||||||
case plainEncodingType:
|
case plainEncodingType:
|
||||||
writePlainAny(writer, level, val, buildFields(fields...)...)
|
writePlainAny(writer, level, val, buildFields(fields...)...)
|
||||||
default:
|
default:
|
||||||
entry := make(logEntryWithFields)
|
entry := make(logEntry)
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
entry[field.Key] = field.Value
|
entry[field.Key] = field.Value
|
||||||
}
|
}
|
||||||
@@ -275,6 +303,8 @@ func wrapLevelWithColor(level string) string {
|
|||||||
colour = color.FgBlue
|
colour = color.FgBlue
|
||||||
case levelSlow:
|
case levelSlow:
|
||||||
colour = color.FgYellow
|
colour = color.FgYellow
|
||||||
|
case levelDebug:
|
||||||
|
colour = color.FgYellow
|
||||||
case levelStat:
|
case levelStat:
|
||||||
colour = color.FgGreen
|
colour = color.FgGreen
|
||||||
}
|
}
|
||||||
@@ -307,34 +337,12 @@ func writePlainAny(writer io.Writer, level string, val interface{}, fields ...st
|
|||||||
case fmt.Stringer:
|
case fmt.Stringer:
|
||||||
writePlainText(writer, level, v.String(), fields...)
|
writePlainText(writer, level, v.String(), fields...)
|
||||||
default:
|
default:
|
||||||
var buf strings.Builder
|
writePlainValue(writer, level, v, fields...)
|
||||||
buf.WriteString(getTimestamp())
|
|
||||||
buf.WriteByte(plainEncodingSep)
|
|
||||||
buf.WriteString(level)
|
|
||||||
buf.WriteByte(plainEncodingSep)
|
|
||||||
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
|
||||||
log.Println(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range fields {
|
|
||||||
buf.WriteByte(plainEncodingSep)
|
|
||||||
buf.WriteString(item)
|
|
||||||
}
|
|
||||||
buf.WriteByte('\n')
|
|
||||||
if writer == nil {
|
|
||||||
log.Println(buf.String())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprint(writer, buf.String()); err != nil {
|
|
||||||
log.Println(err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
||||||
var buf strings.Builder
|
var buf bytes.Buffer
|
||||||
buf.WriteString(getTimestamp())
|
buf.WriteString(getTimestamp())
|
||||||
buf.WriteByte(plainEncodingSep)
|
buf.WriteByte(plainEncodingSep)
|
||||||
buf.WriteString(level)
|
buf.WriteString(level)
|
||||||
@@ -350,7 +358,33 @@ func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := fmt.Fprint(writer, buf.String()); err != nil {
|
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||||
|
log.Println(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writePlainValue(writer io.Writer, level string, val interface{}, fields ...string) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.WriteString(getTimestamp())
|
||||||
|
buf.WriteByte(plainEncodingSep)
|
||||||
|
buf.WriteString(level)
|
||||||
|
buf.WriteByte(plainEncodingSep)
|
||||||
|
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
||||||
|
log.Println(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range fields {
|
||||||
|
buf.WriteByte(plainEncodingSep)
|
||||||
|
buf.WriteString(item)
|
||||||
|
}
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
if writer == nil {
|
||||||
|
log.Println(buf.String())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ func TestNewWriter(t *testing.T) {
|
|||||||
w := NewWriter(&buf)
|
w := NewWriter(&buf)
|
||||||
w.Info(literal)
|
w.Info(literal)
|
||||||
assert.Contains(t, buf.String(), literal)
|
assert.Contains(t, buf.String(), literal)
|
||||||
|
buf.Reset()
|
||||||
|
w.Debug(literal)
|
||||||
|
assert.Contains(t, buf.String(), literal)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConsoleWriter(t *testing.T) {
|
func TestConsoleWriter(t *testing.T) {
|
||||||
@@ -97,6 +100,7 @@ func TestNopWriter(t *testing.T) {
|
|||||||
assert.NotPanics(t, func() {
|
assert.NotPanics(t, func() {
|
||||||
var w nopWriter
|
var w nopWriter
|
||||||
w.Alert("foo")
|
w.Alert("foo")
|
||||||
|
w.Debug("foo")
|
||||||
w.Error("foo")
|
w.Error("foo")
|
||||||
w.Info("foo")
|
w.Info("foo")
|
||||||
w.Severe("foo")
|
w.Severe("foo")
|
||||||
@@ -123,6 +127,12 @@ func TestWritePlainAny(t *testing.T) {
|
|||||||
writePlainAny(nil, levelInfo, "foo")
|
writePlainAny(nil, levelInfo, "foo")
|
||||||
assert.Contains(t, buf.String(), "foo")
|
assert.Contains(t, buf.String(), "foo")
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
writePlainAny(nil, levelDebug, make(chan int))
|
||||||
|
assert.Contains(t, buf.String(), "unsupported type")
|
||||||
|
writePlainAny(nil, levelDebug, 100)
|
||||||
|
assert.Contains(t, buf.String(), "100")
|
||||||
|
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
writePlainAny(nil, levelError, make(chan int))
|
writePlainAny(nil, levelError, make(chan int))
|
||||||
assert.Contains(t, buf.String(), "unsupported type")
|
assert.Contains(t, buf.String(), "unsupported type")
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ type (
|
|||||||
// use context and OptionalDep option to determine the value of Optional
|
// use context and OptionalDep option to determine the value of Optional
|
||||||
// nothing to do with context.Context
|
// nothing to do with context.Context
|
||||||
fieldOptionsWithContext struct {
|
fieldOptionsWithContext struct {
|
||||||
|
Inherit bool
|
||||||
FromString bool
|
FromString bool
|
||||||
Optional bool
|
Optional bool
|
||||||
Options []string
|
Options []string
|
||||||
Default string
|
Default string
|
||||||
|
EnvVar string
|
||||||
Range *numberRange
|
Range *numberRange
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,6 +42,10 @@ func (o *fieldOptionsWithContext) getDefault() (string, bool) {
|
|||||||
return o.Default, len(o.Default) > 0
|
return o.Default, len(o.Default) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *fieldOptionsWithContext) inherit() bool {
|
||||||
|
return o != nil && o.Inherit
|
||||||
|
}
|
||||||
|
|
||||||
func (o *fieldOptionsWithContext) optional() bool {
|
func (o *fieldOptionsWithContext) optional() bool {
|
||||||
return o != nil && o.Optional
|
return o != nil && o.Optional
|
||||||
}
|
}
|
||||||
@@ -101,5 +107,6 @@ func (o *fieldOptions) toOptionsWithContext(key string, m Valuer, fullName strin
|
|||||||
Optional: optional,
|
Optional: optional,
|
||||||
Options: o.Options,
|
Options: o.Options,
|
||||||
Default: o.Default,
|
Default: o.Default,
|
||||||
|
EnvVar: o.EnvVar,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,18 +11,26 @@ const jsonTagKey = "json"
|
|||||||
var jsonUnmarshaler = NewUnmarshaler(jsonTagKey)
|
var jsonUnmarshaler = NewUnmarshaler(jsonTagKey)
|
||||||
|
|
||||||
// UnmarshalJsonBytes unmarshals content into v.
|
// UnmarshalJsonBytes unmarshals content into v.
|
||||||
func UnmarshalJsonBytes(content []byte, v interface{}) error {
|
func UnmarshalJsonBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
|
||||||
return unmarshalJsonBytes(content, v, jsonUnmarshaler)
|
return unmarshalJsonBytes(content, v, getJsonUnmarshaler(opts...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJsonMap unmarshals content from m into v.
|
// UnmarshalJsonMap unmarshals content from m into v.
|
||||||
func UnmarshalJsonMap(m map[string]interface{}, v interface{}) error {
|
func UnmarshalJsonMap(m map[string]interface{}, v interface{}, opts ...UnmarshalOption) error {
|
||||||
return jsonUnmarshaler.Unmarshal(m, v)
|
return getJsonUnmarshaler(opts...).Unmarshal(m, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJsonReader unmarshals content from reader into v.
|
// UnmarshalJsonReader unmarshals content from reader into v.
|
||||||
func UnmarshalJsonReader(reader io.Reader, v interface{}) error {
|
func UnmarshalJsonReader(reader io.Reader, v interface{}, opts ...UnmarshalOption) error {
|
||||||
return unmarshalJsonReader(reader, v, jsonUnmarshaler)
|
return unmarshalJsonReader(reader, v, getJsonUnmarshaler(opts...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getJsonUnmarshaler(opts ...UnmarshalOption) *Unmarshaler {
|
||||||
|
if len(opts) > 0 {
|
||||||
|
return NewUnmarshaler(jsonTagKey, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonUnmarshaler
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error {
|
func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error {
|
||||||
|
|||||||
@@ -900,7 +900,9 @@ func TestUnmarshalMap(t *testing.T) {
|
|||||||
Any string `json:",optional"`
|
Any string `json:",optional"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UnmarshalJsonMap(m, &v)
|
err := UnmarshalJsonMap(m, &v, WithCanonicalKeyFunc(func(s string) string {
|
||||||
|
return s
|
||||||
|
}))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, len(v.Any) == 0)
|
assert.True(t, len(v.Any) == 0)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -261,6 +261,78 @@ func TestMarshal_RangeOut(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarshal_RangeIllegal(t *testing.T) {
|
||||||
|
tests := []interface{}{
|
||||||
|
struct {
|
||||||
|
Int int `json:"int,range=[3:1]"`
|
||||||
|
}{
|
||||||
|
Int: 2,
|
||||||
|
},
|
||||||
|
struct {
|
||||||
|
Int int `json:"int,range=(3:1]"`
|
||||||
|
}{
|
||||||
|
Int: 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
_, err := Marshal(test)
|
||||||
|
assert.Equal(t, err, errNumberRange)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshal_RangeLeftEqualsToRight(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
value interface{}
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "left inclusive, right inclusive",
|
||||||
|
value: struct {
|
||||||
|
Int int `json:"int,range=[2:2]"`
|
||||||
|
}{
|
||||||
|
Int: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "left inclusive, right exclusive",
|
||||||
|
value: struct {
|
||||||
|
Int int `json:"int,range=[2:2)"`
|
||||||
|
}{
|
||||||
|
Int: 2,
|
||||||
|
},
|
||||||
|
err: errNumberRange,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "left exclusive, right inclusive",
|
||||||
|
value: struct {
|
||||||
|
Int int `json:"int,range=(2:2]"`
|
||||||
|
}{
|
||||||
|
Int: 2,
|
||||||
|
},
|
||||||
|
err: errNumberRange,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "left exclusive, right exclusive",
|
||||||
|
value: struct {
|
||||||
|
Int int `json:"int,range=(2:2)"`
|
||||||
|
}{
|
||||||
|
Int: 2,
|
||||||
|
},
|
||||||
|
err: errNumberRange,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
_, err := Marshal(test.value)
|
||||||
|
assert.Equal(t, test.err, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMarshal_FromString(t *testing.T) {
|
func TestMarshal_FromString(t *testing.T) {
|
||||||
v := struct {
|
v := struct {
|
||||||
Age int `json:"age,string"`
|
Age int `json:"age,string"`
|
||||||
|
|||||||
@@ -1,29 +1,27 @@
|
|||||||
package mapping
|
package mapping
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/zeromicro/go-zero/internal/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnmarshalTomlBytes unmarshals TOML bytes into the given v.
|
// UnmarshalTomlBytes unmarshals TOML bytes into the given v.
|
||||||
func UnmarshalTomlBytes(content []byte, v interface{}) error {
|
func UnmarshalTomlBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
|
||||||
return UnmarshalTomlReader(bytes.NewReader(content), v)
|
b, err := encoding.TomlToJson(content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return UnmarshalJsonBytes(b, v, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalTomlReader unmarshals TOML from the given io.Reader into the given v.
|
// UnmarshalTomlReader unmarshals TOML from the given io.Reader into the given v.
|
||||||
func UnmarshalTomlReader(r io.Reader, v interface{}) error {
|
func UnmarshalTomlReader(r io.Reader, v interface{}, opts ...UnmarshalOption) error {
|
||||||
var val interface{}
|
b, err := io.ReadAll(r)
|
||||||
if err := toml.NewDecoder(r).Decode(&val); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
return UnmarshalTomlBytes(b, v, opts...)
|
||||||
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return UnmarshalJsonReader(&buf, v)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package mapping
|
package mapping
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -18,7 +19,7 @@ d = "abcd!@#$112"
|
|||||||
C string `json:"c"`
|
C string `json:"c"`
|
||||||
D string `json:"d"`
|
D string `json:"d"`
|
||||||
}
|
}
|
||||||
assert.Nil(t, UnmarshalTomlBytes([]byte(input), &val))
|
assert.NoError(t, UnmarshalTomlReader(strings.NewReader(input), &val))
|
||||||
assert.Equal(t, "foo", val.A)
|
assert.Equal(t, "foo", val.A)
|
||||||
assert.Equal(t, 1, val.B)
|
assert.Equal(t, 1, val.B)
|
||||||
assert.Equal(t, "${FOO}", val.C)
|
assert.Equal(t, "${FOO}", val.C)
|
||||||
@@ -37,5 +38,12 @@ d = "abcd!@#$112"
|
|||||||
C string `json:"c"`
|
C string `json:"c"`
|
||||||
D string `json:"d"`
|
D string `json:"d"`
|
||||||
}
|
}
|
||||||
assert.NotNil(t, UnmarshalTomlBytes([]byte(input), &val))
|
assert.Error(t, UnmarshalTomlReader(strings.NewReader(input), &val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalTomlBadReader(t *testing.T) {
|
||||||
|
var val struct {
|
||||||
|
A string `json:"a"`
|
||||||
|
}
|
||||||
|
assert.Error(t, UnmarshalTomlReader(new(badReader), &val))
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -10,11 +10,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/lang"
|
||||||
"github.com/zeromicro/go-zero/core/stringx"
|
"github.com/zeromicro/go-zero/core/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultOption = "default"
|
defaultOption = "default"
|
||||||
|
envOption = "env"
|
||||||
|
inheritOption = "inherit"
|
||||||
stringOption = "string"
|
stringOption = "string"
|
||||||
optionalOption = "optional"
|
optionalOption = "optional"
|
||||||
optionsOption = "options"
|
optionsOption = "options"
|
||||||
@@ -62,22 +65,7 @@ func Deref(t reflect.Type) reflect.Type {
|
|||||||
|
|
||||||
// Repr returns the string representation of v.
|
// Repr returns the string representation of v.
|
||||||
func Repr(v interface{}) string {
|
func Repr(v interface{}) string {
|
||||||
if v == nil {
|
return lang.Repr(v)
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// if func (v *Type) String() string, we can't use Elem()
|
|
||||||
switch vt := v.(type) {
|
|
||||||
case fmt.Stringer:
|
|
||||||
return vt.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
val := reflect.ValueOf(v)
|
|
||||||
if val.Kind() == reflect.Ptr && !val.IsNil() {
|
|
||||||
val = val.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
return reprOfValue(val)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidatePtr validates v if it's a valid pointer.
|
// ValidatePtr validates v if it's a valid pointer.
|
||||||
@@ -94,7 +82,14 @@ func ValidatePtr(v *reflect.Value) error {
|
|||||||
func convertType(kind reflect.Kind, str string) (interface{}, error) {
|
func convertType(kind reflect.Kind, str string) (interface{}, error) {
|
||||||
switch kind {
|
switch kind {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
return str == "1" || strings.ToLower(str) == "true", nil
|
switch strings.ToLower(str) {
|
||||||
|
case "1", "true":
|
||||||
|
return true, nil
|
||||||
|
case "0", "false":
|
||||||
|
return false, nil
|
||||||
|
default:
|
||||||
|
return false, errTypeMismatch
|
||||||
|
}
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
intValue, err := strconv.ParseInt(str, 10, 64)
|
intValue, err := strconv.ParseInt(str, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -215,8 +210,8 @@ func isRightInclude(b byte) (bool, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func maybeNewValue(field reflect.StructField, value reflect.Value) {
|
func maybeNewValue(fieldType reflect.Type, value reflect.Value) {
|
||||||
if field.Type.Kind() == reflect.Ptr && value.IsNil() {
|
if fieldType.Kind() == reflect.Ptr && value.IsNil() {
|
||||||
value.Set(reflect.New(value.Type().Elem()))
|
value.Set(reflect.New(value.Type().Elem()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -311,6 +306,20 @@ func parseNumberRange(str string) (*numberRange, error) {
|
|||||||
right = math.MaxFloat64
|
right = math.MaxFloat64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if left > right {
|
||||||
|
return nil, errNumberRange
|
||||||
|
}
|
||||||
|
|
||||||
|
// [2:2] valid
|
||||||
|
// [2:2) invalid
|
||||||
|
// (2:2] invalid
|
||||||
|
// (2:2) invalid
|
||||||
|
if left == right {
|
||||||
|
if !leftInclude || !rightInclude {
|
||||||
|
return nil, errNumberRange
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &numberRange{
|
return &numberRange{
|
||||||
left: left,
|
left: left,
|
||||||
leftInclude: leftInclude,
|
leftInclude: leftInclude,
|
||||||
@@ -321,6 +330,8 @@ func parseNumberRange(str string) (*numberRange, error) {
|
|||||||
|
|
||||||
func parseOption(fieldOpts *fieldOptions, fieldName, option string) error {
|
func parseOption(fieldOpts *fieldOptions, fieldName, option string) error {
|
||||||
switch {
|
switch {
|
||||||
|
case option == inheritOption:
|
||||||
|
fieldOpts.Inherit = true
|
||||||
case option == stringOption:
|
case option == stringOption:
|
||||||
fieldOpts.FromString = true
|
fieldOpts.FromString = true
|
||||||
case strings.HasPrefix(option, optionalOption):
|
case strings.HasPrefix(option, optionalOption):
|
||||||
@@ -337,26 +348,33 @@ func parseOption(fieldOpts *fieldOptions, fieldName, option string) error {
|
|||||||
case option == optionalOption:
|
case option == optionalOption:
|
||||||
fieldOpts.Optional = true
|
fieldOpts.Optional = true
|
||||||
case strings.HasPrefix(option, optionsOption):
|
case strings.HasPrefix(option, optionsOption):
|
||||||
segs := strings.Split(option, equalToken)
|
val, err := parseProperty(fieldName, optionsOption, option)
|
||||||
if len(segs) != 2 {
|
if err != nil {
|
||||||
return fmt.Errorf("field %s has wrong options", fieldName)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldOpts.Options = parseOptions(segs[1])
|
fieldOpts.Options = parseOptions(val)
|
||||||
case strings.HasPrefix(option, defaultOption):
|
case strings.HasPrefix(option, defaultOption):
|
||||||
segs := strings.Split(option, equalToken)
|
val, err := parseProperty(fieldName, defaultOption, option)
|
||||||
if len(segs) != 2 {
|
if err != nil {
|
||||||
return fmt.Errorf("field %s has wrong default option", fieldName)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldOpts.Default = strings.TrimSpace(segs[1])
|
fieldOpts.Default = val
|
||||||
|
case strings.HasPrefix(option, envOption):
|
||||||
|
val, err := parseProperty(fieldName, envOption, option)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldOpts.EnvVar = val
|
||||||
case strings.HasPrefix(option, rangeOption):
|
case strings.HasPrefix(option, rangeOption):
|
||||||
segs := strings.Split(option, equalToken)
|
val, err := parseProperty(fieldName, rangeOption, option)
|
||||||
if len(segs) != 2 {
|
if err != nil {
|
||||||
return fmt.Errorf("field %s has wrong range", fieldName)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
nr, err := parseNumberRange(segs[1])
|
nr, err := parseNumberRange(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -381,6 +399,15 @@ func parseOptions(val string) []string {
|
|||||||
return strings.Split(val, optionSeparator)
|
return strings.Split(val, optionSeparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseProperty(field, tag, val string) (string, error) {
|
||||||
|
segs := strings.Split(val, equalToken)
|
||||||
|
if len(segs) != 2 {
|
||||||
|
return "", fmt.Errorf("field %s has wrong %s", field, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(segs[1]), nil
|
||||||
|
}
|
||||||
|
|
||||||
func parseSegments(val string) []string {
|
func parseSegments(val string) []string {
|
||||||
var segments []string
|
var segments []string
|
||||||
var escaped, grouped bool
|
var escaped, grouped bool
|
||||||
@@ -430,47 +457,6 @@ func parseSegments(val string) []string {
|
|||||||
return segments
|
return segments
|
||||||
}
|
}
|
||||||
|
|
||||||
func reprOfValue(val reflect.Value) string {
|
|
||||||
switch vt := val.Interface().(type) {
|
|
||||||
case bool:
|
|
||||||
return strconv.FormatBool(vt)
|
|
||||||
case error:
|
|
||||||
return vt.Error()
|
|
||||||
case float32:
|
|
||||||
return strconv.FormatFloat(float64(vt), 'f', -1, 32)
|
|
||||||
case float64:
|
|
||||||
return strconv.FormatFloat(vt, 'f', -1, 64)
|
|
||||||
case fmt.Stringer:
|
|
||||||
return vt.String()
|
|
||||||
case int:
|
|
||||||
return strconv.Itoa(vt)
|
|
||||||
case int8:
|
|
||||||
return strconv.Itoa(int(vt))
|
|
||||||
case int16:
|
|
||||||
return strconv.Itoa(int(vt))
|
|
||||||
case int32:
|
|
||||||
return strconv.Itoa(int(vt))
|
|
||||||
case int64:
|
|
||||||
return strconv.FormatInt(vt, 10)
|
|
||||||
case string:
|
|
||||||
return vt
|
|
||||||
case uint:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint8:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint16:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint32:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint64:
|
|
||||||
return strconv.FormatUint(vt, 10)
|
|
||||||
case []byte:
|
|
||||||
return string(vt)
|
|
||||||
default:
|
|
||||||
return fmt.Sprint(val.Interface())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v interface{}) error {
|
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v interface{}) error {
|
||||||
switch kind {
|
switch kind {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
|
|||||||
@@ -296,127 +296,3 @@ func TestSetValueFormatErrors(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepr(t *testing.T) {
|
|
||||||
var (
|
|
||||||
f32 float32 = 1.1
|
|
||||||
f64 = 2.2
|
|
||||||
i8 int8 = 1
|
|
||||||
i16 int16 = 2
|
|
||||||
i32 int32 = 3
|
|
||||||
i64 int64 = 4
|
|
||||||
u8 uint8 = 5
|
|
||||||
u16 uint16 = 6
|
|
||||||
u32 uint32 = 7
|
|
||||||
u64 uint64 = 8
|
|
||||||
)
|
|
||||||
tests := []struct {
|
|
||||||
v interface{}
|
|
||||||
expect string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
nil,
|
|
||||||
"",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
mockStringable{},
|
|
||||||
"mocked",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
new(mockStringable),
|
|
||||||
"mocked",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
newMockPtr(),
|
|
||||||
"mockptr",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
&mockOpacity{
|
|
||||||
val: 1,
|
|
||||||
},
|
|
||||||
"{1}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
true,
|
|
||||||
"true",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
false,
|
|
||||||
"false",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
f32,
|
|
||||||
"1.1",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
f64,
|
|
||||||
"2.2",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
i8,
|
|
||||||
"1",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
i16,
|
|
||||||
"2",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
i32,
|
|
||||||
"3",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
i64,
|
|
||||||
"4",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
u8,
|
|
||||||
"5",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
u16,
|
|
||||||
"6",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
u32,
|
|
||||||
"7",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
u64,
|
|
||||||
"8",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
[]byte(`abcd`),
|
|
||||||
"abcd",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
mockOpacity{val: 1},
|
|
||||||
"{1}",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.expect, func(t *testing.T) {
|
|
||||||
assert.Equal(t, test.expect, Repr(test.v))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockStringable struct{}
|
|
||||||
|
|
||||||
func (m mockStringable) String() string {
|
|
||||||
return "mocked"
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockPtr struct{}
|
|
||||||
|
|
||||||
func newMockPtr() *mockPtr {
|
|
||||||
return new(mockPtr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockPtr) String() string {
|
|
||||||
return "mockptr"
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockOpacity struct {
|
|
||||||
val int
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,12 +7,106 @@ type (
|
|||||||
Value(key string) (interface{}, bool)
|
Value(key string) (interface{}, bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A MapValuer is a map that can use Value method to get values with given keys.
|
// A valuerWithParent defines a node that has a parent node.
|
||||||
MapValuer map[string]interface{}
|
valuerWithParent interface {
|
||||||
|
Valuer
|
||||||
|
// Parent get the parent valuer for current node.
|
||||||
|
Parent() valuerWithParent
|
||||||
|
}
|
||||||
|
|
||||||
|
// A node is a map that can use Value method to get values with given keys.
|
||||||
|
node struct {
|
||||||
|
current Valuer
|
||||||
|
parent valuerWithParent
|
||||||
|
}
|
||||||
|
|
||||||
|
// A valueWithParent is used to wrap the value with its parent.
|
||||||
|
valueWithParent struct {
|
||||||
|
value interface{}
|
||||||
|
parent valuerWithParent
|
||||||
|
}
|
||||||
|
|
||||||
|
// mapValuer is a type for map to meet the Valuer interface.
|
||||||
|
mapValuer map[string]interface{}
|
||||||
|
// simpleValuer is a type to get value from current node.
|
||||||
|
simpleValuer node
|
||||||
|
// recursiveValuer is a type to get the value recursively from current and parent nodes.
|
||||||
|
recursiveValuer node
|
||||||
)
|
)
|
||||||
|
|
||||||
// Value gets the value associated with the given key from mv.
|
// Value gets the value assciated with the given key from mv.
|
||||||
func (mv MapValuer) Value(key string) (interface{}, bool) {
|
func (mv mapValuer) Value(key string) (interface{}, bool) {
|
||||||
v, ok := mv[key]
|
v, ok := mv[key]
|
||||||
return v, ok
|
return v, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Value gets the value associated with the given key from sv.
|
||||||
|
func (sv simpleValuer) Value(key string) (interface{}, bool) {
|
||||||
|
v, ok := sv.current.Value(key)
|
||||||
|
return v, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parent get the parent valuer from sv.
|
||||||
|
func (sv simpleValuer) Parent() valuerWithParent {
|
||||||
|
if sv.parent == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return recursiveValuer{
|
||||||
|
current: sv.parent,
|
||||||
|
parent: sv.parent.Parent(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value gets the value associated with the given key from rv,
|
||||||
|
// and it will inherit the value from parent nodes.
|
||||||
|
func (rv recursiveValuer) Value(key string) (interface{}, bool) {
|
||||||
|
val, ok := rv.current.Value(key)
|
||||||
|
if !ok {
|
||||||
|
if parent := rv.Parent(); parent != nil {
|
||||||
|
return parent.Value(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
vm, ok := val.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return val, true
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := rv.Parent()
|
||||||
|
if parent == nil {
|
||||||
|
return val, true
|
||||||
|
}
|
||||||
|
|
||||||
|
pv, ok := parent.Value(key)
|
||||||
|
if !ok {
|
||||||
|
return val, true
|
||||||
|
}
|
||||||
|
|
||||||
|
pm, ok := pv.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return val, true
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range pm {
|
||||||
|
if _, ok := vm[k]; !ok {
|
||||||
|
vm[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return vm, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parent get the parent valuer from rv.
|
||||||
|
func (rv recursiveValuer) Parent() valuerWithParent {
|
||||||
|
if rv.parent == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return recursiveValuer{
|
||||||
|
current: rv.parent,
|
||||||
|
parent: rv.parent.Parent(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
33
core/mapping/valuer_test.go
Normal file
33
core/mapping/valuer_test.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package mapping
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMapValuerWithInherit_Value(t *testing.T) {
|
||||||
|
input := map[string]interface{}{
|
||||||
|
"discovery": map[string]interface{}{
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 8080,
|
||||||
|
},
|
||||||
|
"component": map[string]interface{}{
|
||||||
|
"name": "test",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
valuer := recursiveValuer{
|
||||||
|
current: mapValuer(input["component"].(map[string]interface{})),
|
||||||
|
parent: simpleValuer{
|
||||||
|
current: mapValuer(input),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
val, ok := valuer.Value("discovery")
|
||||||
|
assert.True(t, ok)
|
||||||
|
|
||||||
|
m, ok := val.(map[string]interface{})
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, "localhost", m["host"])
|
||||||
|
assert.Equal(t, 8080, m["port"])
|
||||||
|
}
|
||||||
@@ -1,101 +1,27 @@
|
|||||||
package mapping
|
package mapping
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"github.com/zeromicro/go-zero/internal/encoding"
|
||||||
)
|
|
||||||
|
|
||||||
// To make .json & .yaml consistent, we just use json as the tag key.
|
|
||||||
const yamlTagKey = "json"
|
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrUnsupportedType is an error that indicates the config format is not supported.
|
|
||||||
ErrUnsupportedType = errors.New("only map-like configs are supported")
|
|
||||||
|
|
||||||
yamlUnmarshaler = NewUnmarshaler(yamlTagKey)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnmarshalYamlBytes unmarshals content into v.
|
// UnmarshalYamlBytes unmarshals content into v.
|
||||||
func UnmarshalYamlBytes(content []byte, v interface{}) error {
|
func UnmarshalYamlBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
|
||||||
return unmarshalYamlBytes(content, v, yamlUnmarshaler)
|
b, err := encoding.YamlToJson(content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return UnmarshalJsonBytes(b, v, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalYamlReader unmarshals content from reader into v.
|
// UnmarshalYamlReader unmarshals content from reader into v.
|
||||||
func UnmarshalYamlReader(reader io.Reader, v interface{}) error {
|
func UnmarshalYamlReader(reader io.Reader, v interface{}, opts ...UnmarshalOption) error {
|
||||||
return unmarshalYamlReader(reader, v, yamlUnmarshaler)
|
b, err := io.ReadAll(reader)
|
||||||
}
|
if err != nil {
|
||||||
|
|
||||||
func cleanupInterfaceMap(in map[interface{}]interface{}) map[string]interface{} {
|
|
||||||
res := make(map[string]interface{})
|
|
||||||
for k, v := range in {
|
|
||||||
res[Repr(k)] = cleanupMapValue(v)
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanupInterfaceNumber(in interface{}) json.Number {
|
|
||||||
return json.Number(Repr(in))
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanupInterfaceSlice(in []interface{}) []interface{} {
|
|
||||||
res := make([]interface{}, len(in))
|
|
||||||
for i, v := range in {
|
|
||||||
res[i] = cleanupMapValue(v)
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanupMapValue(v interface{}) interface{} {
|
|
||||||
switch v := v.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
return cleanupInterfaceSlice(v)
|
|
||||||
case map[interface{}]interface{}:
|
|
||||||
return cleanupInterfaceMap(v)
|
|
||||||
case bool, string:
|
|
||||||
return v
|
|
||||||
case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64:
|
|
||||||
return cleanupInterfaceNumber(v)
|
|
||||||
default:
|
|
||||||
return Repr(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshal(unmarshaler *Unmarshaler, o interface{}, v interface{}) error {
|
|
||||||
if m, ok := o.(map[string]interface{}); ok {
|
|
||||||
return unmarshaler.Unmarshal(m, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ErrUnsupportedType
|
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshalYamlBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error {
|
|
||||||
var o interface{}
|
|
||||||
if err := yamlUnmarshal(content, &o); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return unmarshal(unmarshaler, o, v)
|
return UnmarshalYamlBytes(b, v, opts...)
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshalYamlReader(reader io.Reader, v interface{}, unmarshaler *Unmarshaler) error {
|
|
||||||
var res interface{}
|
|
||||||
if err := yaml.NewDecoder(reader).Decode(&res); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return unmarshal(unmarshaler, cleanupMapValue(res), v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// yamlUnmarshal YAML to map[string]interface{} instead of map[interface{}]interface{}.
|
|
||||||
func yamlUnmarshal(in []byte, out interface{}) error {
|
|
||||||
var res interface{}
|
|
||||||
if err := yaml.Unmarshal(in, &res); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
*out.(*interface{}) = cleanupMapValue(res)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -934,9 +934,8 @@ func TestUnmarshalYamlReaderError(t *testing.T) {
|
|||||||
err := UnmarshalYamlReader(reader, &v)
|
err := UnmarshalYamlReader(reader, &v)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
reader = strings.NewReader("chenquan")
|
reader = strings.NewReader("foo")
|
||||||
err = UnmarshalYamlReader(reader, &v)
|
assert.Error(t, UnmarshalYamlReader(reader, &v))
|
||||||
assert.ErrorIs(t, err, ErrUnsupportedType)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalYamlBadReader(t *testing.T) {
|
func TestUnmarshalYamlBadReader(t *testing.T) {
|
||||||
@@ -1012,6 +1011,13 @@ func TestUnmarshalYamlMapRune(t *testing.T) {
|
|||||||
assert.Equal(t, rune(3), v.Machine["node3"])
|
assert.Equal(t, rune(3), v.Machine["node3"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalYamlBadInput(t *testing.T) {
|
||||||
|
var v struct {
|
||||||
|
Any string
|
||||||
|
}
|
||||||
|
assert.Error(t, UnmarshalYamlBytes([]byte("':foo"), &v))
|
||||||
|
}
|
||||||
|
|
||||||
type badReader struct{}
|
type badReader struct{}
|
||||||
|
|
||||||
func (b *badReader) Read(_ []byte) (n int, err error) {
|
func (b *badReader) Read(_ []byte) (n int, err error) {
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Unstable is used to generate random value around the mean value base on given deviation.
|
// An Unstable is used to generate random value around the mean value base on given deviation.
|
||||||
type Unstable struct {
|
type Unstable struct {
|
||||||
deviation float64
|
deviation float64
|
||||||
r *rand.Rand
|
r *rand.Rand
|
||||||
lock *sync.Mutex
|
lock *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUnstable returns a Unstable.
|
// NewUnstable returns an Unstable.
|
||||||
func NewUnstable(deviation float64) Unstable {
|
func NewUnstable(deviation float64) Unstable {
|
||||||
if deviation < 0 {
|
if deviation < 0 {
|
||||||
deviation = 0
|
deviation = 0
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package metric
|
|||||||
import (
|
import (
|
||||||
prom "github.com/prometheus/client_golang/prometheus"
|
prom "github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/zeromicro/go-zero/core/proc"
|
"github.com/zeromicro/go-zero/core/proc"
|
||||||
|
"github.com/zeromicro/go-zero/core/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -47,10 +48,18 @@ func NewCounterVec(cfg *CounterVecOpts) CounterVec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cv *promCounterVec) Inc(labels ...string) {
|
func (cv *promCounterVec) Inc(labels ...string) {
|
||||||
|
if !prometheus.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
cv.counter.WithLabelValues(labels...).Inc()
|
cv.counter.WithLabelValues(labels...).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cv *promCounterVec) Add(v float64, labels ...string) {
|
func (cv *promCounterVec) Add(v float64, labels ...string) {
|
||||||
|
if !prometheus.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
cv.counter.WithLabelValues(labels...).Add(v)
|
cv.counter.WithLabelValues(labels...).Add(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus/testutil"
|
"github.com/prometheus/client_golang/prometheus/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/core/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewCounterVec(t *testing.T) {
|
func TestNewCounterVec(t *testing.T) {
|
||||||
@@ -21,6 +22,7 @@ func TestNewCounterVec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCounterIncr(t *testing.T) {
|
func TestCounterIncr(t *testing.T) {
|
||||||
|
startAgent()
|
||||||
counterVec := NewCounterVec(&CounterVecOpts{
|
counterVec := NewCounterVec(&CounterVecOpts{
|
||||||
Namespace: "http_client",
|
Namespace: "http_client",
|
||||||
Subsystem: "call",
|
Subsystem: "call",
|
||||||
@@ -37,6 +39,7 @@ func TestCounterIncr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCounterAdd(t *testing.T) {
|
func TestCounterAdd(t *testing.T) {
|
||||||
|
startAgent()
|
||||||
counterVec := NewCounterVec(&CounterVecOpts{
|
counterVec := NewCounterVec(&CounterVecOpts{
|
||||||
Namespace: "rpc_server",
|
Namespace: "rpc_server",
|
||||||
Subsystem: "requests",
|
Subsystem: "requests",
|
||||||
@@ -51,3 +54,11 @@ func TestCounterAdd(t *testing.T) {
|
|||||||
r := testutil.ToFloat64(cv.counter)
|
r := testutil.ToFloat64(cv.counter)
|
||||||
assert.Equal(t, float64(33), r)
|
assert.Equal(t, float64(33), r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startAgent() {
|
||||||
|
prometheus.StartAgent(prometheus.Config{
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Port: 9101,
|
||||||
|
Path: "/metrics",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package metric
|
|||||||
import (
|
import (
|
||||||
prom "github.com/prometheus/client_golang/prometheus"
|
prom "github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/zeromicro/go-zero/core/proc"
|
"github.com/zeromicro/go-zero/core/proc"
|
||||||
|
"github.com/zeromicro/go-zero/core/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -50,14 +51,26 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gv *promGaugeVec) Inc(labels ...string) {
|
func (gv *promGaugeVec) Inc(labels ...string) {
|
||||||
|
if !prometheus.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
gv.gauge.WithLabelValues(labels...).Inc()
|
gv.gauge.WithLabelValues(labels...).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gv *promGaugeVec) Add(v float64, labels ...string) {
|
func (gv *promGaugeVec) Add(v float64, labels ...string) {
|
||||||
|
if !prometheus.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
gv.gauge.WithLabelValues(labels...).Add(v)
|
gv.gauge.WithLabelValues(labels...).Add(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gv *promGaugeVec) Set(v float64, labels ...string) {
|
func (gv *promGaugeVec) Set(v float64, labels ...string) {
|
||||||
|
if !prometheus.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
gv.gauge.WithLabelValues(labels...).Set(v)
|
gv.gauge.WithLabelValues(labels...).Set(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ func TestNewGaugeVec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGaugeInc(t *testing.T) {
|
func TestGaugeInc(t *testing.T) {
|
||||||
|
startAgent()
|
||||||
gaugeVec := NewGaugeVec(&GaugeVecOpts{
|
gaugeVec := NewGaugeVec(&GaugeVecOpts{
|
||||||
Namespace: "rpc_client2",
|
Namespace: "rpc_client2",
|
||||||
Subsystem: "requests",
|
Subsystem: "requests",
|
||||||
@@ -37,6 +38,7 @@ func TestGaugeInc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGaugeAdd(t *testing.T) {
|
func TestGaugeAdd(t *testing.T) {
|
||||||
|
startAgent()
|
||||||
gaugeVec := NewGaugeVec(&GaugeVecOpts{
|
gaugeVec := NewGaugeVec(&GaugeVecOpts{
|
||||||
Namespace: "rpc_client",
|
Namespace: "rpc_client",
|
||||||
Subsystem: "request",
|
Subsystem: "request",
|
||||||
@@ -53,6 +55,7 @@ func TestGaugeAdd(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGaugeSet(t *testing.T) {
|
func TestGaugeSet(t *testing.T) {
|
||||||
|
startAgent()
|
||||||
gaugeVec := NewGaugeVec(&GaugeVecOpts{
|
gaugeVec := NewGaugeVec(&GaugeVecOpts{
|
||||||
Namespace: "http_client",
|
Namespace: "http_client",
|
||||||
Subsystem: "request",
|
Subsystem: "request",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package metric
|
|||||||
import (
|
import (
|
||||||
prom "github.com/prometheus/client_golang/prometheus"
|
prom "github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/zeromicro/go-zero/core/proc"
|
"github.com/zeromicro/go-zero/core/proc"
|
||||||
|
"github.com/zeromicro/go-zero/core/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -53,6 +54,10 @@ func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hv *promHistogramVec) Observe(v int64, labels ...string) {
|
func (hv *promHistogramVec) Observe(v int64, labels ...string) {
|
||||||
|
if !prometheus.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
hv.histogram.WithLabelValues(labels...).Observe(float64(v))
|
hv.histogram.WithLabelValues(labels...).Observe(float64(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ func TestNewHistogramVec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHistogramObserve(t *testing.T) {
|
func TestHistogramObserve(t *testing.T) {
|
||||||
|
startAgent()
|
||||||
histogramVec := NewHistogramVec(&HistogramVecOpts{
|
histogramVec := NewHistogramVec(&HistogramVecOpts{
|
||||||
Name: "counts",
|
Name: "counts",
|
||||||
Help: "rpc server requests duration(ms).",
|
Help: "rpc server requests duration(ms).",
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ func MapReduceChan(source <-chan interface{}, mapper MapperFunc, reducer Reducer
|
|||||||
return mapReduceWithPanicChan(source, panicChan, mapper, reducer, opts...)
|
return mapReduceWithPanicChan(source, panicChan, mapper, reducer, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MapReduceChan maps all elements from source, and reduce the output elements with given reducer.
|
// mapReduceWithPanicChan maps all elements from source, and reduce the output elements with given reducer.
|
||||||
func mapReduceWithPanicChan(source <-chan interface{}, panicChan *onceChan, mapper MapperFunc,
|
func mapReduceWithPanicChan(source <-chan interface{}, panicChan *onceChan, mapper MapperFunc,
|
||||||
reducer ReducerFunc, opts ...Option) (interface{}, error) {
|
reducer ReducerFunc, opts ...Option) (interface{}, error) {
|
||||||
options := buildOptions(opts...)
|
options := buildOptions(opts...)
|
||||||
@@ -212,6 +212,8 @@ func mapReduceWithPanicChan(source <-chan interface{}, panicChan *onceChan, mapp
|
|||||||
cancel(context.DeadlineExceeded)
|
cancel(context.DeadlineExceeded)
|
||||||
return nil, context.DeadlineExceeded
|
return nil, context.DeadlineExceeded
|
||||||
case v := <-panicChan.channel:
|
case v := <-panicChan.channel:
|
||||||
|
// drain output here, otherwise for loop panic in defer
|
||||||
|
drain(output)
|
||||||
panic(v)
|
panic(v)
|
||||||
case v, ok := <-output:
|
case v, ok := <-output:
|
||||||
if err := retErr.Load(); err != nil {
|
if err := retErr.Load(); err != nil {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func FuzzMapReduce(f *testing.F) {
|
|||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
f.Add(uint(10), uint(runtime.NumCPU()))
|
f.Add(uint(10), uint(runtime.NumCPU()))
|
||||||
f.Fuzz(func(t *testing.T, num uint, workers uint) {
|
f.Fuzz(func(t *testing.T, num, workers uint) {
|
||||||
n := int64(num)%5000 + 5000
|
n := int64(num)%5000 + 5000
|
||||||
genPanic := rand.Intn(100) == 0
|
genPanic := rand.Intn(100) == 0
|
||||||
mapperPanic := rand.Intn(100) == 0
|
mapperPanic := rand.Intn(100) == 0
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package mr
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
var errDummy = errors.New("dummy")
|
var errDummy = errors.New("dummy")
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(io.Discard)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFinish(t *testing.T) {
|
func TestFinish(t *testing.T) {
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import "github.com/zeromicro/go-zero/core/logx"
|
|||||||
|
|
||||||
// Recover is used with defer to do cleanup on panics.
|
// Recover is used with defer to do cleanup on panics.
|
||||||
// Use it like:
|
// Use it like:
|
||||||
// defer Recover(func() {})
|
//
|
||||||
|
// defer Recover(func() {})
|
||||||
func Recover(cleanups ...func()) {
|
func Recover(cleanups ...func()) {
|
||||||
for _, cleanup := range cleanups {
|
for _, cleanup := range cleanups {
|
||||||
cleanup()
|
cleanup()
|
||||||
|
|||||||
@@ -5,9 +5,11 @@ import (
|
|||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/load"
|
"github.com/zeromicro/go-zero/core/load"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
"github.com/zeromicro/go-zero/core/proc"
|
||||||
"github.com/zeromicro/go-zero/core/prometheus"
|
"github.com/zeromicro/go-zero/core/prometheus"
|
||||||
"github.com/zeromicro/go-zero/core/stat"
|
"github.com/zeromicro/go-zero/core/stat"
|
||||||
"github.com/zeromicro/go-zero/core/trace"
|
"github.com/zeromicro/go-zero/core/trace"
|
||||||
|
"github.com/zeromicro/go-zero/internal/devserver"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -27,10 +29,12 @@ const (
|
|||||||
type ServiceConf struct {
|
type ServiceConf struct {
|
||||||
Name string
|
Name string
|
||||||
Log logx.LogConf
|
Log logx.LogConf
|
||||||
Mode string `json:",default=pro,options=dev|test|rt|pre|pro"`
|
Mode string `json:",default=pro,options=dev|test|rt|pre|pro"`
|
||||||
MetricsUrl string `json:",optional"`
|
MetricsUrl string `json:",optional"`
|
||||||
|
// Deprecated: please use DevServer
|
||||||
Prometheus prometheus.Config `json:",optional"`
|
Prometheus prometheus.Config `json:",optional"`
|
||||||
Telemetry trace.Config `json:",optional"`
|
Telemetry trace.Config `json:",optional"`
|
||||||
|
DevServer devserver.Config `json:",optional"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustSetUp sets up the service, exits on error.
|
// MustSetUp sets up the service, exits on error.
|
||||||
@@ -56,10 +60,14 @@ func (sc ServiceConf) SetUp() error {
|
|||||||
sc.Telemetry.Name = sc.Name
|
sc.Telemetry.Name = sc.Name
|
||||||
}
|
}
|
||||||
trace.StartAgent(sc.Telemetry)
|
trace.StartAgent(sc.Telemetry)
|
||||||
|
proc.AddShutdownListener(func() {
|
||||||
|
trace.StopAgent()
|
||||||
|
})
|
||||||
|
|
||||||
if len(sc.MetricsUrl) > 0 {
|
if len(sc.MetricsUrl) > 0 {
|
||||||
stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl))
|
stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl))
|
||||||
}
|
}
|
||||||
|
devserver.StartAgent(sc.DevServer)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,14 +46,14 @@ func Report(msg string) {
|
|||||||
if fn != nil {
|
if fn != nil {
|
||||||
reported := lessExecutor.DoOrDiscard(func() {
|
reported := lessExecutor.DoOrDiscard(func() {
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
fmt.Fprintf(&builder, "%s\n", time.Now().Format(timeFormat))
|
builder.WriteString(fmt.Sprintln(time.Now().Format(timeFormat)))
|
||||||
if len(clusterName) > 0 {
|
if len(clusterName) > 0 {
|
||||||
fmt.Fprintf(&builder, "cluster: %s\n", clusterName)
|
builder.WriteString(fmt.Sprintf("cluster: %s\n", clusterName))
|
||||||
}
|
}
|
||||||
fmt.Fprintf(&builder, "host: %s\n", sysx.Hostname())
|
builder.WriteString(fmt.Sprintf("host: %s\n", sysx.Hostname()))
|
||||||
dp := atomic.SwapInt32(&dropped, 0)
|
dp := atomic.SwapInt32(&dropped, 0)
|
||||||
if dp > 0 {
|
if dp > 0 {
|
||||||
fmt.Fprintf(&builder, "dropped: %d\n", dp)
|
builder.WriteString(fmt.Sprintf("dropped: %d\n", dp))
|
||||||
}
|
}
|
||||||
builder.WriteString(strings.TrimSpace(msg))
|
builder.WriteString(strings.TrimSpace(msg))
|
||||||
fn(builder.String())
|
fn(builder.String())
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package stat
|
package stat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -12,6 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestReport(t *testing.T) {
|
func TestReport(t *testing.T) {
|
||||||
|
os.Setenv(clusterNameKey, "test-cluster")
|
||||||
|
defer os.Unsetenv(clusterNameKey)
|
||||||
|
|
||||||
var count int32
|
var count int32
|
||||||
SetReporter(func(s string) {
|
SetReporter(func(s string) {
|
||||||
atomic.AddInt32(&count, 1)
|
atomic.AddInt32(&count, 1)
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ func parseUints(val string) ([]uint64, error) {
|
|||||||
return sets, nil
|
return sets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// runningInUserNS detects whether we are currently running in a user namespace.
|
// runningInUserNS detects whether we are currently running in an user namespace.
|
||||||
func runningInUserNS() bool {
|
func runningInUserNS() bool {
|
||||||
nsOnce.Do(func() {
|
nsOnce.Do(func() {
|
||||||
file, err := os.Open("/proc/self/uid_map")
|
file, err := os.Open("/proc/self/uid_map")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/iox"
|
"github.com/zeromicro/go-zero/core/iox"
|
||||||
@@ -20,10 +21,11 @@ var (
|
|||||||
preTotal uint64
|
preTotal uint64
|
||||||
quota float64
|
quota float64
|
||||||
cores uint64
|
cores uint64
|
||||||
|
initOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
// if /proc not present, ignore the cpu calculation, like wsl linux
|
// if /proc not present, ignore the cpu calculation, like wsl linux
|
||||||
func init() {
|
func initialize() {
|
||||||
cpus, err := cpuSets()
|
cpus, err := cpuSets()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Error(err)
|
logx.Error(err)
|
||||||
@@ -31,13 +33,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cores = uint64(len(cpus))
|
cores = uint64(len(cpus))
|
||||||
sets, err := cpuSets()
|
quota = float64(len(cpus))
|
||||||
if err != nil {
|
|
||||||
logx.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
quota = float64(len(sets))
|
|
||||||
cq, err := cpuQuota()
|
cq, err := cpuQuota()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if cq != -1 {
|
if cq != -1 {
|
||||||
@@ -69,10 +65,13 @@ func init() {
|
|||||||
|
|
||||||
// RefreshCpu refreshes cpu usage and returns.
|
// RefreshCpu refreshes cpu usage and returns.
|
||||||
func RefreshCpu() uint64 {
|
func RefreshCpu() uint64 {
|
||||||
|
initOnce.Do(initialize)
|
||||||
|
|
||||||
total, err := totalCpuUsage()
|
total, err := totalCpuUsage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
system, err := systemCpuUsage()
|
system, err := systemCpuUsage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
6
core/stores/cache/cachenode.go
vendored
6
core/stores/cache/cachenode.go
vendored
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -130,7 +131,7 @@ func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val interfa
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.rds.SetexCtx(ctx, key, string(data), int(expire.Seconds()))
|
return c.rds.SetexCtx(ctx, key, string(data), int(math.Ceil(expire.Seconds())))
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a string that represents the cacheNode.
|
// String returns a string that represents the cacheNode.
|
||||||
@@ -275,5 +276,6 @@ func (c cacheNode) processCache(ctx context.Context, key, data string, v interfa
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c cacheNode) setCacheWithNotFound(ctx context.Context, key string) error {
|
func (c cacheNode) setCacheWithNotFound(ctx context.Context, key string) error {
|
||||||
return c.rds.SetexCtx(ctx, key, notFoundPlaceholder, int(c.aroundDuration(c.notFoundExpiry).Seconds()))
|
seconds := int(math.Ceil(c.aroundDuration(c.notFoundExpiry).Seconds()))
|
||||||
|
return c.rds.SetexCtx(ctx, key, notFoundPlaceholder, seconds)
|
||||||
}
|
}
|
||||||
|
|||||||
4
core/stores/cache/cacheopt.go
vendored
4
core/stores/cache/cacheopt.go
vendored
@@ -34,14 +34,14 @@ func newOptions(opts ...Option) Options {
|
|||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithExpiry returns a func to customize a Options with given expiry.
|
// WithExpiry returns a func to customize an Options with given expiry.
|
||||||
func WithExpiry(expiry time.Duration) Option {
|
func WithExpiry(expiry time.Duration) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Expiry = expiry
|
o.Expiry = expiry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithNotFoundExpiry returns a func to customize a Options with given not found expiry.
|
// WithNotFoundExpiry returns a func to customize an Options with given not found expiry.
|
||||||
func WithNotFoundExpiry(expiry time.Duration) Option {
|
func WithNotFoundExpiry(expiry time.Duration) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.NotFoundExpiry = expiry
|
o.NotFoundExpiry = expiry
|
||||||
|
|||||||
@@ -110,7 +110,9 @@ type (
|
|||||||
Ttl(key string) (int, error)
|
Ttl(key string) (int, error)
|
||||||
TtlCtx(ctx context.Context, key string) (int, error)
|
TtlCtx(ctx context.Context, key string) (int, error)
|
||||||
Zadd(key string, score int64, value string) (bool, error)
|
Zadd(key string, score int64, value string) (bool, error)
|
||||||
|
ZaddFloat(key string, score float64, value string) (bool, error)
|
||||||
ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error)
|
ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error)
|
||||||
|
ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (bool, error)
|
||||||
Zadds(key string, ps ...redis.Pair) (int64, error)
|
Zadds(key string, ps ...redis.Pair) (int64, error)
|
||||||
ZaddsCtx(ctx context.Context, key string, ps ...redis.Pair) (int64, error)
|
ZaddsCtx(ctx context.Context, key string, ps ...redis.Pair) (int64, error)
|
||||||
Zcard(key string) (int, error)
|
Zcard(key string) (int, error)
|
||||||
@@ -787,13 +789,21 @@ func (cs clusterStore) Zadd(key string, score int64, value string) (bool, error)
|
|||||||
return cs.ZaddCtx(context.Background(), key, score, value)
|
return cs.ZaddCtx(context.Background(), key, score, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cs clusterStore) ZaddFloat(key string, score float64, value string) (bool, error) {
|
||||||
|
return cs.ZaddFloatCtx(context.Background(), key, score, value)
|
||||||
|
}
|
||||||
|
|
||||||
func (cs clusterStore) ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error) {
|
func (cs clusterStore) ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error) {
|
||||||
|
return cs.ZaddFloatCtx(ctx, key, float64(score), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cs clusterStore) ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (bool, error) {
|
||||||
node, err := cs.getRedis(key)
|
node, err := cs.getRedis(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return node.ZaddCtx(ctx, key, score, value)
|
return node.ZaddFloatCtx(ctx, key, score, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs clusterStore) Zadds(key string, ps ...redis.Pair) (int64, error) {
|
func (cs clusterStore) Zadds(key string, ps ...redis.Pair) (int64, error) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func TestRedis_Decr(t *testing.T) {
|
|||||||
_, err := store.Decr("a")
|
_, err := store.Decr("a")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
val, err := client.Decr("a")
|
val, err := client.Decr("a")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, int64(-1), val)
|
assert.Equal(t, int64(-1), val)
|
||||||
@@ -37,7 +37,7 @@ func TestRedis_DecrBy(t *testing.T) {
|
|||||||
_, err := store.Incrby("a", 2)
|
_, err := store.Incrby("a", 2)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
val, err := client.Decrby("a", 2)
|
val, err := client.Decrby("a", 2)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, int64(-2), val)
|
assert.Equal(t, int64(-2), val)
|
||||||
@@ -52,7 +52,7 @@ func TestRedis_Exists(t *testing.T) {
|
|||||||
_, err := store.Exists("foo")
|
_, err := store.Exists("foo")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
ok, err := client.Exists("a")
|
ok, err := client.Exists("a")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
@@ -68,7 +68,7 @@ func TestRedis_Eval(t *testing.T) {
|
|||||||
_, err := store.Eval(`redis.call("EXISTS", KEYS[1])`, "key1")
|
_, err := store.Eval(`redis.call("EXISTS", KEYS[1])`, "key1")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
_, err := client.Eval(`redis.call("EXISTS", KEYS[1])`, "notexist")
|
_, err := client.Eval(`redis.call("EXISTS", KEYS[1])`, "notexist")
|
||||||
assert.Equal(t, redis.Nil, err)
|
assert.Equal(t, redis.Nil, err)
|
||||||
err = client.Set("key1", "value1")
|
err = client.Set("key1", "value1")
|
||||||
@@ -88,7 +88,7 @@ func TestRedis_Hgetall(t *testing.T) {
|
|||||||
_, err = store.Hgetall("a")
|
_, err = store.Hgetall("a")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
||||||
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
||||||
vals, err := client.Hgetall("a")
|
vals, err := client.Hgetall("a")
|
||||||
@@ -105,7 +105,7 @@ func TestRedis_Hvals(t *testing.T) {
|
|||||||
_, err := store.Hvals("a")
|
_, err := store.Hvals("a")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
||||||
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
||||||
vals, err := client.Hvals("a")
|
vals, err := client.Hvals("a")
|
||||||
@@ -119,7 +119,7 @@ func TestRedis_Hsetnx(t *testing.T) {
|
|||||||
_, err := store.Hsetnx("a", "dd", "ddd")
|
_, err := store.Hsetnx("a", "dd", "ddd")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
||||||
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
||||||
ok, err := client.Hsetnx("a", "bb", "ccc")
|
ok, err := client.Hsetnx("a", "bb", "ccc")
|
||||||
@@ -141,7 +141,7 @@ func TestRedis_HdelHlen(t *testing.T) {
|
|||||||
_, err = store.Hlen("a")
|
_, err = store.Hlen("a")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
||||||
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
||||||
num, err := client.Hlen("a")
|
num, err := client.Hlen("a")
|
||||||
@@ -161,7 +161,7 @@ func TestRedis_HIncrBy(t *testing.T) {
|
|||||||
_, err := store.Hincrby("key", "field", 3)
|
_, err := store.Hincrby("key", "field", 3)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
val, err := client.Hincrby("key", "field", 2)
|
val, err := client.Hincrby("key", "field", 2)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, 2, val)
|
assert.Equal(t, 2, val)
|
||||||
@@ -176,7 +176,7 @@ func TestRedis_Hkeys(t *testing.T) {
|
|||||||
_, err := store.Hkeys("a")
|
_, err := store.Hkeys("a")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
||||||
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
||||||
vals, err := client.Hkeys("a")
|
vals, err := client.Hkeys("a")
|
||||||
@@ -190,7 +190,7 @@ func TestRedis_Hmget(t *testing.T) {
|
|||||||
_, err := store.Hmget("a", "aa", "bb")
|
_, err := store.Hmget("a", "aa", "bb")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
assert.Nil(t, client.Hset("a", "aa", "aaa"))
|
||||||
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
assert.Nil(t, client.Hset("a", "bb", "bbb"))
|
||||||
vals, err := client.Hmget("a", "aa", "bb")
|
vals, err := client.Hmget("a", "aa", "bb")
|
||||||
@@ -209,7 +209,7 @@ func TestRedis_Hmset(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
assert.Nil(t, client.Hmset("a", map[string]string{
|
assert.Nil(t, client.Hmset("a", map[string]string{
|
||||||
"aa": "aaa",
|
"aa": "aaa",
|
||||||
"bb": "bbb",
|
"bb": "bbb",
|
||||||
@@ -225,7 +225,7 @@ func TestRedis_Incr(t *testing.T) {
|
|||||||
_, err := store.Incr("a")
|
_, err := store.Incr("a")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
val, err := client.Incr("a")
|
val, err := client.Incr("a")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, int64(1), val)
|
assert.Equal(t, int64(1), val)
|
||||||
@@ -240,7 +240,7 @@ func TestRedis_IncrBy(t *testing.T) {
|
|||||||
_, err := store.Incrby("a", 2)
|
_, err := store.Incrby("a", 2)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
val, err := client.Incrby("a", 2)
|
val, err := client.Incrby("a", 2)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, int64(2), val)
|
assert.Equal(t, int64(2), val)
|
||||||
@@ -267,7 +267,7 @@ func TestRedis_List(t *testing.T) {
|
|||||||
_, err = store.Lindex("key", 0)
|
_, err = store.Lindex("key", 0)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
val, err := client.Lpush("key", "value1", "value2")
|
val, err := client.Lpush("key", "value1", "value2")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, 2, val)
|
assert.Equal(t, 2, val)
|
||||||
@@ -316,7 +316,7 @@ func TestRedis_Persist(t *testing.T) {
|
|||||||
err = store.Expireat("key", time.Now().Unix()+5)
|
err = store.Expireat("key", time.Now().Unix()+5)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
ok, err := client.Persist("key")
|
ok, err := client.Persist("key")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
@@ -348,7 +348,7 @@ func TestRedis_Sscan(t *testing.T) {
|
|||||||
_, err = store.Del(key)
|
_, err = store.Del(key)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
var list []string
|
var list []string
|
||||||
for i := 0; i < 1550; i++ {
|
for i := 0; i < 1550; i++ {
|
||||||
list = append(list, stringx.Randn(i))
|
list = append(list, stringx.Randn(i))
|
||||||
@@ -390,7 +390,7 @@ func TestRedis_Set(t *testing.T) {
|
|||||||
_, err = store.Spop("key")
|
_, err = store.Spop("key")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
num, err := client.Sadd("key", 1, 2, 3, 4)
|
num, err := client.Sadd("key", 1, 2, 3, 4)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, 4, num)
|
assert.Equal(t, 4, num)
|
||||||
@@ -434,7 +434,7 @@ func TestRedis_SetGetDel(t *testing.T) {
|
|||||||
_, err = store.Del("hello")
|
_, err = store.Del("hello")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
err := client.Set("hello", "world")
|
err := client.Set("hello", "world")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
val, err := client.Get("hello")
|
val, err := client.Get("hello")
|
||||||
@@ -457,7 +457,7 @@ func TestRedis_SetExNx(t *testing.T) {
|
|||||||
_, err = store.SetnxEx("newhello", "newworld", 5)
|
_, err = store.SetnxEx("newhello", "newworld", 5)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
err := client.Setex("hello", "world", 5)
|
err := client.Setex("hello", "world", 5)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
ok, err := client.Setnx("hello", "newworld")
|
ok, err := client.Setnx("hello", "newworld")
|
||||||
@@ -495,7 +495,7 @@ func TestRedis_Getset(t *testing.T) {
|
|||||||
_, err := store.GetSet("hello", "world")
|
_, err := store.GetSet("hello", "world")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
val, err := client.GetSet("hello", "world")
|
val, err := client.GetSet("hello", "world")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "", val)
|
assert.Equal(t, "", val)
|
||||||
@@ -524,7 +524,7 @@ func TestRedis_SetGetDelHashField(t *testing.T) {
|
|||||||
_, err = store.Hdel("key", "field")
|
_, err = store.Hdel("key", "field")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
err := client.Hset("key", "field", "value")
|
err := client.Hset("key", "field", "value")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
val, err := client.Hget("key", "field")
|
val, err := client.Hget("key", "field")
|
||||||
@@ -587,8 +587,8 @@ func TestRedis_SortedSet(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(client Store) {
|
runOnCluster(func(client Store) {
|
||||||
ok, err := client.Zadd("key", 1, "value1")
|
ok, err := client.ZaddFloat("key", 1, "value1")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
ok, err = client.Zadd("key", 2, "value1")
|
ok, err = client.Zadd("key", 2, "value1")
|
||||||
@@ -724,7 +724,7 @@ func TestRedis_HyperLogLog(t *testing.T) {
|
|||||||
_, err = store.Pfcount("key")
|
_, err = store.Pfcount("key")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
runOnCluster(t, func(cluster Store) {
|
runOnCluster(func(cluster Store) {
|
||||||
ok, err := cluster.Pfadd("key", "value")
|
ok, err := cluster.Pfadd("key", "value")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
@@ -734,7 +734,7 @@ func TestRedis_HyperLogLog(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func runOnCluster(t *testing.T, fn func(cluster Store)) {
|
func runOnCluster(fn func(cluster Store)) {
|
||||||
s1.FlushAll()
|
s1.FlushAll()
|
||||||
s2.FlushAll()
|
s2.FlushAll()
|
||||||
|
|
||||||
|
|||||||
@@ -83,12 +83,12 @@ type (
|
|||||||
// FindOneAndReplace returns at most one document that matches the filter. If the filter
|
// FindOneAndReplace returns at most one document that matches the filter. If the filter
|
||||||
// matches multiple documents, FindOneAndReplace returns the first document in the
|
// matches multiple documents, FindOneAndReplace returns the first document in the
|
||||||
// collection that matches the filter.
|
// collection that matches the filter.
|
||||||
FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{},
|
FindOneAndReplace(ctx context.Context, filter, replacement interface{},
|
||||||
opts ...*mopt.FindOneAndReplaceOptions) (*mongo.SingleResult, error)
|
opts ...*mopt.FindOneAndReplaceOptions) (*mongo.SingleResult, error)
|
||||||
// FindOneAndUpdate returns at most one document that matches the filter. If the filter
|
// FindOneAndUpdate returns at most one document that matches the filter. If the filter
|
||||||
// matches multiple documents, FindOneAndUpdate returns the first document in the
|
// matches multiple documents, FindOneAndUpdate returns the first document in the
|
||||||
// collection that matches the filter.
|
// collection that matches the filter.
|
||||||
FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{},
|
FindOneAndUpdate(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.FindOneAndUpdateOptions) (*mongo.SingleResult, error)
|
opts ...*mopt.FindOneAndUpdateOptions) (*mongo.SingleResult, error)
|
||||||
// Indexes returns the index view for this collection.
|
// Indexes returns the index view for this collection.
|
||||||
Indexes() mongo.IndexView
|
Indexes() mongo.IndexView
|
||||||
@@ -99,16 +99,16 @@ type (
|
|||||||
InsertOne(ctx context.Context, document interface{}, opts ...*mopt.InsertOneOptions) (
|
InsertOne(ctx context.Context, document interface{}, opts ...*mopt.InsertOneOptions) (
|
||||||
*mongo.InsertOneResult, error)
|
*mongo.InsertOneResult, error)
|
||||||
// ReplaceOne replaces at most one document that matches the filter.
|
// ReplaceOne replaces at most one document that matches the filter.
|
||||||
ReplaceOne(ctx context.Context, filter interface{}, replacement interface{},
|
ReplaceOne(ctx context.Context, filter, replacement interface{},
|
||||||
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error)
|
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error)
|
||||||
// UpdateByID updates a single document matching the provided filter.
|
// UpdateByID updates a single document matching the provided filter.
|
||||||
UpdateByID(ctx context.Context, id interface{}, update interface{},
|
UpdateByID(ctx context.Context, id, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
||||||
// UpdateMany updates the provided documents.
|
// UpdateMany updates the provided documents.
|
||||||
UpdateMany(ctx context.Context, filter interface{}, update interface{},
|
UpdateMany(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
||||||
// UpdateOne updates a single document matching the provided filter.
|
// UpdateOne updates a single document matching the provided filter.
|
||||||
UpdateOne(ctx context.Context, filter interface{}, update interface{},
|
UpdateOne(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
||||||
// Watch returns a change stream cursor used to receive notifications of changes to the collection.
|
// Watch returns a change stream cursor used to receive notifications of changes to the collection.
|
||||||
Watch(ctx context.Context, pipeline interface{}, opts ...*mopt.ChangeStreamOptions) (
|
Watch(ctx context.Context, pipeline interface{}, opts ...*mopt.ChangeStreamOptions) (
|
||||||
@@ -359,7 +359,7 @@ func (c *decoratedCollection) FindOneAndReplace(ctx context.Context, filter inte
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *decoratedCollection) FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{},
|
func (c *decoratedCollection) FindOneAndUpdate(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.FindOneAndUpdateOptions) (res *mongo.SingleResult, err error) {
|
opts ...*mopt.FindOneAndUpdateOptions) (res *mongo.SingleResult, err error) {
|
||||||
ctx, span := startSpan(ctx, findOneAndUpdate)
|
ctx, span := startSpan(ctx, findOneAndUpdate)
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -420,7 +420,7 @@ func (c *decoratedCollection) InsertOne(ctx context.Context, document interface{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *decoratedCollection) ReplaceOne(ctx context.Context, filter interface{}, replacement interface{},
|
func (c *decoratedCollection) ReplaceOne(ctx context.Context, filter, replacement interface{},
|
||||||
opts ...*mopt.ReplaceOptions) (res *mongo.UpdateResult, err error) {
|
opts ...*mopt.ReplaceOptions) (res *mongo.UpdateResult, err error) {
|
||||||
ctx, span := startSpan(ctx, replaceOne)
|
ctx, span := startSpan(ctx, replaceOne)
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -440,7 +440,7 @@ func (c *decoratedCollection) ReplaceOne(ctx context.Context, filter interface{}
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *decoratedCollection) UpdateByID(ctx context.Context, id interface{}, update interface{},
|
func (c *decoratedCollection) UpdateByID(ctx context.Context, id, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
||||||
ctx, span := startSpan(ctx, updateByID)
|
ctx, span := startSpan(ctx, updateByID)
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -460,7 +460,7 @@ func (c *decoratedCollection) UpdateByID(ctx context.Context, id interface{}, up
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *decoratedCollection) UpdateMany(ctx context.Context, filter interface{}, update interface{},
|
func (c *decoratedCollection) UpdateMany(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
||||||
ctx, span := startSpan(ctx, updateMany)
|
ctx, span := startSpan(ctx, updateMany)
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -480,7 +480,7 @@ func (c *decoratedCollection) UpdateMany(ctx context.Context, filter interface{}
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *decoratedCollection) UpdateOne(ctx context.Context, filter interface{}, update interface{},
|
func (c *decoratedCollection) UpdateOne(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
||||||
ctx, span := startSpan(ctx, updateOne)
|
ctx, span := startSpan(ctx, updateOne)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
@@ -20,10 +20,6 @@ import (
|
|||||||
|
|
||||||
var errDummy = errors.New("dummy")
|
var errDummy = errors.New("dummy")
|
||||||
|
|
||||||
func init() {
|
|
||||||
logx.Disable()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKeepPromise_accept(t *testing.T) {
|
func TestKeepPromise_accept(t *testing.T) {
|
||||||
p := new(mockPromise)
|
p := new(mockPromise)
|
||||||
kp := keepablePromise{
|
kp := keepablePromise{
|
||||||
@@ -110,14 +106,14 @@ func TestCollection_BulkWrite(t *testing.T) {
|
|||||||
}
|
}
|
||||||
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
|
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
|
||||||
res, err := c.BulkWrite(context.Background(), []mongo.WriteModel{
|
res, err := c.BulkWrite(context.Background(), []mongo.WriteModel{
|
||||||
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}})},
|
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}}),
|
||||||
)
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, res)
|
assert.NotNil(t, res)
|
||||||
c.brk = new(dropBreaker)
|
c.brk = new(dropBreaker)
|
||||||
_, err = c.BulkWrite(context.Background(), []mongo.WriteModel{
|
_, err = c.BulkWrite(context.Background(), []mongo.WriteModel{
|
||||||
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}})},
|
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}}),
|
||||||
)
|
})
|
||||||
assert.Equal(t, errDummy, err)
|
assert.Equal(t, errDummy, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -208,7 +204,7 @@ func TestCollection_EstimatedDocumentCount(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCollectionFind(t *testing.T) {
|
func TestCollection_Find(t *testing.T) {
|
||||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||||
defer mt.Close()
|
defer mt.Close()
|
||||||
|
|
||||||
@@ -256,7 +252,7 @@ func TestCollectionFind(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCollectionFindOne(t *testing.T) {
|
func TestCollection_FindOne(t *testing.T) {
|
||||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||||
defer mt.Close()
|
defer mt.Close()
|
||||||
|
|
||||||
@@ -440,7 +436,7 @@ func TestCollection_InsertMany(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCollection_Remove(t *testing.T) {
|
func TestCollection_DeleteOne(t *testing.T) {
|
||||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||||
defer mt.Close()
|
defer mt.Close()
|
||||||
|
|
||||||
@@ -460,7 +456,7 @@ func TestCollection_Remove(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCollectionRemoveAll(t *testing.T) {
|
func TestCollection_DeleteMany(t *testing.T) {
|
||||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||||
defer mt.Close()
|
defer mt.Close()
|
||||||
|
|
||||||
@@ -569,7 +565,7 @@ func TestCollection_UpdateMany(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DecoratedCollectionLogDuration(t *testing.T) {
|
func TestDecoratedCollection_LogDuration(t *testing.T) {
|
||||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||||
defer mt.Close()
|
defer mt.Close()
|
||||||
c := decoratedCollection{
|
c := decoratedCollection{
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ func (m *Model) FindOneAndDelete(ctx context.Context, v, filter interface{},
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindOneAndReplace finds a single document and replaces it.
|
// FindOneAndReplace finds a single document and replaces it.
|
||||||
func (m *Model) FindOneAndReplace(ctx context.Context, v, filter interface{}, replacement interface{},
|
func (m *Model) FindOneAndReplace(ctx context.Context, v, filter, replacement interface{},
|
||||||
opts ...*mopt.FindOneAndReplaceOptions) error {
|
opts ...*mopt.FindOneAndReplaceOptions) error {
|
||||||
res, err := m.Collection.FindOneAndReplace(ctx, filter, replacement, opts...)
|
res, err := m.Collection.FindOneAndReplace(ctx, filter, replacement, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -170,7 +170,7 @@ func (m *Model) FindOneAndReplace(ctx context.Context, v, filter interface{}, re
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindOneAndUpdate finds a single document and updates it.
|
// FindOneAndUpdate finds a single document and updates it.
|
||||||
func (m *Model) FindOneAndUpdate(ctx context.Context, v, filter interface{}, update interface{},
|
func (m *Model) FindOneAndUpdate(ctx context.Context, v, filter, update interface{},
|
||||||
opts ...*mopt.FindOneAndUpdateOptions) error {
|
opts ...*mopt.FindOneAndUpdateOptions) error {
|
||||||
res, err := m.Collection.FindOneAndUpdate(ctx, filter, update, opts...)
|
res, err := m.Collection.FindOneAndUpdate(ctx, filter, update, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ func (mm *Model) InsertOneNoCache(ctx context.Context, document interface{},
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceOne replaces a single document in the collection, and remove the cache.
|
// ReplaceOne replaces a single document in the collection, and remove the cache.
|
||||||
func (mm *Model) ReplaceOne(ctx context.Context, key string, filter interface{}, replacement interface{},
|
func (mm *Model) ReplaceOne(ctx context.Context, key string, filter, replacement interface{},
|
||||||
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
|
||||||
res, err := mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
|
res, err := mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -207,7 +207,7 @@ func (mm *Model) ReplaceOne(ctx context.Context, key string, filter interface{},
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceOneNoCache replaces a single document in the collection.
|
// ReplaceOneNoCache replaces a single document in the collection.
|
||||||
func (mm *Model) ReplaceOneNoCache(ctx context.Context, filter interface{}, replacement interface{},
|
func (mm *Model) ReplaceOneNoCache(ctx context.Context, filter, replacement interface{},
|
||||||
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
|
||||||
return mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
|
return mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
|
||||||
}
|
}
|
||||||
@@ -218,7 +218,7 @@ func (mm *Model) SetCache(key string, v interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateByID updates the document with given id with update, and remove the cache.
|
// UpdateByID updates the document with given id with update, and remove the cache.
|
||||||
func (mm *Model) UpdateByID(ctx context.Context, key string, id interface{}, update interface{},
|
func (mm *Model) UpdateByID(ctx context.Context, key string, id, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||||
res, err := mm.Model.UpdateByID(ctx, id, update, opts...)
|
res, err := mm.Model.UpdateByID(ctx, id, update, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -233,13 +233,13 @@ func (mm *Model) UpdateByID(ctx context.Context, key string, id interface{}, upd
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateByIDNoCache updates the document with given id with update.
|
// UpdateByIDNoCache updates the document with given id with update.
|
||||||
func (mm *Model) UpdateByIDNoCache(ctx context.Context, id interface{}, update interface{},
|
func (mm *Model) UpdateByIDNoCache(ctx context.Context, id, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||||
return mm.Model.UpdateByID(ctx, id, update, opts...)
|
return mm.Model.UpdateByID(ctx, id, update, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateMany updates the documents that match filter with update, and remove the cache.
|
// UpdateMany updates the documents that match filter with update, and remove the cache.
|
||||||
func (mm *Model) UpdateMany(ctx context.Context, keys []string, filter interface{}, update interface{},
|
func (mm *Model) UpdateMany(ctx context.Context, keys []string, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||||
res, err := mm.Model.UpdateMany(ctx, filter, update, opts...)
|
res, err := mm.Model.UpdateMany(ctx, filter, update, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -254,13 +254,13 @@ func (mm *Model) UpdateMany(ctx context.Context, keys []string, filter interface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateManyNoCache updates the documents that match filter with update.
|
// UpdateManyNoCache updates the documents that match filter with update.
|
||||||
func (mm *Model) UpdateManyNoCache(ctx context.Context, filter interface{}, update interface{},
|
func (mm *Model) UpdateManyNoCache(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||||
return mm.Model.UpdateMany(ctx, filter, update, opts...)
|
return mm.Model.UpdateMany(ctx, filter, update, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateOne updates the first document that matches filter with update, and remove the cache.
|
// UpdateOne updates the first document that matches filter with update, and remove the cache.
|
||||||
func (mm *Model) UpdateOne(ctx context.Context, key string, filter interface{}, update interface{},
|
func (mm *Model) UpdateOne(ctx context.Context, key string, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||||
res, err := mm.Model.UpdateOne(ctx, filter, update, opts...)
|
res, err := mm.Model.UpdateOne(ctx, filter, update, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -275,7 +275,7 @@ func (mm *Model) UpdateOne(ctx context.Context, key string, filter interface{},
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateOneNoCache updates the first document that matches filter with update.
|
// UpdateOneNoCache updates the first document that matches filter with update.
|
||||||
func (mm *Model) UpdateOneNoCache(ctx context.Context, filter interface{}, update interface{},
|
func (mm *Model) UpdateOneNoCache(ctx context.Context, filter, update interface{},
|
||||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||||
return mm.Model.UpdateOne(ctx, filter, update, opts...)
|
return mm.Model.UpdateOne(ctx, filter, update, opts...)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ import (
|
|||||||
|
|
||||||
var errDummy = errors.New("dummy")
|
var errDummy = errors.New("dummy")
|
||||||
|
|
||||||
func init() {
|
|
||||||
logx.Disable()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKeepPromise_accept(t *testing.T) {
|
func TestKeepPromise_accept(t *testing.T) {
|
||||||
p := new(mockPromise)
|
p := new(mockPromise)
|
||||||
kp := keepablePromise{
|
kp := keepablePromise{
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package mongoc
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -117,7 +117,7 @@ func TestStat(t *testing.T) {
|
|||||||
|
|
||||||
func TestStatCacheFails(t *testing.T) {
|
func TestStatCacheFails(t *testing.T) {
|
||||||
resetStats()
|
resetStats()
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(io.Discard)
|
||||||
defer log.SetOutput(os.Stdout)
|
defer log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
r := redis.New("localhost:59999")
|
r := redis.New("localhost:59999")
|
||||||
|
|||||||
@@ -2,10 +2,13 @@ package redis
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
red "github.com/go-redis/redis/v8"
|
red "github.com/go-redis/redis/v8"
|
||||||
|
"github.com/zeromicro/go-zero/core/breaker"
|
||||||
"github.com/zeromicro/go-zero/core/errorx"
|
"github.com/zeromicro/go-zero/core/errorx"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"github.com/zeromicro/go-zero/core/mapping"
|
"github.com/zeromicro/go-zero/core/mapping"
|
||||||
@@ -53,7 +56,12 @@ func (h hook) AfterProcess(ctx context.Context, cmd red.Cmder) error {
|
|||||||
|
|
||||||
duration := timex.Since(start)
|
duration := timex.Since(start)
|
||||||
if duration > slowThreshold.Load() {
|
if duration > slowThreshold.Load() {
|
||||||
logDuration(ctx, cmd, duration)
|
logDuration(ctx, []red.Cmder{cmd}, duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
metricReqDur.Observe(int64(duration/time.Millisecond), cmd.Name())
|
||||||
|
if msg := formatError(err); len(msg) > 0 {
|
||||||
|
metricReqErr.Inc(cmd.Name(), msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -95,19 +103,53 @@ func (h hook) AfterProcessPipeline(ctx context.Context, cmds []red.Cmder) error
|
|||||||
|
|
||||||
duration := timex.Since(start)
|
duration := timex.Since(start)
|
||||||
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
|
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
|
||||||
logDuration(ctx, cmds[0], duration)
|
logDuration(ctx, cmds, duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
metricReqDur.Observe(int64(duration/time.Millisecond), "Pipeline")
|
||||||
|
if msg := formatError(batchError.Err()); len(msg) > 0 {
|
||||||
|
metricReqErr.Inc("Pipeline", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func logDuration(ctx context.Context, cmd red.Cmder, duration time.Duration) {
|
func formatError(err error) string {
|
||||||
|
if err == nil || err == red.Nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
opErr, ok := err.(*net.OpError)
|
||||||
|
if ok && opErr.Timeout() {
|
||||||
|
return "timeout"
|
||||||
|
}
|
||||||
|
|
||||||
|
switch err {
|
||||||
|
case io.EOF:
|
||||||
|
return "eof"
|
||||||
|
case context.DeadlineExceeded:
|
||||||
|
return "context deadline"
|
||||||
|
case breaker.ErrServiceUnavailable:
|
||||||
|
return "breaker"
|
||||||
|
default:
|
||||||
|
return "unexpected error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func logDuration(ctx context.Context, cmds []red.Cmder, duration time.Duration) {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
for i, arg := range cmd.Args() {
|
for k, cmd := range cmds {
|
||||||
if i > 0 {
|
if k > 0 {
|
||||||
buf.WriteByte(' ')
|
buf.WriteByte('\n')
|
||||||
}
|
}
|
||||||
buf.WriteString(mapping.Repr(arg))
|
var build strings.Builder
|
||||||
|
for i, arg := range cmd.Args() {
|
||||||
|
if i > 0 {
|
||||||
|
build.WriteByte(' ')
|
||||||
|
}
|
||||||
|
build.WriteString(mapping.Repr(arg))
|
||||||
|
}
|
||||||
|
buf.WriteString(build.String())
|
||||||
}
|
}
|
||||||
logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
|
logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ func TestHookProcessCase1(t *testing.T) {
|
|||||||
Batcher: "jaeger",
|
Batcher: "jaeger",
|
||||||
Sampler: 1.0,
|
Sampler: 1.0,
|
||||||
})
|
})
|
||||||
|
defer ztrace.StopAgent()
|
||||||
|
|
||||||
writer := log.Writer()
|
writer := log.Writer()
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
@@ -44,6 +45,7 @@ func TestHookProcessCase2(t *testing.T) {
|
|||||||
Batcher: "jaeger",
|
Batcher: "jaeger",
|
||||||
Sampler: 1.0,
|
Sampler: 1.0,
|
||||||
})
|
})
|
||||||
|
defer ztrace.StopAgent()
|
||||||
|
|
||||||
w, restore := injectLog()
|
w, restore := injectLog()
|
||||||
defer restore()
|
defer restore()
|
||||||
@@ -89,10 +91,10 @@ func TestHookProcessPipelineCase1(t *testing.T) {
|
|||||||
log.SetOutput(&buf)
|
log.SetOutput(&buf)
|
||||||
defer log.SetOutput(writer)
|
defer log.SetOutput(writer)
|
||||||
|
|
||||||
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{red.NewCmd(context.Background())})
|
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
|
||||||
if err != nil {
|
red.NewCmd(context.Background()),
|
||||||
t.Fatal(err)
|
})
|
||||||
}
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||||||
|
|
||||||
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
||||||
@@ -108,14 +110,15 @@ func TestHookProcessPipelineCase2(t *testing.T) {
|
|||||||
Batcher: "jaeger",
|
Batcher: "jaeger",
|
||||||
Sampler: 1.0,
|
Sampler: 1.0,
|
||||||
})
|
})
|
||||||
|
defer ztrace.StopAgent()
|
||||||
|
|
||||||
w, restore := injectLog()
|
w, restore := injectLog()
|
||||||
defer restore()
|
defer restore()
|
||||||
|
|
||||||
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{red.NewCmd(context.Background())})
|
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
|
||||||
if err != nil {
|
red.NewCmd(context.Background()),
|
||||||
t.Fatal(err)
|
})
|
||||||
}
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||||||
|
|
||||||
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
||||||
@@ -156,10 +159,28 @@ func TestHookProcessPipelineCase5(t *testing.T) {
|
|||||||
defer log.SetOutput(writer)
|
defer log.SetOutput(writer)
|
||||||
|
|
||||||
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
|
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
|
||||||
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{red.NewCmd(context.Background())}))
|
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
||||||
|
red.NewCmd(context.Background()),
|
||||||
|
}))
|
||||||
assert.True(t, buf.Len() == 0)
|
assert.True(t, buf.Len() == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLogDuration(t *testing.T) {
|
||||||
|
w, restore := injectLog()
|
||||||
|
defer restore()
|
||||||
|
|
||||||
|
logDuration(context.Background(), []red.Cmder{
|
||||||
|
red.NewCmd(context.Background(), "get", "foo"),
|
||||||
|
}, 1*time.Second)
|
||||||
|
assert.True(t, strings.Contains(w.String(), "get foo"))
|
||||||
|
|
||||||
|
logDuration(context.Background(), []red.Cmder{
|
||||||
|
red.NewCmd(context.Background(), "get", "foo"),
|
||||||
|
red.NewCmd(context.Background(), "set", "bar", 0),
|
||||||
|
}, 1*time.Second)
|
||||||
|
assert.True(t, strings.Contains(w.String(), `get foo\nset bar 0`))
|
||||||
|
}
|
||||||
|
|
||||||
func injectLog() (r *strings.Builder, restore func()) {
|
func injectLog() (r *strings.Builder, restore func()) {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
w := logx.NewWriter(&buf)
|
w := logx.NewWriter(&buf)
|
||||||
|
|||||||
23
core/stores/redis/metrics.go
Normal file
23
core/stores/redis/metrics.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import "github.com/zeromicro/go-zero/core/metric"
|
||||||
|
|
||||||
|
const namespace = "redis_client"
|
||||||
|
|
||||||
|
var (
|
||||||
|
metricReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: "requests",
|
||||||
|
Name: "duration_ms",
|
||||||
|
Help: "redis client requests duration(ms).",
|
||||||
|
Labels: []string{"command"},
|
||||||
|
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000, 2500},
|
||||||
|
})
|
||||||
|
metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: "requests",
|
||||||
|
Name: "error_total",
|
||||||
|
Help: "redis client requests error count.",
|
||||||
|
Labels: []string{"command", "error"},
|
||||||
|
})
|
||||||
|
)
|
||||||
@@ -226,20 +226,7 @@ func (s *Redis) Blpop(node RedisNode, key string) (string, error) {
|
|||||||
// BlpopCtx uses passed in redis connection to execute blocking queries.
|
// BlpopCtx uses passed in redis connection to execute blocking queries.
|
||||||
// Doesn't benefit from pooling redis connections of blocking queries
|
// Doesn't benefit from pooling redis connections of blocking queries
|
||||||
func (s *Redis) BlpopCtx(ctx context.Context, node RedisNode, key string) (string, error) {
|
func (s *Redis) BlpopCtx(ctx context.Context, node RedisNode, key string) (string, error) {
|
||||||
if node == nil {
|
return s.BlpopWithTimeoutCtx(ctx, node, blockingQueryTimeout, key)
|
||||||
return "", ErrNilNode
|
|
||||||
}
|
|
||||||
|
|
||||||
vals, err := node.BLPop(ctx, blockingQueryTimeout, key).Result()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(vals) < 2 {
|
|
||||||
return "", fmt.Errorf("no value on key: %s", key)
|
|
||||||
}
|
|
||||||
|
|
||||||
return vals[1], nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlpopEx uses passed in redis connection to execute blpop command.
|
// BlpopEx uses passed in redis connection to execute blpop command.
|
||||||
@@ -267,6 +254,32 @@ func (s *Redis) BlpopExCtx(ctx context.Context, node RedisNode, key string) (str
|
|||||||
return vals[1], true, nil
|
return vals[1], true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BlpopWithTimeout uses passed in redis connection to execute blpop command.
|
||||||
|
// Control blocking query timeout
|
||||||
|
func (s *Redis) BlpopWithTimeout(node RedisNode, timeout time.Duration, key string) (string, error) {
|
||||||
|
return s.BlpopWithTimeoutCtx(context.Background(), node, timeout, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlpopWithTimeoutCtx uses passed in redis connection to execute blpop command.
|
||||||
|
// Control blocking query timeout
|
||||||
|
func (s *Redis) BlpopWithTimeoutCtx(ctx context.Context, node RedisNode, timeout time.Duration,
|
||||||
|
key string) (string, error) {
|
||||||
|
if node == nil {
|
||||||
|
return "", ErrNilNode
|
||||||
|
}
|
||||||
|
|
||||||
|
vals, err := node.BLPop(ctx, timeout, key).Result()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(vals) < 2 {
|
||||||
|
return "", fmt.Errorf("no value on key: %s", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
return vals[1], nil
|
||||||
|
}
|
||||||
|
|
||||||
// Decr is the implementation of redis decr command.
|
// Decr is the implementation of redis decr command.
|
||||||
func (s *Redis) Decr(key string) (int64, error) {
|
func (s *Redis) Decr(key string) (int64, error) {
|
||||||
return s.DecrCtx(context.Background(), key)
|
return s.DecrCtx(context.Background(), key)
|
||||||
@@ -1836,8 +1849,19 @@ func (s *Redis) Zadd(key string, score int64, value string) (bool, error) {
|
|||||||
return s.ZaddCtx(context.Background(), key, score, value)
|
return s.ZaddCtx(context.Background(), key, score, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ZaddFloat is the implementation of redis zadd command.
|
||||||
|
func (s *Redis) ZaddFloat(key string, score float64, value string) (bool, error) {
|
||||||
|
return s.ZaddFloatCtx(context.Background(), key, score, value)
|
||||||
|
}
|
||||||
|
|
||||||
// ZaddCtx is the implementation of redis zadd command.
|
// ZaddCtx is the implementation of redis zadd command.
|
||||||
func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (
|
func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (
|
||||||
|
val bool, err error) {
|
||||||
|
return s.ZaddFloatCtx(ctx, key, float64(score), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZaddFloatCtx is the implementation of redis zadd command.
|
||||||
|
func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (
|
||||||
val bool, err error) {
|
val bool, err error) {
|
||||||
err = s.brk.DoWithAcceptable(func() error {
|
err = s.brk.DoWithAcceptable(func() error {
|
||||||
conn, err := getRedis(s)
|
conn, err := getRedis(s)
|
||||||
@@ -1846,7 +1870,7 @@ func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
v, err := conn.ZAdd(ctx, key, &red.Z{
|
v, err := conn.ZAdd(ctx, key, &red.Z{
|
||||||
Score: float64(score),
|
Score: score,
|
||||||
Member: value,
|
Member: value,
|
||||||
}).Result()
|
}).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -810,7 +810,7 @@ func TestRedis_SetGetDelHashField(t *testing.T) {
|
|||||||
|
|
||||||
func TestRedis_SortedSet(t *testing.T) {
|
func TestRedis_SortedSet(t *testing.T) {
|
||||||
runOnRedis(t, func(client *Redis) {
|
runOnRedis(t, func(client *Redis) {
|
||||||
ok, err := client.Zadd("key", 1, "value1")
|
ok, err := client.ZaddFloat("key", 1, "value1")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
ok, err = client.Zadd("key", 2, "value1")
|
ok, err = client.Zadd("key", 2, "value1")
|
||||||
@@ -988,8 +988,8 @@ func TestRedis_SortedSet(t *testing.T) {
|
|||||||
assert.Equal(t, 0, len(pairs))
|
assert.Equal(t, 0, len(pairs))
|
||||||
_, err = New(client.Addr, badType()).Zrevrank("key", "value")
|
_, err = New(client.Addr, badType()).Zrevrank("key", "value")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
client.Zadd("second", 2, "aa")
|
_, _ = client.Zadd("second", 2, "aa")
|
||||||
client.Zadd("third", 3, "bbb")
|
_, _ = client.Zadd("third", 3, "bbb")
|
||||||
val, err = client.Zunionstore("union", &ZStore{
|
val, err = client.Zunionstore("union", &ZStore{
|
||||||
Keys: []string{"second", "third"},
|
Keys: []string{"second", "third"},
|
||||||
Weights: []float64{1, 2},
|
Weights: []float64{1, 2},
|
||||||
@@ -1117,6 +1117,17 @@ func TestRedisBlpopEx(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRedisBlpopWithTimeout(t *testing.T) {
|
||||||
|
runOnRedis(t, func(client *Redis) {
|
||||||
|
client.Ping()
|
||||||
|
var node mockedNode
|
||||||
|
_, err := client.BlpopWithTimeout(nil, 10*time.Second, "foo")
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
_, err = client.BlpopWithTimeout(node, 10*time.Second, "foo")
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestRedisGeo(t *testing.T) {
|
func TestRedisGeo(t *testing.T) {
|
||||||
runOnRedis(t, func(client *Redis) {
|
runOnRedis(t, func(client *Redis) {
|
||||||
client.Ping()
|
client.Ping()
|
||||||
@@ -1176,7 +1187,7 @@ func runOnRedis(t *testing.T, fn func(client *Redis)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if client != nil {
|
if client != nil {
|
||||||
client.Close()
|
_ = client.Close()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
fn(New(s.Addr()))
|
fn(New(s.Addr()))
|
||||||
@@ -1198,7 +1209,7 @@ func runOnRedisTLS(t *testing.T, fn func(client *Redis)) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if client != nil {
|
if client != nil {
|
||||||
client.Close()
|
_ = client.Close()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
fn(New(s.Addr(), WithTLS()))
|
fn(New(s.Addr(), WithTLS()))
|
||||||
@@ -1214,6 +1225,6 @@ type mockedNode struct {
|
|||||||
RedisNode
|
RedisNode
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n mockedNode) BLPop(ctx context.Context, timeout time.Duration, keys ...string) *red.StringSliceCmd {
|
func (n mockedNode) BLPop(_ context.Context, _ time.Duration, _ ...string) *red.StringSliceCmd {
|
||||||
return red.NewStringSliceCmd(context.Background(), "foo", "bar")
|
return red.NewStringSliceCmd(context.Background(), "foo", "bar")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateRedis returns a in process redis.Redis.
|
// CreateRedis returns an in process redis.Redis.
|
||||||
func CreateRedis() (r *redis.Redis, clean func(), err error) {
|
func CreateRedis() (r *redis.Redis, clean func(), err error) {
|
||||||
mr, err := miniredis.Run()
|
mr, err := miniredis.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -284,7 +284,7 @@ func TestCachedConn_QueryRowIndex_HasWrongCache(t *testing.T) {
|
|||||||
|
|
||||||
func TestStatCacheFails(t *testing.T) {
|
func TestStatCacheFails(t *testing.T) {
|
||||||
resetStats()
|
resetStats()
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(io.Discard)
|
||||||
defer log.SetOutput(os.Stdout)
|
defer log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
r := redis.New("localhost:59999")
|
r := redis.New("localhost:59999")
|
||||||
|
|||||||
23
core/stores/sqlx/metrics.go
Normal file
23
core/stores/sqlx/metrics.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package sqlx
|
||||||
|
|
||||||
|
import "github.com/zeromicro/go-zero/core/metric"
|
||||||
|
|
||||||
|
const namespace = "sql_client"
|
||||||
|
|
||||||
|
var (
|
||||||
|
metricReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: "requests",
|
||||||
|
Name: "duration_ms",
|
||||||
|
Help: "mysql client requests duration(ms).",
|
||||||
|
Labels: []string{"command"},
|
||||||
|
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000, 2500},
|
||||||
|
})
|
||||||
|
metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: "requests",
|
||||||
|
Name: "error_total",
|
||||||
|
Help: "mysql client requests error count.",
|
||||||
|
Labels: []string{"command", "error"},
|
||||||
|
})
|
||||||
|
)
|
||||||
@@ -153,6 +153,9 @@ func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...interfac
|
|||||||
result, err = exec(ctx, conn, q, args...)
|
result, err = exec(ctx, conn, q, args...)
|
||||||
return err
|
return err
|
||||||
}, db.acceptable)
|
}, db.acceptable)
|
||||||
|
if err == breaker.ErrServiceUnavailable {
|
||||||
|
metricReqErr.Inc("Exec", "breaker")
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -186,6 +189,9 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}, db.acceptable)
|
}, db.acceptable)
|
||||||
|
if err == breaker.ErrServiceUnavailable {
|
||||||
|
metricReqErr.Inc("Prepare", "breaker")
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -270,9 +276,14 @@ func (db *commonSqlConn) TransactCtx(ctx context.Context, fn func(context.Contex
|
|||||||
endSpan(span, err)
|
endSpan(span, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return db.brk.DoWithAcceptable(func() error {
|
err = db.brk.DoWithAcceptable(func() error {
|
||||||
return transact(ctx, db, db.beginTx, fn)
|
return transact(ctx, db, db.beginTx, fn)
|
||||||
}, db.acceptable)
|
}, db.acceptable)
|
||||||
|
if err == breaker.ErrServiceUnavailable {
|
||||||
|
metricReqErr.Inc("Transact", "breaker")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *commonSqlConn) acceptable(err error) bool {
|
func (db *commonSqlConn) acceptable(err error) bool {
|
||||||
@@ -287,7 +298,7 @@ func (db *commonSqlConn) acceptable(err error) bool {
|
|||||||
func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) error,
|
func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) error,
|
||||||
q string, args ...interface{}) (err error) {
|
q string, args ...interface{}) (err error) {
|
||||||
var qerr error
|
var qerr error
|
||||||
return db.brk.DoWithAcceptable(func() error {
|
err = db.brk.DoWithAcceptable(func() error {
|
||||||
conn, err := db.connProv()
|
conn, err := db.connProv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
db.onError(err)
|
db.onError(err)
|
||||||
@@ -301,6 +312,11 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows)
|
|||||||
}, func(err error) bool {
|
}, func(err error) bool {
|
||||||
return qerr == err || db.acceptable(err)
|
return qerr == err || db.acceptable(err)
|
||||||
})
|
})
|
||||||
|
if err == breaker.ErrServiceUnavailable {
|
||||||
|
metricReqErr.Inc("queryRows", "breaker")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statement) Close() error {
|
func (s statement) Close() error {
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSqlConn(t *testing.T) {
|
func TestSqlConn(t *testing.T) {
|
||||||
mock := buildConn()
|
mock, err := buildConn()
|
||||||
|
assert.Nil(t, err)
|
||||||
mock.ExpectExec("any")
|
mock.ExpectExec("any")
|
||||||
mock.ExpectQuery("any").WillReturnRows(sqlmock.NewRows([]string{"foo"}))
|
mock.ExpectQuery("any").WillReturnRows(sqlmock.NewRows([]string{"foo"}))
|
||||||
conn := NewMysql(mockedDatasource)
|
conn := NewMysql(mockedDatasource)
|
||||||
@@ -50,8 +51,8 @@ func TestSqlConn(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildConn() (mock sqlmock.Sqlmock) {
|
func buildConn() (mock sqlmock.Sqlmock, err error) {
|
||||||
connManager.GetResource(mockedDatasource, func() (io.Closer, error) {
|
_, err = connManager.GetResource(mockedDatasource, func() (io.Closer, error) {
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
var err error
|
var err error
|
||||||
db, mock, err = sqlmock.New()
|
db, mock, err = sqlmock.New()
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user