Compare commits

...

195 Commits

Author SHA1 Message Date
Kevin Wan
25821bdee6 chore: coding style (#3960) 2024-03-03 00:17:38 +08:00
dependabot[bot]
b624b966f0 chore(deps): bump actions/stale from 8 to 9 (#3963)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-03-03 00:11:56 +08:00
dependabot[bot]
df96262235 chore(deps): bump codecov/codecov-action from 3 to 4 (#3962)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-03-03 00:06:41 +08:00
dependabot[bot]
2629636f64 chore(deps): bump actions/setup-go from 4 to 5 (#3964)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-03-03 00:01:14 +08:00
dependabot[bot]
708ad207d7 chore(deps): bump github/codeql-action from 2 to 3 (#3961)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-03-02 23:54:37 +08:00
Rene Leonhardt
b53ba76a99 feat: Improve Docker build (#3682) 2024-03-02 15:40:31 +00:00
POABOB
be7f93924a feature: add a mongo registry option to convert type easier. (#3780)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
2024-03-02 15:13:44 +00:00
Kevin Wan
45be48a4ee chore: coding style (#3959) 2024-03-02 22:45:24 +08:00
kesonan
e08ba2fee8 (goctl)fix parser issues (#3930) 2024-03-02 14:27:39 +00:00
Kevin Wan
a5d2b971a1 chore: add more tests (#3958) 2024-03-02 21:58:13 +08:00
Qiu shao
9763c8b143 feat:add redis mset func (#3820) 2024-03-02 12:00:25 +00:00
Kevin Wan
4e3f1776dc chore: coding style (#3957) 2024-03-02 19:09:14 +08:00
fearlessfei
e38036cea2 feat: retry ignore specified errors (#3808) 2024-03-02 18:53:20 +08:00
Kevin Wan
8e97c5819f chore: add more tests (#3954) 2024-03-02 12:22:55 +08:00
#Suyghur
0ee44c7064 feat(redis): added and impl ZADDNX command (#3944) 2024-03-02 10:15:10 +08:00
Kevin Wan
a1bacd3fc8 feat: a concurrent runner with messages taken in pushing order (#3941) 2024-03-02 10:03:58 +08:00
Kevin Wan
c98d5fdaf4 chore: simplify linux nocgroup logic (#3953) 2024-03-02 07:13:41 +08:00
dependabot[bot]
2ee43b41b8 chore(deps): bump github.com/stretchr/testify from 1.8.4 to 1.9.0 in /tools/goctl (#3952)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-03-02 06:50:33 +08:00
dependabot[bot]
8367af3416 chore(deps): bump github.com/stretchr/testify from 1.8.4 to 1.9.0 (#3951)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-03-02 06:42:25 +08:00
Kevin Wan
03b6e377d7 chore: add lock for batcherror (#3950) 2024-03-02 00:59:15 +08:00
chentong
ec41880476 fix: BatchError.Add() non thread safe (#3946) 2024-03-01 16:32:39 +00:00
Kevin Wan
5263805b3b chore: simplify linux nocgroup logic (#3949) 2024-03-02 00:23:52 +08:00
Alex Last
a7363f0c21 feat: add nocgroup build tag for systems without cgroup (#3948) 2024-03-01 15:52:20 +00:00
mongobaba
52e5d85221 feat: add break metrics for sqlx.statement (#3947) 2024-03-01 14:55:32 +00:00
MarkJoyMa
88aab8f635 fix: mapping FillDefault is optional! bug (#3940) 2024-02-27 16:23:47 +00:00
Kevin Wan
1f63cbe9c6 Update readme-cn.md (#3939) 2024-02-26 17:24:52 +08:00
Kevin Wan
0dfaf135dd feat: support breaker with sql statements (#3936) 2024-02-25 11:24:44 +08:00
Kevin Wan
914bcdcf2b chore: add tests (#3931) 2024-02-23 23:00:35 +08:00
fffreedom
e38cb0118d when the Unmarshaler parsing value by fillSliceFromString, if the val… (#3927)
Co-authored-by: danahan <danahan@tencent.com>
2024-02-23 14:00:58 +00:00
dependabot[bot]
cb8161c799 chore(deps): bump google.golang.org/grpc from 1.61.1 to 1.62.0 in /tools/goctl (#3929) 2024-02-23 10:26:41 +08:00
dependabot[bot]
c4dac2095f chore(deps): bump google.golang.org/grpc from 1.61.1 to 1.62.0 (#3928) 2024-02-23 03:34:23 +08:00
Kevin Wan
25a807afb2 chore: add tests (#3921) 2024-02-20 10:11:43 +08:00
Kevin Wan
6be37ad533 chore: optimize coding style and add unit tests (#3917) 2024-02-17 15:50:07 +08:00
chen quan
28cb2c5804 feat: support sse ignore timeout (#2041)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
2024-02-17 07:06:45 +00:00
Kevin Wan
0f1d4c6bca optimize: improve performance on log disabled (#3916) 2024-02-17 14:55:48 +08:00
dependabot[bot]
bfe8335cb2 chore(deps): bump k8s.io/client-go from 0.29.1 to 0.29.2 (#3913)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-16 10:43:40 +08:00
dependabot[bot]
3c10ce0115 chore(deps): bump k8s.io/apimachinery from 0.29.1 to 0.29.2 (#3912)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-16 10:33:54 +08:00
Kevin Wan
1303e0fe6f feat: optimize circuit breaker algorithm (#3897) 2024-02-15 20:29:24 +08:00
Kevin Wan
9c17499757 optimize: shedding algorithm performance (#3908) 2024-02-15 20:22:22 +08:00
dependabot[bot]
8ceb2885db chore(deps): bump google.golang.org/grpc from 1.61.0 to 1.61.1 in /tools/goctl (#3911)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-15 20:09:38 +08:00
dependabot[bot]
00944894b4 chore(deps): bump google.golang.org/grpc from 1.61.0 to 1.61.1 (#3910)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-15 17:31:10 +08:00
dependabot[bot]
609fb3d59e chore(deps): bump golang.org/x/net from 0.20.0 to 0.21.0 (#3907)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-10 11:58:12 +08:00
dependabot[bot]
01c330abe7 chore(deps): bump golang.org/x/sys from 0.16.0 to 0.17.0 (#3902)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-08 22:37:11 +08:00
Kevin Wan
2ccef5bb4f feat: support ScheduleImmediately in TaskRunner (#3896) 2024-02-06 06:26:22 +00:00
dependabot[bot]
10f1d93e2a chore(deps): bump github.com/jackc/pgx/v5 from 5.5.2 to 5.5.3 (#3895) 2024-02-06 09:42:05 +08:00
Kevin Wan
dd518c8eac chore: update goctl version to 1.6.2 (#3890) 2024-02-03 21:31:11 +08:00
Kevin Wan
97cf2421de chore: add more tests (#3888) 2024-02-03 20:33:20 +08:00
Kevin Wan
786a80131e chore: fix test failure (#3883) 2024-02-01 22:41:05 +08:00
dependabot[bot]
93d257f9f5 chore(deps): bump go.etcd.io/etcd/client/v3 from 3.5.11 to 3.5.12 (#3887)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-01 10:34:46 +08:00
dependabot[bot]
f79535057f chore(deps): bump github.com/jhump/protoreflect from 1.15.5 to 1.15.6 (#3886)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-31 10:37:30 +08:00
dependabot[bot]
a905f4c20c chore(deps): bump github.com/emicklei/proto from 1.13.0 to 1.13.2 in /tools/goctl (#3882)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-30 15:02:02 +08:00
kesonan
3331954a78 (goctl): fix unresolved type if linked api imported (#3881) 2024-01-29 13:05:08 +00:00
dependabot[bot]
f54c2e384f chore(deps): bump google.golang.org/grpc from 1.60.1 to 1.61.0 (#3879)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-27 19:15:05 +08:00
dependabot[bot]
4b83f2ebd0 chore(deps): bump k8s.io/client-go from 0.29.0 to 0.29.1 (#3861)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-26 21:42:55 +08:00
dependabot[bot]
1c572ee16b chore(deps): bump github.com/jhump/protoreflect from 1.15.4 to 1.15.5 (#3875)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-26 13:44:15 +08:00
dependabot[bot]
b3402430e8 chore(deps): bump github.com/google/uuid from 1.5.0 to 1.6.0 (#3876)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-26 13:02:09 +08:00
dependabot[bot]
076f5de7d9 chore(deps): bump google.golang.org/grpc from 1.60.1 to 1.61.0 in /tools/goctl (#3870)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-25 22:57:17 +08:00
dependabot[bot]
303a74559a chore(deps): bump k8s.io/apimachinery from 0.29.0 to 0.29.1 (#3864)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-25 22:14:22 +08:00
Kevin Wan
c08e741d7a fix: cpu stat in cgroup v2 (#3857) 2024-01-17 23:35:42 +08:00
dependabot[bot]
06d2c07fce chore(deps): bump github.com/jackc/pgx/v5 from 5.5.1 to 5.5.2 (#3850)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-17 22:55:53 +08:00
Kevin Wan
b6f00a5789 Update readme-cn.md (#3849) 2024-01-15 18:42:28 +08:00
MarkJoyMa
dace520654 fix: revert sqlx metric namespace (#3847) 2024-01-14 02:38:10 +00:00
Kevin Wan
44d347d48a fix: issue #3840 (#3846) 2024-01-14 09:37:01 +08:00
Kevin Wan
408827d876 fix: issue 3840 (#3845) 2024-01-13 23:48:50 +08:00
Kevin Wan
9e33b557b1 chore: refactor redis (#3844) 2024-01-13 23:02:19 +08:00
Kevin Wan
368caa7608 feat: upgrade go-redis to v9 (#3088)
Co-authored-by: cong <zhangcong1992@gmail.com>
2024-01-13 22:40:58 +08:00
Kevin Wan
7822a4c1cb chore: refactor mapping errors (#3843) 2024-01-13 22:11:19 +08:00
Remember
0441f84606 fix(mapping): call fillSliceValue panic if the value is nil (#3839) 2024-01-13 13:48:43 +00:00
Kevin Wan
81d72b5010 chore: make cpu usage more smooth (#3842) 2024-01-13 19:36:25 +08:00
kesonan
7ba8adfc74 fix(goctl)/new parser (#3834)
Co-authored-by: keson <keson@kesondeMacBook-Pro.local>
2024-01-11 15:50:53 +00:00
dependabot[bot]
ffd2a78623 chore(deps): bump github.com/DATA-DOG/go-sqlmock from 1.5.1 to 1.5.2 (#3830)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-11 11:42:47 +08:00
Kevin Wan
1b9b3cada7 Update readme-cn.md (#3836) 2024-01-10 20:08:48 +08:00
dependabot[bot]
38c8f9cf21 chore(deps): bump golang.org/x/net from 0.19.0 to 0.20.0 (#3831)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-09 23:37:04 +08:00
dependabot[bot]
54dbb05bb9 chore(deps): bump github.com/DATA-DOG/go-sqlmock from 1.5.1 to 1.5.2 in /tools/goctl (#3832)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-09 21:45:06 +08:00
kesonan
9a671f6059 fix #3825 (#3828) 2024-01-06 14:45:46 +00:00
dependabot[bot]
80aab0b3f8 chore(deps): bump github.com/alicebob/miniredis/v2 from 2.31.0 to 2.31.1 (#3826)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-06 21:10:12 +08:00
dependabot[bot]
2d0286646f chore(deps): bump golang.org/x/sys from 0.15.0 to 0.16.0 (#3829)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-06 21:01:00 +08:00
dependabot[bot]
d012fe97b1 chore(deps): bump github.com/prometheus/client_golang from 1.17.0 to 1.18.0 (#3822)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-30 10:25:20 +08:00
dependabot[bot]
7ca13bc25e chore(deps): bump google.golang.org/protobuf from 1.31.1-0.20231027082548-f4a6c1f6e5c1 to 1.32.0 (#3809)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-23 23:12:33 +08:00
dependabot[bot]
9c20f10743 chore(deps): bump google.golang.org/protobuf from 1.31.1-0.20231027082548-f4a6c1f6e5c1 to 1.32.0 in /tools/goctl (#3810)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-23 22:57:37 +08:00
Kimjin-gd
6ec38ec056 fix: negative float32 overflow when unmarshalling (#3811)
Co-authored-by: kim1.jin <kim1.jin@bkyo.io>
2023-12-23 14:47:11 +00:00
dependabot[bot]
28c742a1e1 chore(deps): bump k8s.io/client-go from 0.28.4 to 0.29.0 (#3800)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-22 23:46:19 +08:00
dependabot[bot]
b3b6cfe947 chore(deps): bump github.com/pelletier/go-toml/v2 from 2.1.0 to 2.1.1 (#3801)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-21 19:18:55 +08:00
dependabot[bot]
a8ef7b51eb chore(deps): bump k8s.io/apimachinery from 0.28.4 to 0.29.0 (#3802)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-21 18:59:55 +08:00
dependabot[bot]
124968114a chore(deps): bump google.golang.org/grpc from 1.60.0 to 1.60.1 (#3806)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-21 18:21:21 +08:00
dependabot[bot]
04ed821b65 chore(deps): bump google.golang.org/grpc from 1.60.0 to 1.60.1 in /tools/goctl (#3807)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-21 17:47:30 +08:00
dependabot[bot]
15599ac0a0 chore(deps): bump github.com/google/uuid from 1.4.0 to 1.5.0 (#3799) 2023-12-20 00:48:09 +08:00
dependabot[bot]
0cf6971664 chore(deps): bump golang.org/x/crypto from 0.16.0 to 0.17.0 (#3803) 2023-12-20 00:35:43 +08:00
dependabot[bot]
47c4f2831c chore(deps): bump golang.org/x/crypto from 0.16.0 to 0.17.0 in /tools/goctl (#3804)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-19 23:44:50 +08:00
Kevin Wan
2b18dd1764 chore: update goctl deps (#3797) 2023-12-17 14:06:32 +08:00
Kevin Wan
27c4908342 chore: coding style (#3796) 2023-12-17 13:44:55 +08:00
李登富
48625fa381 fix endless loop caused by ErrCompacted (#3774)
Co-authored-by: lidengfu <lidengfu@excean.com>
2023-12-17 05:28:19 +00:00
Kevin Wan
83a776a190 chore: upgrade otel, removed ut temporarily because of otel API changes (#3795) 2023-12-17 12:26:48 +08:00
Qiu shao
431f9af43e feat:add redis ExistsMany method (#3769) 2023-12-16 14:49:16 +08:00
gongluck
8c2f4c1899 Fixed #3771 (#3788) 2023-12-16 06:37:35 +00:00
Alex Last
919477ffe4 fix(servicegroup): use logx for shutdown message (#3719) 2023-12-16 06:25:02 +00:00
Summer-lights
400386459c fix(redis): redis ttl -1 and -2 (#3783) 2023-12-16 05:46:53 +00:00
dependabot[bot]
ebe0801d2f chore(deps): bump github.com/emicklei/proto from 1.12.2 to 1.13.0 in /tools/goctl (#3782)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-16 13:39:59 +08:00
dependabot[bot]
b76d85f204 chore(deps): bump google.golang.org/grpc from 1.59.0 to 1.60.0 in /tools/goctl (#3787)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-16 13:25:31 +08:00
dependabot[bot]
28ba57afb3 chore(deps): bump github.com/jhump/protoreflect from 1.15.3 to 1.15.4 (#3794)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-16 13:18:36 +08:00
dependabot[bot]
d6873047ce chore(deps): bump google.golang.org/grpc from 1.59.0 to 1.60.0 (#3791) 2023-12-16 00:44:30 +08:00
dependabot[bot]
54c0f2e5cf chore(deps): bump github.com/DATA-DOG/go-sqlmock from 1.5.0 to 1.5.1 in /tools/goctl (#3781) 2023-12-15 12:13:12 +08:00
dependabot[bot]
7795231cc6 chore(deps): bump github.com/DATA-DOG/go-sqlmock from 1.5.0 to 1.5.1 (#3779)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-15 00:24:28 +08:00
zzZZzzz888
4835e4fe51 fix: coredump: goctl model mysql ddl --src user_base.sql --dir . area… (#3777)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
2023-12-12 16:09:15 +00:00
dependabot[bot]
daef970091 chore(deps): bump github.com/jackc/pgx/v5 from 5.5.0 to 5.5.1 (#3778)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-12 23:53:29 +08:00
guangwu
05020a92e8 fix: primary key unique key simultaneously exist cacheIdPrefix duplicate (#3763)
Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
2023-12-11 23:13:35 +08:00
POABOB
a1bbac3c6c fix: prevent a crash if there is a unique key constraint with a nil field. (#3770) 2023-12-11 13:29:05 +00:00
Kevin Wan
22c98beb24 feat: add dbtest to facility db test (#3768) 2023-12-09 22:52:06 +08:00
dependabot[bot]
8fd710d5e7 chore(deps): bump go.mongodb.org/mongo-driver from 1.13.0 to 1.13.1 (#3767) 2023-12-09 18:47:08 +08:00
dependabot[bot]
91a735ae47 chore(deps): bump go.etcd.io/etcd/client/v3 from 3.5.10 to 3.5.11 (#3764)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-08 17:01:12 +08:00
Kevin Wan
39c662eece Update readme-cn.md (#3755) 2023-12-04 21:02:30 +08:00
kesonan
5e63002cf8 (goctl:) fix circle import in case new parser (#3750)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
2023-11-29 11:13:39 +00:00
dependabot[bot]
c46bcf7e1b chore(deps): bump golang.org/x/net from 0.18.0 to 0.19.0 (#3749)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-29 19:00:37 +08:00
dependabot[bot]
3c65bdbb66 chore(deps): bump golang.org/x/sys from 0.14.0 to 0.15.0 (#3747)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-28 15:37:52 +08:00
null
5630bce286 Fix incorrect description in documentation (#3745) 2023-11-28 06:58:08 +00:00
dependabot[bot]
75524da21e chore(deps): bump golang.org/x/time from 0.4.0 to 0.5.0 (#3746)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-28 14:15:58 +08:00
Kevin Wan
ede7e683fd feat: auto stop profiling after one minute (#3742) 2023-11-24 21:27:05 +08:00
Kevin Wan
eb14d1347e chore: refactor ring (#3739) 2023-11-23 23:57:26 +08:00
POABOB
c220b5d886 fix: prevent ring index overflow (#3738) 2023-11-23 15:44:33 +00:00
dependabot[bot]
5e8e21b257 chore(deps): bump k8s.io/client-go from 0.28.3 to 0.28.4 (#3737)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-23 23:13:16 +08:00
dependabot[bot]
0635a4ac96 chore(deps): bump k8s.io/apimachinery from 0.28.3 to 0.28.4 (#3733)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-22 23:37:24 +08:00
MarkJoyMa
c71b753c78 fix: goctl FindOne error (#3731) 2023-11-21 03:52:51 +00:00
Kevin Wan
2f8cffc699 chore: update mongo driver (#3727) 2023-11-19 16:35:25 +08:00
Kevin Wan
9c1aa6da3d chore: refact dart code generation (#3726) 2023-11-18 22:41:26 +08:00
anstns
da67ea2300 add map type (#3704) 2023-11-18 13:55:13 +00:00
kesonan
72dd2736f5 change command-line arg 'table' from string to slice type (#3707) 2023-11-13 11:46:17 +00:00
dependabot[bot]
24695bba09 chore(deps): bump golang.org/x/net from 0.17.0 to 0.18.0 (#3709)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-10 10:55:38 +08:00
dependabot[bot]
c7c43062c5 chore(deps): bump golang.org/x/text from 0.13.0 to 0.14.0 in /tools/goctl (#3701)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-07 20:25:33 +08:00
dependabot[bot]
97e1ea0633 chore(deps): bump github.com/spf13/cobra from 1.7.0 to 1.8.0 in /tools/goctl (#3700)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-07 20:11:03 +08:00
dependabot[bot]
04b9737a61 chore(deps): bump github.com/fatih/color from 1.15.0 to 1.16.0 (#3698)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-07 19:31:41 +08:00
dependabot[bot]
b0fb246693 chore(deps): bump golang.org/x/sys from 0.13.0 to 0.14.0 (#3699)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-07 18:03:49 +08:00
dependabot[bot]
41140ac78c chore(deps): bump golang.org/x/time from 0.3.0 to 0.4.0 (#3697)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-07 17:44:25 +08:00
dependabot[bot]
1281904572 chore(deps): bump github.com/jackc/pgx/v5 from 5.4.3 to 5.5.0 (#3696)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-07 11:32:10 +08:00
kesonan
c8a8ff7cad Feat/default new api parser (#3683) 2023-11-04 14:48:44 +00:00
zhaolei
df2799fff1 fix import error if generate multiple proto (#3694) 2023-11-04 12:42:25 +00:00
dependabot[bot]
fd8ee0b851 chore(deps): bump github.com/emicklei/proto from 1.12.1 to 1.12.2 in /tools/goctl (#3690)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-11-03 10:24:21 +08:00
Ash V
6ecc5e7b73 Enhanced the CODE_OF_CONDUCT Guidelines (#3680) 2023-10-29 07:07:02 +00:00
Kevin Wan
52963c2ebf chore: update go-zero version to v1.6.0 in goctl (#3679) 2023-10-28 21:42:16 +08:00
Kevin Wan
07e3e14c0e chore: remove go build version in fuzz test (#3678) 2023-10-28 20:53:03 +08:00
Rene Leonhardt
34c5f6616c chore: upgrade go to 1.19 (#3677) 2023-10-28 12:12:04 +00:00
MarkJoyMa
32600f2619 fix: adjust log encode output mode (#3676) 2023-10-28 11:46:52 +00:00
dependabot[bot]
b07df1c344 chore(deps): bump go.etcd.io/etcd/client/v3 from 3.5.9 to 3.5.10 (#3675)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-28 13:16:25 +08:00
Kevin Wan
a1fca3a1da chore: upgrade go dependencies (#3657) 2023-10-28 00:19:22 +08:00
Kevin Wan
9394e59597 chore: update goctl version to 1.6.0 (#3674) 2023-10-27 21:59:35 +08:00
dependabot[bot]
f8adc71529 chore(deps): bump github.com/google/uuid from 1.3.1 to 1.4.0 (#3673)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-27 21:34:27 +08:00
MarkJoyMa
c05e03bb5a feat: add metrics (#3624) 2023-10-26 15:51:28 +00:00
Kevin Wan
199e86050e chore: simplify prometheus check (#3672) 2023-10-26 20:32:13 +08:00
#Suyghur
1e2a12b3d6 feat(metric): added Dec() and Sub() in GaugeVec interface (#3666) 2023-10-26 20:13:42 +08:00
Kevin Wan
922efbfc2d chore: refactor zrpc timeout (#3671) 2023-10-26 08:55:26 +08:00
vankillua
842c4d81cc feat: support the specified timeout of rpc methods (#2742)
Co-authored-by: hanzijian <hanzijian@52tt.com>
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
2023-10-25 13:01:57 +00:00
dependabot[bot]
2a335c7608 chore(deps): bump github.com/fullstorydev/grpcurl from 1.8.8 to 1.8.9 (#3668)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-25 15:15:30 +08:00
Bhargav Shirin Nalamati
35edd6b19d fixed typo: reds to redis (#3664) 2023-10-24 02:54:15 +00:00
Kevin Wan
36bbc6a2e2 chore: add error handling on registering event handlers to k8s (#3663) 2023-10-23 21:57:09 +08:00
唐小鸭
e20ccdd011 Support for resource injection (#3383)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
2023-10-23 13:22:16 +00:00
Kevin Wan
c2ff00883a chore: update restful/grpc servers shutdown stages (#3662) 2023-10-23 13:03:05 +00:00
MarkJoyMa
00db97fcc1 feat: model add withSession (#3658) 2023-10-23 04:43:05 +00:00
7134g
117c3a9069 fix: multiple files import the same api file (#3642) 2023-10-23 04:04:52 +00:00
Kevin Wan
172ff407f3 chore: refactor mongo logs (#3660) 2023-10-23 11:03:55 +08:00
shenbaise9527
a242fec5e1 feat: support for disable mon logs like sqlx (#3606)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
2023-10-22 15:00:02 +00:00
Kevin Wan
6286941ebf chore: add go-zero users (#3659) 2023-10-22 22:23:24 +08:00
Kevin Wan
42e0a6f90c chore: refactor errors to use errors.Is (#3654) 2023-10-21 00:00:57 +08:00
cary
81ae7d36b5 Support for adding ignore_columns parameters to the goctl model pg (#3427) 2023-10-20 08:58:18 +00:00
Kevin Wan
944e76edb9 chore: refactor errors (#3651) 2023-10-20 14:58:38 +08:00
MarkJoyMa
151768ef82 feat: optimize logx print error (#3649) 2023-10-19 13:46:52 +00:00
Surav Shrestha
50581c7f5c docs fix typo in core/logx/readme.md (#3650) 2023-10-19 13:33:25 +00:00
dependabot[bot]
54041ef9e4 chore(deps): bump google.golang.org/grpc from 1.58.2 to 1.59.0 in /tools/goctl (#3645) 2023-10-19 12:38:08 +08:00
dependabot[bot]
5a9ae5ef02 chore(deps): bump google.golang.org/grpc from 1.58.2 to 1.59.0 (#3647) 2023-10-19 12:26:36 +08:00
guonaihong
19de13bb04 Upgrade grpc-go,fix 0day problem. (#3623) 2023-10-19 03:45:59 +00:00
Kevin Wan
3ab4e82168 chore: upgrade go to 1.19 (#3648) 2023-10-19 11:30:37 +08:00
Armaan
619e838513 updated CONTRIBUTING.md with emojified , fun, precise and engaging text (#3643) 2023-10-19 03:06:38 +00:00
kesonan
423597a01c feat: export devserver.Config (#3638) 2023-10-17 15:38:21 +00:00
kesonan
d84dfe1b20 fix: goctl unit test (#3636) 2023-10-17 11:15:32 +00:00
Soham Tembhurne
87b7a1120d Update documenation section #background (#3634) 2023-10-16 00:11:17 +00:00
Kevin Wan
528af8a99d chore: update readme for Mac install instructions (#3633) 2023-10-16 08:08:42 +08:00
Soham Tembhurne
17fc68ac5a Update readme.md (#3630) 2023-10-15 23:45:25 +08:00
Kevin Wan
804a56bd14 fix: optimize logx for less GC objects (#3627) 2023-10-15 23:37:45 +08:00
Kevin Wan
88f60d7736 chore: refactor signal sigterm and sigint (#3632) 2023-10-15 23:24:17 +08:00
#Suyghur
95b7a3d3ce feat: add the SIGINT signal in signals.go to subscribe the user input ctrl+c to exit the application operation (#3611) 2023-10-15 22:58:15 +08:00
Kevin Wan
d71c0da7b7 chore: refactor error comparison (#3629) 2023-10-15 13:41:06 +00:00
Kevin Wan
fd070fec91 feat: retry with ctx deadline (#3626) 2023-10-15 13:39:44 +00:00
Kevin Wan
4f22034342 fix: unmarshal from number to string with incorrect error message (#3625) 2023-10-15 02:06:00 +00:00
Ikko Eltociear Ashimine
b731aa38af refactor: update builder.go (#3620) 2023-10-13 07:03:15 +00:00
dependabot[bot]
bf996a1812 chore(deps): bump github.com/alicebob/miniredis/v2 from 2.30.5 to 2.31.0 (#3616)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-12 11:44:55 +08:00
dependabot[bot]
af7ce65244 chore(deps): bump golang.org/x/net from 0.15.0 to 0.17.0 in /tools/goctl (#3618)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-12 11:37:06 +08:00
dependabot[bot]
952db71835 chore(deps): bump golang.org/x/net from 0.16.0 to 0.17.0 (#3612)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-11 17:37:14 +08:00
Kevin Wan
abd1fa96a9 fix: UpdateStmt doesn't update the statement correctly in sqlx/bulkinserter.go (#3607) 2023-10-09 21:57:26 +08:00
Kevin Wan
5aedd9c076 chore: simplify parsing numbers with overflow (#3610) 2023-10-09 13:00:09 +00:00
Kevin Wan
ff230c4b1d chore: refactor goctl api (#3605) 2023-10-07 22:58:29 +08:00
kesonan
02c95108b9 optimize: fix experimental api (#3604) 2023-10-07 19:48:41 +08:00
dependabot[bot]
1ff541afe4 chore(deps): bump golang.org/x/net from 0.15.0 to 0.16.0 (#3603)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-07 10:50:49 +08:00
Kevin Wan
11a8cbc1e5 chore: rename noOpBreaker to nopBreaker (#3602) 2023-10-06 23:41:09 +08:00
dependabot[bot]
c063976822 chore(deps): bump golang.org/x/sys from 0.12.0 to 0.13.0 (#3601)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-06 22:04:14 +08:00
dependabot[bot]
cb707034ce chore(deps): bump github.com/jhump/protoreflect from 1.15.2 to 1.15.3 (#3600)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-05 15:26:21 +08:00
dependabot[bot]
f10db27efd chore(deps): bump github.com/prometheus/client_golang from 1.16.0 to 1.17.0 (#3594)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-28 11:38:20 +08:00
Kevin Wan
4878f90546 chore: update goctl version to 1.5.6 (#3593) 2023-09-27 22:55:50 +08:00
202 changed files with 5484 additions and 3865 deletions

View File

@@ -1,3 +1,7 @@
coverage:
status:
patch: true
project: false # disabled because project coverage is not stable
comment: comment:
layout: "flags, files" layout: "flags, files"
behavior: once behavior: once

View File

@@ -1 +1,7 @@
**/.git **/.git
.dockerignore
Dockerfile
goctl
Makefile
readme.md
readme-cn.md

View File

@@ -5,6 +5,14 @@
version: 2 version: 2
updates: updates:
- package-ecosystem: "docker" # Update image tags in Dockerfile
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "github-actions" # Update GitHub Actions
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "gomod" # See documentation for possible values - package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests directory: "/" # Location of package manifests
schedule: schedule:

View File

@@ -35,11 +35,11 @@ jobs:
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v3 uses: actions/checkout@v4
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL - name: Initialize CodeQL
uses: github/codeql-action/init@v2 uses: github/codeql-action/init@v3
with: with:
languages: ${{ matrix.language }} languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file. # If you wish to specify custom queries, you can do so here or in a config file.
@@ -50,7 +50,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below) # If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild - name: Autobuild
uses: github/codeql-action/autobuild@v2 uses: github/codeql-action/autobuild@v3
# Command-line programs to run using the OS shell. # Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl # 📚 https://git.io/JvXDl
@@ -64,4 +64,4 @@ jobs:
# make release # make release
- name: Perform CodeQL Analysis - name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2 uses: github/codeql-action/analyze@v3

View File

@@ -12,12 +12,12 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- 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@v4
- name: Set up Go 1.x - name: Set up Go 1.x
uses: actions/setup-go@v3 uses: actions/setup-go@v5
with: with:
go-version: 1.18 go-version: '1.19'
check-latest: true check-latest: true
cache: true cache: true
id: go id: go
@@ -40,20 +40,20 @@ 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@v3 uses: codecov/codecov-action@v4
test-win: test-win:
name: Windows name: Windows
runs-on: windows-latest runs-on: windows-latest
steps: steps:
- name: Checkout codebase - name: Checkout codebase
uses: actions/checkout@v3 uses: actions/checkout@v4
- name: Set up Go 1.x - name: Set up Go 1.x
uses: actions/setup-go@v3 uses: actions/setup-go@v5
with: with:
# use 1.18 to guarantee Go 1.18 compatibility # use 1.19 to guarantee Go 1.19 compatibility
go-version: 1.18 go-version: '1.19'
check-latest: true check-latest: true
cache: true cache: true

View File

@@ -7,7 +7,7 @@ jobs:
close-issues: close-issues:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/stale@v6 - uses: actions/stale@v9
with: with:
days-before-issue-stale: 365 days-before-issue-stale: 365
days-before-issue-close: 90 days-before-issue-close: 90

View File

@@ -16,13 +16,13 @@ jobs:
- goarch: "386" - goarch: "386"
goos: darwin goos: darwin
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- uses: zeromicro/go-zero-release-action@master - uses: zeromicro/go-zero-release-action@master
with: with:
github_token: ${{ secrets.GITHUB_TOKEN }} github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }} goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }} goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.18.10.linux-amd64.tar.gz" goversion: "https://dl.google.com/go/go1.19.13.linux-amd64.tar.gz"
project_path: "tools/goctl" project_path: "tools/goctl"
binary_name: "goctl" binary_name: "goctl"
extra_files: tools/goctl/readme.md tools/goctl/readme-cn.md extra_files: tools/goctl/readme.md tools/goctl/readme-cn.md

View File

@@ -5,7 +5,7 @@ jobs:
name: runner / staticcheck name: runner / staticcheck
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- uses: reviewdog/action-staticcheck@v1 - uses: reviewdog/action-staticcheck@v1
with: with:
github_token: ${{ secrets.github_token }} github_token: ${{ secrets.github_token }}

View File

@@ -1,102 +1,76 @@
# Contributing # 🚀 Contributing to go-zero
Welcome to go-zero! Welcome to the go-zero community! We're thrilled to have you here. Contributing to our project is a fantastic way to be a part of the go-zero journey. Let's make this guide exciting and fun!
- [Before you get started](#before-you-get-started) ## 📜 Before You Dive In
- [Code of Conduct](#code-of-conduct)
- [Community Expectations](#community-expectations)
- [Getting started](#getting-started)
- [Your First Contribution](#your-first-contribution)
- [Find something to work on](#find-something-to-work-on)
- [Find a good first topic](#find-a-good-first-topic)
- [Work on an Issue](#work-on-an-issue)
- [File an Issue](#file-an-issue)
- [Contributor Workflow](#contributor-workflow)
- [Creating Pull Requests](#creating-pull-requests)
- [Code Review](#code-review)
- [Testing](#testing)
# Before you get started ### 🤝 Code of Conduct
## Code of Conduct Let's start on the right foot. Please take a moment to read and embrace our [Code of Conduct](/code-of-conduct.md). We're all about creating a welcoming and respectful environment.
Please make sure to read and observe our [Code of Conduct](/code-of-conduct.md). ### 🌟 Community Expectations
## Community Expectations At go-zero, we're like a close-knit family, and we believe in creating a healthy, friendly, and productive atmosphere. It's all about sharing knowledge and building amazing things together.
go-zero is a community project driven by its community which strives to promote a healthy, friendly and productive environment. ## 🚀 Getting Started
go-zero is a web and rpc framework written in Go. It's born to ensure the stability of the busy sites with resilient design. Builtin goctl greatly improves the development productivity.
# Getting started Get your adventure rolling! Here's how to begin:
- Fork the repository on GitHub. 1. 🍴 **Fork the Repository**: Head over to the GitHub repository and fork it to your own space.
- Make your changes on your fork repository.
- Submit a PR.
2. 🛠️ **Make Your Magic**: Work your magic in your forked repository. Create new features, squash bugs, or improve documentation - it's your world to conquer!
# Your First Contribution 3. 🚀 **Submit a PR (Pull Request)**: When you're ready to unveil your creation, submit a Pull Request. We can't wait to see your awesome work!
We will help you to contribute in different areas like filing issues, developing features, fixing critical bugs and ## 🌟 Your First Contribution
getting your work reviewed and merged.
If you have questions about the development process, We're here to guide you on your quest to become a go-zero contributor. Whether you want to file issues, develop features, or tame some critical bugs, we've got you covered.
feel free to [file an issue](https://github.com/zeromicro/go-zero/issues/new/choose).
## Find something to work on If you have questions or need guidance at any stage, don't hesitate to [open an issue](https://github.com/zeromicro/go-zero/issues/new/choose).
We are always in need of help, be it fixing documentation, reporting bugs or writing some code. ## 🔍 Find Something to Work On
Look at places where you feel best coding practices aren't followed, code refactoring is needed or tests are missing.
Here is how you get started.
### Find a good first topic Ready to dive into the action? There are several ways to contribute:
[go-zero](https://github.com/zeromicro/go-zero) has beginner-friendly issues that provide a good first issue. ### 💼 Find a Good First Topic
For example, [go-zero](https://github.com/zeromicro/go-zero) has
[help wanted](https://github.com/zeromicro/go-zero/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) and
[good first issue](https://github.com/zeromicro/go-zero/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22)
labels for issues that should not need deep knowledge of the system.
We can help new contributors who wish to work on such issues.
Another good way to contribute is to find a documentation improvement, such as a missing/broken link. Discover easy-entry issues labeled as [help wanted](https://github.com/zeromicro/go-zero/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) or [good first issue](https://github.com/zeromicro/go-zero/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22). These issues are perfect for newcomers and don't require deep knowledge of the system. We're here to assist you with these tasks.
Please see [Contributing](#contributing) below for the workflow.
#### Work on an issue ### 🪄 Work on an Issue
When you are willing to take on an issue, just reply on the issue. The maintainer will assign it to you. Once you've picked an issue that excites you, let us know by commenting on it. Our maintainers will assign it to you, and you can embark on your mission!
### File an Issue ### 📢 File an Issue
While we encourage everyone to contribute code, it is also appreciated when someone reports an issue. Reporting an issue is just as valuable as code contributions. If you discover a problem, don't hesitate to [open an issue](https://github.com/zeromicro/go-zero/issues/new/choose). Be sure to follow our guidelines when submitting an issue.
Please follow the prompted submission guidelines while opening an issue. ## 🎯 Contributor Workflow
# Contributor Workflow Here's a rough guide to your contributor journey:
Please do not ever hesitate to ask a question or send a pull request. 1. 🌱 Create a New Branch: Start by creating a topic branch, usually based on the 'master' branch. This is where your contribution will grow.
This is a rough outline of what a contributor's workflow looks like: 2. 💡 Make Commits: Commit your work in logical units. Each commit should tell a story.
- Create a topic branch from where to base the contribution. This is usually master. 3. 🚀 Push Changes: Push the changes in your topic branch to your personal fork of the repository.
- Make commits of logical units.
- Push changes in a topic branch to a personal fork of the repository.
- Submit a pull request to [go-zero](https://github.com/zeromicro/go-zero).
## Creating Pull Requests 4. 📦 Submit a Pull Request: When your creation is complete, submit a Pull Request to the [go-zero repository](https://github.com/zeromicro/go-zero).
Pull requests are often called simply "PR". ## 🌠 Creating Pull Requests
go-zero generally follows the standard [github pull request](https://help.github.com/articles/about-pull-requests/) process.
To submit a proposed change, please develop the code/fix and add new test cases.
After that, run these local verifications before submitting pull request to predict the pass or
fail of continuous integration.
* Format the code with `gofmt` Pull Requests (PRs) are your way of making a grand entrance with your contribution. Here's how to do it:
* Run the test with data race enabled `go test -race ./...`
## Code Review - 💼 Format Your Code: Ensure your code is beautifully formatted with `gofmt`.
- 🏃 Run Tests: Verify that your changes pass all the tests, including data race tests. Run `go test -race ./...` for the ultimate validation.
To make it easier for your PR to receive reviews, consider the reviewers will need you to: ## 👁️‍🗨️ Code Review
* follow [good coding guidelines](https://github.com/golang/go/wiki/CodeReviewComments). Getting your PR reviewed is the final step before your contribution becomes part of go-zero's magical world. To make the process smooth, keep these things in mind:
* write [good commit messages](https://chris.beams.io/posts/git-commit/).
* break large changes into a logical series of smaller patches which individually make easily understandable changes, and in aggregate solve a broader issue.
- 🧙‍♀️ Follow Good Coding Practices: Stick to [good coding guidelines](https://github.com/golang/go/wiki/CodeReviewComments).
- 📝 Write Awesome Commit Messages: Craft [impressive commit messages](https://chris.beams.io/posts/git-commit/) - they're like spells in the wizard's book!
- 🔍 Break It Down: For larger changes, consider breaking them into a series of smaller, logical patches. Each patch should make an understandable and meaningful improvement.
Congratulations on your contribution journey! We're thrilled to have you as part of our go-zero community. Let's make amazing things together! 🌟
Now, go out there and start your adventure! If you have any more magical ideas to enhance this guide, please share them. 🔥

View File

@@ -1,76 +1,127 @@
# Contributor Covenant Code of Conduct # Contributor Covenant Code of Conduct
## Our Pledge ## Our Pledge
In the interest of fostering an open and welcoming environment, we as We as members, contributors, and leaders pledge to make participation in our
contributors and maintainers pledge to make participation in our project and community a harassment-free experience for everyone, regardless of age, body
our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender
size, disability, ethnicity, sex characteristics, gender identity and expression, identity and expression, level of experience, education, socio-economic status,
level of experience, education, socio-economic status, nationality, personal nationality, personal appearance, race, caste, color, religion, or sexual
appearance, race, religion, or sexual identity and orientation. identity and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards ## Our Standards
Examples of behavior that contributes to creating a positive environment Examples of behavior that contributes to a positive environment for our
include: community include:
* Using welcoming and inclusive language * Demonstrating empathy and kindness toward other people
* Being respectful of differing viewpoints and experiences * Being respectful of differing opinions, viewpoints, and experiences
* Gracefully accepting constructive criticism * Giving and gracefully accepting constructive feedback
* Focusing on what is best for the community * Accepting responsibility and apologizing to those affected by our mistakes,
* Showing empathy towards other community members and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall
community
Examples of unacceptable behavior by participants include: Examples of unacceptable behavior include:
* The use of sexualized language or imagery and unwelcome sexual attention or * The use of sexualized language or imagery, and sexual attention or advances of
advances any kind
* Trolling, insulting/derogatory comments, and personal or political attacks * Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment * Public or private harassment
* Publishing others' private information, such as a physical or electronic * Publishing others' private information, such as a physical or email address,
address, without explicit permission without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a * Other conduct which could reasonably be considered inappropriate in a
professional setting professional setting
## Our Responsibilities ## Enforcement Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable Community leaders are responsible for clarifying and enforcing our standards of
behavior and are expected to take appropriate and fair corrective action in acceptable behavior and will take appropriate and fair corrective action in
response to any instances of unacceptable behavior. response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Project maintainers have the right and responsibility to remove, edit, or Community leaders have the right and responsibility to remove, edit, or reject
reject comments, commits, code, wiki edits, issues, and other contributions comments, commits, code, wiki edits, issues, and other contributions that are
that are not aligned to this Code of Conduct, or to ban temporarily or not aligned to this Code of Conduct, and will communicate reasons for moderation
permanently any contributor for other behaviors that they deem inappropriate, decisions when appropriate.
threatening, offensive, or harmful.
## Scope ## Scope
This Code of Conduct applies within all project spaces, and it also applies when This Code of Conduct applies within all community spaces, and also applies when
an individual is representing the project or its community in public spaces. an individual is officially representing the community in public spaces.
Examples of representing a project or community include using an official Examples of representing our community include using an official e-mail address,
project e-mail address, posting via an official social media account, or acting posting via an official social media account, or acting as an appointed
as an appointed representative at an online or offline event. Representation of representative at an online or offline event.
a project may be further defined and clarified by project maintainers.
## Enforcement ## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at [INSERT EMAIL ADDRESS]. All reported to the community leaders responsible for enforcement at
complaints will be reviewed and investigated and will result in a response that [INSERT CONTACT METHOD].
is deemed necessary and appropriate to the circumstances. The project team is All complaints will be reviewed and investigated promptly and fairly.
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good All community leaders are obligated to respect the privacy and security of the
faith may face temporary or permanent repercussions as determined by other reporter of any incident.
members of the project's leadership.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series of
actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within the
community.
## Attribution ## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, This Code of Conduct is adapted from the [Contributor Covenant][homepage],
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html version 2.1, available at
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
[homepage]: https://www.contributor-covenant.org Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
For answers to common questions about this code of conduct, see For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
[https://www.contributor-covenant.org/translations][translations].

View File

@@ -46,23 +46,26 @@ type (
// DoWithAcceptable returns an error instantly if the Breaker rejects the request. // DoWithAcceptable returns an error instantly if the Breaker rejects the request.
// If a panic occurs in the request, the Breaker handles it as an error // If a panic occurs in the request, the Breaker handles it as an error
// and causes the same panic again. // and causes the same panic again.
// acceptable checks if it's a successful call, even if the err is not nil. // acceptable checks if it's a successful call, even if the error is not nil.
DoWithAcceptable(req func() error, acceptable Acceptable) error DoWithAcceptable(req func() error, acceptable Acceptable) error
// DoWithFallback runs the given request if the Breaker accepts it. // DoWithFallback runs the given request if the Breaker accepts it.
// DoWithFallback runs the fallback if the Breaker rejects the request. // DoWithFallback runs the fallback if the Breaker rejects the request.
// If a panic occurs in the request, the Breaker handles it as an error // If a panic occurs in the request, the Breaker handles it as an error
// and causes the same panic again. // and causes the same panic again.
DoWithFallback(req func() error, fallback func(err error) error) error DoWithFallback(req func() error, fallback Fallback) error
// DoWithFallbackAcceptable runs the given request if the Breaker accepts it. // DoWithFallbackAcceptable runs the given request if the Breaker accepts it.
// DoWithFallbackAcceptable runs the fallback if the Breaker rejects the request. // DoWithFallbackAcceptable runs the fallback if the Breaker rejects the request.
// If a panic occurs in the request, the Breaker handles it as an error // If a panic occurs in the request, the Breaker handles it as an error
// and causes the same panic again. // and causes the same panic again.
// acceptable checks if it's a successful call, even if the err is not nil. // acceptable checks if it's a successful call, even if the error is not nil.
DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error DoWithFallbackAcceptable(req func() error, fallback Fallback, acceptable Acceptable) error
} }
// Fallback is the func to be called if the request is rejected.
Fallback func(err error) error
// Option defines the method to customize a Breaker. // Option defines the method to customize a Breaker.
Option func(breaker *circuitBreaker) Option func(breaker *circuitBreaker)
@@ -86,12 +89,12 @@ type (
internalThrottle interface { internalThrottle interface {
allow() (internalPromise, error) allow() (internalPromise, error)
doReq(req func() error, fallback func(err error) error, acceptable Acceptable) error doReq(req func() error, fallback Fallback, acceptable Acceptable) error
} }
throttle interface { throttle interface {
allow() (Promise, error) allow() (Promise, error)
doReq(req func() error, fallback func(err error) error, acceptable Acceptable) error doReq(req func() error, fallback Fallback, acceptable Acceptable) error
} }
) )
@@ -122,11 +125,11 @@ func (cb *circuitBreaker) DoWithAcceptable(req func() error, acceptable Acceptab
return cb.throttle.doReq(req, nil, acceptable) return cb.throttle.doReq(req, nil, acceptable)
} }
func (cb *circuitBreaker) DoWithFallback(req func() error, fallback func(err error) error) error { func (cb *circuitBreaker) DoWithFallback(req func() error, fallback Fallback) error {
return cb.throttle.doReq(req, fallback, defaultAcceptable) return cb.throttle.doReq(req, fallback, defaultAcceptable)
} }
func (cb *circuitBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error, func (cb *circuitBreaker) DoWithFallbackAcceptable(req func() error, fallback Fallback,
acceptable Acceptable) error { acceptable Acceptable) error {
return cb.throttle.doReq(req, fallback, acceptable) return cb.throttle.doReq(req, fallback, acceptable)
} }
@@ -168,7 +171,7 @@ func (lt loggedThrottle) allow() (Promise, error) {
}, lt.logError(err) }, lt.logError(err)
} }
func (lt loggedThrottle) doReq(req func() error, fallback func(err error) error, acceptable Acceptable) error { func (lt loggedThrottle) doReq(req func() error, fallback Fallback, acceptable Acceptable) error {
return lt.logError(lt.internalThrottle.doReq(req, fallback, func(err error) bool { return lt.logError(lt.internalThrottle.doReq(req, fallback, func(err error) bool {
accept := acceptable(err) accept := acceptable(err)
if !accept && err != nil { if !accept && err != nil {
@@ -179,7 +182,7 @@ func (lt loggedThrottle) doReq(req func() error, fallback func(err error) error,
} }
func (lt loggedThrottle) logError(err error) error { func (lt loggedThrottle) logError(err error) error {
if err == ErrServiceUnavailable { if errors.Is(err, ErrServiceUnavailable) {
// if circuit open, not possible to have empty error window // if circuit open, not possible to have empty error window
stat.Report(fmt.Sprintf( stat.Report(fmt.Sprintf(
"proc(%s/%d), callee: %s, breaker is open and requests dropped\nlast errors:\n%s", "proc(%s/%d), callee: %s, breaker is open and requests dropped\nlast errors:\n%s",

View File

@@ -22,14 +22,14 @@ func DoWithAcceptable(name string, req func() error, acceptable Acceptable) erro
} }
// DoWithFallback calls Breaker.DoWithFallback on the Breaker with given name. // DoWithFallback calls Breaker.DoWithFallback on the Breaker with given name.
func DoWithFallback(name string, req func() error, fallback func(err error) error) error { func DoWithFallback(name string, req func() error, fallback Fallback) error {
return do(name, func(b Breaker) error { return do(name, func(b Breaker) error {
return b.DoWithFallback(req, fallback) return b.DoWithFallback(req, fallback)
}) })
} }
// DoWithFallbackAcceptable calls Breaker.DoWithFallbackAcceptable on the Breaker with given name. // DoWithFallbackAcceptable calls Breaker.DoWithFallbackAcceptable on the Breaker with given name.
func DoWithFallbackAcceptable(name string, req func() error, fallback func(err error) error, func DoWithFallbackAcceptable(name string, req func() error, fallback Fallback,
acceptable Acceptable) error { acceptable Acceptable) error {
return do(name, func(b Breaker) error { return do(name, func(b Breaker) error {
return b.DoWithFallbackAcceptable(req, fallback, acceptable) return b.DoWithFallbackAcceptable(req, fallback, acceptable)
@@ -59,7 +59,7 @@ func GetBreaker(name string) Breaker {
// NoBreakerFor disables the circuit breaker for the given name. // NoBreakerFor disables the circuit breaker for the given name.
func NoBreakerFor(name string) { func NoBreakerFor(name string) {
lock.Lock() lock.Lock()
breakers[name] = newNoOpBreaker() breakers[name] = NopBreaker()
lock.Unlock() lock.Unlock()
} }

View File

@@ -30,7 +30,7 @@ func TestBreakersDoWithAcceptable(t *testing.T) {
assert.Equal(t, errDummy, GetBreaker("anyone").DoWithAcceptable(func() error { assert.Equal(t, errDummy, GetBreaker("anyone").DoWithAcceptable(func() error {
return errDummy return errDummy
}, func(err error) bool { }, func(err error) bool {
return err == nil || err == errDummy return err == nil || errors.Is(err, errDummy)
})) }))
} }
verify(t, func() bool { verify(t, func() bool {
@@ -45,12 +45,12 @@ func TestBreakersDoWithAcceptable(t *testing.T) {
}, func(err error) bool { }, func(err error) bool {
return err == nil return err == nil
}) })
assert.True(t, err == errDummy || err == ErrServiceUnavailable) assert.True(t, errors.Is(err, errDummy) || errors.Is(err, ErrServiceUnavailable))
} }
verify(t, func() bool { verify(t, func() bool {
return ErrServiceUnavailable == Do("another", func() error { return errors.Is(Do("another", func() error {
return nil return nil
}) }), ErrServiceUnavailable)
}) })
} }
@@ -75,12 +75,12 @@ func TestBreakersFallback(t *testing.T) {
}, func(err error) error { }, func(err error) error {
return nil return nil
}) })
assert.True(t, err == nil || err == errDummy) assert.True(t, err == nil || errors.Is(err, errDummy))
} }
verify(t, func() bool { verify(t, func() bool {
return ErrServiceUnavailable == Do("fallback", func() error { return errors.Is(Do("fallback", func() error {
return nil return nil
}) }), ErrServiceUnavailable)
}) })
} }
@@ -94,12 +94,12 @@ func TestBreakersAcceptableFallback(t *testing.T) {
}, func(err error) bool { }, func(err error) bool {
return err == nil return err == nil
}) })
assert.True(t, err == nil || err == errDummy) assert.True(t, err == nil || errors.Is(err, errDummy))
} }
verify(t, func() bool { verify(t, func() bool {
return ErrServiceUnavailable == Do("acceptablefallback", func() error { return errors.Is(Do("acceptablefallback", func() error {
return nil return nil
}) }), ErrServiceUnavailable)
}) })
} }

View File

@@ -1,7 +1,6 @@
package breaker package breaker
import ( import (
"math"
"time" "time"
"github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/core/collection"
@@ -38,7 +37,8 @@ func (b *googleBreaker) accept() error {
accepts, total := b.history() accepts, total := b.history()
weightedAccepts := b.k * float64(accepts) weightedAccepts := b.k * float64(accepts)
// https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101 // https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101
dropRatio := math.Max(0, (float64(total-protection)-weightedAccepts)/float64(total+1)) // for better performance, no need to care about negative ratio
dropRatio := (float64(total-protection) - weightedAccepts) / float64(total+1)
if dropRatio <= 0 { if dropRatio <= 0 {
return nil return nil
} }
@@ -60,8 +60,9 @@ func (b *googleBreaker) allow() (internalPromise, error) {
}, nil }, nil
} }
func (b *googleBreaker) doReq(req func() error, fallback func(err error) error, acceptable Acceptable) error { func (b *googleBreaker) doReq(req func() error, fallback Fallback, acceptable Acceptable) error {
if err := b.accept(); err != nil { if err := b.accept(); err != nil {
b.markFailure()
if fallback != nil { if fallback != nil {
return fallback(err) return fallback(err)
} }
@@ -69,18 +70,19 @@ func (b *googleBreaker) doReq(req func() error, fallback func(err error) error,
return err return err
} }
var success bool
defer func() { defer func() {
if e := recover(); e != nil { // if req() panic, success is false, mark as failure
if success {
b.markSuccess()
} else {
b.markFailure() b.markFailure()
panic(e)
} }
}() }()
err := req() err := req()
if acceptable(err) { if acceptable(err) {
b.markSuccess() success = true
} else {
b.markFailure()
} }
return err return err

View File

@@ -95,7 +95,7 @@ func TestGoogleBreakerAcceptable(t *testing.T) {
assert.Equal(t, errAcceptable, b.doReq(func() error { assert.Equal(t, errAcceptable, b.doReq(func() error {
return errAcceptable return errAcceptable
}, nil, func(err error) bool { }, nil, func(err error) bool {
return err == errAcceptable return errors.Is(err, errAcceptable)
})) }))
} }
@@ -105,7 +105,7 @@ func TestGoogleBreakerNotAcceptable(t *testing.T) {
assert.Equal(t, errAcceptable, b.doReq(func() error { assert.Equal(t, errAcceptable, b.doReq(func() error {
return errAcceptable return errAcceptable
}, nil, func(err error) bool { }, nil, func(err error) bool {
return err != errAcceptable return !errors.Is(err, errAcceptable)
})) }))
} }
@@ -206,7 +206,7 @@ func BenchmarkGoogleBreakerAllow(b *testing.B) {
breaker := getGoogleBreaker() breaker := getGoogleBreaker()
b.ResetTimer() b.ResetTimer()
for i := 0; i <= b.N; i++ { for i := 0; i <= b.N; i++ {
breaker.accept() _ = breaker.accept()
if i%2 == 0 { if i%2 == 0 {
breaker.markSuccess() breaker.markSuccess()
} else { } else {
@@ -215,6 +215,16 @@ func BenchmarkGoogleBreakerAllow(b *testing.B) {
} }
} }
func BenchmarkGoogleBreakerDoReq(b *testing.B) {
breaker := getGoogleBreaker()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
_ = breaker.doReq(func() error {
return nil
}, nil, defaultAcceptable)
}
}
func markSuccess(b *googleBreaker, count int) { func markSuccess(b *googleBreaker, count int) {
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
p, err := b.allow() p, err := b.allow()

View File

@@ -1,35 +1,35 @@
package breaker package breaker
const noOpBreakerName = "nopBreaker" const nopBreakerName = "nopBreaker"
type noOpBreaker struct{} type nopBreaker struct{}
func newNoOpBreaker() Breaker { // NopBreaker returns a breaker that never trigger breaker circuit.
return noOpBreaker{} func NopBreaker() Breaker {
return nopBreaker{}
} }
func (b noOpBreaker) Name() string { func (b nopBreaker) Name() string {
return noOpBreakerName return nopBreakerName
} }
func (b noOpBreaker) Allow() (Promise, error) { func (b nopBreaker) Allow() (Promise, error) {
return nopPromise{}, nil return nopPromise{}, nil
} }
func (b noOpBreaker) Do(req func() error) error { func (b nopBreaker) Do(req func() error) error {
return req() return req()
} }
func (b noOpBreaker) DoWithAcceptable(req func() error, _ Acceptable) error { func (b nopBreaker) DoWithAcceptable(req func() error, _ Acceptable) error {
return req() return req()
} }
func (b noOpBreaker) DoWithFallback(req func() error, _ func(err error) error) error { func (b nopBreaker) DoWithFallback(req func() error, _ Fallback) error {
return req() return req()
} }
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, _ func(err error) error, func (b nopBreaker) DoWithFallbackAcceptable(req func() error, _ Fallback, _ Acceptable) error {
_ Acceptable) error {
return req() return req()
} }

View File

@@ -8,8 +8,8 @@ import (
) )
func TestNopBreaker(t *testing.T) { func TestNopBreaker(t *testing.T) {
b := newNoOpBreaker() b := NopBreaker()
assert.Equal(t, noOpBreakerName, b.Name()) assert.Equal(t, nopBreakerName, b.Name())
p, err := b.Allow() p, err := b.Allow()
assert.Nil(t, err) assert.Nil(t, err)
p.Accept() p.Accept()

View File

@@ -25,8 +25,14 @@ func (r *Ring) Add(v any) {
r.lock.Lock() r.lock.Lock()
defer r.lock.Unlock() defer r.lock.Unlock()
r.elements[r.index%len(r.elements)] = v rlen := len(r.elements)
r.elements[r.index%rlen] = v
r.index++ r.index++
// prevent ring index overflow
if r.index >= rlen<<1 {
r.index -= rlen
}
} }
// Take takes all items from r. // Take takes all items from r.
@@ -36,16 +42,18 @@ func (r *Ring) Take() []any {
var size int var size int
var start int var start int
if r.index > len(r.elements) { rlen := len(r.elements)
size = len(r.elements)
start = r.index % len(r.elements) if r.index > rlen {
size = rlen
start = r.index % rlen
} else { } else {
size = r.index size = r.index
} }
elements := make([]any, size) elements := make([]any, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
elements[i] = r.elements[(start+i)%len(r.elements)] elements[i] = r.elements[(start+i)%rlen]
} }
return elements return elements

View File

@@ -12,7 +12,7 @@ type RestfulConf struct {
MaxConns int `json:",default=10000"` MaxConns int `json:",default=10000"`
MaxBytes int64 `json:",default=1048576"` MaxBytes int64 `json:",default=1048576"`
Timeout time.Duration `json:",default=3s"` Timeout time.Duration `json:",default=3s"`
CpuThreshold int64 `json:",default=900,range=[0:1000]"` CpuThreshold int64 `json:",default=900,range=[0:1000)"`
} }
``` ```

View File

@@ -2,6 +2,7 @@ package internal
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"sort" "sort"
@@ -14,6 +15,7 @@ import (
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/threading" "github.com/zeromicro/go-zero/core/threading"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
clientv3 "go.etcd.io/etcd/client/v3" clientv3 "go.etcd.io/etcd/client/v3"
) )
@@ -22,6 +24,7 @@ var (
clusters: make(map[string]*cluster), clusters: make(map[string]*cluster),
} }
connManager = syncx.NewResourceManager() connManager = syncx.NewResourceManager()
errClosed = errors.New("etcd monitor chan has been closed")
) )
// A Registry is a registry that manages the etcd client connections. // A Registry is a registry that manages the etcd client connections.
@@ -288,40 +291,47 @@ func (c *cluster) reload(cli EtcdClient) {
func (c *cluster) watch(cli EtcdClient, key string, rev int64) { func (c *cluster) watch(cli EtcdClient, key string, rev int64) {
for { for {
if c.watchStream(cli, key, rev) { err := c.watchStream(cli, key, rev)
if err == nil {
return return
} }
if rev != 0 && errors.Is(err, rpctypes.ErrCompacted) {
logx.Errorf("etcd watch stream has been compacted, try to reload, rev %d", rev)
rev = c.load(cli, key)
}
// log the error and retry
logx.Error(err)
} }
} }
func (c *cluster) watchStream(cli EtcdClient, key string, rev int64) bool { func (c *cluster) watchStream(cli EtcdClient, key string, rev int64) error {
var rch clientv3.WatchChan var rch clientv3.WatchChan
if rev != 0 { if rev != 0 {
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix(), rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key),
clientv3.WithRev(rev+1)) clientv3.WithPrefix(), clientv3.WithRev(rev+1))
} else { } else {
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix()) rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key),
clientv3.WithPrefix())
} }
for { for {
select { select {
case wresp, ok := <-rch: case wresp, ok := <-rch:
if !ok { if !ok {
logx.Error("etcd monitor chan has been closed") return errClosed
return false
} }
if wresp.Canceled { if wresp.Canceled {
logx.Errorf("etcd monitor chan has been canceled, error: %v", wresp.Err()) return fmt.Errorf("etcd monitor chan has been canceled, error: %w", wresp.Err())
return false
} }
if wresp.Err() != nil { if wresp.Err() != nil {
logx.Error(fmt.Sprintf("etcd monitor chan error: %v", wresp.Err())) return fmt.Errorf("etcd monitor chan error: %w", wresp.Err())
return false
} }
c.handleWatchEvents(key, wresp.Events) c.handleWatchEvents(key, wresp.Events)
case <-c.done: case <-c.done:
return true return nil
} }
} }
} }

View File

@@ -1,11 +1,15 @@
package errorx package errorx
import "bytes" import (
"bytes"
"sync"
)
type ( type (
// A BatchError is an error that can hold multiple errors. // A BatchError is an error that can hold multiple errors.
BatchError struct { BatchError struct {
errs errorArray errs errorArray
lock sync.Mutex
} }
errorArray []error errorArray []error
@@ -13,6 +17,9 @@ type (
// Add adds errs to be, nil errors are ignored. // Add adds errs to be, nil errors are ignored.
func (be *BatchError) Add(errs ...error) { func (be *BatchError) Add(errs ...error) {
be.lock.Lock()
defer be.lock.Unlock()
for _, err := range errs { for _, err := range errs {
if err != nil { if err != nil {
be.errs = append(be.errs, err) be.errs = append(be.errs, err)
@@ -22,6 +29,9 @@ func (be *BatchError) Add(errs ...error) {
// Err returns an error that represents all errors. // Err returns an error that represents all errors.
func (be *BatchError) Err() error { func (be *BatchError) Err() error {
be.lock.Lock()
defer be.lock.Unlock()
switch len(be.errs) { switch len(be.errs) {
case 0: case 0:
return nil return nil
@@ -34,6 +44,9 @@ func (be *BatchError) Err() error {
// NotNil checks if any error inside. // NotNil checks if any error inside.
func (be *BatchError) NotNil() bool { func (be *BatchError) NotNil() bool {
be.lock.Lock()
defer be.lock.Unlock()
return len(be.errs) > 0 return len(be.errs) > 0
} }

View File

@@ -3,6 +3,7 @@ package errorx
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -33,7 +34,7 @@ func TestBatchErrorNilFromFunc(t *testing.T) {
func TestBatchErrorOneError(t *testing.T) { func TestBatchErrorOneError(t *testing.T) {
var batch BatchError var batch BatchError
batch.Add(errors.New(err1)) batch.Add(errors.New(err1))
assert.NotNil(t, batch) assert.NotNil(t, batch.Err())
assert.Equal(t, err1, batch.Err().Error()) assert.Equal(t, err1, batch.Err().Error())
assert.True(t, batch.NotNil()) assert.True(t, batch.NotNil())
} }
@@ -42,7 +43,26 @@ func TestBatchErrorWithErrors(t *testing.T) {
var batch BatchError var batch BatchError
batch.Add(errors.New(err1)) batch.Add(errors.New(err1))
batch.Add(errors.New(err2)) batch.Add(errors.New(err2))
assert.NotNil(t, batch) assert.NotNil(t, batch.Err())
assert.Equal(t, fmt.Sprintf("%s\n%s", err1, err2), batch.Err().Error()) assert.Equal(t, fmt.Sprintf("%s\n%s", err1, err2), batch.Err().Error())
assert.True(t, batch.NotNil()) assert.True(t, batch.NotNil())
} }
func TestBatchErrorConcurrentAdd(t *testing.T) {
const count = 10000
var batch BatchError
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
defer wg.Done()
batch.Add(errors.New(err1))
}()
}
wg.Wait()
assert.NotNil(t, batch.Err())
assert.Equal(t, count, len(batch.errs))
assert.True(t, batch.NotNil())
}

View File

@@ -10,16 +10,15 @@ import (
const defaultRetryTimes = 3 const defaultRetryTimes = 3
var errTimeout = errors.New("retry timeout")
type ( type (
// RetryOption defines the method to customize DoWithRetry. // RetryOption defines the method to customize DoWithRetry.
RetryOption func(*retryOptions) RetryOption func(*retryOptions)
retryOptions struct { retryOptions struct {
times int times int
interval time.Duration interval time.Duration
timeout time.Duration timeout time.Duration
ignoreErrors []error
} }
) )
@@ -28,7 +27,7 @@ type (
// and performs modification operations, it is best to lock them, // and performs modification operations, it is best to lock them,
// otherwise there may be data race issues // otherwise there may be data race issues
func DoWithRetry(fn func() error, opts ...RetryOption) error { func DoWithRetry(fn func() error, opts ...RetryOption) error {
return retry(func(errChan chan error, retryCount int) { return retry(context.Background(), func(errChan chan error, retryCount int) {
errChan <- fn() errChan <- fn()
}, opts...) }, opts...)
} }
@@ -40,12 +39,12 @@ func DoWithRetry(fn func() error, opts ...RetryOption) error {
// otherwise there may be data race issues // otherwise there may be data race issues
func DoWithRetryCtx(ctx context.Context, fn func(ctx context.Context, retryCount int) error, func DoWithRetryCtx(ctx context.Context, fn func(ctx context.Context, retryCount int) error,
opts ...RetryOption) error { opts ...RetryOption) error {
return retry(func(errChan chan error, retryCount int) { return retry(ctx, func(errChan chan error, retryCount int) {
errChan <- fn(ctx, retryCount) errChan <- fn(ctx, retryCount)
}, opts...) }, opts...)
} }
func retry(fn func(errChan chan error, retryCount int), opts ...RetryOption) error { func retry(ctx context.Context, fn func(errChan chan error, retryCount int), opts ...RetryOption) error {
options := newRetryOptions() options := newRetryOptions()
for _, opt := range opts { for _, opt := range opts {
opt(options) opt(options)
@@ -53,7 +52,6 @@ func retry(fn func(errChan chan error, retryCount int), opts ...RetryOption) err
var berr errorx.BatchError var berr errorx.BatchError
var cancelFunc context.CancelFunc var cancelFunc context.CancelFunc
ctx := context.Background()
if options.timeout > 0 { if options.timeout > 0 {
ctx, cancelFunc = context.WithTimeout(ctx, options.timeout) ctx, cancelFunc = context.WithTimeout(ctx, options.timeout)
defer cancelFunc() defer cancelFunc()
@@ -66,19 +64,24 @@ func retry(fn func(errChan chan error, retryCount int), opts ...RetryOption) err
select { select {
case err := <-errChan: case err := <-errChan:
if err != nil { if err != nil {
for _, ignoreErr := range options.ignoreErrors {
if errors.Is(err, ignoreErr) {
return nil
}
}
berr.Add(err) berr.Add(err)
} else { } else {
return nil return nil
} }
case <-ctx.Done(): case <-ctx.Done():
berr.Add(errTimeout) berr.Add(ctx.Err())
return berr.Err() return berr.Err()
} }
if options.interval > 0 { if options.interval > 0 {
select { select {
case <-ctx.Done(): case <-ctx.Done():
berr.Add(errTimeout) berr.Add(ctx.Err())
return berr.Err() return berr.Err()
case <-time.After(options.interval): case <-time.After(options.interval):
} }
@@ -88,19 +91,28 @@ func retry(fn func(errChan chan error, retryCount int), opts ...RetryOption) err
return berr.Err() return berr.Err()
} }
// WithRetry customize a DoWithRetry call with given retry times. // WithIgnoreErrors Ignore the specified errors
func WithRetry(times int) RetryOption { func WithIgnoreErrors(ignoreErrors []error) RetryOption {
return func(options *retryOptions) { return func(options *retryOptions) {
options.times = times options.ignoreErrors = ignoreErrors
} }
} }
// WithInterval customizes a DoWithRetry call with given interval.
func WithInterval(interval time.Duration) RetryOption { func WithInterval(interval time.Duration) RetryOption {
return func(options *retryOptions) { return func(options *retryOptions) {
options.interval = interval options.interval = interval
} }
} }
// WithRetry customizes a DoWithRetry call with given retry times.
func WithRetry(times int) RetryOption {
return func(options *retryOptions) {
options.times = times
}
}
// WithTimeout customizes a DoWithRetry call with given timeout.
func WithTimeout(timeout time.Duration) RetryOption { func WithTimeout(timeout time.Duration) RetryOption {
return func(options *retryOptions) { return func(options *retryOptions) {
options.timeout = timeout options.timeout = timeout

View File

@@ -97,20 +97,70 @@ func TestRetryWithInterval(t *testing.T) {
} }
func TestRetryCtx(t *testing.T) { func TestRetryWithWithIgnoreErrors(t *testing.T) {
assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error { ignoreErr1 := errors.New("ignore error1")
if retryCount == 0 { ignoreErr2 := errors.New("ignore error2")
return errors.New("any") ignoreErrs := []error{ignoreErr1, ignoreErr2}
}
time.Sleep(time.Millisecond * 150)
return nil
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error { assert.Nil(t, DoWithRetry(func() error {
if retryCount == 1 { return ignoreErr1
return nil }, WithIgnoreErrors(ignoreErrs)))
}
time.Sleep(time.Millisecond * 150) assert.Nil(t, DoWithRetry(func() error {
return errors.New("any ") return ignoreErr2
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150))) }, WithIgnoreErrors(ignoreErrs)))
assert.NotNil(t, DoWithRetry(func() error {
return errors.New("any")
}))
}
func TestRetryCtx(t *testing.T) {
t.Run("with timeout", func(t *testing.T) {
assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
if retryCount == 0 {
return errors.New("any")
}
time.Sleep(time.Millisecond * 150)
return nil
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
if retryCount == 1 {
return nil
}
time.Sleep(time.Millisecond * 150)
return errors.New("any ")
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
})
t.Run("with deadline exceeded", func(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*250))
defer cancel()
var times int
assert.Error(t, DoWithRetryCtx(ctx, func(ctx context.Context, retryCount int) error {
times++
time.Sleep(time.Millisecond * 150)
return errors.New("any")
}, WithInterval(time.Millisecond*150)))
assert.Equal(t, 1, times)
})
t.Run("with deadline not exceeded", func(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*250))
defer cancel()
var times int
assert.NoError(t, DoWithRetryCtx(ctx, func(ctx context.Context, retryCount int) error {
times++
if times == defaultRetryTimes {
return nil
}
time.Sleep(time.Millisecond * 50)
return errors.New("any")
}))
assert.Equal(t, defaultRetryTimes, times)
})
} }

View File

@@ -9,6 +9,7 @@ import (
"github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/core/collection"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"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" "github.com/zeromicro/go-zero/core/timex"
@@ -21,8 +22,11 @@ const (
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
flyingBeta = 0.9 flyingBeta = 0.9
coolOffDuration = time.Second coolOffDuration = time.Second
cpuMax = 1000 // millicpu
millisecondsPerSecond = 1000
overloadFactorLowerBound = 0.1
) )
var ( var (
@@ -66,7 +70,7 @@ type (
adaptiveShedder struct { adaptiveShedder struct {
cpuThreshold int64 cpuThreshold int64
windows int64 windowScale float64
flying int64 flying int64
avgFlying float64 avgFlying float64
avgFlyingLock syncx.SpinLock avgFlyingLock syncx.SpinLock
@@ -105,7 +109,7 @@ func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
bucketDuration := options.window / time.Duration(options.buckets) bucketDuration := options.window / time.Duration(options.buckets)
return &adaptiveShedder{ return &adaptiveShedder{
cpuThreshold: options.cpuThreshold, cpuThreshold: options.cpuThreshold,
windows: int64(time.Second / bucketDuration), windowScale: float64(time.Second) / float64(bucketDuration) / millisecondsPerSecond,
overloadTime: syncx.NewAtomicDuration(), overloadTime: syncx.NewAtomicDuration(),
droppedRecently: syncx.NewAtomicBool(), droppedRecently: syncx.NewAtomicBool(),
passCounter: collection.NewRollingWindow(options.buckets, bucketDuration, passCounter: collection.NewRollingWindow(options.buckets, bucketDuration,
@@ -136,7 +140,7 @@ func (as *adaptiveShedder) addFlying(delta int64) {
// update avgFlying when the request is finished. // update avgFlying when the request is finished.
// this strategy makes avgFlying have a little bit lag against flying, and smoother. // this strategy makes avgFlying have a little bit lag against flying, and smoother.
// when the flying requests increase rapidly, avgFlying increase slower, accept more requests. // when the flying requests increase rapidly, avgFlying increase slower, accept more requests.
// when the flying requests drop rapidly, avgFlying drop slower, accept less requests. // when the flying requests drop rapidly, avgFlying drop slower, accept fewer requests.
// it makes the service to serve as more requests as possible. // it makes the service to serve as more requests as possible.
if delta < 0 { if delta < 0 {
as.avgFlyingLock.Lock() as.avgFlyingLock.Lock()
@@ -149,16 +153,17 @@ func (as *adaptiveShedder) highThru() bool {
as.avgFlyingLock.Lock() as.avgFlyingLock.Lock()
avgFlying := as.avgFlying avgFlying := as.avgFlying
as.avgFlyingLock.Unlock() as.avgFlyingLock.Unlock()
maxFlight := as.maxFlight() maxFlight := as.maxFlight() * as.overloadFactor()
return int64(avgFlying) > maxFlight && atomic.LoadInt64(&as.flying) > maxFlight return avgFlying > maxFlight && float64(atomic.LoadInt64(&as.flying)) > maxFlight
} }
func (as *adaptiveShedder) maxFlight() int64 { func (as *adaptiveShedder) maxFlight() float64 {
// windows = buckets per second // windows = buckets per second
// maxQPS = maxPASS * windows // maxQPS = maxPASS * windows
// minRT = min average response time in milliseconds // minRT = min average response time in milliseconds
// maxQPS * minRT / milliseconds_per_second // allowedFlying = maxQPS * minRT / milliseconds_per_second
return int64(math.Max(1, float64(as.maxPass()*as.windows)*(as.minRt()/1e3))) maxFlight := float64(as.maxPass()) * as.minRt() * as.windowScale
return mathx.AtLeast(maxFlight, 1)
} }
func (as *adaptiveShedder) maxPass() int64 { func (as *adaptiveShedder) maxPass() int64 {
@@ -174,6 +179,8 @@ func (as *adaptiveShedder) maxPass() int64 {
} }
func (as *adaptiveShedder) minRt() float64 { func (as *adaptiveShedder) minRt() float64 {
// if no requests in previous windows, return defaultMinRt,
// its a reasonable large value to avoid dropping requests.
result := defaultMinRt result := defaultMinRt
as.rtCounter.Reduce(func(b *collection.Bucket) { as.rtCounter.Reduce(func(b *collection.Bucket) {
@@ -190,6 +197,13 @@ func (as *adaptiveShedder) minRt() float64 {
return result return result
} }
func (as *adaptiveShedder) overloadFactor() float64 {
// as.cpuThreshold must be less than cpuMax
factor := (cpuMax - float64(stat.CpuUsage())) / (cpuMax - float64(as.cpuThreshold))
// at least accept 10% of acceptable requests even cpu is highly overloaded.
return mathx.Between(factor, overloadFactorLowerBound, 1)
}
func (as *adaptiveShedder) shouldDrop() bool { func (as *adaptiveShedder) shouldDrop() bool {
if as.systemOverloaded() || as.stillHot() { if as.systemOverloaded() || as.stillHot() {
if as.highThru() { if as.highThru() {

View File

@@ -19,6 +19,7 @@ import (
const ( const (
buckets = 10 buckets = 10
bucketDuration = time.Millisecond * 50 bucketDuration = time.Millisecond * 50
windowFactor = 0.01
) )
func init() { func init() {
@@ -114,10 +115,10 @@ func TestAdaptiveShedderMaxFlight(t *testing.T) {
shedder := &adaptiveShedder{ shedder := &adaptiveShedder{
passCounter: passCounter, passCounter: passCounter,
rtCounter: rtCounter, rtCounter: rtCounter,
windows: buckets, windowScale: windowFactor,
droppedRecently: syncx.NewAtomicBool(), droppedRecently: syncx.NewAtomicBool(),
} }
assert.Equal(t, int64(54), shedder.maxFlight()) assert.Equal(t, float64(54), shedder.maxFlight())
} }
func TestAdaptiveShedderShouldDrop(t *testing.T) { func TestAdaptiveShedderShouldDrop(t *testing.T) {
@@ -136,7 +137,7 @@ func TestAdaptiveShedderShouldDrop(t *testing.T) {
shedder := &adaptiveShedder{ shedder := &adaptiveShedder{
passCounter: passCounter, passCounter: passCounter,
rtCounter: rtCounter, rtCounter: rtCounter,
windows: buckets, windowScale: windowFactor,
overloadTime: syncx.NewAtomicDuration(), overloadTime: syncx.NewAtomicDuration(),
droppedRecently: syncx.NewAtomicBool(), droppedRecently: syncx.NewAtomicBool(),
} }
@@ -149,7 +150,8 @@ func TestAdaptiveShedderShouldDrop(t *testing.T) {
// cpu >= 800, inflight > maxPass // cpu >= 800, inflight > maxPass
shedder.avgFlying = 80 shedder.avgFlying = 80
shedder.flying = 50 // because of the overloadFactor, so we need to make sure maxFlight is greater than flying
shedder.flying = int64(shedder.maxFlight()*shedder.overloadFactor()) - 5
assert.False(t, shedder.shouldDrop()) assert.False(t, shedder.shouldDrop())
// cpu >= 800, inflight > maxPass // cpu >= 800, inflight > maxPass
@@ -190,7 +192,7 @@ func TestAdaptiveShedderStillHot(t *testing.T) {
shedder := &adaptiveShedder{ shedder := &adaptiveShedder{
passCounter: passCounter, passCounter: passCounter,
rtCounter: rtCounter, rtCounter: rtCounter,
windows: buckets, windowScale: windowFactor,
overloadTime: syncx.NewAtomicDuration(), overloadTime: syncx.NewAtomicDuration(),
droppedRecently: syncx.ForAtomicBool(true), droppedRecently: syncx.ForAtomicBool(true),
} }
@@ -239,6 +241,30 @@ func BenchmarkAdaptiveShedder_Allow(b *testing.B) {
b.Run("low load", bench) b.Run("low load", bench)
} }
func BenchmarkMaxFlight(b *testing.B) {
passCounter := newRollingWindow()
rtCounter := newRollingWindow()
for i := 0; i < 10; i++ {
if i > 0 {
time.Sleep(bucketDuration)
}
passCounter.Add(float64((i + 1) * 100))
for j := i*10 + 1; j <= i*10+10; j++ {
rtCounter.Add(float64(j))
}
}
shedder := &adaptiveShedder{
passCounter: passCounter,
rtCounter: rtCounter,
windowScale: windowFactor,
droppedRecently: syncx.NewAtomicBool(),
}
for i := 0; i < b.N; i++ {
_ = shedder.maxFlight()
}
}
func newRollingWindow() *collection.RollingWindow { func newRollingWindow() *collection.RollingWindow {
return collection.NewRollingWindow(buckets, bucketDuration, collection.IgnoreCurrentBucket()) return collection.NewRollingWindow(buckets, bucketDuration, collection.IgnoreCurrentBucket())
} }

View File

@@ -17,14 +17,13 @@ import (
const callerDepth = 4 const callerDepth = 4
var ( var (
timeFormat = "2006-01-02T15:04:05.000Z07:00" timeFormat = "2006-01-02T15:04:05.000Z07:00"
logLevel uint32
encoding uint32 = jsonEncodingType encoding uint32 = jsonEncodingType
// maxContentLength is used to truncate the log content, 0 for not truncating. // maxContentLength is used to truncate the log content, 0 for not truncating.
maxContentLength uint32 maxContentLength uint32
// use uint32 for atomic operations // use uint32 for atomic operations
disableLog uint32
disableStat uint32 disableStat uint32
logLevel uint32
options logOptions options logOptions
writer = new(atomicWriter) writer = new(atomicWriter)
setupOnce sync.Once setupOnce sync.Once
@@ -96,7 +95,7 @@ func Debugw(msg string, fields ...LogField) {
// Disable disables the logging. // Disable disables the logging.
func Disable() { func Disable() {
atomic.StoreUint32(&disableLog, 1) atomic.StoreUint32(&logLevel, disableLevel)
writer.Store(nopWriter{}) writer.Store(nopWriter{})
} }
@@ -250,7 +249,7 @@ 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) {
if atomic.LoadUint32(&disableLog) == 0 { if atomic.LoadUint32(&logLevel) != disableLevel {
writer.Store(w) writer.Store(w)
} }
} }

View File

@@ -666,6 +666,7 @@ func TestDisable(t *testing.T) {
WithMaxSize(1024)(&opt) WithMaxSize(1024)(&opt)
assert.Nil(t, Close()) assert.Nil(t, Close())
assert.Nil(t, Close()) assert.Nil(t, Close())
assert.Equal(t, uint32(disableLevel), atomic.LoadUint32(&logLevel))
} }
func TestDisableStat(t *testing.T) { func TestDisableStat(t *testing.T) {
@@ -680,7 +681,7 @@ func TestDisableStat(t *testing.T) {
} }
func TestSetWriter(t *testing.T) { func TestSetWriter(t *testing.T) {
atomic.StoreUint32(&disableLog, 0) atomic.StoreUint32(&logLevel, 0)
Reset() Reset()
SetWriter(nopWriter{}) SetWriter(nopWriter{})
assert.NotNil(t, writer.Load()) assert.NotNil(t, writer.Load())

View File

@@ -40,7 +40,7 @@ type LogConf struct {
- `Compress`: whether or not to compress log files, only works with `file` mode. - `Compress`: whether or not to compress log files, only works with `file` mode.
- `KeepDays`: how many days that the log files are kept, after the given days, the outdated files will be deleted automatically. It has no effect on `console` mode. - `KeepDays`: how many days that the log files are kept, after the given days, the outdated files will be deleted automatically. It has no effect on `console` mode.
- `StackCooldownMillis`: how many milliseconds to rewrite stacktrace again. Its used to avoid stacktrace flooding. - `StackCooldownMillis`: how many milliseconds to rewrite stacktrace again. Its used to avoid stacktrace flooding.
- `MaxBackups`: represents how many backup log files will be kept. 0 means all files will be kept forever. Only take effect when `Rotation` is `size`. NOTE: the level of option `KeepDays` will be higher. Even thougth `MaxBackups` sets 0, log files will still be removed if the `KeepDays` limitation is reached. - `MaxBackups`: represents how many backup log files will be kept. 0 means all files will be kept forever. Only take effect when `Rotation` is `size`. NOTE: the level of option `KeepDays` will be higher. Even though `MaxBackups` sets 0, log files will still be removed if the `KeepDays` limitation is reached.
- `MaxSize`: represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`. Only take effect when `Rotation` is `size`. - `MaxSize`: represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`. Only take effect when `Rotation` is `size`.
- `Rotation`: represents the type of log rotation rule. Default is `daily`. - `Rotation`: represents the type of log rotation rule. Default is `daily`.
- `daily` rotate the logs by day. - `daily` rotate the logs by day.

View File

@@ -41,67 +41,99 @@ type richLogger struct {
} }
func (l *richLogger) Debug(v ...any) { func (l *richLogger) Debug(v ...any) {
l.debug(fmt.Sprint(v...)) if shallLog(DebugLevel) {
l.debug(fmt.Sprint(v...))
}
} }
func (l *richLogger) Debugf(format string, v ...any) { func (l *richLogger) Debugf(format string, v ...any) {
l.debug(fmt.Sprintf(format, v...)) if shallLog(DebugLevel) {
l.debug(fmt.Sprintf(format, v...))
}
} }
func (l *richLogger) Debugv(v any) { func (l *richLogger) Debugv(v any) {
l.debug(v) if shallLog(DebugLevel) {
l.debug(v)
}
} }
func (l *richLogger) Debugw(msg string, fields ...LogField) { func (l *richLogger) Debugw(msg string, fields ...LogField) {
l.debug(msg, fields...) if shallLog(DebugLevel) {
l.debug(msg, fields...)
}
} }
func (l *richLogger) Error(v ...any) { func (l *richLogger) Error(v ...any) {
l.err(fmt.Sprint(v...)) if shallLog(ErrorLevel) {
l.err(fmt.Sprint(v...))
}
} }
func (l *richLogger) Errorf(format string, v ...any) { func (l *richLogger) Errorf(format string, v ...any) {
l.err(fmt.Sprintf(format, v...)) if shallLog(ErrorLevel) {
l.err(fmt.Sprintf(format, v...))
}
} }
func (l *richLogger) Errorv(v any) { func (l *richLogger) Errorv(v any) {
l.err(v) if shallLog(ErrorLevel) {
l.err(v)
}
} }
func (l *richLogger) Errorw(msg string, fields ...LogField) { func (l *richLogger) Errorw(msg string, fields ...LogField) {
l.err(msg, fields...) if shallLog(ErrorLevel) {
l.err(msg, fields...)
}
} }
func (l *richLogger) Info(v ...any) { func (l *richLogger) Info(v ...any) {
l.info(fmt.Sprint(v...)) if shallLog(InfoLevel) {
l.info(fmt.Sprint(v...))
}
} }
func (l *richLogger) Infof(format string, v ...any) { func (l *richLogger) Infof(format string, v ...any) {
l.info(fmt.Sprintf(format, v...)) if shallLog(InfoLevel) {
l.info(fmt.Sprintf(format, v...))
}
} }
func (l *richLogger) Infov(v any) { func (l *richLogger) Infov(v any) {
l.info(v) if shallLog(InfoLevel) {
l.info(v)
}
} }
func (l *richLogger) Infow(msg string, fields ...LogField) { func (l *richLogger) Infow(msg string, fields ...LogField) {
l.info(msg, fields...) if shallLog(InfoLevel) {
l.info(msg, fields...)
}
} }
func (l *richLogger) Slow(v ...any) { func (l *richLogger) Slow(v ...any) {
l.slow(fmt.Sprint(v...)) if shallLog(ErrorLevel) {
l.slow(fmt.Sprint(v...))
}
} }
func (l *richLogger) Slowf(format string, v ...any) { func (l *richLogger) Slowf(format string, v ...any) {
l.slow(fmt.Sprintf(format, v...)) if shallLog(ErrorLevel) {
l.slow(fmt.Sprintf(format, v...))
}
} }
func (l *richLogger) Slowv(v any) { func (l *richLogger) Slowv(v any) {
l.slow(v) if shallLog(ErrorLevel) {
l.slow(v)
}
} }
func (l *richLogger) Sloww(msg string, fields ...LogField) { func (l *richLogger) Sloww(msg string, fields ...LogField) {
l.slow(msg, fields...) if shallLog(ErrorLevel) {
l.slow(msg, fields...)
}
} }
func (l *richLogger) WithCallerSkip(skip int) Logger { func (l *richLogger) WithCallerSkip(skip int) Logger {

View File

@@ -15,6 +15,8 @@ const (
ErrorLevel ErrorLevel
// SevereLevel only log severe messages // SevereLevel only log severe messages
SevereLevel SevereLevel
// disableLevel doesn't log any messages
disableLevel = 0xff
) )
const ( const (

View File

@@ -7,6 +7,7 @@ import (
"io" "io"
"log" "log"
"path" "path"
"runtime/debug"
"sync" "sync"
"sync/atomic" "sync/atomic"
@@ -332,11 +333,13 @@ func wrapLevelWithColor(level string) string {
func writeJson(writer io.Writer, info any) { func writeJson(writer io.Writer, info any) {
if content, err := json.Marshal(info); err != nil { if content, err := json.Marshal(info); err != nil {
log.Println(err.Error()) log.Printf("err: %s\n\n%s", err.Error(), debug.Stack())
} else if writer == nil { } else if writer == nil {
log.Println(string(content)) log.Println(string(content))
} else { } else {
writer.Write(append(content, '\n')) if _, err := writer.Write(append(content, '\n')); err != nil {
log.Println(err.Error())
}
} }
} }
@@ -384,7 +387,7 @@ func writePlainValue(writer io.Writer, level string, val any, fields ...string)
buf.WriteString(level) buf.WriteString(level)
buf.WriteByte(plainEncodingSep) buf.WriteByte(plainEncodingSep)
if err := json.NewEncoder(&buf).Encode(val); err != nil { if err := json.NewEncoder(&buf).Encode(val); err != nil {
log.Println(err.Error()) log.Printf("err: %s\n\n%s", err.Error(), debug.Stack())
return return
} }

View File

@@ -126,9 +126,23 @@ func TestWriteJson(t *testing.T) {
log.SetOutput(&buf) log.SetOutput(&buf)
writeJson(nil, "foo") writeJson(nil, "foo")
assert.Contains(t, buf.String(), "foo") assert.Contains(t, buf.String(), "foo")
buf.Reset()
writeJson(hardToWriteWriter{}, "foo")
assert.Contains(t, buf.String(), "write error")
buf.Reset() buf.Reset()
writeJson(nil, make(chan int)) writeJson(nil, make(chan int))
assert.Contains(t, buf.String(), "unsupported type") assert.Contains(t, buf.String(), "unsupported type")
buf.Reset()
type C struct {
RC func()
}
writeJson(nil, C{
RC: func() {},
})
assert.Contains(t, buf.String(), "runtime/debug.Stack")
} }
func TestWritePlainAny(t *testing.T) { func TestWritePlainAny(t *testing.T) {
@@ -165,6 +179,14 @@ func TestWritePlainAny(t *testing.T) {
writePlainAny(hardToWriteWriter{}, levelFatal, "foo") writePlainAny(hardToWriteWriter{}, levelFatal, "foo")
assert.Contains(t, buf.String(), "write error") assert.Contains(t, buf.String(), "write error")
buf.Reset()
type C struct {
RC func()
}
writePlainAny(nil, levelError, C{
RC: func() {},
})
assert.Contains(t, buf.String(), "runtime/debug.Stack")
} }
func TestLogWithLimitContentLength(t *testing.T) { func TestLogWithLimitContentLength(t *testing.T) {

View File

@@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"math"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@@ -19,9 +18,10 @@ import (
) )
const ( const (
defaultKeyName = "key" defaultKeyName = "key"
delimiter = '.' delimiter = '.'
ignoreKey = "-" ignoreKey = "-"
numberTypeString = "number"
) )
var ( var (
@@ -223,11 +223,11 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.
switch v := mapValue.(type) { switch v := mapValue.(type) {
case fmt.Stringer: case fmt.Stringer:
if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil { if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil {
return err return fmt.Errorf("fullName: `%s`, error: `%w`", fullName, err)
} }
case string: case string:
if err := jsonx.UnmarshalFromString(v, &slice); err != nil { if err := jsonx.UnmarshalFromString(v, &slice); err != nil {
return err return fmt.Errorf("fullName: `%s`, error: `%w`", fullName, err)
} }
default: default:
return errUnsupportedType return errUnsupportedType
@@ -249,6 +249,10 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.
func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int,
baseKind reflect.Kind, value any, fullName string) error { baseKind reflect.Kind, value any, fullName string) error {
if value == nil {
return errNilSliceElement
}
ithVal := slice.Index(index) ithVal := slice.Index(index)
switch v := value.(type) { switch v := value.(type) {
case fmt.Stringer: case fmt.Stringer:
@@ -424,6 +428,10 @@ func (u *Unmarshaler) parseOptionsWithContext(field reflect.StructField, m Value
} }
} }
if u.opts.fillDefault {
return key, &options.fieldOptionsWithContext, nil
}
optsWithContext, err := options.toOptionsWithContext(key, m, fullName) optsWithContext, err := options.toOptionsWithContext(key, m, fullName)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
@@ -621,8 +629,13 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(fieldType reflect.Type
return err return err
} }
if fValue > math.MaxFloat32 { // if value is a pointer, we need to check overflow with the pointer's value.
return float32OverflowError(v.String()) derefedValue := value
for derefedValue.Type().Kind() == reflect.Ptr {
derefedValue = derefedValue.Elem()
}
if derefedValue.CanFloat() && derefedValue.OverflowFloat(fValue) {
return fmt.Errorf("parsing %q as float32: value out of range", v.String())
} }
target.SetFloat(fValue) target.SetFloat(fValue)
@@ -634,7 +647,7 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(fieldType reflect.Type
target.SetFloat(fValue) target.SetFloat(fValue)
default: default:
return newTypeMismatchErrorWithHint(fullName, typeKind.String(), value.Type().String()) return newTypeMismatchErrorWithHint(fullName, typeKind.String(), numberTypeString)
} }
SetValue(fieldType, value, target) SetValue(fieldType, value, target)

View File

@@ -976,6 +976,19 @@ func TestUnmarshalFloat32WithOverflow(t *testing.T) {
assert.Error(t, UnmarshalKey(m, &in)) assert.Error(t, UnmarshalKey(m, &in))
}) })
t.Run("float32 from string less than float32", func(t *testing.T) {
type inner struct {
Value float32 `key:"float, string"`
}
m := map[string]any{
"float": "-1.79769313486231570814527423731704356798070e+300", // overflow
}
var in inner
assert.Error(t, UnmarshalKey(m, &in))
})
t.Run("float32 from json.Number greater than float64", func(t *testing.T) { t.Run("float32 from json.Number greater than float64", func(t *testing.T) {
type inner struct { type inner struct {
Value float32 `key:"float"` Value float32 `key:"float"`
@@ -1001,6 +1014,19 @@ func TestUnmarshalFloat32WithOverflow(t *testing.T) {
var in inner var in inner
assert.Error(t, UnmarshalKey(m, &in)) assert.Error(t, UnmarshalKey(m, &in))
}) })
t.Run("float32 from json number less than float32", func(t *testing.T) {
type inner struct {
Value float32 `key:"float"`
}
m := map[string]any{
"float": json.Number("-1.79769313486231570814527423731704356798070e+300"), // overflow
}
var in inner
assert.Error(t, UnmarshalKey(m, &in))
})
} }
func TestUnmarshalFloat64WithOverflow(t *testing.T) { func TestUnmarshalFloat64WithOverflow(t *testing.T) {
@@ -1303,6 +1329,47 @@ func TestUnmarshalInt64Slice(t *testing.T) {
} }
} }
func TestUnmarshalNullableSlice(t *testing.T) {
var v struct {
Ages []int64 `key:"ages"`
Slice []int8 `key:"slice"`
}
m := map[string]any{
"ages": []int64{1, 2},
"slice": `[null,2]`,
}
assert.New(t).Equal(UnmarshalKey(m, &v), errNilSliceElement)
}
func TestUnmarshalWithFloatPtr(t *testing.T) {
t.Run("*float32", func(t *testing.T) {
var v struct {
WeightFloat32 *float32 `key:"weightFloat32,optional"`
}
m := map[string]any{
"weightFloat32": json.Number("3.2"),
}
if assert.NoError(t, UnmarshalKey(m, &v)) {
assert.Equal(t, float32(3.2), *v.WeightFloat32)
}
})
t.Run("**float32", func(t *testing.T) {
var v struct {
WeightFloat32 **float32 `key:"weightFloat32,optional"`
}
m := map[string]any{
"weightFloat32": json.Number("3.2"),
}
if assert.NoError(t, UnmarshalKey(m, &v)) {
assert.Equal(t, float32(3.2), **v.WeightFloat32)
}
})
}
func TestUnmarshalIntSlice(t *testing.T) { func TestUnmarshalIntSlice(t *testing.T) {
var v struct { var v struct {
Ages []int `key:"ages"` Ages []int `key:"ages"`
@@ -5344,6 +5411,15 @@ func TestFillDefaultUnmarshal(t *testing.T) {
assert.Equal(t, "c", st.C) assert.Equal(t, "c", st.C)
}) })
t.Run("optional !", func(t *testing.T) {
var st struct {
A string `json:",optional"`
B string `json:",optional=!A"`
}
err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
assert.NoError(t, err)
})
t.Run("has value", func(t *testing.T) { t.Run("has value", func(t *testing.T) {
type St struct { type St struct {
A string `json:",default=a"` A string `json:",default=a"`
@@ -5490,7 +5566,7 @@ func TestUnmarshalerProcessFieldPrimitiveWithJSONNumber(t *testing.T) {
err := m.processFieldPrimitiveWithJSONNumber(fieldType, value.Elem(), v, err := m.processFieldPrimitiveWithJSONNumber(fieldType, value.Elem(), v,
&fieldOptionsWithContext{}, "field") &fieldOptionsWithContext{}, "field")
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, `type mismatch for field "field", expect "string", actual "int"`, err.Error()) assert.Equal(t, `type mismatch for field "field", expect "string", actual "number"`, err.Error())
}) })
t.Run("right type", func(t *testing.T) { t.Run("right type", func(t *testing.T) {

View File

@@ -30,11 +30,13 @@ const (
leftSquareBracket = '[' leftSquareBracket = '['
rightSquareBracket = ']' rightSquareBracket = ']'
segmentSeparator = ',' segmentSeparator = ','
intSize = 32 << (^uint(0) >> 63) // 32 or 64
) )
var ( var (
errUnsupportedType = errors.New("unsupported type on setting field value") errUnsupportedType = errors.New("unsupported type on setting field value")
errNumberRange = errors.New("wrong number range setting") errNumberRange = errors.New("wrong number range setting")
errNilSliceElement = errors.New("null element for slice")
optionsCache = make(map[string]optionsCacheValue) optionsCache = make(map[string]optionsCacheValue)
cacheLock sync.RWMutex cacheLock sync.RWMutex
structRequiredCache = make(map[reflect.Type]requiredCacheValue) structRequiredCache = make(map[reflect.Type]requiredCacheValue)
@@ -42,10 +44,6 @@ var (
) )
type ( type (
integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
optionsCacheValue struct { optionsCacheValue struct {
key string key string
options *fieldOptions options *fieldOptions
@@ -104,38 +102,30 @@ func convertTypeFromString(kind reflect.Kind, str string) (any, error) {
default: default:
return false, errTypeMismatch return false, errTypeMismatch
} }
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int:
intValue, err := strconv.ParseInt(str, 10, 64) return strconv.ParseInt(str, 10, intSize)
if err != nil { case reflect.Int8:
return 0, err return strconv.ParseInt(str, 10, 8)
} case reflect.Int16:
return strconv.ParseInt(str, 10, 16)
return intValue, nil case reflect.Int32:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: return strconv.ParseInt(str, 10, 32)
uintValue, err := strconv.ParseUint(str, 10, 64) case reflect.Int64:
if err != nil { return strconv.ParseInt(str, 10, 64)
return 0, err case reflect.Uint:
} return strconv.ParseUint(str, 10, intSize)
case reflect.Uint8:
return uintValue, nil return strconv.ParseUint(str, 10, 8)
case reflect.Uint16:
return strconv.ParseUint(str, 10, 16)
case reflect.Uint32:
return strconv.ParseUint(str, 10, 32)
case reflect.Uint64:
return strconv.ParseUint(str, 10, 64)
case reflect.Float32: case reflect.Float32:
floatValue, err := strconv.ParseFloat(str, 64) return strconv.ParseFloat(str, 32)
if err != nil {
return 0, err
}
if floatValue > math.MaxFloat32 {
return 0, float32OverflowError(str)
}
return floatValue, nil
case reflect.Float64: case reflect.Float64:
floatValue, err := strconv.ParseFloat(str, 64) return strconv.ParseFloat(str, 64)
if err != nil {
return 0, err
}
return floatValue, nil
case reflect.String: case reflect.String:
return str, nil return str, nil
default: default:
@@ -230,10 +220,6 @@ func implicitValueRequiredStruct(tag string, tp reflect.Type) (bool, error) {
return false, nil return false, nil
} }
func intOverflowError[T integer](v T, kind reflect.Kind) error {
return fmt.Errorf("parsing \"%d\" as %s: value out of range", v, kind.String())
}
func isLeftInclude(b byte) (bool, error) { func isLeftInclude(b byte) (bool, error) {
switch b { switch b {
case '[': case '[':
@@ -256,10 +242,6 @@ func isRightInclude(b byte) (bool, error) {
} }
} }
func float32OverflowError(str string) error {
return fmt.Errorf("parsing %q as float32: value out of range", str)
}
func maybeNewValue(fieldType reflect.Type, value reflect.Value) { func maybeNewValue(fieldType reflect.Type, value reflect.Value) {
if fieldType.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()))
@@ -505,41 +487,15 @@ func parseSegments(val string) []string {
return segments return segments
} }
func setIntValue(value reflect.Value, v any, min, max int64) error {
iv := v.(int64)
if iv < min || iv > max {
return intOverflowError(iv, value.Kind())
}
value.SetInt(iv)
return nil
}
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v any) error { func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v any) error {
switch kind { switch kind {
case reflect.Bool: case reflect.Bool:
value.SetBool(v.(bool)) value.SetBool(v.(bool))
return nil return nil
case reflect.Int: // int depends on int size, 32 or 64 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return setIntValue(value, v, math.MinInt, math.MaxInt)
case reflect.Int8:
return setIntValue(value, v, math.MinInt8, math.MaxInt8)
case reflect.Int16:
return setIntValue(value, v, math.MinInt16, math.MaxInt16)
case reflect.Int32:
return setIntValue(value, v, math.MinInt32, math.MaxInt32)
case reflect.Int64:
value.SetInt(v.(int64)) value.SetInt(v.(int64))
return nil return nil
case reflect.Uint: // uint depends on int size, 32 or 64 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return setUintValue(value, v, math.MaxUint)
case reflect.Uint8:
return setUintValue(value, v, math.MaxUint8)
case reflect.Uint16:
return setUintValue(value, v, math.MaxUint16)
case reflect.Uint32:
return setUintValue(value, v, math.MaxUint32)
case reflect.Uint64:
value.SetUint(v.(uint64)) value.SetUint(v.(uint64))
return nil return nil
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
@@ -553,16 +509,6 @@ func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v any) err
} }
} }
func setUintValue(value reflect.Value, v any, boundary uint64) error {
iv := v.(uint64)
if iv > boundary {
return intOverflowError(iv, value.Kind())
}
value.SetUint(iv)
return nil
}
func setValueFromString(kind reflect.Kind, value reflect.Value, str string) error { func setValueFromString(kind reflect.Kind, value reflect.Value, str string) error {
if !value.CanSet() { if !value.CanSet() {
return errValueNotSettable return errValueNotSettable

View File

@@ -1011,6 +1011,15 @@ func TestUnmarshalYamlMapRune(t *testing.T) {
assert.Equal(t, rune(3), v.Machine["node3"]) assert.Equal(t, rune(3), v.Machine["node3"])
} }
func TestUnmarshalYamlStringOfInt(t *testing.T) {
text := `password: 123456`
var v struct {
Password string `json:"password"`
}
reader := strings.NewReader(text)
assert.Error(t, UnmarshalYamlReader(reader, &v))
}
func TestUnmarshalYamlBadInput(t *testing.T) { func TestUnmarshalYamlBadInput(t *testing.T) {
var v struct { var v struct {
Any string Any string

View File

@@ -15,3 +15,17 @@ func TestCalcEntropy(t *testing.T) {
} }
assert.True(t, CalcEntropy(m) > .99) assert.True(t, CalcEntropy(m) > .99)
} }
func TestCalcEmptyEntropy(t *testing.T) {
m := make(map[any]int)
assert.Equal(t, float64(1), CalcEntropy(m))
}
func TestCalcDiffEntropy(t *testing.T) {
const total = 1000
m := make(map[any]int, total)
for i := 0; i < total; i++ {
m[i] = i
}
assert.True(t, CalcEntropy(m) < .99)
}

34
core/mathx/range.go Normal file
View File

@@ -0,0 +1,34 @@
package mathx
type numerical interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64
}
// AtLeast returns the greater of x or lower.
func AtLeast[T numerical](x, lower T) T {
if x < lower {
return lower
}
return x
}
// AtMost returns the smaller of x or upper.
func AtMost[T numerical](x, upper T) T {
if x > upper {
return upper
}
return x
}
// Between returns the value of x clamped to the range [lower, upper].
func Between[T numerical](x, lower, upper T) T {
if x < lower {
return lower
}
if x > upper {
return upper
}
return x
}

513
core/mathx/range_test.go Normal file
View File

@@ -0,0 +1,513 @@
package mathx
import "testing"
func TestAtLeast(t *testing.T) {
t.Run("test int", func(t *testing.T) {
if got := AtLeast(10, 5); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(3, 5); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(5, 5); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test int8", func(t *testing.T) {
if got := AtLeast(int8(10), int8(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(int8(3), int8(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(int8(5), int8(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test int16", func(t *testing.T) {
if got := AtLeast(int16(10), int16(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(int16(3), int16(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(int16(5), int16(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test int32", func(t *testing.T) {
if got := AtLeast(int32(10), int32(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(int32(3), int32(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(int32(5), int32(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test int64", func(t *testing.T) {
if got := AtLeast(int64(10), int64(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(int64(3), int64(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(int64(5), int64(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test uint", func(t *testing.T) {
if got := AtLeast(uint(10), uint(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(uint(3), uint(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(uint(5), uint(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test uint8", func(t *testing.T) {
if got := AtLeast(uint8(10), uint8(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(uint8(3), uint8(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(uint8(5), uint8(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test uint16", func(t *testing.T) {
if got := AtLeast(uint16(10), uint16(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(uint16(3), uint16(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(uint16(5), uint16(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test uint32", func(t *testing.T) {
if got := AtLeast(uint32(10), uint32(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(uint32(3), uint32(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(uint32(5), uint32(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test uint64", func(t *testing.T) {
if got := AtLeast(uint64(10), uint64(5)); got != 10 {
t.Errorf("AtLeast() = %v, want 10", got)
}
if got := AtLeast(uint64(3), uint64(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
if got := AtLeast(uint64(5), uint64(5)); got != 5 {
t.Errorf("AtLeast() = %v, want 5", got)
}
})
t.Run("test float32", func(t *testing.T) {
if got := AtLeast(float32(10.0), float32(5.0)); got != 10.0 {
t.Errorf("AtLeast() = %v, want 10.0", got)
}
if got := AtLeast(float32(3.0), float32(5.0)); got != 5.0 {
t.Errorf("AtLeast() = %v, want 5.0", got)
}
if got := AtLeast(float32(5.0), float32(5.0)); got != 5.0 {
t.Errorf("AtLeast() = %v, want 5.0", got)
}
})
t.Run("test float64", func(t *testing.T) {
if got := AtLeast(10.0, 5.0); got != 10.0 {
t.Errorf("AtLeast() = %v, want 10.0", got)
}
if got := AtLeast(3.0, 5.0); got != 5.0 {
t.Errorf("AtLeast() = %v, want 5.0", got)
}
if got := AtLeast(5.0, 5.0); got != 5.0 {
t.Errorf("AtLeast() = %v, want 5.0", got)
}
})
}
func TestAtMost(t *testing.T) {
t.Run("test int", func(t *testing.T) {
if got := AtMost(10, 5); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(3, 5); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(5, 5); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test int8", func(t *testing.T) {
if got := AtMost(int8(10), int8(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(int8(3), int8(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(int8(5), int8(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test int16", func(t *testing.T) {
if got := AtMost(int16(10), int16(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(int16(3), int16(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(int16(5), int16(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test int32", func(t *testing.T) {
if got := AtMost(int32(10), int32(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(int32(3), int32(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(int32(5), int32(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test int64", func(t *testing.T) {
if got := AtMost(int64(10), int64(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(int64(3), int64(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(int64(5), int64(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test uint", func(t *testing.T) {
if got := AtMost(uint(10), uint(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(uint(3), uint(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(uint(5), uint(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test uint8", func(t *testing.T) {
if got := AtMost(uint8(10), uint8(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(uint8(3), uint8(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(uint8(5), uint8(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test uint16", func(t *testing.T) {
if got := AtMost(uint16(10), uint16(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(uint16(3), uint16(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(uint16(5), uint16(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test uint32", func(t *testing.T) {
if got := AtMost(uint32(10), uint32(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(uint32(3), uint32(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(uint32(5), uint32(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test uint64", func(t *testing.T) {
if got := AtMost(uint64(10), uint64(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
if got := AtMost(uint64(3), uint64(5)); got != 3 {
t.Errorf("AtMost() = %v, want 3", got)
}
if got := AtMost(uint64(5), uint64(5)); got != 5 {
t.Errorf("AtMost() = %v, want 5", got)
}
})
t.Run("test float32", func(t *testing.T) {
if got := AtMost(float32(10.0), float32(5.0)); got != 5.0 {
t.Errorf("AtMost() = %v, want 5.0", got)
}
if got := AtMost(float32(3.0), float32(5.0)); got != 3.0 {
t.Errorf("AtMost() = %v, want 3.0", got)
}
if got := AtMost(float32(5.0), float32(5.0)); got != 5.0 {
t.Errorf("AtMost() = %v, want 5.0", got)
}
})
t.Run("test float64", func(t *testing.T) {
if got := AtMost(10.0, 5.0); got != 5.0 {
t.Errorf("AtMost() = %v, want 5.0", got)
}
if got := AtMost(3.0, 5.0); got != 3.0 {
t.Errorf("AtMost() = %v, want 3.0", got)
}
if got := AtMost(5.0, 5.0); got != 5.0 {
t.Errorf("AtMost() = %v, want 5.0", got)
}
})
}
func TestBetween(t *testing.T) {
t.Run("test int", func(t *testing.T) {
if got := Between(10, 5, 15); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(3, 5, 15); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(20, 5, 15); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(5, 5, 15); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(15, 5, 15); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test int8", func(t *testing.T) {
if got := Between(int8(10), int8(5), int8(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(int8(3), int8(5), int8(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int8(20), int8(5), int8(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(int8(5), int8(5), int8(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int8(15), int8(5), int8(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test int16", func(t *testing.T) {
if got := Between(int16(10), int16(5), int16(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(int16(3), int16(5), int16(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int16(20), int16(5), int16(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(int16(5), int16(5), int16(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int16(15), int16(5), int16(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test int32", func(t *testing.T) {
if got := Between(int32(10), int32(5), int32(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(int32(3), int32(5), int32(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int32(20), int32(5), int32(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(int32(5), int32(5), int32(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int32(15), int32(5), int32(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test int64", func(t *testing.T) {
if got := Between(int64(10), int64(5), int64(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(int64(3), int64(5), int64(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int64(20), int64(5), int64(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(int64(5), int64(5), int64(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(int64(15), int64(5), int64(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test uint", func(t *testing.T) {
if got := Between(uint(10), uint(5), uint(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(uint(3), uint(5), uint(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint(20), uint(5), uint(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(uint(5), uint(5), uint(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint(15), uint(5), uint(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test uint8", func(t *testing.T) {
if got := Between(uint8(10), uint8(5), uint8(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(uint8(3), uint8(5), uint8(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint8(20), uint8(5), uint8(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(uint8(5), uint8(5), uint8(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint8(15), uint8(5), uint8(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test uint16", func(t *testing.T) {
if got := Between(uint16(10), uint16(5), uint16(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(uint16(3), uint16(5), uint16(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint16(20), uint16(5), uint16(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(uint16(5), uint16(5), uint16(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint16(15), uint16(5), uint16(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test uint32", func(t *testing.T) {
if got := Between(uint32(10), uint32(5), uint32(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(uint32(3), uint32(5), uint32(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint32(20), uint32(5), uint32(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(uint32(5), uint32(5), uint32(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint32(15), uint32(5), uint32(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test uint64", func(t *testing.T) {
if got := Between(uint64(10), uint64(5), uint64(15)); got != 10 {
t.Errorf("Between() = %v, want 10", got)
}
if got := Between(uint64(3), uint64(5), uint64(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint64(20), uint64(5), uint64(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
if got := Between(uint64(5), uint64(5), uint64(15)); got != 5 {
t.Errorf("Between() = %v, want 5", got)
}
if got := Between(uint64(15), uint64(5), uint64(15)); got != 15 {
t.Errorf("Between() = %v, want 15", got)
}
})
t.Run("test float32", func(t *testing.T) {
if got := Between(float32(10.0), float32(5.0), float32(15.0)); got != 10.0 {
t.Errorf("Between() = %v, want 10.0", got)
}
if got := Between(float32(3.0), float32(5.0), float32(15.0)); got != 5.0 {
t.Errorf("Between() = %v, want 5.0", got)
}
if got := Between(float32(20.0), float32(5.0), float32(15.0)); got != 15.0 {
t.Errorf("Between() = %v, want 15.0", got)
}
if got := Between(float32(5.0), float32(5.0), float32(15.0)); got != 5.0 {
t.Errorf("Between() = %v, want 5.0", got)
}
if got := Between(float32(15.0), float32(5.0), float32(15.0)); got != 15.0 {
t.Errorf("Between() = %v, want 15.0", got)
}
})
t.Run("test float64", func(t *testing.T) {
if got := Between(10.0, 5.0, 15.0); got != 10.0 {
t.Errorf("Between() = %v, want 10.0", got)
}
if got := Between(3.0, 5.0, 15.0); got != 5.0 {
t.Errorf("Between() = %v, want 5.0", got)
}
if got := Between(20.0, 5.0, 15.0); got != 15.0 {
t.Errorf("Between() = %v, want 15.0", got)
}
if got := Between(5.0, 5.0, 15.0); got != 5.0 {
t.Errorf("Between() = %v, want 5.0", got)
}
if got := Between(15.0, 5.0, 15.0); got != 15.0 {
t.Errorf("Between() = %v, want 15.0", got)
}
})
}

View File

@@ -3,7 +3,6 @@ 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,20 +46,16 @@ func NewCounterVec(cfg *CounterVecOpts) CounterVec {
return cv return cv
} }
func (cv *promCounterVec) Inc(labels ...string) { func (cv *promCounterVec) Add(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return cv.counter.WithLabelValues(labels...).Add(v)
} })
cv.counter.WithLabelValues(labels...).Inc()
} }
func (cv *promCounterVec) Add(v float64, labels ...string) { func (cv *promCounterVec) Inc(labels ...string) {
if !prometheus.Enabled() { update(func() {
return cv.counter.WithLabelValues(labels...).Inc()
} })
cv.counter.WithLabelValues(labels...).Add(v)
} }
func (cv *promCounterVec) close() bool { func (cv *promCounterVec) close() bool {

View File

@@ -3,7 +3,6 @@ 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 (
@@ -16,8 +15,12 @@ type (
Set(v float64, labels ...string) Set(v float64, labels ...string)
// Inc increments labels. // Inc increments labels.
Inc(labels ...string) Inc(labels ...string)
// Dec decrements labels.
Dec(labels ...string)
// Add adds v to labels. // Add adds v to labels.
Add(v float64, labels ...string) Add(v float64, labels ...string)
// Sub subtracts v to labels.
Sub(v float64, labels ...string)
close() bool close() bool
} }
@@ -32,13 +35,12 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
return nil return nil
} }
vec := prom.NewGaugeVec( vec := prom.NewGaugeVec(prom.GaugeOpts{
prom.GaugeOpts{ Namespace: cfg.Namespace,
Namespace: cfg.Namespace, Subsystem: cfg.Subsystem,
Subsystem: cfg.Subsystem, Name: cfg.Name,
Name: cfg.Name, Help: cfg.Help,
Help: cfg.Help, }, cfg.Labels)
}, cfg.Labels)
prom.MustRegister(vec) prom.MustRegister(vec)
gv := &promGaugeVec{ gv := &promGaugeVec{
gauge: vec, gauge: vec,
@@ -50,28 +52,34 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
return gv return gv
} }
func (gv *promGaugeVec) Inc(labels ...string) { func (gv *promGaugeVec) Add(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return gv.gauge.WithLabelValues(labels...).Add(v)
} })
gv.gauge.WithLabelValues(labels...).Inc()
} }
func (gv *promGaugeVec) Add(v float64, labels ...string) { func (gv *promGaugeVec) Dec(labels ...string) {
if !prometheus.Enabled() { update(func() {
return gv.gauge.WithLabelValues(labels...).Dec()
} })
}
gv.gauge.WithLabelValues(labels...).Add(v) func (gv *promGaugeVec) Inc(labels ...string) {
update(func() {
gv.gauge.WithLabelValues(labels...).Inc()
})
} }
func (gv *promGaugeVec) Set(v float64, labels ...string) { func (gv *promGaugeVec) Set(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return gv.gauge.WithLabelValues(labels...).Set(v)
} })
}
gv.gauge.WithLabelValues(labels...).Set(v) func (gv *promGaugeVec) Sub(v float64, labels ...string) {
update(func() {
gv.gauge.WithLabelValues(labels...).Sub(v)
})
} }
func (gv *promGaugeVec) close() bool { func (gv *promGaugeVec) close() bool {

View File

@@ -40,6 +40,23 @@ func TestGaugeInc(t *testing.T) {
assert.Equal(t, float64(2), r) assert.Equal(t, float64(2), r)
} }
func TestGaugeDec(t *testing.T) {
startAgent()
gaugeVec := NewGaugeVec(&GaugeVecOpts{
Namespace: "rpc_client",
Subsystem: "requests",
Name: "duration_ms",
Help: "rpc server requests duration(ms).",
Labels: []string{"path"},
})
defer gaugeVec.close()
gv, _ := gaugeVec.(*promGaugeVec)
gv.Dec("/users")
gv.Dec("/users")
r := testutil.ToFloat64(gv.gauge)
assert.Equal(t, float64(-2), r)
}
func TestGaugeAdd(t *testing.T) { func TestGaugeAdd(t *testing.T) {
startAgent() startAgent()
gaugeVec := NewGaugeVec(&GaugeVecOpts{ gaugeVec := NewGaugeVec(&GaugeVecOpts{
@@ -57,6 +74,23 @@ func TestGaugeAdd(t *testing.T) {
assert.Equal(t, float64(20), r) assert.Equal(t, float64(20), r)
} }
func TestGaugeSub(t *testing.T) {
startAgent()
gaugeVec := NewGaugeVec(&GaugeVecOpts{
Namespace: "rpc_client",
Subsystem: "request",
Name: "duration_ms",
Help: "rpc server requests duration(ms).",
Labels: []string{"path"},
})
defer gaugeVec.close()
gv, _ := gaugeVec.(*promGaugeVec)
gv.Sub(-100, "/classroom")
gv.Sub(30, "/classroom")
r := testutil.ToFloat64(gv.gauge)
assert.Equal(t, float64(70), r)
}
func TestGaugeSet(t *testing.T) { func TestGaugeSet(t *testing.T) {
startAgent() startAgent()
gaugeVec := NewGaugeVec(&GaugeVecOpts{ gaugeVec := NewGaugeVec(&GaugeVecOpts{

View File

@@ -3,24 +3,26 @@ 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 (
// A HistogramVecOpts is a histogram vector options. // A HistogramVecOpts is a histogram vector options.
HistogramVecOpts struct { HistogramVecOpts struct {
Namespace string Namespace string
Subsystem string Subsystem string
Name string Name string
Help string Help string
Labels []string Labels []string
Buckets []float64 Buckets []float64
ConstLabels map[string]string
} }
// A HistogramVec interface represents a histogram vector. // A HistogramVec interface represents a histogram vector.
HistogramVec interface { HistogramVec interface {
// Observe adds observation v to labels. // Observe adds observation v to labels.
Observe(v int64, labels ...string) Observe(v int64, labels ...string)
// ObserveFloat allow to observe float64 values.
ObserveFloat(v float64, labels ...string)
close() bool close() bool
} }
@@ -36,11 +38,12 @@ func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec {
} }
vec := prom.NewHistogramVec(prom.HistogramOpts{ vec := prom.NewHistogramVec(prom.HistogramOpts{
Namespace: cfg.Namespace, Namespace: cfg.Namespace,
Subsystem: cfg.Subsystem, Subsystem: cfg.Subsystem,
Name: cfg.Name, Name: cfg.Name,
Help: cfg.Help, Help: cfg.Help,
Buckets: cfg.Buckets, Buckets: cfg.Buckets,
ConstLabels: cfg.ConstLabels,
}, cfg.Labels) }, cfg.Labels)
prom.MustRegister(vec) prom.MustRegister(vec)
hv := &promHistogramVec{ hv := &promHistogramVec{
@@ -54,11 +57,15 @@ func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec {
} }
func (hv *promHistogramVec) Observe(v int64, labels ...string) { func (hv *promHistogramVec) Observe(v int64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return hv.histogram.WithLabelValues(labels...).Observe(float64(v))
} })
}
hv.histogram.WithLabelValues(labels...).Observe(float64(v)) func (hv *promHistogramVec) ObserveFloat(v float64, labels ...string) {
update(func() {
hv.histogram.WithLabelValues(labels...).Observe(v)
})
} }
func (hv *promHistogramVec) close() bool { func (hv *promHistogramVec) close() bool {

View File

@@ -14,7 +14,7 @@ func TestNewHistogramVec(t *testing.T) {
Help: "rpc server requests duration(ms).", Help: "rpc server requests duration(ms).",
Buckets: []float64{1, 2, 3}, Buckets: []float64{1, 2, 3},
}) })
defer histogramVec.close() defer histogramVec.(*promHistogramVec).close()
histogramVecNil := NewHistogramVec(nil) histogramVecNil := NewHistogramVec(nil)
assert.NotNil(t, histogramVec) assert.NotNil(t, histogramVec)
assert.Nil(t, histogramVecNil) assert.Nil(t, histogramVecNil)
@@ -28,9 +28,10 @@ func TestHistogramObserve(t *testing.T) {
Buckets: []float64{1, 2, 3}, Buckets: []float64{1, 2, 3},
Labels: []string{"method"}, Labels: []string{"method"},
}) })
defer histogramVec.close() defer histogramVec.(*promHistogramVec).close()
hv, _ := histogramVec.(*promHistogramVec) hv, _ := histogramVec.(*promHistogramVec)
hv.Observe(2, "/Users") hv.Observe(2, "/Users")
hv.ObserveFloat(1.1, "/Users")
metadata := ` metadata := `
# HELP counts rpc server requests duration(ms). # HELP counts rpc server requests duration(ms).
@@ -38,11 +39,11 @@ func TestHistogramObserve(t *testing.T) {
` `
val := ` val := `
counts_bucket{method="/Users",le="1"} 0 counts_bucket{method="/Users",le="1"} 0
counts_bucket{method="/Users",le="2"} 1 counts_bucket{method="/Users",le="2"} 2
counts_bucket{method="/Users",le="3"} 1 counts_bucket{method="/Users",le="3"} 2
counts_bucket{method="/Users",le="+Inf"} 1 counts_bucket{method="/Users",le="+Inf"} 2
counts_sum{method="/Users"} 2 counts_sum{method="/Users"} 3.1
counts_count{method="/Users"} 1 counts_count{method="/Users"} 2
` `
err := testutil.CollectAndCompare(hv.histogram, strings.NewReader(metadata+val)) err := testutil.CollectAndCompare(hv.histogram, strings.NewReader(metadata+val))

View File

@@ -1,5 +1,7 @@
package metric package metric
import "github.com/zeromicro/go-zero/core/prometheus"
// A VectorOpts is a general configuration. // A VectorOpts is a general configuration.
type VectorOpts struct { type VectorOpts struct {
Namespace string Namespace string
@@ -8,3 +10,11 @@ type VectorOpts struct {
Help string Help string
Labels []string Labels []string
} }
func update(fn func()) {
if !prometheus.Enabled() {
return
}
fn()
}

View File

@@ -3,7 +3,6 @@ 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,11 +52,9 @@ func NewSummaryVec(cfg *SummaryVecOpts) SummaryVec {
} }
func (sv *promSummaryVec) Observe(v float64, labels ...string) { func (sv *promSummaryVec) Observe(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return sv.summary.WithLabelValues(labels...).Observe(v)
} })
sv.summary.WithLabelValues(labels...).Observe(v)
} }
func (sv *promSummaryVec) close() bool { func (sv *promSummaryVec) close() bool {

View File

@@ -13,7 +13,7 @@ import (
) )
func FuzzMapReduce(f *testing.F) { func FuzzMapReduce(f *testing.F) {
rand.Seed(time.Now().UnixNano()) rand.NewSource(time.Now().UnixNano())
f.Add(int64(10), runtime.NumCPU()) f.Add(int64(10), runtime.NumCPU())
f.Fuzz(func(t *testing.T, n int64, workers int) { f.Fuzz(func(t *testing.T, n int64, workers int) {

View File

@@ -20,7 +20,7 @@ import (
// If Fuzz stuck, we don't know why, because it only returns hung or unexpected, // If Fuzz stuck, we don't know why, because it only returns hung or unexpected,
// so we need to simulate the fuzz test in test mode. // so we need to simulate the fuzz test in test mode.
func TestMapReduceRandom(t *testing.T) { func TestMapReduceRandom(t *testing.T) {
rand.Seed(time.Now().UnixNano()) rand.NewSource(time.Now().UnixNano())
const ( const (
times = 10000 times = 10000

View File

@@ -52,10 +52,10 @@ func WrapUp() {
wrapUpListeners.notifyListeners() wrapUpListeners.notifyListeners()
} }
func gracefulStop(signals chan os.Signal) { func gracefulStop(signals chan os.Signal, sig syscall.Signal) {
signal.Stop(signals) signal.Stop(signals)
logx.Info("Got signal SIGTERM, shutting down...") logx.Infof("Got signal %d, shutting down...", sig)
go wrapUpListeners.notifyListeners() go wrapUpListeners.notifyListeners()
time.Sleep(wrapUpTime) time.Sleep(wrapUpTime)
@@ -63,7 +63,7 @@ func gracefulStop(signals chan os.Signal) {
time.Sleep(delayTimeBeforeForceQuit - wrapUpTime) time.Sleep(delayTimeBeforeForceQuit - wrapUpTime)
logx.Infof("Still alive after %v, going to force kill the process...", delayTimeBeforeForceQuit) logx.Infof("Still alive after %v, going to force kill the process...", delayTimeBeforeForceQuit)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM) _ = syscall.Kill(syscall.Getpid(), sig)
} }
type listenerManager struct { type listenerManager struct {

View File

@@ -6,21 +6,23 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
const timeFormat = "0102150405" const (
profileDuration = time.Minute
timeFormat = "0102150405"
)
var done = make(chan struct{}) var done = make(chan struct{})
func init() { func init() {
go func() { go func() {
var profiler Stopper
// https://golang.org/pkg/os/signal/#Notify // https://golang.org/pkg/os/signal/#Notify
signals := make(chan os.Signal, 1) signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM) signal.Notify(signals, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM, syscall.SIGINT)
for { for {
v := <-signals v := <-signals
@@ -28,21 +30,14 @@ func init() {
case syscall.SIGUSR1: case syscall.SIGUSR1:
dumpGoroutines(fileCreator{}) dumpGoroutines(fileCreator{})
case syscall.SIGUSR2: case syscall.SIGUSR2:
if profiler == nil { profiler := StartProfile()
profiler = StartProfile() time.AfterFunc(profileDuration, profiler.Stop)
} else {
profiler.Stop()
profiler = nil
}
case syscall.SIGTERM: case syscall.SIGTERM:
select { stopOnSignal()
case <-done: gracefulStop(signals, syscall.SIGTERM)
// already closed case syscall.SIGINT:
default: stopOnSignal()
close(done) gracefulStop(signals, syscall.SIGINT)
}
gracefulStop(signals)
default: default:
logx.Error("Got unregistered signal:", v) logx.Error("Got unregistered signal:", v)
} }
@@ -54,3 +49,12 @@ func init() {
func Done() <-chan struct{} { func Done() <-chan struct{} {
return done return done
} }
func stopOnSignal() {
select {
case <-done:
// already closed
default:
close(done)
}
}

View File

@@ -69,10 +69,10 @@ func (t *Tree) Add(route string, item any) error {
} }
err := add(t.root, route[1:], item) err := add(t.root, route[1:], item)
switch err { switch {
case errDupItem: case errors.Is(err, errDupItem):
return duplicatedItem(route) return duplicatedItem(route)
case errDupSlash: case errors.Is(err, errDupSlash):
return duplicatedSlash(route) return duplicatedSlash(route)
default: default:
return err return err

View File

@@ -23,17 +23,22 @@ const (
ProMode = "pro" ProMode = "pro"
) )
// A ServiceConf is a service config. type (
type ServiceConf struct { // DevServerConfig is type alias for devserver.Config
Name string DevServerConfig = devserver.Config
Log logx.LogConf
Mode string `json:",default=pro,options=dev|test|rt|pre|pro"` // A ServiceConf is a service config.
MetricsUrl string `json:",optional"` ServiceConf struct {
// Deprecated: please use DevServer Name string
Prometheus prometheus.Config `json:",optional"` Log logx.LogConf
Telemetry trace.Config `json:",optional"` Mode string `json:",default=pro,options=dev|test|rt|pre|pro"`
DevServer devserver.Config `json:",optional"` MetricsUrl string `json:",optional"`
} // Deprecated: please use DevServer
Prometheus prometheus.Config `json:",optional"`
Telemetry trace.Config `json:",optional"`
DevServer DevServerConfig `json:",optional"`
}
)
// MustSetUp sets up the service, exits on error. // MustSetUp sets up the service, exits on error.
func (sc ServiceConf) MustSetUp() { func (sc ServiceConf) MustSetUp() {

View File

@@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/internal/devserver"
) )
func TestServiceConf(t *testing.T) { func TestServiceConf(t *testing.T) {
@@ -14,6 +15,10 @@ func TestServiceConf(t *testing.T) {
Mode: "console", Mode: "console",
}, },
Mode: "dev", Mode: "dev",
DevServer: devserver.Config{
Port: 6470,
HealthPath: "/healthz",
},
} }
c.MustSetUp() c.MustSetUp()
} }

View File

@@ -1,8 +1,7 @@
package service package service
import ( import (
"log" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/threading" "github.com/zeromicro/go-zero/core/threading"
@@ -51,7 +50,7 @@ func (sg *ServiceGroup) Add(service Service) {
// Also, quitting this method will close the logx output. // Also, quitting this method will close the logx output.
func (sg *ServiceGroup) Start() { func (sg *ServiceGroup) Start() {
proc.AddShutdownListener(func() { proc.AddShutdownListener(func() {
log.Println("Shutting down...") logx.Info("Shutting down services in group")
sg.stopOnce() sg.stopOnce()
}) })

View File

@@ -2,6 +2,7 @@ package internal
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"math" "math"
"os" "os"
@@ -18,6 +19,7 @@ import (
const ( const (
cgroupDir = "/sys/fs/cgroup" cgroupDir = "/sys/fs/cgroup"
cpuMaxFile = cgroupDir + "/cpu.max"
cpuStatFile = cgroupDir + "/cpu.stat" cpuStatFile = cgroupDir + "/cpu.stat"
cpusetFile = cgroupDir + "/cpuset.cpus.effective" cpusetFile = cgroupDir + "/cpuset.cpus.effective"
) )
@@ -30,10 +32,9 @@ var (
) )
type cgroup interface { type cgroup interface {
cpuQuotaUs() (int64, error) cpuQuota() (float64, error)
cpuPeriodUs() (uint64, error) cpuUsage() (uint64, error)
cpus() ([]uint64, error) effectiveCpus() (int, error)
usageAllCpus() (uint64, error)
} }
func currentCgroup() (cgroup, error) { func currentCgroup() (cgroup, error) {
@@ -48,13 +49,22 @@ type cgroupV1 struct {
cgroups map[string]string cgroups map[string]string
} }
func (c *cgroupV1) cpuQuotaUs() (int64, error) { func (c *cgroupV1) cpuQuota() (float64, error) {
data, err := iox.ReadText(path.Join(c.cgroups["cpu"], "cpu.cfs_quota_us")) quotaUs, err := c.cpuQuotaUs()
if err != nil { if err != nil {
return 0, err return 0, err
} }
return strconv.ParseInt(data, 10, 64) if quotaUs == -1 {
return -1, nil
}
periodUs, err := c.cpuPeriodUs()
if err != nil {
return 0, err
}
return float64(quotaUs) / float64(periodUs), nil
} }
func (c *cgroupV1) cpuPeriodUs() (uint64, error) { func (c *cgroupV1) cpuPeriodUs() (uint64, error) {
@@ -66,16 +76,16 @@ func (c *cgroupV1) cpuPeriodUs() (uint64, error) {
return parseUint(data) return parseUint(data)
} }
func (c *cgroupV1) cpus() ([]uint64, error) { func (c *cgroupV1) cpuQuotaUs() (int64, error) {
data, err := iox.ReadText(path.Join(c.cgroups["cpuset"], "cpuset.cpus")) data, err := iox.ReadText(path.Join(c.cgroups["cpu"], "cpu.cfs_quota_us"))
if err != nil { if err != nil {
return nil, err return 0, err
} }
return parseUints(data) return strconv.ParseInt(data, 10, 64)
} }
func (c *cgroupV1) usageAllCpus() (uint64, error) { func (c *cgroupV1) cpuUsage() (uint64, error) {
data, err := iox.ReadText(path.Join(c.cgroups["cpuacct"], "cpuacct.usage")) data, err := iox.ReadText(path.Join(c.cgroups["cpuacct"], "cpuacct.usage"))
if err != nil { if err != nil {
return 0, err return 0, err
@@ -84,38 +94,53 @@ func (c *cgroupV1) usageAllCpus() (uint64, error) {
return parseUint(data) return parseUint(data)
} }
func (c *cgroupV1) effectiveCpus() (int, error) {
data, err := iox.ReadText(path.Join(c.cgroups["cpuset"], "cpuset.cpus"))
if err != nil {
return 0, err
}
cpus, err := parseUints(data)
if err != nil {
return 0, err
}
return len(cpus), nil
}
type cgroupV2 struct { type cgroupV2 struct {
cgroups map[string]string cgroups map[string]string
} }
func (c *cgroupV2) cpuQuotaUs() (int64, error) { func (c *cgroupV2) cpuQuota() (float64, error) {
data, err := iox.ReadText(path.Join(cgroupDir, "cpu.cfs_quota_us")) data, err := iox.ReadText(cpuMaxFile)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return strconv.ParseInt(data, 10, 64) fields := strings.Fields(data)
} if len(fields) != 2 {
return 0, fmt.Errorf("cgroup: bad /sys/fs/cgroup/cpu.max file: %s", data)
}
func (c *cgroupV2) cpuPeriodUs() (uint64, error) { if fields[0] == "max" {
data, err := iox.ReadText(path.Join(cgroupDir, "cpu.cfs_period_us")) return -1, nil
}
quotaUs, err := strconv.ParseInt(fields[0], 10, 64)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return parseUint(data) periodUs, err := strconv.ParseUint(fields[1], 10, 64)
}
func (c *cgroupV2) cpus() ([]uint64, error) {
data, err := iox.ReadText(cpusetFile)
if err != nil { if err != nil {
return nil, err return 0, err
} }
return parseUints(data) return float64(quotaUs) / float64(periodUs), nil
} }
func (c *cgroupV2) usageAllCpus() (uint64, error) { func (c *cgroupV2) cpuUsage() (uint64, error) {
usec, err := parseUint(c.cgroups["usage_usec"]) usec, err := parseUint(c.cgroups["usage_usec"])
if err != nil { if err != nil {
return 0, err return 0, err
@@ -124,6 +149,20 @@ func (c *cgroupV2) usageAllCpus() (uint64, error) {
return usec * uint64(time.Microsecond), nil return usec * uint64(time.Microsecond), nil
} }
func (c *cgroupV2) effectiveCpus() (int, error) {
data, err := iox.ReadText(cpusetFile)
if err != nil {
return 0, err
}
cpus, err := parseUints(data)
if err != nil {
return 0, err
}
return len(cpus), nil
}
func currentCgroupV1() (cgroup, error) { func currentCgroupV1() (cgroup, error) {
cgroupFile := fmt.Sprintf("/proc/%d/cgroup", os.Getpid()) cgroupFile := fmt.Sprintf("/proc/%d/cgroup", os.Getpid())
lines, err := iox.ReadTextLines(cgroupFile, iox.WithoutBlank()) lines, err := iox.ReadTextLines(cgroupFile, iox.WithoutBlank())
@@ -200,7 +239,7 @@ func isCgroup2UnifiedMode() bool {
func parseUint(s string) (uint64, error) { func parseUint(s string) (uint64, error) {
v, err := strconv.ParseInt(s, 10, 64) v, err := strconv.ParseInt(s, 10, 64)
if err != nil { if err != nil {
if err.(*strconv.NumError).Err == strconv.ErrRange { if errors.Is(err, strconv.ErrRange) {
return 0, nil return 0, nil
} }
@@ -225,21 +264,21 @@ func parseUints(val string) ([]uint64, error) {
for _, r := range cols { for _, r := range cols {
if strings.Contains(r, "-") { if strings.Contains(r, "-") {
fields := strings.SplitN(r, "-", 2) fields := strings.SplitN(r, "-", 2)
min, err := parseUint(fields[0]) minimum, err := parseUint(fields[0])
if err != nil { if err != nil {
return nil, fmt.Errorf("cgroup: bad int list format: %s", val) return nil, fmt.Errorf("cgroup: bad int list format: %s", val)
} }
max, err := parseUint(fields[1]) maximum, err := parseUint(fields[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("cgroup: bad int list format: %s", val) return nil, fmt.Errorf("cgroup: bad int list format: %s", val)
} }
if max < min { if maximum < minimum {
return nil, fmt.Errorf("cgroup: bad int list format: %s", val) return nil, fmt.Errorf("cgroup: bad int list format: %s", val)
} }
for i := min; i <= max; i++ { for i := minimum; i <= maximum; i++ {
if _, ok := ints[i]; !ok { if _, ok := ints[i]; !ok {
ints[i] = lang.Placeholder ints[i] = lang.Placeholder
sets = append(sets, i) sets = append(sets, i)

View File

@@ -12,18 +12,29 @@ func TestRunningInUserNS(t *testing.T) {
assert.False(t, runningInUserNS()) assert.False(t, runningInUserNS())
} }
func TestCgroupV1(t *testing.T) { func TestCgroups(t *testing.T) {
if isCgroup2UnifiedMode() { // test cgroup legacy(v1) & hybrid
if !isCgroup2UnifiedMode() {
cg, err := currentCgroupV1() cg, err := currentCgroupV1()
assert.NoError(t, err) assert.NoError(t, err)
_, err = cg.cpus() _, err = cg.effectiveCpus()
assert.Error(t, err) assert.NoError(t, err)
_, err = cg.cpuPeriodUs() _, err = cg.cpuQuota()
assert.Error(t, err) assert.NoError(t, err)
_, err = cg.cpuQuotaUs() _, err = cg.cpuUsage()
assert.Error(t, err) assert.NoError(t, err)
_, err = cg.usageAllCpus() }
// test cgroup v2
if isCgroup2UnifiedMode() {
cg, err := currentCgroupV2()
assert.NoError(t, err)
_, err = cg.effectiveCpus()
assert.NoError(t, err)
_, err = cg.cpuQuota()
assert.Error(t, err) assert.Error(t, err)
_, err = cg.cpuUsage()
assert.NoError(t, err)
} }
} }

View File

@@ -14,60 +14,28 @@ import (
const ( const (
cpuTicks = 100 cpuTicks = 100
cpuFields = 8 cpuFields = 8
cpuMax = 1000
statFile = "/proc/stat"
) )
var ( var (
preSystem uint64 preSystem uint64
preTotal uint64 preTotal uint64
quota float64 limit float64
cores uint64 cores uint64
noCgroup bool
initOnce sync.Once initOnce sync.Once
) )
// if /proc not present, ignore the cpu calculation, like wsl linux
func initialize() {
cpus, err := cpuSets()
if err != nil {
logx.Error(err)
return
}
cores = uint64(len(cpus))
quota = float64(len(cpus))
cq, err := cpuQuota()
if err == nil {
if cq != -1 {
period, err := cpuPeriod()
if err != nil {
logx.Error(err)
return
}
limit := float64(cq) / float64(period)
if limit < quota {
quota = limit
}
}
}
preSystem, err = systemCpuUsage()
if err != nil {
logx.Error(err)
return
}
preTotal, err = totalCpuUsage()
if err != nil {
logx.Error(err)
return
}
}
// RefreshCpu refreshes cpu usage and returns. // RefreshCpu refreshes cpu usage and returns.
func RefreshCpu() uint64 { func RefreshCpu() uint64 {
initOnce.Do(initialize) initializeOnce()
total, err := totalCpuUsage() if noCgroup {
return 0
}
total, err := cpuUsage()
if err != nil { if err != nil {
return 0 return 0
} }
@@ -81,7 +49,10 @@ func RefreshCpu() uint64 {
cpuDelta := total - preTotal cpuDelta := total - preTotal
systemDelta := system - preSystem systemDelta := system - preSystem
if cpuDelta > 0 && systemDelta > 0 { if cpuDelta > 0 && systemDelta > 0 {
usage = uint64(float64(cpuDelta*cores*1e3) / (float64(systemDelta) * quota)) usage = uint64(float64(cpuDelta*cores*cpuMax) / (float64(systemDelta) * limit))
if usage > cpuMax {
usage = cpuMax
}
} }
preSystem = system preSystem = system
preTotal = total preTotal = total
@@ -89,35 +60,76 @@ func RefreshCpu() uint64 {
return usage return usage
} }
func cpuQuota() (int64, error) { func cpuQuota() (float64, error) {
cg, err := currentCgroup() cg, err := currentCgroup()
if err != nil { if err != nil {
return 0, err return 0, err
} }
return cg.cpuQuotaUs() return cg.cpuQuota()
} }
func cpuPeriod() (uint64, error) { func cpuUsage() (uint64, error) {
cg, err := currentCgroup() cg, err := currentCgroup()
if err != nil { if err != nil {
return 0, err return 0, err
} }
return cg.cpuPeriodUs() return cg.cpuUsage()
} }
func cpuSets() ([]uint64, error) { func effectiveCpus() (int, error) {
cg, err := currentCgroup() cg, err := currentCgroup()
if err != nil { if err != nil {
return nil, err return 0, err
} }
return cg.cpus() return cg.effectiveCpus()
}
// if /proc not present, ignore the cpu calculation, like wsl linux
func initialize() error {
cpus, err := effectiveCpus()
if err != nil {
return err
}
cores = uint64(cpus)
limit = float64(cpus)
quota, err := cpuQuota()
if err == nil && quota > 0 {
if quota < limit {
limit = quota
}
}
preSystem, err = systemCpuUsage()
if err != nil {
return err
}
preTotal, err = cpuUsage()
return err
}
func initializeOnce() {
initOnce.Do(func() {
defer func() {
if p := recover(); p != nil {
noCgroup = true
logx.Error(p)
}
}()
if err := initialize(); err != nil {
noCgroup = true
logx.Error(err)
}
})
} }
func systemCpuUsage() (uint64, error) { func systemCpuUsage() (uint64, error) {
lines, err := iox.ReadTextLines("/proc/stat", iox.WithoutBlank()) lines, err := iox.ReadTextLines(statFile, iox.WithoutBlank())
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -145,12 +157,3 @@ func systemCpuUsage() (uint64, error) {
return 0, errors.New("bad stats format") return 0, errors.New("bad stats format")
} }
func totalCpuUsage() (usage uint64, err error) {
var cg cgroup
if cg, err = currentCgroup(); err != nil {
return
}
return cg.usageAllCpus()
}

View File

@@ -1,11 +1,13 @@
package stat package stat
import ( import (
"errors"
"strconv" "strconv"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx/logtest"
) )
func TestMetrics(t *testing.T) { func TestMetrics(t *testing.T) {
@@ -30,6 +32,34 @@ func TestMetrics(t *testing.T) {
} }
} }
func TestTopDurationWithEmpty(t *testing.T) {
assert.Equal(t, float32(0), getTopDuration(nil))
assert.Equal(t, float32(0), getTopDuration([]Task{}))
}
func TestLogAndReport(t *testing.T) {
buf := logtest.NewCollector(t)
old := logEnabled.True()
logEnabled.Set(true)
t.Cleanup(func() {
logEnabled.Set(old)
})
log(&StatReport{})
assert.NotEmpty(t, buf.String())
writerLock.Lock()
writer := reportWriter
writerLock.Unlock()
buf = logtest.NewCollector(t)
t.Cleanup(func() {
SetReportWriter(writer)
})
SetReportWriter(&badWriter{})
writeReport(&StatReport{})
assert.NotEmpty(t, buf.String())
}
type mockedWriter struct { type mockedWriter struct {
report *StatReport report *StatReport
} }
@@ -38,3 +68,9 @@ func (m *mockedWriter) Write(report *StatReport) error {
m.report = report m.report = report
return nil return nil
} }
type badWriter struct{}
func (b *badWriter) Write(report *StatReport) error {
return errors.New("bad")
}

View File

@@ -9,7 +9,7 @@ import (
const dbTag = "db" const dbTag = "db"
// RawFieldNames converts golang struct field into slice string. // RawFieldNames converts golang struct field into slice string.
func RawFieldNames(in any, postgresSql ...bool) []string { func RawFieldNames(in any, postgreSql ...bool) []string {
out := make([]string, 0) out := make([]string, 0)
v := reflect.ValueOf(in) v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr { if v.Kind() == reflect.Ptr {
@@ -17,8 +17,8 @@ func RawFieldNames(in any, postgresSql ...bool) []string {
} }
var pg bool var pg bool
if len(postgresSql) > 0 { if len(postgreSql) > 0 {
pg = postgresSql[0] pg = postgreSql[0]
} }
// we only accept structs // we only accept structs

View File

@@ -96,7 +96,7 @@ func (c cacheNode) Get(key string, val any) error {
// GetCtx gets the cache with key and fills into v. // GetCtx gets the cache with key and fills into v.
func (c cacheNode) GetCtx(ctx context.Context, key string, val any) error { func (c cacheNode) GetCtx(ctx context.Context, key string, val any) error {
err := c.doGetCache(ctx, key, val) err := c.doGetCache(ctx, key, val)
if err == errPlaceholder { if errors.Is(err, errPlaceholder) {
return c.errNotFound return c.errNotFound
} }
@@ -210,16 +210,16 @@ func (c cacheNode) doTake(ctx context.Context, v any, key string,
logger := logx.WithContext(ctx) logger := logx.WithContext(ctx)
val, fresh, err := c.barrier.DoEx(key, func() (any, error) { val, fresh, err := c.barrier.DoEx(key, func() (any, error) {
if err := c.doGetCache(ctx, key, v); err != nil { if err := c.doGetCache(ctx, key, v); err != nil {
if err == errPlaceholder { if errors.Is(err, errPlaceholder) {
return nil, c.errNotFound return nil, c.errNotFound
} else if err != c.errNotFound { } else if !errors.Is(err, c.errNotFound) {
// why we just return the error instead of query from db, // why we just return the error instead of query from db,
// because we don't allow the disaster pass to the dbs. // because we don't allow the disaster pass to the dbs.
// fail fast, in case we bring down the dbs. // fail fast, in case we bring down the dbs.
return nil, err return nil, err
} }
if err = query(v); err == c.errNotFound { if err = query(v); errors.Is(err, c.errNotFound) {
if err = c.setCacheWithNotFound(ctx, key); err != nil { if err = c.setCacheWithNotFound(ctx, key); err != nil {
logger.Error(err) logger.Error(err)
} }

View File

@@ -11,8 +11,6 @@ import (
func TestBulkInserter(t *testing.T) { func TestBulkInserter(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...)) mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
bulk, err := NewBulkInserter(createModel(mt).Collection) bulk, err := NewBulkInserter(createModel(mt).Collection)

View File

@@ -9,8 +9,6 @@ import (
func TestClientManger_getClient(t *testing.T) { func TestClientManger_getClient(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
Inject(mtest.ClusterURI(), mt.Client) Inject(mtest.ClusterURI(), mt.Client)
cli, err := getClient(mtest.ClusterURI()) cli, err := getClient(mtest.ClusterURI())

View File

@@ -2,11 +2,10 @@ package mon
import ( import (
"context" "context"
"encoding/json" "errors"
"time" "time"
"github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/timex" "github.com/zeromicro/go-zero/core/timex"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
mopt "go.mongodb.org/mongo-driver/mongo/options" mopt "go.mongodb.org/mongo-driver/mongo/options"
@@ -502,45 +501,11 @@ func (c *decoratedCollection) UpdateOne(ctx context.Context, filter, update any,
func (c *decoratedCollection) logDuration(ctx context.Context, method string, func (c *decoratedCollection) logDuration(ctx context.Context, method string,
startTime time.Duration, err error, docs ...any) { startTime time.Duration, err error, docs ...any) {
duration := timex.Since(startTime) logDurationWithDocs(ctx, c.name, method, startTime, err, docs...)
logger := logx.WithContext(ctx).WithDuration(duration)
content, jerr := json.Marshal(docs)
// jerr should not be non-nil, but we don't care much on this,
// if non-nil, we just log without docs.
if jerr != nil {
if err != nil {
if duration > slowThreshold.Load() {
logger.Slowf("[MONGO] mongo(%s) - slowcall - %s - fail(%s)", c.name, method, err.Error())
} else {
logger.Infof("mongo(%s) - %s - fail(%s)", c.name, method, err.Error())
}
} else {
if duration > slowThreshold.Load() {
logger.Slowf("[MONGO] mongo(%s) - slowcall - %s - ok", c.name, method)
} else {
logger.Infof("mongo(%s) - %s - ok", c.name, method)
}
}
} else if err != nil {
if duration > slowThreshold.Load() {
logger.Slowf("[MONGO] mongo(%s) - slowcall - %s - fail(%s) - %s",
c.name, method, err.Error(), string(content))
} else {
logger.Infof("mongo(%s) - %s - fail(%s) - %s",
c.name, method, err.Error(), string(content))
}
} else {
if duration > slowThreshold.Load() {
logger.Slowf("[MONGO] mongo(%s) - slowcall - %s - ok - %s",
c.name, method, string(content))
} else {
logger.Infof("mongo(%s) - %s - ok - %s", c.name, method, string(content))
}
}
} }
func (c *decoratedCollection) logDurationSimple(ctx context.Context, method string, startTime time.Duration, err error) { func (c *decoratedCollection) logDurationSimple(ctx context.Context, method string,
startTime time.Duration, err error) {
logDuration(ctx, c.name, method, startTime, err) logDuration(ctx, c.name, method, startTime, err)
} }
@@ -562,11 +527,19 @@ func (p keepablePromise) keep(err error) error {
} }
func acceptable(err error) bool { func acceptable(err error) bool {
return err == nil || err == mongo.ErrNoDocuments || err == mongo.ErrNilValue || return err == nil ||
err == mongo.ErrNilDocument || err == mongo.ErrNilCursor || err == mongo.ErrEmptySlice || errors.Is(err, mongo.ErrNoDocuments) ||
errors.Is(err, mongo.ErrNilValue) ||
errors.Is(err, mongo.ErrNilDocument) ||
errors.Is(err, mongo.ErrNilCursor) ||
errors.Is(err, mongo.ErrEmptySlice) ||
// session errors // session errors
err == session.ErrSessionEnded || err == session.ErrNoTransactStarted || errors.Is(err, session.ErrSessionEnded) ||
err == session.ErrTransactInProgress || err == session.ErrAbortAfterCommit || errors.Is(err, session.ErrNoTransactStarted) ||
err == session.ErrAbortTwice || err == session.ErrCommitAfterAbort || errors.Is(err, session.ErrTransactInProgress) ||
err == session.ErrUnackWCUnsupported || err == session.ErrSnapshotTransaction errors.Is(err, session.ErrAbortAfterCommit) ||
errors.Is(err, session.ErrAbortTwice) ||
errors.Is(err, session.ErrCommitAfterAbort) ||
errors.Is(err, session.ErrUnackWCUnsupported) ||
errors.Is(err, session.ErrSnapshotTransaction)
} }

View File

@@ -68,7 +68,6 @@ func TestKeepPromise_keep(t *testing.T) {
func TestNewCollection(t *testing.T) { func TestNewCollection(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
coll := mt.Coll coll := mt.Coll
assert.NotNil(t, coll) assert.NotNil(t, coll)
@@ -79,7 +78,6 @@ func TestNewCollection(t *testing.T) {
func TestCollection_Aggregate(t *testing.T) { func TestCollection_Aggregate(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
coll := mt.Coll coll := mt.Coll
assert.NotNil(t, coll) assert.NotNil(t, coll)
@@ -96,8 +94,6 @@ func TestCollection_Aggregate(t *testing.T) {
func TestCollection_BulkWrite(t *testing.T) { func TestCollection_BulkWrite(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -119,8 +115,6 @@ func TestCollection_BulkWrite(t *testing.T) {
func TestCollection_CountDocuments(t *testing.T) { func TestCollection_CountDocuments(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -145,8 +139,6 @@ func TestCollection_CountDocuments(t *testing.T) {
func TestDecoratedCollection_DeleteMany(t *testing.T) { func TestDecoratedCollection_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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -165,8 +157,6 @@ func TestDecoratedCollection_DeleteMany(t *testing.T) {
func TestCollection_Distinct(t *testing.T) { func TestCollection_Distinct(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -185,8 +175,6 @@ func TestCollection_Distinct(t *testing.T) {
func TestCollection_EstimatedDocumentCount(t *testing.T) { func TestCollection_EstimatedDocumentCount(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -205,8 +193,6 @@ func TestCollection_EstimatedDocumentCount(t *testing.T) {
func TestCollection_Find(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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -253,8 +239,6 @@ func TestCollection_Find(t *testing.T) {
func TestCollection_FindOne(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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -297,8 +281,6 @@ func TestCollection_FindOne(t *testing.T) {
func TestCollection_FindOneAndDelete(t *testing.T) { func TestCollection_FindOneAndDelete(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -328,8 +310,6 @@ func TestCollection_FindOneAndDelete(t *testing.T) {
func TestCollection_FindOneAndReplace(t *testing.T) { func TestCollection_FindOneAndReplace(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -360,8 +340,6 @@ func TestCollection_FindOneAndReplace(t *testing.T) {
func TestCollection_FindOneAndUpdate(t *testing.T) { func TestCollection_FindOneAndUpdate(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -393,8 +371,6 @@ func TestCollection_FindOneAndUpdate(t *testing.T) {
func TestCollection_InsertOne(t *testing.T) { func TestCollection_InsertOne(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -413,8 +389,6 @@ func TestCollection_InsertOne(t *testing.T) {
func TestCollection_InsertMany(t *testing.T) { func TestCollection_InsertMany(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -437,8 +411,6 @@ func TestCollection_InsertMany(t *testing.T) {
func TestCollection_DeleteOne(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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -457,8 +429,6 @@ func TestCollection_DeleteOne(t *testing.T) {
func TestCollection_DeleteMany(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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -477,8 +447,6 @@ func TestCollection_DeleteMany(t *testing.T) {
func TestCollection_ReplaceOne(t *testing.T) { func TestCollection_ReplaceOne(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -500,8 +468,6 @@ func TestCollection_ReplaceOne(t *testing.T) {
func TestCollection_UpdateOne(t *testing.T) { func TestCollection_UpdateOne(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -522,8 +488,6 @@ func TestCollection_UpdateOne(t *testing.T) {
func TestCollection_UpdateByID(t *testing.T) { func TestCollection_UpdateByID(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -544,8 +508,6 @@ func TestCollection_UpdateByID(t *testing.T) {
func TestCollection_UpdateMany(t *testing.T) { func TestCollection_UpdateMany(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
@@ -566,7 +528,6 @@ func TestCollection_UpdateMany(t *testing.T) {
func TestDecoratedCollection_LogDuration(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()
c := decoratedCollection{ c := decoratedCollection{
Collection: mt.Coll, Collection: mt.Coll,
brk: breaker.NewBreaker(), brk: breaker.NewBreaker(),
@@ -599,13 +560,11 @@ func TestDecoratedCollection_LogDuration(t *testing.T) {
errors.New("bar"), make(chan int)) errors.New("bar"), make(chan int))
assert.Contains(t, buf.String(), "foo") assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar") assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "slowcall")
buf.Reset() buf.Reset()
c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2, c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2,
errors.New("bar")) errors.New("bar"))
assert.Contains(t, buf.String(), "foo") assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "slowcall")
buf.Reset() buf.Reset()
c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2, nil) c.logDuration(context.Background(), "foo", timex.Now()-slowThreshold.Load()*2, nil)
@@ -644,11 +603,11 @@ func (d *dropBreaker) DoWithAcceptable(_ func() error, _ breaker.Acceptable) err
return errDummy return errDummy
} }
func (d *dropBreaker) DoWithFallback(_ func() error, _ func(err error) error) error { func (d *dropBreaker) DoWithFallback(_ func() error, _ breaker.Fallback) error {
return nil return nil
} }
func (d *dropBreaker) DoWithFallbackAcceptable(_ func() error, _ func(err error) error, func (d *dropBreaker) DoWithFallbackAcceptable(_ func() error, _ breaker.Fallback,
_ breaker.Acceptable) error { _ breaker.Acceptable) error {
return nil return nil
} }

View File

@@ -12,8 +12,6 @@ import (
func TestModel_StartSession(t *testing.T) { func TestModel_StartSession(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
sess, err := m.StartSession() sess, err := m.StartSession()
@@ -34,8 +32,6 @@ func TestModel_StartSession(t *testing.T) {
func TestModel_Aggregate(t *testing.T) { func TestModel_Aggregate(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
find := mtest.CreateCursorResponse( find := mtest.CreateCursorResponse(
@@ -71,8 +67,6 @@ func TestModel_Aggregate(t *testing.T) {
func TestModel_DeleteMany(t *testing.T) { func TestModel_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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...)) mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
@@ -88,8 +82,6 @@ func TestModel_DeleteMany(t *testing.T) {
func TestModel_DeleteOne(t *testing.T) { func TestModel_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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...)) mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
@@ -105,8 +97,6 @@ func TestModel_DeleteOne(t *testing.T) {
func TestModel_Find(t *testing.T) { func TestModel_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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
find := mtest.CreateCursorResponse( find := mtest.CreateCursorResponse(
@@ -142,8 +132,6 @@ func TestModel_Find(t *testing.T) {
func TestModel_FindOne(t *testing.T) { func TestModel_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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
find := mtest.CreateCursorResponse( find := mtest.CreateCursorResponse(
@@ -170,8 +158,6 @@ func TestModel_FindOne(t *testing.T) {
func TestModel_FindOneAndDelete(t *testing.T) { func TestModel_FindOneAndDelete(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
@@ -189,8 +175,6 @@ func TestModel_FindOneAndDelete(t *testing.T) {
func TestModel_FindOneAndReplace(t *testing.T) { func TestModel_FindOneAndReplace(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
@@ -212,8 +196,6 @@ func TestModel_FindOneAndReplace(t *testing.T) {
func TestModel_FindOneAndUpdate(t *testing.T) { func TestModel_FindOneAndUpdate(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(mt) m := createModel(mt)
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{

View File

@@ -1,37 +1,74 @@
package mon package mon
import ( import (
"reflect"
"time" "time"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
mopt "go.mongodb.org/mongo-driver/mongo/options" mopt "go.mongodb.org/mongo-driver/mongo/options"
) )
const defaultTimeout = time.Second * 3 const defaultTimeout = time.Second * 3
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold) var (
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
logMon = syncx.ForAtomicBool(true)
logSlowMon = syncx.ForAtomicBool(true)
)
type ( type (
options = mopt.ClientOptions
// Option defines the method to customize a mongo model. // Option defines the method to customize a mongo model.
Option func(opts *options) Option func(opts *options)
// TypeCodec is a struct that stores specific type Encoder/Decoder.
TypeCodec struct {
ValueType reflect.Type
Encoder bsoncodec.ValueEncoder
Decoder bsoncodec.ValueDecoder
}
options = mopt.ClientOptions
) )
// DisableLog disables logging of mongo commands, includes info and slow logs.
func DisableLog() {
logMon.Set(false)
logSlowMon.Set(false)
}
// DisableInfoLog disables info logging of mongo commands, but keeps slow logs.
func DisableInfoLog() {
logMon.Set(false)
}
// SetSlowThreshold sets the slow threshold. // SetSlowThreshold sets the slow threshold.
func SetSlowThreshold(threshold time.Duration) { func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold) slowThreshold.Set(threshold)
} }
func defaultTimeoutOption() Option {
return func(opts *options) {
opts.SetTimeout(defaultTimeout)
}
}
// WithTimeout set the mon client operation timeout. // WithTimeout set the mon client operation timeout.
func WithTimeout(timeout time.Duration) Option { func WithTimeout(timeout time.Duration) Option {
return func(opts *options) { return func(opts *options) {
opts.SetTimeout(timeout) opts.SetTimeout(timeout)
} }
} }
// WithTypeCodec registers TypeCodecs to convert custom types.
func WithTypeCodec(typeCodecs ...TypeCodec) Option {
return func(opts *options) {
registry := bson.NewRegistry()
for _, v := range typeCodecs {
registry.RegisterTypeEncoder(v.ValueType, v.Encoder)
registry.RegisterTypeDecoder(v.ValueType, v.Decoder)
}
opts.SetRegistry(registry)
}
}
func defaultTimeoutOption() Option {
return func(opts *options) {
opts.SetTimeout(defaultTimeout)
}
}

View File

@@ -1,10 +1,14 @@
package mon package mon
import ( import (
"fmt"
"reflect"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
mopt "go.mongodb.org/mongo-driver/mongo/options" mopt "go.mongodb.org/mongo-driver/mongo/options"
) )
@@ -25,3 +29,82 @@ func TestWithTimeout(t *testing.T) {
WithTimeout(time.Second)(opts) WithTimeout(time.Second)(opts)
assert.Equal(t, time.Second, *opts.Timeout) assert.Equal(t, time.Second, *opts.Timeout)
} }
func TestDisableLog(t *testing.T) {
assert.True(t, logMon.True())
assert.True(t, logSlowMon.True())
defer func() {
logMon.Set(true)
logSlowMon.Set(true)
}()
DisableLog()
assert.False(t, logMon.True())
assert.False(t, logSlowMon.True())
}
func TestDisableInfoLog(t *testing.T) {
assert.True(t, logMon.True())
assert.True(t, logSlowMon.True())
defer func() {
logMon.Set(true)
logSlowMon.Set(true)
}()
DisableInfoLog()
assert.False(t, logMon.True())
assert.True(t, logSlowMon.True())
}
func TestWithRegistryForTimestampRegisterType(t *testing.T) {
opts := mopt.Client()
// mongoDateTimeEncoder allow user convert time.Time to primitive.DateTime.
var mongoDateTimeEncoder bsoncodec.ValueEncoderFunc = func(ect bsoncodec.EncodeContext, w bsonrw.ValueWriter, value reflect.Value) error {
// Use reflect, determine if it can be converted to time.Time.
dec, ok := value.Interface().(time.Time)
if !ok {
return fmt.Errorf("value %v to encode is not of type time.Time", value)
}
return w.WriteDateTime(dec.Unix())
}
// mongoDateTimeEncoder allow user convert primitive.DateTime to time.Time.
var mongoDateTimeDecoder bsoncodec.ValueDecoderFunc = func(ect bsoncodec.DecodeContext, r bsonrw.ValueReader, value reflect.Value) error {
primTime, err := r.ReadDateTime()
if err != nil {
return fmt.Errorf("error reading primitive.DateTime from ValueReader: %v", err)
}
value.Set(reflect.ValueOf(time.Unix(primTime, 0)))
return nil
}
codecs := []TypeCodec{
{
ValueType: reflect.TypeOf(time.Time{}),
Encoder: mongoDateTimeEncoder,
Decoder: mongoDateTimeDecoder,
},
}
WithTypeCodec(codecs...)(opts)
for _, v := range codecs {
// Validate Encoder
enc, err := opts.Registry.LookupEncoder(v.ValueType)
if err != nil {
t.Fatal(err)
}
if assert.ObjectsAreEqual(v.Encoder, enc) {
t.Errorf("Encoder got from Registry: %v, but want: %v", enc, v.Encoder)
}
// Validate Decoder
dec, err := opts.Registry.LookupDecoder(v.ValueType)
if err != nil {
t.Fatal(err)
}
if assert.ObjectsAreEqual(v.Decoder, dec) {
t.Errorf("Decoder got from Registry: %v, but want: %v", dec, v.Decoder)
}
}
}

View File

@@ -2,6 +2,7 @@ package mon
import ( import (
"context" "context"
"errors"
"github.com/zeromicro/go-zero/core/trace" "github.com/zeromicro/go-zero/core/trace"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
@@ -23,8 +24,8 @@ func startSpan(ctx context.Context, cmd string) (context.Context, oteltrace.Span
func endSpan(span oteltrace.Span, err error) { func endSpan(span oteltrace.Span, err error) {
defer span.End() defer span.End()
if err == nil || err == mongo.ErrNoDocuments || if err == nil || errors.Is(err, mongo.ErrNoDocuments) ||
err == mongo.ErrNilValue || err == mongo.ErrNilDocument { errors.Is(err, mongo.ErrNilValue) || errors.Is(err, mongo.ErrNilDocument) {
span.SetStatus(codes.Ok, "") span.SetStatus(codes.Ok, "")
return return
} }

View File

@@ -2,6 +2,7 @@ package mon
import ( import (
"context" "context"
"encoding/json"
"strings" "strings"
"time" "time"
@@ -20,8 +21,41 @@ func logDuration(ctx context.Context, name, method string, startTime time.Durati
duration := timex.Since(startTime) duration := timex.Since(startTime)
logger := logx.WithContext(ctx).WithDuration(duration) logger := logx.WithContext(ctx).WithDuration(duration)
if err != nil { if err != nil {
logger.Infof("mongo(%s) - %s - fail(%s)", name, method, err.Error()) logger.Errorf("mongo(%s) - %s - fail(%s)", name, method, err.Error())
} else { return
}
if logSlowMon.True() && duration > slowThreshold.Load() {
logger.Slowf("[MONGO] mongo(%s) - slowcall - %s - ok", name, method)
} else if logMon.True() {
logger.Infof("mongo(%s) - %s - ok", name, method) logger.Infof("mongo(%s) - %s - ok", name, method)
} }
} }
func logDurationWithDocs(ctx context.Context, name, method string, startTime time.Duration,
err error, docs ...any) {
duration := timex.Since(startTime)
logger := logx.WithContext(ctx).WithDuration(duration)
content, jerr := json.Marshal(docs)
// jerr should not be non-nil, but we don't care much on this,
// if non-nil, we just log without docs.
if jerr != nil {
if err != nil {
logger.Errorf("mongo(%s) - %s - fail(%s)", name, method, err.Error())
} else if logSlowMon.True() && duration > slowThreshold.Load() {
logger.Slowf("[MONGO] mongo(%s) - slowcall - %s - ok", name, method)
} else if logMon.True() {
logger.Infof("mongo(%s) - %s - ok", name, method)
}
return
}
if err != nil {
logger.Errorf("mongo(%s) - %s - fail(%s) - %s", name, method, err.Error(), string(content))
} else if logSlowMon.True() && duration > slowThreshold.Load() {
logger.Slowf("[MONGO] mongo(%s) - slowcall - %s - ok - %s", name, method, string(content))
} else if logMon.True() {
logger.Infof("mongo(%s) - %s - ok - %s", name, method, string(content))
}
}

View File

@@ -4,10 +4,10 @@ import (
"context" "context"
"errors" "errors"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx/logtest" "github.com/zeromicro/go-zero/core/logx/logtest"
"github.com/zeromicro/go-zero/core/timex"
) )
func TestFormatAddrs(t *testing.T) { func TestFormatAddrs(t *testing.T) {
@@ -42,13 +42,148 @@ func Test_logDuration(t *testing.T) {
buf := logtest.NewCollector(t) buf := logtest.NewCollector(t)
buf.Reset() buf.Reset()
logDuration(context.Background(), "foo", "bar", time.Millisecond, nil) logDuration(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil)
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "slow")
buf.Reset()
logDuration(context.Background(), "foo", "bar", timex.Now(), nil)
assert.Contains(t, buf.String(), "foo") assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar") assert.Contains(t, buf.String(), "bar")
buf.Reset() buf.Reset()
logDuration(context.Background(), "foo", "bar", time.Millisecond, errors.New("bar")) logDuration(context.Background(), "foo", "bar", timex.Now(), errors.New("bar"))
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "fail")
defer func() {
logMon.Set(true)
logSlowMon.Set(true)
}()
buf.Reset()
DisableInfoLog()
logDuration(context.Background(), "foo", "bar", timex.Now(), nil)
assert.Empty(t, buf.String())
buf.Reset()
logDuration(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil)
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "slow")
buf.Reset()
DisableLog()
logDuration(context.Background(), "foo", "bar", timex.Now(), nil)
assert.Empty(t, buf.String())
buf.Reset()
logDuration(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil)
assert.Empty(t, buf.String())
buf.Reset()
logDuration(context.Background(), "foo", "bar", timex.Now(), errors.New("bar"))
assert.Contains(t, buf.String(), "foo") assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar") assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "fail") assert.Contains(t, buf.String(), "fail")
} }
func Test_logDurationWithDoc(t *testing.T) {
buf := logtest.NewCollector(t)
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil, make(chan int))
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "slow")
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil, "{'json': ''}")
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "slow")
assert.Contains(t, buf.String(), "json")
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), nil, make(chan int))
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), nil, "{'json': ''}")
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "json")
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), errors.New("bar"), make(chan int))
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "fail")
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), errors.New("bar"), "{'json': ''}")
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "fail")
assert.Contains(t, buf.String(), "json")
defer func() {
logMon.Set(true)
logSlowMon.Set(true)
}()
buf.Reset()
DisableInfoLog()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), nil, make(chan int))
assert.Empty(t, buf.String())
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), nil, "{'json': ''}")
assert.Empty(t, buf.String())
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil, make(chan int))
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "slow")
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil, "{'json': ''}")
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "slow")
assert.Contains(t, buf.String(), "json")
buf.Reset()
DisableLog()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), nil, make(chan int))
assert.Empty(t, buf.String())
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), nil, "{'json': ''}")
assert.Empty(t, buf.String())
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil, make(chan int))
assert.Empty(t, buf.String())
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now()-slowThreshold.Load()*2, nil, "{'json': ''}")
assert.Empty(t, buf.String())
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), errors.New("bar"), make(chan int))
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "fail")
buf.Reset()
logDurationWithDocs(context.Background(), "foo", "bar", timex.Now(), errors.New("bar"), "{'json': ''}")
assert.Contains(t, buf.String(), "foo")
assert.Contains(t, buf.String(), "bar")
assert.Contains(t, buf.String(), "fail")
assert.Contains(t, buf.String(), "json")
}

View File

@@ -17,8 +17,6 @@ import (
func TestNewModel(t *testing.T) { func TestNewModel(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
_, err := newModel("foo", mt.DB.Name(), mt.Coll.Name(), nil) _, err := newModel("foo", mt.DB.Name(), mt.Coll.Name(), nil)
assert.NotNil(mt, err) assert.NotNil(mt, err)
@@ -27,8 +25,6 @@ func TestNewModel(t *testing.T) {
func TestModel_DelCache(t *testing.T) { func TestModel_DelCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(t, mt) m := createModel(t, mt)
assert.Nil(t, m.cache.Set("foo", "bar")) assert.Nil(t, m.cache.Set("foo", "bar"))
@@ -42,8 +38,6 @@ func TestModel_DelCache(t *testing.T) {
func TestModel_DeleteOne(t *testing.T) { func TestModel_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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...)) mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
m := createModel(t, mt) m := createModel(t, mt)
@@ -65,8 +59,6 @@ func TestModel_DeleteOne(t *testing.T) {
func TestModel_DeleteOneNoCache(t *testing.T) { func TestModel_DeleteOneNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...)) mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "n", Value: 1}}...))
m := createModel(t, mt) m := createModel(t, mt)
@@ -81,8 +73,6 @@ func TestModel_DeleteOneNoCache(t *testing.T) {
func TestModel_FindOne(t *testing.T) { func TestModel_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()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
resp := mtest.CreateCursorResponse( resp := mtest.CreateCursorResponse(
1, 1,
@@ -104,8 +94,6 @@ func TestModel_FindOne(t *testing.T) {
func TestModel_FindOneNoCache(t *testing.T) { func TestModel_FindOneNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
resp := mtest.CreateCursorResponse( resp := mtest.CreateCursorResponse(
1, 1,
@@ -126,8 +114,6 @@ func TestModel_FindOneNoCache(t *testing.T) {
func TestModel_FindOneAndDelete(t *testing.T) { func TestModel_FindOneAndDelete(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -152,8 +138,6 @@ func TestModel_FindOneAndDelete(t *testing.T) {
func TestModel_FindOneAndDeleteNoCache(t *testing.T) { func TestModel_FindOneAndDeleteNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -169,8 +153,6 @@ func TestModel_FindOneAndDeleteNoCache(t *testing.T) {
func TestModel_FindOneAndReplace(t *testing.T) { func TestModel_FindOneAndReplace(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -201,8 +183,6 @@ func TestModel_FindOneAndReplace(t *testing.T) {
func TestModel_FindOneAndReplaceNoCache(t *testing.T) { func TestModel_FindOneAndReplaceNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -220,8 +200,6 @@ func TestModel_FindOneAndReplaceNoCache(t *testing.T) {
func TestModel_FindOneAndUpdate(t *testing.T) { func TestModel_FindOneAndUpdate(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -252,8 +230,6 @@ func TestModel_FindOneAndUpdate(t *testing.T) {
func TestModel_FindOneAndUpdateNoCache(t *testing.T) { func TestModel_FindOneAndUpdateNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -271,8 +247,6 @@ func TestModel_FindOneAndUpdateNoCache(t *testing.T) {
func TestModel_GetCache(t *testing.T) { func TestModel_GetCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(t, mt) m := createModel(t, mt)
assert.NotNil(t, m.cache) assert.NotNil(t, m.cache)
@@ -285,8 +259,6 @@ func TestModel_GetCache(t *testing.T) {
func TestModel_InsertOne(t *testing.T) { func TestModel_InsertOne(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -318,8 +290,6 @@ func TestModel_InsertOne(t *testing.T) {
func TestModel_InsertOneNoCache(t *testing.T) { func TestModel_InsertOneNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -335,8 +305,6 @@ func TestModel_InsertOneNoCache(t *testing.T) {
func TestModel_ReplaceOne(t *testing.T) { func TestModel_ReplaceOne(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -368,8 +336,6 @@ func TestModel_ReplaceOne(t *testing.T) {
func TestModel_ReplaceOneNoCache(t *testing.T) { func TestModel_ReplaceOneNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -385,8 +351,6 @@ func TestModel_ReplaceOneNoCache(t *testing.T) {
func TestModel_SetCache(t *testing.T) { func TestModel_SetCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
m := createModel(t, mt) m := createModel(t, mt)
assert.Nil(t, m.SetCache("foo", "bar")) assert.Nil(t, m.SetCache("foo", "bar"))
@@ -398,8 +362,6 @@ func TestModel_SetCache(t *testing.T) {
func TestModel_UpdateByID(t *testing.T) { func TestModel_UpdateByID(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -431,8 +393,6 @@ func TestModel_UpdateByID(t *testing.T) {
func TestModel_UpdateByIDNoCache(t *testing.T) { func TestModel_UpdateByIDNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -448,8 +408,6 @@ func TestModel_UpdateByIDNoCache(t *testing.T) {
func TestModel_UpdateMany(t *testing.T) { func TestModel_UpdateMany(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -483,8 +441,6 @@ func TestModel_UpdateMany(t *testing.T) {
func TestModel_UpdateManyNoCache(t *testing.T) { func TestModel_UpdateManyNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -500,8 +456,6 @@ func TestModel_UpdateManyNoCache(t *testing.T) {
func TestModel_UpdateOne(t *testing.T) { func TestModel_UpdateOne(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},
@@ -533,8 +487,6 @@ func TestModel_UpdateOne(t *testing.T) {
func TestModel_UpdateOneNoCache(t *testing.T) { func TestModel_UpdateOneNoCache(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{ mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{
{Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}}, {Key: "value", Value: bson.D{{Key: "foo", Value: "bar"}}},

View File

@@ -2,14 +2,14 @@ package redis
import ( import (
"context" "context"
"errors"
"io" "io"
"net" "net"
"strings" "strings"
"time" "time"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/breaker"
"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"
"github.com/zeromicro/go-zero/core/timex" "github.com/zeromicro/go-zero/core/timex"
@@ -23,111 +23,84 @@ import (
const spanName = "redis" const spanName = "redis"
var ( var (
startTimeKey = contextKey("startTime")
durationHook = hook{} durationHook = hook{}
redisCmdsAttributeKey = attribute.Key("redis.cmds") redisCmdsAttributeKey = attribute.Key("redis.cmds")
) )
type ( type hook struct{}
contextKey string
hook struct{}
)
func (h hook) BeforeProcess(ctx context.Context, cmd red.Cmder) (context.Context, error) { func (h hook) DialHook(next red.DialHook) red.DialHook {
return h.startSpan(context.WithValue(ctx, startTimeKey, timex.Now()), cmd), nil return next
} }
func (h hook) AfterProcess(ctx context.Context, cmd red.Cmder) error { func (h hook) ProcessHook(next red.ProcessHook) red.ProcessHook {
err := cmd.Err() return func(ctx context.Context, cmd red.Cmder) error {
h.endSpan(ctx, err) start := timex.Now()
ctx, endSpan := h.startSpan(ctx, cmd)
val := ctx.Value(startTimeKey) err := next(ctx, cmd)
if val == nil {
return nil
}
start, ok := val.(time.Duration) endSpan(err)
if !ok { duration := timex.Since(start)
return nil
}
duration := timex.Since(start) if duration > slowThreshold.Load() {
if duration > slowThreshold.Load() { logDuration(ctx, []red.Cmder{cmd}, duration)
logDuration(ctx, []red.Cmder{cmd}, duration) metricSlowCount.Inc(cmd.Name())
}
metricReqDur.Observe(duration.Milliseconds(), cmd.Name())
if msg := formatError(err); len(msg) > 0 {
metricReqErr.Inc(cmd.Name(), msg)
}
return nil
}
func (h hook) BeforeProcessPipeline(ctx context.Context, cmds []red.Cmder) (context.Context, error) {
if len(cmds) == 0 {
return ctx, nil
}
return h.startSpan(context.WithValue(ctx, startTimeKey, timex.Now()), cmds...), nil
}
func (h hook) AfterProcessPipeline(ctx context.Context, cmds []red.Cmder) error {
if len(cmds) == 0 {
return nil
}
batchError := errorx.BatchError{}
for _, cmd := range cmds {
err := cmd.Err()
if err == nil {
continue
} }
batchError.Add(err) metricReqDur.Observe(duration.Milliseconds(), cmd.Name())
} if msg := formatError(err); len(msg) > 0 {
h.endSpan(ctx, batchError.Err()) metricReqErr.Inc(cmd.Name(), msg)
}
val := ctx.Value(startTimeKey) return err
if val == nil {
return nil
} }
}
start, ok := val.(time.Duration) func (h hook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipelineHook {
if !ok { return func(ctx context.Context, cmds []red.Cmder) error {
return nil if len(cmds) == 0 {
return next(ctx, cmds)
}
start := timex.Now()
ctx, endSpan := h.startSpan(ctx, cmds...)
err := next(ctx, cmds)
endSpan(err)
duration := timex.Since(start)
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
logDuration(ctx, cmds, duration)
}
metricReqDur.Observe(duration.Milliseconds(), "Pipeline")
if msg := formatError(err); len(msg) > 0 {
metricReqErr.Inc("Pipeline", msg)
}
return err
} }
duration := timex.Since(start)
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
logDuration(ctx, cmds, duration)
}
metricReqDur.Observe(duration.Milliseconds(), "Pipeline")
if msg := formatError(batchError.Err()); len(msg) > 0 {
metricReqErr.Inc("Pipeline", msg)
}
return nil
} }
func formatError(err error) string { func formatError(err error) string {
if err == nil || err == red.Nil { if err == nil || errors.Is(err, red.Nil) {
return "" return ""
} }
opErr, ok := err.(*net.OpError) var opErr *net.OpError
ok := errors.As(err, &opErr)
if ok && opErr.Timeout() { if ok && opErr.Timeout() {
return "timeout" return "timeout"
} }
switch err { switch {
case io.EOF: case err == io.EOF:
return "eof" return "eof"
case context.DeadlineExceeded: case errors.Is(err, context.DeadlineExceeded):
return "context deadline" return "context deadline"
case breaker.ErrServiceUnavailable: case errors.Is(err, breaker.ErrServiceUnavailable):
return "breaker" return "breaker open"
default: default:
return "unexpected error" return "unexpected error"
} }
@@ -151,7 +124,7 @@ func logDuration(ctx context.Context, cmds []red.Cmder, duration time.Duration)
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())
} }
func (h hook) startSpan(ctx context.Context, cmds ...red.Cmder) context.Context { func (h hook) startSpan(ctx context.Context, cmds ...red.Cmder) (context.Context, func(err error)) {
tracer := trace.TracerFromContext(ctx) tracer := trace.TracerFromContext(ctx)
ctx, span := tracer.Start(ctx, ctx, span := tracer.Start(ctx,
@@ -165,18 +138,15 @@ func (h hook) startSpan(ctx context.Context, cmds ...red.Cmder) context.Context
} }
span.SetAttributes(redisCmdsAttributeKey.StringSlice(cmdStrs)) span.SetAttributes(redisCmdsAttributeKey.StringSlice(cmdStrs))
return ctx return ctx, func(err error) {
} defer span.End()
func (h hook) endSpan(ctx context.Context, err error) { if err == nil || errors.Is(err, red.Nil) {
span := oteltrace.SpanFromContext(ctx) span.SetStatus(codes.Ok, "")
defer span.End() return
}
if err == nil || err == red.Nil { span.SetStatus(codes.Error, err.Error())
span.SetStatus(codes.Ok, "") span.RecordError(err)
return
} }
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
} }

View File

@@ -4,168 +4,103 @@ import (
"context" "context"
"errors" "errors"
"io" "io"
"log"
"net" "net"
"strings" "strings"
"testing" "testing"
"time" "time"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx/logtest" "github.com/zeromicro/go-zero/core/logx/logtest"
ztrace "github.com/zeromicro/go-zero/core/trace" "github.com/zeromicro/go-zero/core/trace/tracetest"
tracesdk "go.opentelemetry.io/otel/trace" tracesdk "go.opentelemetry.io/otel/trace"
) )
func TestHookProcessCase1(t *testing.T) { func TestHookProcessCase1(t *testing.T) {
ztrace.StartAgent(ztrace.Config{ tracetest.NewInMemoryExporter(t)
Name: "go-zero-test", w := logtest.NewCollector(t)
Endpoint: "http://localhost:14268/api/traces",
Batcher: "jaeger",
Sampler: 1.0,
})
defer ztrace.StopAgent()
writer := log.Writer() err := durationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error {
var buf strings.Builder assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
log.SetOutput(&buf) return nil
defer log.SetOutput(writer) })(context.Background(), red.NewCmd(context.Background()))
ctx, err := durationHook.BeforeProcess(context.Background(), red.NewCmd(context.Background()))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
assert.Nil(t, durationHook.AfterProcess(ctx, red.NewCmd(context.Background()))) assert.False(t, strings.Contains(w.String(), "slow"))
assert.False(t, strings.Contains(buf.String(), "slow"))
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
} }
func TestHookProcessCase2(t *testing.T) { func TestHookProcessCase2(t *testing.T) {
ztrace.StartAgent(ztrace.Config{ tracetest.NewInMemoryExporter(t)
Name: "go-zero-test",
Endpoint: "http://localhost:14268/api/traces",
Batcher: "jaeger",
Sampler: 1.0,
})
defer ztrace.StopAgent()
w := logtest.NewCollector(t) w := logtest.NewCollector(t)
ctx, err := durationHook.BeforeProcess(context.Background(), red.NewCmd(context.Background())) err := durationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error {
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
time.Sleep(slowThreshold.Load() + time.Millisecond)
return nil
})(context.Background(), red.NewCmd(context.Background()))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
time.Sleep(slowThreshold.Load() + time.Millisecond)
assert.Nil(t, durationHook.AfterProcess(ctx, red.NewCmd(context.Background(), "foo", "bar")))
assert.True(t, strings.Contains(w.String(), "slow")) assert.True(t, strings.Contains(w.String(), "slow"))
assert.True(t, strings.Contains(w.String(), "trace")) assert.True(t, strings.Contains(w.String(), "trace"))
assert.True(t, strings.Contains(w.String(), "span")) assert.True(t, strings.Contains(w.String(), "span"))
} }
func TestHookProcessCase3(t *testing.T) {
writer := log.Writer()
var buf strings.Builder
log.SetOutput(&buf)
defer log.SetOutput(writer)
assert.Nil(t, durationHook.AfterProcess(context.Background(), red.NewCmd(context.Background())))
assert.True(t, buf.Len() == 0)
}
func TestHookProcessCase4(t *testing.T) {
writer := log.Writer()
var buf strings.Builder
log.SetOutput(&buf)
defer log.SetOutput(writer)
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
assert.Nil(t, durationHook.AfterProcess(ctx, red.NewCmd(context.Background())))
assert.True(t, buf.Len() == 0)
}
func TestHookProcessPipelineCase1(t *testing.T) { func TestHookProcessPipelineCase1(t *testing.T) {
writer := log.Writer() tracetest.NewInMemoryExporter(t)
var buf strings.Builder w := logtest.NewCollector(t)
log.SetOutput(&buf)
defer log.SetOutput(writer)
_, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{}) err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
return nil
})(context.Background(), nil)
assert.NoError(t, err) assert.NoError(t, err)
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
err = durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
return nil
})(context.Background(), []red.Cmder{
red.NewCmd(context.Background()), red.NewCmd(context.Background()),
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
assert.NoError(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{})) assert.False(t, strings.Contains(w.String(), "slow"))
assert.NoError(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
red.NewCmd(context.Background()),
}))
assert.False(t, strings.Contains(buf.String(), "slow"))
} }
func TestHookProcessPipelineCase2(t *testing.T) { func TestHookProcessPipelineCase2(t *testing.T) {
ztrace.StartAgent(ztrace.Config{ tracetest.NewInMemoryExporter(t)
Name: "go-zero-test",
Endpoint: "http://localhost:14268/api/traces",
Batcher: "jaeger",
Sampler: 1.0,
})
defer ztrace.StopAgent()
w := logtest.NewCollector(t) w := logtest.NewCollector(t)
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{ err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
time.Sleep(slowThreshold.Load() + time.Millisecond)
return nil
})(context.Background(), []red.Cmder{
red.NewCmd(context.Background()), red.NewCmd(context.Background()),
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
time.Sleep(slowThreshold.Load() + time.Millisecond)
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
red.NewCmd(context.Background(), "foo", "bar"),
}))
assert.True(t, strings.Contains(w.String(), "slow")) assert.True(t, strings.Contains(w.String(), "slow"))
assert.True(t, strings.Contains(w.String(), "trace")) assert.True(t, strings.Contains(w.String(), "trace"))
assert.True(t, strings.Contains(w.String(), "span")) assert.True(t, strings.Contains(w.String(), "span"))
} }
func TestHookProcessPipelineCase3(t *testing.T) { func TestHookProcessPipelineCase3(t *testing.T) {
w := logtest.NewCollector(t) te := tracetest.NewInMemoryExporter(t)
assert.Nil(t, durationHook.AfterProcessPipeline(context.Background(), []red.Cmder{ err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
return assert.AnError
})(context.Background(), []red.Cmder{
red.NewCmd(context.Background()), red.NewCmd(context.Background()),
})) })
assert.True(t, len(w.String()) == 0) assert.ErrorIs(t, err, assert.AnError)
} traceLogs := te.GetSpans().Snapshots()[0]
assert.Equal(t, "redis", traceLogs.Name())
func TestHookProcessPipelineCase4(t *testing.T) { assert.Equal(t, assert.AnError.Error(), traceLogs.Events()[0].Attributes[1].Value.AsString(), "trace should record error")
w := logtest.NewCollector(t)
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
red.NewCmd(context.Background()),
}))
assert.True(t, len(w.String()) == 0)
}
func TestHookProcessPipelineCase5(t *testing.T) {
writer := log.Writer()
var buf strings.Builder
log.SetOutput(&buf)
defer log.SetOutput(writer)
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
red.NewCmd(context.Background()),
}))
assert.True(t, buf.Len() == 0)
} }
func TestLogDuration(t *testing.T) { func TestLogDuration(t *testing.T) {
@@ -203,7 +138,7 @@ func TestFormatError(t *testing.T) {
assert.Equal(t, "context deadline", formatError(context.DeadlineExceeded)) assert.Equal(t, "context deadline", formatError(context.DeadlineExceeded))
// Test case: err is breaker.ErrServiceUnavailable // Test case: err is breaker.ErrServiceUnavailable
assert.Equal(t, "breaker", formatError(breaker.ErrServiceUnavailable)) assert.Equal(t, "breaker open", formatError(breaker.ErrServiceUnavailable))
// Test case: err is unknown // Test case: err is unknown
assert.Equal(t, "unexpected error", formatError(errors.New("some error"))) assert.Equal(t, "unexpected error", formatError(errors.New("some error")))

View File

@@ -1,6 +1,12 @@
package redis package redis
import "github.com/zeromicro/go-zero/core/metric" import (
"sync"
"github.com/prometheus/client_golang/prometheus"
red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/metric"
)
const namespace = "redis_client" const namespace = "redis_client"
@@ -11,7 +17,7 @@ var (
Name: "duration_ms", Name: "duration_ms",
Help: "redis client requests duration(ms).", Help: "redis client requests duration(ms).",
Labels: []string{"command"}, Labels: []string{"command"},
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000, 2500}, Buckets: []float64{0.25, 0.5, 1, 1.5, 2, 3, 5, 10, 25, 50, 100, 250, 500, 1000, 2000, 5000, 10000, 15000},
}) })
metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{ metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: namespace, Namespace: namespace,
@@ -20,4 +26,162 @@ var (
Help: "redis client requests error count.", Help: "redis client requests error count.",
Labels: []string{"command", "error"}, Labels: []string{"command", "error"},
}) })
metricSlowCount = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: namespace,
Subsystem: "requests",
Name: "slow_total",
Help: "redis client requests slow count.",
Labels: []string{"command"},
})
connLabels = []string{"key", "client_type"}
connCollector = newCollector()
_ prometheus.Collector = (*collector)(nil)
) )
type (
statGetter struct {
clientType string
key string
poolSize int
poolStats func() *red.PoolStats
}
// collector collects statistics from a redis client.
// It implements the prometheus.Collector interface.
collector struct {
hitDesc *prometheus.Desc
missDesc *prometheus.Desc
timeoutDesc *prometheus.Desc
totalDesc *prometheus.Desc
idleDesc *prometheus.Desc
staleDesc *prometheus.Desc
maxDesc *prometheus.Desc
clients []*statGetter
lock sync.Mutex
}
)
func newCollector() *collector {
c := &collector{
hitDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pool_hit_total"),
"Number of times a connection was found in the pool",
connLabels, nil,
),
missDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pool_miss_total"),
"Number of times a connection was not found in the pool",
connLabels, nil,
),
timeoutDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pool_timeout_total"),
"Number of times a timeout occurred when looking for a connection in the pool",
connLabels, nil,
),
totalDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pool_conn_total_current"),
"Current number of connections in the pool",
connLabels, nil,
),
idleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pool_conn_idle_current"),
"Current number of idle connections in the pool",
connLabels, nil,
),
staleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pool_conn_stale_total"),
"Number of times a connection was removed from the pool because it was stale",
connLabels, nil,
),
maxDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pool_conn_max"),
"Max number of connections in the pool",
connLabels, nil,
),
}
prometheus.MustRegister(c)
return c
}
// Describe implements the prometheus.Collector interface.
func (s *collector) Describe(descs chan<- *prometheus.Desc) {
descs <- s.hitDesc
descs <- s.missDesc
descs <- s.timeoutDesc
descs <- s.totalDesc
descs <- s.idleDesc
descs <- s.staleDesc
descs <- s.maxDesc
}
// Collect implements the prometheus.Collector interface.
func (s *collector) Collect(metrics chan<- prometheus.Metric) {
s.lock.Lock()
defer s.lock.Unlock()
for _, client := range s.clients {
key, clientType := client.key, client.clientType
stats := client.poolStats()
metrics <- prometheus.MustNewConstMetric(
s.hitDesc,
prometheus.CounterValue,
float64(stats.Hits),
key,
clientType,
)
metrics <- prometheus.MustNewConstMetric(
s.missDesc,
prometheus.CounterValue,
float64(stats.Misses),
key,
clientType,
)
metrics <- prometheus.MustNewConstMetric(
s.timeoutDesc,
prometheus.CounterValue,
float64(stats.Timeouts),
key,
clientType,
)
metrics <- prometheus.MustNewConstMetric(
s.totalDesc,
prometheus.GaugeValue,
float64(stats.TotalConns),
key,
clientType,
)
metrics <- prometheus.MustNewConstMetric(
s.idleDesc,
prometheus.GaugeValue,
float64(stats.IdleConns),
key,
clientType,
)
metrics <- prometheus.MustNewConstMetric(
s.staleDesc,
prometheus.CounterValue,
float64(stats.StaleConns),
key,
clientType,
)
metrics <- prometheus.MustNewConstMetric(
s.maxDesc,
prometheus.CounterValue,
float64(client.poolSize),
key,
clientType,
)
}
}
func (s *collector) registerClient(client *statGetter) {
s.lock.Lock()
defer s.lock.Unlock()
s.clients = append(s.clients, client)
}

View File

@@ -0,0 +1,130 @@
package redis
import (
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
red "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/internal/devserver"
)
func TestRedisMetric(t *testing.T) {
cfg := devserver.Config{}
_ = conf.FillDefault(&cfg)
server := devserver.NewServer(cfg)
server.StartAsync()
time.Sleep(time.Second)
metricReqDur.Observe(8, "test-cmd")
metricReqErr.Inc("test-cmd", "internal-error")
metricSlowCount.Inc("test-cmd")
url := "http://127.0.0.1:6060/metrics"
resp, err := http.Get(url)
assert.Nil(t, err)
defer resp.Body.Close()
s, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
content := string(s)
assert.Contains(t, content, "redis_client_requests_duration_ms_sum{command=\"test-cmd\"} 8\n")
assert.Contains(t, content, "redis_client_requests_duration_ms_count{command=\"test-cmd\"} 1\n")
assert.Contains(t, content, "redis_client_requests_error_total{command=\"test-cmd\",error=\"internal-error\"} 1\n")
assert.Contains(t, content, "redis_client_requests_slow_total{command=\"test-cmd\"} 1\n")
}
func Test_newCollector(t *testing.T) {
prometheus.Unregister(connCollector)
c := newCollector()
c.registerClient(&statGetter{
clientType: "node",
key: "test1",
poolSize: 10,
poolStats: func() *red.PoolStats {
return &red.PoolStats{
Hits: 10000,
Misses: 10,
Timeouts: 5,
TotalConns: 100,
IdleConns: 20,
StaleConns: 1,
}
},
})
c.registerClient(&statGetter{
clientType: "node",
key: "test2",
poolSize: 11,
poolStats: func() *red.PoolStats {
return &red.PoolStats{
Hits: 10001,
Misses: 11,
Timeouts: 6,
TotalConns: 101,
IdleConns: 21,
StaleConns: 2,
}
},
})
c.registerClient(&statGetter{
clientType: "cluster",
key: "test3",
poolSize: 5,
poolStats: func() *red.PoolStats {
return &red.PoolStats{
Hits: 20000,
Misses: 20,
Timeouts: 10,
TotalConns: 200,
IdleConns: 40,
StaleConns: 2,
}
},
})
val := `
# HELP redis_client_pool_conn_idle_current Current number of idle connections in the pool
# TYPE redis_client_pool_conn_idle_current gauge
redis_client_pool_conn_idle_current{client_type="cluster",key="test3"} 40
redis_client_pool_conn_idle_current{client_type="node",key="test1"} 20
redis_client_pool_conn_idle_current{client_type="node",key="test2"} 21
# HELP redis_client_pool_conn_max Max number of connections in the pool
# TYPE redis_client_pool_conn_max counter
redis_client_pool_conn_max{client_type="cluster",key="test3"} 5
redis_client_pool_conn_max{client_type="node",key="test1"} 10
redis_client_pool_conn_max{client_type="node",key="test2"} 11
# HELP redis_client_pool_conn_stale_total Number of times a connection was removed from the pool because it was stale
# TYPE redis_client_pool_conn_stale_total counter
redis_client_pool_conn_stale_total{client_type="cluster",key="test3"} 2
redis_client_pool_conn_stale_total{client_type="node",key="test1"} 1
redis_client_pool_conn_stale_total{client_type="node",key="test2"} 2
# HELP redis_client_pool_conn_total_current Current number of connections in the pool
# TYPE redis_client_pool_conn_total_current gauge
redis_client_pool_conn_total_current{client_type="cluster",key="test3"} 200
redis_client_pool_conn_total_current{client_type="node",key="test1"} 100
redis_client_pool_conn_total_current{client_type="node",key="test2"} 101
# HELP redis_client_pool_hit_total Number of times a connection was found in the pool
# TYPE redis_client_pool_hit_total counter
redis_client_pool_hit_total{client_type="cluster",key="test3"} 20000
redis_client_pool_hit_total{client_type="node",key="test1"} 10000
redis_client_pool_hit_total{client_type="node",key="test2"} 10001
# HELP redis_client_pool_miss_total Number of times a connection was not found in the pool
# TYPE redis_client_pool_miss_total counter
redis_client_pool_miss_total{client_type="cluster",key="test3"} 20
redis_client_pool_miss_total{client_type="node",key="test1"} 10
redis_client_pool_miss_total{client_type="node",key="test2"} 11
# HELP redis_client_pool_timeout_total Number of times a timeout occurred when looking for a connection in the pool
# TYPE redis_client_pool_timeout_total counter
redis_client_pool_timeout_total{client_type="cluster",key="test3"} 10
redis_client_pool_timeout_total{client_type="node",key="test1"} 5
redis_client_pool_timeout_total{client_type="node",key="test2"} 6
`
err := testutil.CollectAndCompare(c, strings.NewReader(val))
assert.NoError(t, err)
}

View File

@@ -7,7 +7,7 @@ import (
"strconv" "strconv"
"time" "time"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/breaker" "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"
@@ -64,6 +64,7 @@ type (
// RedisNode interface represents a redis node. // RedisNode interface represents a redis node.
RedisNode interface { RedisNode interface {
red.Cmdable red.Cmdable
red.BitMapCmdable
} }
// GeoLocation is used with GeoAdd to add geospatial location. // GeoLocation is used with GeoAdd to add geospatial location.
@@ -467,6 +468,28 @@ func (s *Redis) ExistsCtx(ctx context.Context, key string) (val bool, err error)
return return
} }
// ExistsMany is the implementation of redis exists command.
// checks the existence of multiple keys in Redis using the EXISTS command.
func (s *Redis) ExistsMany(keys ...string) (int64, error) {
return s.ExistsManyCtx(context.Background(), keys...)
}
// ExistsManyCtx is the implementation of redis exists command.
// checks the existence of multiple keys in Redis using the EXISTS command.
func (s *Redis) ExistsManyCtx(ctx context.Context, keys ...string) (val int64, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}
val, err = conn.Exists(ctx, keys...).Result()
return err
}, acceptable)
return
}
// Expire is the implementation of redis expire command. // Expire is the implementation of redis expire command.
func (s *Redis) Expire(key string, seconds int) error { func (s *Redis) Expire(key string, seconds int) error {
return s.ExpireCtx(context.Background(), key, seconds) return s.ExpireCtx(context.Background(), key, seconds)
@@ -515,13 +538,8 @@ func (s *Redis) GeoAddCtx(ctx context.Context, key string, geoLocation ...*GeoLo
return err return err
} }
v, err := conn.GeoAdd(ctx, key, geoLocation...).Result() val, err = conn.GeoAdd(ctx, key, geoLocation...).Result()
if err != nil { return err
return err
}
val = v
return nil
}, acceptable) }, acceptable)
return return
@@ -541,13 +559,8 @@ func (s *Redis) GeoDistCtx(ctx context.Context, key, member1, member2, unit stri
return err return err
} }
v, err := conn.GeoDist(ctx, key, member1, member2, unit).Result() val, err = conn.GeoDist(ctx, key, member1, member2, unit).Result()
if err != nil { return err
return err
}
val = v
return nil
}, acceptable) }, acceptable)
return return
@@ -567,13 +580,8 @@ func (s *Redis) GeoHashCtx(ctx context.Context, key string, members ...string) (
return err return err
} }
v, err := conn.GeoHash(ctx, key, members...).Result() val, err = conn.GeoHash(ctx, key, members...).Result()
if err != nil { return err
return err
}
val = v
return nil
}, acceptable) }, acceptable)
return return
@@ -594,13 +602,8 @@ func (s *Redis) GeoRadiusCtx(ctx context.Context, key string, longitude, latitud
return err return err
} }
v, err := conn.GeoRadius(ctx, key, longitude, latitude, query).Result() val, err = conn.GeoRadius(ctx, key, longitude, latitude, query).Result()
if err != nil { return err
return err
}
val = v
return nil
}, acceptable) }, acceptable)
return return
@@ -620,13 +623,8 @@ func (s *Redis) GeoRadiusByMemberCtx(ctx context.Context, key, member string,
return err return err
} }
v, err := conn.GeoRadiusByMember(ctx, key, member, query).Result() val, err = conn.GeoRadiusByMember(ctx, key, member, query).Result()
if err != nil { return err
return err
}
val = v
return nil
}, acceptable) }, acceptable)
return return
@@ -646,13 +644,8 @@ func (s *Redis) GeoPosCtx(ctx context.Context, key string, members ...string) (
return err return err
} }
v, err := conn.GeoPos(ctx, key, members...).Result() val, err = conn.GeoPos(ctx, key, members...).Result()
if err != nil { return err
return err
}
val = v
return nil
}, acceptable) }, acceptable)
return return
@@ -671,7 +664,7 @@ func (s *Redis) GetCtx(ctx context.Context, key string) (val string, err error)
return err return err
} }
if val, err = conn.Get(ctx, key).Result(); err == red.Nil { if val, err = conn.Get(ctx, key).Result(); errors.Is(err, red.Nil) {
return nil return nil
} else if err != nil { } else if err != nil {
return err return err
@@ -721,7 +714,7 @@ func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, e
return err return err
} }
if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil { if val, err = conn.GetSet(ctx, key, value).Result(); errors.Is(err, red.Nil) {
return nil return nil
} }
@@ -847,17 +840,16 @@ func (s *Redis) HincrbyFloat(key, field string, increment float64) (float64, err
} }
// HincrbyFloatCtx is the implementation of redis hincrbyfloat command. // HincrbyFloatCtx is the implementation of redis hincrbyfloat command.
func (s *Redis) HincrbyFloatCtx(ctx context.Context, key, field string, increment float64) (val float64, err error) { func (s *Redis) HincrbyFloatCtx(ctx context.Context, key, field string, increment float64) (
val float64, err error) {
err = s.brk.DoWithAcceptable(func() error { err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s) conn, err := getRedis(s)
if err != nil { if err != nil {
return err return err
} }
val, err = conn.HIncrByFloat(ctx, key, field, increment).Result() val, err = conn.HIncrByFloat(ctx, key, field, increment).Result()
if err != nil { return err
return err
}
return nil
}, acceptable) }, acceptable)
return return
@@ -1311,6 +1303,26 @@ func (s *Redis) MgetCtx(ctx context.Context, keys ...string) (val []string, err
return return
} }
// Mset is the implementation of redis mset command.
func (s *Redis) Mset(fieldsAndValues ...any) (string, error) {
return s.MsetCtx(context.Background(), fieldsAndValues...)
}
// MsetCtx is the implementation of redis mset command.
func (s *Redis) MsetCtx(ctx context.Context, fieldsAndValues ...any) (val string, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}
val, err = conn.MSet(ctx, fieldsAndValues...).Result()
return err
}, acceptable)
return
}
// Persist is the implementation of redis persist command. // Persist is the implementation of redis persist command.
func (s *Redis) Persist(key string) (bool, error) { func (s *Redis) Persist(key string) (bool, error) {
return s.PersistCtx(context.Background(), key) return s.PersistCtx(context.Background(), key)
@@ -1993,7 +2005,14 @@ func (s *Redis) TtlCtx(ctx context.Context, key string) (val int, err error) {
return err return err
} }
val = int(duration / time.Second) if duration >= 0 {
val = int(duration / time.Second)
} else {
// -2 means key does not exist
// -1 means key exists but has no expire
val = int(duration)
}
return nil return nil
}, acceptable) }, acceptable)
@@ -2025,7 +2044,47 @@ func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, val
return err return err
} }
v, err := conn.ZAdd(ctx, key, &red.Z{ v, err := conn.ZAdd(ctx, key, red.Z{
Score: score,
Member: value,
}).Result()
if err != nil {
return err
}
val = v == 1
return nil
}, acceptable)
return
}
// Zaddnx is the implementation of redis zadd nx command.
func (s *Redis) Zaddnx(key string, score int64, value string) (val bool, err error) {
return s.ZaddnxCtx(context.Background(), key, score, value)
}
// ZaddnxCtx is the implementation of redis zadd nx command.
func (s *Redis) ZaddnxCtx(ctx context.Context, key string, score int64, value string) (
val bool, err error) {
return s.ZaddnxFloatCtx(ctx, key, float64(score), value)
}
// ZaddnxFloat is the implementation of redis zaddnx command.
func (s *Redis) ZaddnxFloat(key string, score float64, value string) (bool, error) {
return s.ZaddFloatCtx(context.Background(), key, score, value)
}
// ZaddnxFloatCtx is the implementation of redis zaddnx command.
func (s *Redis) ZaddnxFloatCtx(ctx context.Context, key string, score float64, value string) (
val bool, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}
v, err := conn.ZAddNX(ctx, key, red.Z{
Score: score, Score: score,
Member: value, Member: value,
}).Result() }).Result()
@@ -2053,19 +2112,14 @@ func (s *Redis) ZaddsCtx(ctx context.Context, key string, ps ...Pair) (val int64
return err return err
} }
var zs []*red.Z var zs []red.Z
for _, p := range ps { for _, p := range ps {
z := &red.Z{Score: float64(p.Score), Member: p.Key} z := red.Z{Score: float64(p.Score), Member: p.Key}
zs = append(zs, z) zs = append(zs, z)
} }
v, err := conn.ZAdd(ctx, key, zs...).Result() val, err = conn.ZAdd(ctx, key, zs...).Result()
if err != nil { return err
return err
}
val = v
return nil
}, acceptable) }, acceptable)
return return
@@ -2185,6 +2239,7 @@ func (s *Redis) ZscoreByFloatCtx(ctx context.Context, key, value string) (val fl
if err != nil { if err != nil {
return err return err
} }
val, err = conn.ZScore(ctx, key, value).Result() val, err = conn.ZScore(ctx, key, value).Result()
return err return err
}, acceptable) }, acceptable)
@@ -2849,7 +2904,7 @@ func withHook(hook red.Hook) Option {
} }
func acceptable(err error) bool { func acceptable(err error) bool {
return err == nil || err == red.Nil || err == context.Canceled return err == nil || errors.Is(err, red.Nil) || errors.Is(err, context.Canceled)
} }
func getRedis(r *Redis) (RedisNode, error) { func getRedis(r *Redis) (RedisNode, error) {

File diff suppressed because it is too large Load Diff

View File

@@ -3,7 +3,7 @@ package redis
import ( import (
"fmt" "fmt"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )

View File

@@ -3,8 +3,9 @@ package redis
import ( import (
"crypto/tls" "crypto/tls"
"io" "io"
"runtime"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
) )
@@ -14,7 +15,11 @@ const (
idleConns = 8 idleConns = 8
) )
var clientManager = syncx.NewResourceManager() var (
clientManager = syncx.NewResourceManager()
// nodePoolSize is default pool size for node type of redis.
nodePoolSize = 10 * runtime.GOMAXPROCS(0)
)
func getClient(r *Redis) (*red.Client, error) { func getClient(r *Redis) (*red.Client, error) {
val, err := clientManager.GetResource(r.Addr, func() (io.Closer, error) { val, err := clientManager.GetResource(r.Addr, func() (io.Closer, error) {
@@ -37,6 +42,15 @@ func getClient(r *Redis) (*red.Client, error) {
store.AddHook(hook) store.AddHook(hook)
} }
connCollector.registerClient(&statGetter{
clientType: NodeType,
key: r.Addr,
poolSize: nodePoolSize,
poolStats: func() *red.PoolStats {
return store.PoolStats()
},
})
return store, nil return store, nil
}) })
if err != nil { if err != nil {

View File

@@ -3,15 +3,20 @@ package redis
import ( import (
"crypto/tls" "crypto/tls"
"io" "io"
"runtime"
"strings" "strings"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
) )
const addrSep = "," const addrSep = ","
var clusterManager = syncx.NewResourceManager() var (
clusterManager = syncx.NewResourceManager()
// clusterPoolSize is default pool size for cluster type of redis.
clusterPoolSize = 5 * runtime.GOMAXPROCS(0)
)
func getCluster(r *Redis) (*red.ClusterClient, error) { func getCluster(r *Redis) (*red.ClusterClient, error) {
val, err := clusterManager.GetResource(r.Addr, func() (io.Closer, error) { val, err := clusterManager.GetResource(r.Addr, func() (io.Closer, error) {
@@ -33,6 +38,15 @@ func getCluster(r *Redis) (*red.ClusterClient, error) {
store.AddHook(hook) store.AddHook(hook)
} }
connCollector.registerClient(&statGetter{
clientType: ClusterType,
key: r.Addr,
poolSize: clusterPoolSize,
poolStats: func() *red.PoolStats {
return store.PoolStats()
},
})
return store, nil return store, nil
}) })
if err != nil { if err != nil {

View File

@@ -4,7 +4,7 @@ import (
"testing" "testing"
"github.com/alicebob/miniredis/v2" "github.com/alicebob/miniredis/v2"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )

View File

@@ -7,8 +7,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
red "github.com/go-redis/redis/v8" red "github.com/redis/go-redis/v9"
"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"
) )
@@ -42,7 +41,7 @@ type RedisLock struct {
} }
func init() { func init() {
rand.Seed(time.Now().UnixNano()) rand.NewSource(time.Now().UnixNano())
} }
// NewRedisLock returns a RedisLock. // NewRedisLock returns a RedisLock.

View File

@@ -22,11 +22,11 @@ import (
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stat" "github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/dbtest"
"github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/redis/redistest" "github.com/zeromicro/go-zero/core/stores/redis/redistest"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/internal/dbtest"
) )
func init() { func init() {

View File

@@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"strings" "strings"
"sync"
"time" "time"
"github.com/zeromicro/go-zero/core/executors" "github.com/zeromicro/go-zero/core/executors"
@@ -30,6 +31,7 @@ type (
executor *executors.PeriodicalExecutor executor *executors.PeriodicalExecutor
inserter *dbInserter inserter *dbInserter
stmt bulkStmt stmt bulkStmt
lock sync.RWMutex // guards stmt
} }
bulkStmt struct { bulkStmt struct {
@@ -65,6 +67,9 @@ func (bi *BulkInserter) Flush() {
// Insert inserts given args. // Insert inserts given args.
func (bi *BulkInserter) Insert(args ...any) error { func (bi *BulkInserter) Insert(args ...any) error {
bi.lock.RLock()
defer bi.lock.RUnlock()
value, err := format(bi.stmt.valueFormat, args...) value, err := format(bi.stmt.valueFormat, args...)
if err != nil { if err != nil {
return err return err
@@ -95,6 +100,11 @@ func (bi *BulkInserter) UpdateStmt(stmt string) error {
return err return err
} }
bi.lock.Lock()
defer bi.lock.Unlock()
// with write lock, it doesn't matter what's the order of setting bi.stmt and calling flush.
bi.stmt = bkStmt
bi.executor.Flush() bi.executor.Flush()
bi.executor.Sync(func() { bi.executor.Sync(func() {
bi.inserter.stmt = bkStmt bi.inserter.stmt = bkStmt

View File

@@ -5,22 +5,30 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"strconv" "strconv"
"strings"
"sync"
"sync/atomic"
"testing" "testing"
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/internal/dbtest" "github.com/zeromicro/go-zero/core/stores/dbtest"
) )
type mockedConn struct { type mockedConn struct {
query string query string
args []any args []any
execErr error execErr error
updateCallback func(query string, args []any)
} }
func (c *mockedConn) ExecCtx(_ context.Context, query string, args ...any) (sql.Result, error) { func (c *mockedConn) ExecCtx(_ context.Context, query string, args ...any) (sql.Result, error) {
c.query = query c.query = query
c.args = args c.args = args
if c.updateCallback != nil {
c.updateCallback(query, args)
}
return nil, c.execErr return nil, c.execErr
} }
@@ -144,3 +152,50 @@ func TestBulkInserter_Update(t *testing.T) {
assert.NotNil(t, inserter.UpdateStmt("foo")) assert.NotNil(t, inserter.UpdateStmt("foo"))
assert.NotNil(t, inserter.Insert("foo", "bar")) assert.NotNil(t, inserter.Insert("foo", "bar"))
} }
func TestBulkInserter_UpdateStmt(t *testing.T) {
var updated int32
conn := mockedConn{
execErr: errors.New("foo"),
updateCallback: func(query string, args []any) {
count := atomic.AddInt32(&updated, 1)
assert.Empty(t, args)
assert.Equal(t, 100, strings.Count(query, "foo"))
if count == 1 {
assert.Equal(t, 0, strings.Count(query, "bar"))
} else {
assert.Equal(t, 100, strings.Count(query, "bar"))
}
},
}
inserter, err := NewBulkInserter(&conn, `INSERT INTO classroom_dau(classroom) VALUES(?)`)
assert.NoError(t, err)
var wg1 sync.WaitGroup
wg1.Add(2)
for i := 0; i < 2; i++ {
go func() {
defer wg1.Done()
for i := 0; i < 50; i++ {
assert.NoError(t, inserter.Insert("foo"))
}
}()
}
wg1.Wait()
assert.NoError(t, inserter.UpdateStmt(`INSERT INTO classroom_dau(classroom, user) VALUES(?, ?)`))
var wg2 sync.WaitGroup
wg2.Add(1)
go func() {
defer wg2.Done()
for i := 0; i < 100; i++ {
assert.NoError(t, inserter.Insert("foo", "bar"))
}
inserter.Flush()
}()
wg2.Wait()
assert.Equal(t, int32(2), atomic.LoadInt32(&updated))
}

View File

@@ -1,6 +1,12 @@
package sqlx package sqlx
import "github.com/zeromicro/go-zero/core/metric" import (
"database/sql"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/zeromicro/go-zero/core/metric"
)
const namespace = "sql_client" const namespace = "sql_client"
@@ -11,7 +17,7 @@ var (
Name: "duration_ms", Name: "duration_ms",
Help: "mysql client requests duration(ms).", Help: "mysql client requests duration(ms).",
Labels: []string{"command"}, Labels: []string{"command"},
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000, 2500}, Buckets: []float64{0.25, 0.5, 1, 1.5, 2, 3, 5, 10, 25, 50, 100, 250, 500, 1000, 2000, 5000, 10000, 15000},
}) })
metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{ metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: namespace, Namespace: namespace,
@@ -20,4 +26,145 @@ var (
Help: "mysql client requests error count.", Help: "mysql client requests error count.",
Labels: []string{"command", "error"}, Labels: []string{"command", "error"},
}) })
metricSlowCount = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: namespace,
Subsystem: "requests",
Name: "slow_total",
Help: "mysql client requests slow count.",
Labels: []string{"command"},
})
connLabels = []string{"db_name", "hash"}
connCollector = newCollector()
_ prometheus.Collector = (*collector)(nil)
) )
type (
statGetter struct {
dbName string
hash string
poolStats func() sql.DBStats
}
// collector collects statistics from a redis client.
// It implements the prometheus.Collector interface.
collector struct {
maxOpenConnections *prometheus.Desc
openConnections *prometheus.Desc
inUseConnections *prometheus.Desc
idleConnections *prometheus.Desc
waitCount *prometheus.Desc
waitDuration *prometheus.Desc
maxIdleClosed *prometheus.Desc
maxIdleTimeClosed *prometheus.Desc
maxLifetimeClosed *prometheus.Desc
clients []*statGetter
lock sync.Mutex
}
)
func newCollector() *collector {
c := &collector{
maxOpenConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_open_connections"),
"Maximum number of open connections to the database.",
connLabels, nil,
),
openConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "open_connections"),
"The number of established connections both in use and idle.",
connLabels, nil,
),
inUseConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "in_use_connections"),
"The number of connections currently in use.",
connLabels, nil,
),
idleConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "idle_connections"),
"The number of idle connections.",
connLabels, nil,
),
waitCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "wait_count_total"),
"The total number of connections waited for.",
connLabels, nil,
),
waitDuration: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "wait_duration_seconds_total"),
"The total time blocked waiting for a new connection.",
connLabels, nil,
),
maxIdleClosed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_idle_closed_total"),
"The total number of connections closed due to SetMaxIdleConns.",
connLabels, nil,
),
maxIdleTimeClosed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_idle_time_closed_total"),
"The total number of connections closed due to SetConnMaxIdleTime.",
connLabels, nil,
),
maxLifetimeClosed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_lifetime_closed_total"),
"The total number of connections closed due to SetConnMaxLifetime.",
connLabels, nil,
),
}
prometheus.MustRegister(c)
return c
}
// Describe implements the prometheus.Collector interface.
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.maxOpenConnections
ch <- c.openConnections
ch <- c.inUseConnections
ch <- c.idleConnections
ch <- c.waitCount
ch <- c.waitDuration
ch <- c.maxIdleClosed
ch <- c.maxLifetimeClosed
ch <- c.maxIdleTimeClosed
}
// Collect implements the prometheus.Collector interface.
func (c *collector) Collect(ch chan<- prometheus.Metric) {
c.lock.Lock()
defer c.lock.Unlock()
for _, client := range c.clients {
dbName, hash := client.dbName, client.hash
stats := client.poolStats()
ch <- prometheus.MustNewConstMetric(c.maxOpenConnections, prometheus.GaugeValue,
float64(stats.MaxOpenConnections), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.openConnections, prometheus.GaugeValue,
float64(stats.OpenConnections), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.inUseConnections, prometheus.GaugeValue,
float64(stats.InUse), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.idleConnections, prometheus.GaugeValue,
float64(stats.Idle), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.waitCount, prometheus.CounterValue,
float64(stats.WaitCount), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.waitDuration, prometheus.CounterValue,
stats.WaitDuration.Seconds(), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.maxIdleClosed, prometheus.CounterValue,
float64(stats.MaxIdleClosed), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.maxLifetimeClosed, prometheus.CounterValue,
float64(stats.MaxLifetimeClosed), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.maxIdleTimeClosed, prometheus.CounterValue,
float64(stats.MaxIdleTimeClosed), dbName, hash)
}
}
func (c *collector) registerClient(client *statGetter) {
c.lock.Lock()
defer c.lock.Unlock()
c.clients = append(c.clients, client)
}

View File

@@ -0,0 +1,147 @@
package sqlx
import (
"database/sql"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/internal/devserver"
)
func TestSqlxMetric(t *testing.T) {
cfg := devserver.Config{}
_ = conf.FillDefault(&cfg)
cfg.Port = 6480
server := devserver.NewServer(cfg)
server.StartAsync()
time.Sleep(time.Second)
metricReqDur.Observe(8, "test-cmd")
metricReqErr.Inc("test-cmd", "internal-error")
metricSlowCount.Inc("test-cmd")
url := "http://127.0.0.1:6480/metrics"
resp, err := http.Get(url)
assert.Nil(t, err)
defer resp.Body.Close()
s, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
content := string(s)
assert.Contains(t, content, "sql_client_requests_duration_ms_sum{command=\"test-cmd\"} 8\n")
assert.Contains(t, content, "sql_client_requests_duration_ms_count{command=\"test-cmd\"} 1\n")
assert.Contains(t, content, "sql_client_requests_error_total{command=\"test-cmd\",error=\"internal-error\"} 1\n")
assert.Contains(t, content, "sql_client_requests_slow_total{command=\"test-cmd\"} 1\n")
}
func TestMetricCollector(t *testing.T) {
prometheus.Unregister(connCollector)
c := newCollector()
c.registerClient(&statGetter{
dbName: "db-1",
hash: "hash-1",
poolStats: func() sql.DBStats {
return sql.DBStats{
MaxOpenConnections: 1,
OpenConnections: 2,
InUse: 3,
Idle: 4,
WaitCount: 5,
WaitDuration: 6 * time.Second,
MaxIdleClosed: 7,
MaxIdleTimeClosed: 8,
MaxLifetimeClosed: 9,
}
},
})
c.registerClient(&statGetter{
dbName: "db-1",
hash: "hash-2",
poolStats: func() sql.DBStats {
return sql.DBStats{
MaxOpenConnections: 10,
OpenConnections: 20,
InUse: 30,
Idle: 40,
WaitCount: 50,
WaitDuration: 60 * time.Second,
MaxIdleClosed: 70,
MaxIdleTimeClosed: 80,
MaxLifetimeClosed: 90,
}
},
})
c.registerClient(&statGetter{
dbName: "db-2",
hash: "hash-2",
poolStats: func() sql.DBStats {
return sql.DBStats{
MaxOpenConnections: 100,
OpenConnections: 200,
InUse: 300,
Idle: 400,
WaitCount: 500,
WaitDuration: 600 * time.Second,
MaxIdleClosed: 700,
MaxIdleTimeClosed: 800,
MaxLifetimeClosed: 900,
}
},
})
val := `
# HELP sql_client_idle_connections The number of idle connections.
# TYPE sql_client_idle_connections gauge
sql_client_idle_connections{db_name="db-1",hash="hash-1"} 4
sql_client_idle_connections{db_name="db-1",hash="hash-2"} 40
sql_client_idle_connections{db_name="db-2",hash="hash-2"} 400
# HELP sql_client_in_use_connections The number of connections currently in use.
# TYPE sql_client_in_use_connections gauge
sql_client_in_use_connections{db_name="db-1",hash="hash-1"} 3
sql_client_in_use_connections{db_name="db-1",hash="hash-2"} 30
sql_client_in_use_connections{db_name="db-2",hash="hash-2"} 300
# HELP sql_client_max_idle_closed_total The total number of connections closed due to SetMaxIdleConns.
# TYPE sql_client_max_idle_closed_total counter
sql_client_max_idle_closed_total{db_name="db-1",hash="hash-1"} 7
sql_client_max_idle_closed_total{db_name="db-1",hash="hash-2"} 70
sql_client_max_idle_closed_total{db_name="db-2",hash="hash-2"} 700
# HELP sql_client_max_idle_time_closed_total The total number of connections closed due to SetConnMaxIdleTime.
# TYPE sql_client_max_idle_time_closed_total counter
sql_client_max_idle_time_closed_total{db_name="db-1",hash="hash-1"} 8
sql_client_max_idle_time_closed_total{db_name="db-1",hash="hash-2"} 80
sql_client_max_idle_time_closed_total{db_name="db-2",hash="hash-2"} 800
# HELP sql_client_max_lifetime_closed_total The total number of connections closed due to SetConnMaxLifetime.
# TYPE sql_client_max_lifetime_closed_total counter
sql_client_max_lifetime_closed_total{db_name="db-1",hash="hash-1"} 9
sql_client_max_lifetime_closed_total{db_name="db-1",hash="hash-2"} 90
sql_client_max_lifetime_closed_total{db_name="db-2",hash="hash-2"} 900
# HELP sql_client_max_open_connections Maximum number of open connections to the database.
# TYPE sql_client_max_open_connections gauge
sql_client_max_open_connections{db_name="db-1",hash="hash-1"} 1
sql_client_max_open_connections{db_name="db-1",hash="hash-2"} 10
sql_client_max_open_connections{db_name="db-2",hash="hash-2"} 100
# HELP sql_client_open_connections The number of established connections both in use and idle.
# TYPE sql_client_open_connections gauge
sql_client_open_connections{db_name="db-1",hash="hash-1"} 2
sql_client_open_connections{db_name="db-1",hash="hash-2"} 20
sql_client_open_connections{db_name="db-2",hash="hash-2"} 200
# HELP sql_client_wait_count_total The total number of connections waited for.
# TYPE sql_client_wait_count_total counter
sql_client_wait_count_total{db_name="db-1",hash="hash-1"} 5
sql_client_wait_count_total{db_name="db-1",hash="hash-2"} 50
sql_client_wait_count_total{db_name="db-2",hash="hash-2"} 500
# HELP sql_client_wait_duration_seconds_total The total time blocked waiting for a new connection.
# TYPE sql_client_wait_duration_seconds_total counter
sql_client_wait_duration_seconds_total{db_name="db-1",hash="hash-1"} 6
sql_client_wait_duration_seconds_total{db_name="db-1",hash="hash-2"} 60
sql_client_wait_duration_seconds_total{db_name="db-2",hash="hash-2"} 600
`
err := testutil.CollectAndCompare(c, strings.NewReader(val))
assert.NoError(t, err)
}

View File

@@ -1,6 +1,10 @@
package sqlx package sqlx
import "github.com/go-sql-driver/mysql" import (
"errors"
"github.com/go-sql-driver/mysql"
)
const ( const (
mysqlDriverName = "mysql" mysqlDriverName = "mysql"
@@ -18,7 +22,8 @@ func mysqlAcceptable(err error) bool {
return true return true
} }
myerr, ok := err.(*mysql.MySQLError) var myerr *mysql.MySQLError
ok := errors.As(err, &myerr)
if !ok { if !ok {
return false return false
} }

View File

@@ -28,7 +28,7 @@ func TestBreakerOnNotHandlingDuplicateEntry(t *testing.T) {
var found bool var found bool
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
if tryOnDuplicateEntryError(t, nil) == breaker.ErrServiceUnavailable { if errors.Is(tryOnDuplicateEntryError(t, nil), breaker.ErrServiceUnavailable) {
found = true found = true
} }
} }

View File

@@ -8,7 +8,7 @@ import (
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/internal/dbtest" "github.com/zeromicro/go-zero/core/stores/dbtest"
) )
func TestUnmarshalRowBool(t *testing.T) { func TestUnmarshalRowBool(t *testing.T) {

View File

@@ -3,6 +3,7 @@ package sqlx
import ( import (
"context" "context"
"database/sql" "database/sql"
"errors"
"github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
@@ -41,21 +42,6 @@ type (
// SqlOption defines the method to customize a sql connection. // SqlOption defines the method to customize a sql connection.
SqlOption func(*commonSqlConn) SqlOption func(*commonSqlConn)
// StmtSession interface represents a session that can be used to execute statements.
StmtSession interface {
Close() error
Exec(args ...any) (sql.Result, error)
ExecCtx(ctx context.Context, args ...any) (sql.Result, error)
QueryRow(v any, args ...any) error
QueryRowCtx(ctx context.Context, v any, args ...any) error
QueryRowPartial(v any, args ...any) error
QueryRowPartialCtx(ctx context.Context, v any, args ...any) error
QueryRows(v any, args ...any) error
QueryRowsCtx(ctx context.Context, v any, args ...any) error
QueryRowsPartial(v any, args ...any) error
QueryRowsPartialCtx(ctx context.Context, v any, args ...any) error
}
// thread-safe // thread-safe
// Because CORBA doesn't support PREPARE, so we need to combine the // Because CORBA doesn't support PREPARE, so we need to combine the
// query arguments into one string and do underlying query without arguments // query arguments into one string and do underlying query without arguments
@@ -64,7 +50,7 @@ type (
onError func(context.Context, error) onError func(context.Context, error)
beginTx beginnable beginTx beginnable
brk breaker.Breaker brk breaker.Breaker
accept func(error) bool accept breaker.Acceptable
} }
connProvider func() (*sql.DB, error) connProvider func() (*sql.DB, error)
@@ -75,18 +61,6 @@ type (
Query(query string, args ...any) (*sql.Rows, error) Query(query string, args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
} }
statement struct {
query string
stmt *sql.Stmt
}
stmtConn interface {
Exec(args ...any) (sql.Result, error)
ExecContext(ctx context.Context, args ...any) (sql.Result, error)
Query(args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
}
) )
// NewSqlConn returns a SqlConn with given driver name and datasource. // NewSqlConn returns a SqlConn with given driver name and datasource.
@@ -157,7 +131,7 @@ func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...any) (
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 { if errors.Is(err, breaker.ErrServiceUnavailable) {
metricReqErr.Inc("Exec", "breaker") metricReqErr.Inc("Exec", "breaker")
} }
@@ -188,12 +162,14 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm
} }
stmt = statement{ stmt = statement{
query: query, query: query,
stmt: st, stmt: st,
brk: db.brk,
accept: db.acceptable,
} }
return nil return nil
}, db.acceptable) }, db.acceptable)
if err == breaker.ErrServiceUnavailable { if errors.Is(err, breaker.ErrServiceUnavailable) {
metricReqErr.Inc("Prepare", "breaker") metricReqErr.Inc("Prepare", "breaker")
} }
@@ -283,7 +259,7 @@ func (db *commonSqlConn) TransactCtx(ctx context.Context, fn func(context.Contex
err = 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 { if errors.Is(err, breaker.ErrServiceUnavailable) {
metricReqErr.Inc("Transact", "breaker") metricReqErr.Inc("Transact", "breaker")
} }
@@ -291,11 +267,13 @@ func (db *commonSqlConn) TransactCtx(ctx context.Context, fn func(context.Contex
} }
func (db *commonSqlConn) acceptable(err error) bool { func (db *commonSqlConn) acceptable(err error) bool {
if err == nil || err == sql.ErrNoRows || err == sql.ErrTxDone || err == context.Canceled { if err == nil || errors.Is(err, sql.ErrNoRows) || errors.Is(err, sql.ErrTxDone) ||
errors.Is(err, context.Canceled) {
return true return true
} }
if _, ok := err.(acceptableError); ok { var e acceptableError
if errors.As(err, &e) {
return true return true
} }
@@ -308,7 +286,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 ...any) (err error) { q string, args ...any) (err error) {
var qerr error var scanFailed bool
err = 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 {
@@ -317,96 +295,22 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows)
} }
return query(ctx, conn, func(rows *sql.Rows) error { return query(ctx, conn, func(rows *sql.Rows) error {
qerr = scanner(rows) e := scanner(rows)
return qerr if e != nil {
scanFailed = true
}
return e
}, q, args...) }, q, args...)
}, func(err error) bool { }, func(err error) bool {
return qerr == err || db.acceptable(err) return scanFailed || db.acceptable(err)
}) })
if err == breaker.ErrServiceUnavailable { if errors.Is(err, breaker.ErrServiceUnavailable) {
metricReqErr.Inc("queryRows", "breaker") metricReqErr.Inc("queryRows", "breaker")
} }
return return
} }
func (s statement) Close() error {
return s.stmt.Close()
}
func (s statement) Exec(args ...any) (sql.Result, error) {
return s.ExecCtx(context.Background(), args...)
}
func (s statement) ExecCtx(ctx context.Context, args ...any) (result sql.Result, err error) {
ctx, span := startSpan(ctx, "Exec")
defer func() {
endSpan(span, err)
}()
return execStmt(ctx, s.stmt, s.query, args...)
}
func (s statement) QueryRow(v any, args ...any) error {
return s.QueryRowCtx(context.Background(), v, args...)
}
func (s statement) QueryRowCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRow")
defer func() {
endSpan(span, err)
}()
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
return unmarshalRow(v, rows, true)
}, s.query, args...)
}
func (s statement) QueryRowPartial(v any, args ...any) error {
return s.QueryRowPartialCtx(context.Background(), v, args...)
}
func (s statement) QueryRowPartialCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowPartial")
defer func() {
endSpan(span, err)
}()
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
return unmarshalRow(v, rows, false)
}, s.query, args...)
}
func (s statement) QueryRows(v any, args ...any) error {
return s.QueryRowsCtx(context.Background(), v, args...)
}
func (s statement) QueryRowsCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRows")
defer func() {
endSpan(span, err)
}()
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
return unmarshalRows(v, rows, true)
}, s.query, args...)
}
func (s statement) QueryRowsPartial(v any, args ...any) error {
return s.QueryRowsPartialCtx(context.Background(), v, args...)
}
func (s statement) QueryRowsPartialCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowsPartial")
defer func() {
endSpan(span, err)
}()
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
return unmarshalRows(v, rows, false)
}, s.query, args...)
}
// WithAcceptable returns a SqlOption that setting the acceptable function. // WithAcceptable returns a SqlOption that setting the acceptable function.
// acceptable is the func to check if the error can be accepted. // acceptable is the func to check if the error can be accepted.
func WithAcceptable(acceptable func(err error) bool) SqlOption { func WithAcceptable(acceptable func(err error) bool) SqlOption {

View File

@@ -10,8 +10,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/dbtest"
"github.com/zeromicro/go-zero/core/trace/tracetest" "github.com/zeromicro/go-zero/core/trace/tracetest"
"github.com/zeromicro/go-zero/internal/dbtest"
) )
const mockedDatasource = "sqlmock" const mockedDatasource = "sqlmock"
@@ -156,6 +156,7 @@ func TestStatement(t *testing.T) {
st := statement{ st := statement{
query: "foo", query: "foo",
stmt: stmt, stmt: stmt,
brk: breaker.NopBreaker(),
} }
assert.NoError(t, st.Close()) assert.NoError(t, st.Close())
}) })

View File

@@ -3,8 +3,10 @@ package sqlx
import ( import (
"context" "context"
"database/sql" "database/sql"
"errors"
"time" "time"
"github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/timex" "github.com/zeromicro/go-zero/core/timex"
@@ -18,6 +20,145 @@ var (
logSlowSql = syncx.ForAtomicBool(true) logSlowSql = syncx.ForAtomicBool(true)
) )
type (
// StmtSession interface represents a session that can be used to execute statements.
StmtSession interface {
Close() error
Exec(args ...any) (sql.Result, error)
ExecCtx(ctx context.Context, args ...any) (sql.Result, error)
QueryRow(v any, args ...any) error
QueryRowCtx(ctx context.Context, v any, args ...any) error
QueryRowPartial(v any, args ...any) error
QueryRowPartialCtx(ctx context.Context, v any, args ...any) error
QueryRows(v any, args ...any) error
QueryRowsCtx(ctx context.Context, v any, args ...any) error
QueryRowsPartial(v any, args ...any) error
QueryRowsPartialCtx(ctx context.Context, v any, args ...any) error
}
statement struct {
query string
stmt *sql.Stmt
brk breaker.Breaker
accept breaker.Acceptable
}
stmtConn interface {
Exec(args ...any) (sql.Result, error)
ExecContext(ctx context.Context, args ...any) (sql.Result, error)
Query(args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
}
)
func (s statement) Close() error {
return s.stmt.Close()
}
func (s statement) Exec(args ...any) (sql.Result, error) {
return s.ExecCtx(context.Background(), args...)
}
func (s statement) ExecCtx(ctx context.Context, args ...any) (result sql.Result, err error) {
ctx, span := startSpan(ctx, "Exec")
defer func() {
endSpan(span, err)
}()
err = s.brk.DoWithAcceptable(func() error {
result, err = execStmt(ctx, s.stmt, s.query, args...)
return err
}, func(err error) bool {
return s.accept(err)
})
if errors.Is(err, breaker.ErrServiceUnavailable) {
metricReqErr.Inc("stmt_exec", "breaker")
}
return
}
func (s statement) QueryRow(v any, args ...any) error {
return s.QueryRowCtx(context.Background(), v, args...)
}
func (s statement) QueryRowCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRow")
defer func() {
endSpan(span, err)
}()
return s.queryRows(ctx, func(v any, scanner rowsScanner) error {
return unmarshalRow(v, scanner, true)
}, v, args...)
}
func (s statement) QueryRowPartial(v any, args ...any) error {
return s.QueryRowPartialCtx(context.Background(), v, args...)
}
func (s statement) QueryRowPartialCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowPartial")
defer func() {
endSpan(span, err)
}()
return s.queryRows(ctx, func(v any, scanner rowsScanner) error {
return unmarshalRow(v, scanner, false)
}, v, args...)
}
func (s statement) QueryRows(v any, args ...any) error {
return s.QueryRowsCtx(context.Background(), v, args...)
}
func (s statement) QueryRowsCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRows")
defer func() {
endSpan(span, err)
}()
return s.queryRows(ctx, func(v any, scanner rowsScanner) error {
return unmarshalRows(v, scanner, true)
}, v, args...)
}
func (s statement) QueryRowsPartial(v any, args ...any) error {
return s.QueryRowsPartialCtx(context.Background(), v, args...)
}
func (s statement) QueryRowsPartialCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowsPartial")
defer func() {
endSpan(span, err)
}()
return s.queryRows(ctx, func(v any, scanner rowsScanner) error {
return unmarshalRows(v, scanner, false)
}, v, args...)
}
func (s statement) queryRows(ctx context.Context, scanFn func(any, rowsScanner) error,
v any, args ...any) error {
var scanFailed bool
err := s.brk.DoWithAcceptable(func() error {
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
err := scanFn(v, rows)
if err != nil {
scanFailed = true
}
return err
}, s.query, args...)
}, func(err error) bool {
return scanFailed || s.accept(err)
})
if errors.Is(err, breaker.ErrServiceUnavailable) {
metricReqErr.Inc("stmt_queryRows", "breaker")
}
return err
}
// DisableLog disables logging of sql statements, includes info and slow logs. // DisableLog disables logging of sql statements, includes info and slow logs.
func DisableLog() { func DisableLog() {
logSql.Set(false) logSql.Set(false)
@@ -128,6 +269,7 @@ func (e *realSqlGuard) finish(ctx context.Context, err error) {
duration := timex.Since(e.startTime) duration := timex.Since(e.startTime)
if duration > slowThreshold.Load() { if duration > slowThreshold.Load() {
logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] %s: slowcall - %s", e.command, e.stmt) logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] %s: slowcall - %s", e.command, e.stmt)
metricSlowCount.Inc(e.command)
} else if logSql.True() { } else if logSql.True() {
logx.WithContext(ctx).WithDuration(duration).Infof("sql %s: %s", e.command, e.stmt) logx.WithContext(ctx).WithDuration(duration).Infof("sql %s: %s", e.command, e.stmt)
} }
@@ -136,7 +278,7 @@ func (e *realSqlGuard) finish(ctx context.Context, err error) {
logSqlError(ctx, e.stmt, err) logSqlError(ctx, e.stmt, err)
} }
metricReqDur.Observe(duration.Milliseconds(), e.command) metricReqDur.ObserveFloat(float64(duration)/float64(time.Millisecond), e.command)
} }
func (e *realSqlGuard) start(q string, args ...any) error { func (e *realSqlGuard) start(q string, args ...any) error {

View File

@@ -7,7 +7,10 @@ import (
"testing" "testing"
"time" "time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/stores/dbtest"
) )
var errMockedPlaceholder = errors.New("placeholder") var errMockedPlaceholder = errors.New("placeholder")
@@ -219,6 +222,74 @@ func TestNilGuard(t *testing.T) {
assert.Equal(t, nilGuard{}, guard) assert.Equal(t, nilGuard{}, guard)
} }
func TestStmtBreaker(t *testing.T) {
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectPrepare("any")
conn := NewSqlConnFromDB(db)
stmt, err := conn.Prepare("any")
assert.NoError(t, err)
var val struct {
Foo int
Bar string
}
for i := 0; i < 1000; i++ {
row := sqlmock.NewRows([]string{"foo"}).AddRow("bar")
mock.ExpectQuery("any").WillReturnRows(row)
err := stmt.QueryRow(&val)
assert.Error(t, err)
assert.NotErrorIs(t, err, breaker.ErrServiceUnavailable)
}
})
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectPrepare("any")
conn := NewSqlConnFromDB(db)
stmt, err := conn.Prepare("any")
assert.NoError(t, err)
for i := 0; i < 1000; i++ {
assert.Error(t, conn.Transact(func(session Session) error {
return nil
}))
}
var breakerTriggered bool
for i := 0; i < 1000; i++ {
_, err = stmt.Exec("any")
if errors.Is(err, breaker.ErrServiceUnavailable) {
breakerTriggered = true
break
}
}
assert.True(t, breakerTriggered)
})
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectPrepare("any")
conn := NewSqlConnFromDB(db)
stmt, err := conn.Prepare("any")
assert.NoError(t, err)
for i := 0; i < 1000; i++ {
assert.Error(t, conn.Transact(func(session Session) error {
return nil
}))
}
var breakerTriggered bool
for i := 0; i < 1000; i++ {
err = stmt.QueryRows(&struct{}{}, "any")
if errors.Is(err, breaker.ErrServiceUnavailable) {
breakerTriggered = true
break
}
}
assert.True(t, breakerTriggered)
})
}
type mockedSessionConn struct { type mockedSessionConn struct {
lastInsertId int64 lastInsertId int64
rowsAffected int64 rowsAffected int64

View File

@@ -3,6 +3,7 @@ package sqlx
import ( import (
"context" "context"
"database/sql" "database/sql"
"errors"
"github.com/zeromicro/go-zero/core/trace" "github.com/zeromicro/go-zero/core/trace"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
@@ -23,7 +24,7 @@ func startSpan(ctx context.Context, method string) (context.Context, oteltrace.S
func endSpan(span oteltrace.Span, err error) { func endSpan(span oteltrace.Span, err error) {
defer span.End() defer span.End()
if err == nil || err == sql.ErrNoRows { if err == nil || errors.Is(err, sql.ErrNoRows) {
span.SetStatus(codes.Ok, "") span.SetStatus(codes.Ok, "")
return return
} }

View File

@@ -4,6 +4,8 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/zeromicro/go-zero/core/breaker"
) )
type ( type (
@@ -75,6 +77,7 @@ func (t txSession) PrepareCtx(ctx context.Context, q string) (stmtSession StmtSe
return statement{ return statement{
query: q, query: q,
stmt: stmt, stmt: stmt,
brk: breaker.NopBreaker(),
}, nil }, nil
} }

View File

@@ -9,7 +9,7 @@ import (
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/internal/dbtest" "github.com/zeromicro/go-zero/core/stores/dbtest"
) )
const ( const (

View File

@@ -143,7 +143,7 @@ func logInstanceError(ctx context.Context, datasource string, err error) {
} }
func logSqlError(ctx context.Context, stmt string, err error) { func logSqlError(ctx context.Context, stmt string, err error) {
if err != nil && err != ErrNotFound { if err != nil && !errors.Is(err, ErrNotFound) {
logx.WithContext(ctx).Errorf("stmt: %s, error: %s", stmt, err.Error()) logx.WithContext(ctx).Errorf("stmt: %s, error: %s", stmt, err.Error())
} }
} }

Some files were not shown because too many files have changed in this diff Show More