From 6170d7b790922aed130c0801540fbef01f05a7c9 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 19 Mar 2023 20:04:18 +0800 Subject: [PATCH 1/9] feat: rest validation on http requests (#3041) --- core/executors/periodicalexecutor.go | 2 +- core/validation/validator.go | 7 +++++++ rest/httpx/requests.go | 5 ++++- rest/httpx/requests_test.go | 20 ++++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 core/validation/validator.go diff --git a/core/executors/periodicalexecutor.go b/core/executors/periodicalexecutor.go index 6722a6578da7..e13f907e8dfd 100644 --- a/core/executors/periodicalexecutor.go +++ b/core/executors/periodicalexecutor.go @@ -81,7 +81,7 @@ func (pe *PeriodicalExecutor) Flush() bool { }()) } -// Sync lets caller to run fn thread-safe with pe, especially for the underlying container. +// Sync lets caller run fn thread-safe with pe, especially for the underlying container. func (pe *PeriodicalExecutor) Sync(fn func()) { pe.lock.Lock() defer pe.lock.Unlock() diff --git a/core/validation/validator.go b/core/validation/validator.go new file mode 100644 index 000000000000..2903ac9c68db --- /dev/null +++ b/core/validation/validator.go @@ -0,0 +1,7 @@ +package validation + +// Validator represents a validator. +type Validator interface { + // Validate validates the value. + Validate() error +} diff --git a/rest/httpx/requests.go b/rest/httpx/requests.go index cd088bb0216b..02d29ecab256 100644 --- a/rest/httpx/requests.go +++ b/rest/httpx/requests.go @@ -7,6 +7,7 @@ import ( "sync/atomic" "github.com/zeromicro/go-zero/core/mapping" + "github.com/zeromicro/go-zero/core/validation" "github.com/zeromicro/go-zero/rest/internal/encoding" "github.com/zeromicro/go-zero/rest/internal/header" "github.com/zeromicro/go-zero/rest/pathvar" @@ -51,7 +52,9 @@ func Parse(r *http.Request, v any) error { return err } - if val := validator.Load(); val != nil { + if valid, ok := v.(validation.Validator); ok { + return valid.Validate() + } else if val := validator.Load(); val != nil { return val.(Validator).Validate(r, v) } diff --git a/rest/httpx/requests_test.go b/rest/httpx/requests_test.go index fa7dd9dc722f..aa462176ac59 100644 --- a/rest/httpx/requests_test.go +++ b/rest/httpx/requests_test.go @@ -354,6 +354,14 @@ func TestParseWithValidatorWithError(t *testing.T) { assert.Error(t, Parse(r, &v)) } +func TestParseWithValidatorRequest(t *testing.T) { + SetValidator(mockValidator{}) + var v mockRequest + r, err := http.NewRequest(http.MethodGet, "/a?&age=18", http.NoBody) + assert.Nil(t, err) + assert.Error(t, Parse(r, &v)) +} + func BenchmarkParseRaw(b *testing.B) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", http.NoBody) if err != nil { @@ -410,3 +418,15 @@ func (m mockValidator) Validate(r *http.Request, data any) error { return nil } + +type mockRequest struct { + Name string `json:"name,optional"` +} + +func (m mockRequest) Validate() error { + if m.Name != "hello" { + return errors.New("name is not hello") + } + + return nil +} From 0ab06f62ca827bfbeaabc11b17d2d5e92014092b Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 19 Mar 2023 23:56:36 +0800 Subject: [PATCH 2/9] chore: add more tests (#3045) --- core/stat/metrics_test.go | 2 -- core/stat/usage_test.go | 64 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 core/stat/usage_test.go diff --git a/core/stat/metrics_test.go b/core/stat/metrics_test.go index 1f11a21bd3c4..7172994f8a6a 100644 --- a/core/stat/metrics_test.go +++ b/core/stat/metrics_test.go @@ -6,11 +6,9 @@ import ( "time" "github.com/stretchr/testify/assert" - "github.com/zeromicro/go-zero/core/logx" ) func TestMetrics(t *testing.T) { - logx.Disable() DisableLog() defer logEnabled.Set(true) diff --git a/core/stat/usage_test.go b/core/stat/usage_test.go new file mode 100644 index 000000000000..6cb5e19b9b65 --- /dev/null +++ b/core/stat/usage_test.go @@ -0,0 +1,64 @@ +package stat + +import ( + "bytes" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/zeromicro/go-zero/core/logx" +) + +func TestBToMb(t *testing.T) { + tests := []struct { + name string + bytes uint64 + expected float32 + }{ + { + name: "Test 1: Convert 0 bytes to MB", + bytes: 0, + expected: 0, + }, + { + name: "Test 2: Convert 1048576 bytes to MB", + bytes: 1048576, + expected: 1, + }, + { + name: "Test 3: Convert 2097152 bytes to MB", + bytes: 2097152, + expected: 2, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result := bToMb(test.bytes) + assert.Equal(t, test.expected, result) + }) + } +} + +func TestPrintUsage(t *testing.T) { + var buf bytes.Buffer + writer := logx.NewWriter(&buf) + old := logx.Reset() + logx.SetWriter(writer) + defer logx.SetWriter(old) + + printUsage() + + output := buf.String() + assert.Contains(t, output, "CPU:") + assert.Contains(t, output, "MEMORY:") + assert.Contains(t, output, "Alloc=") + assert.Contains(t, output, "TotalAlloc=") + assert.Contains(t, output, "Sys=") + assert.Contains(t, output, "NumGC=") + + lines := strings.Split(output, "\n") + assert.Len(t, lines, 2) + fields := strings.Split(lines[0], ", ") + assert.Len(t, fields, 5) +} From c3648be533c4d8c63e43e0aca570ec5c3a15b21d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 23 Mar 2023 22:23:47 +0800 Subject: [PATCH 3/9] chore(deps): bump go.uber.org/automaxprocs from 1.5.1 to 1.5.2 (#3051) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index cfce5bcf1b1d..18a364152015 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( go.opentelemetry.io/otel/exporters/zipkin v1.14.0 go.opentelemetry.io/otel/sdk v1.14.0 go.opentelemetry.io/otel/trace v1.14.0 - go.uber.org/automaxprocs v1.5.1 + go.uber.org/automaxprocs v1.5.2 go.uber.org/goleak v1.2.1 golang.org/x/net v0.8.0 golang.org/x/sys v0.6.0 diff --git a/go.sum b/go.sum index d039addc77f7..ced61ed13d91 100644 --- a/go.sum +++ b/go.sum @@ -418,8 +418,8 @@ go.opentelemetry.io/proto/otlp v0.19.0 h1:IVN6GR+mhC4s5yfcTbmzHYODqvWAp3ZedA2SJP go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U= go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= -go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk= -go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU= +go.uber.org/automaxprocs v1.5.2 h1:2LxUOGiR3O6tw8ui5sZa2LAaHnsviZdVOUZw4fvbnME= +go.uber.org/automaxprocs v1.5.2/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= From 2e9063a9a1d2a4821adf1b785a92c64cd0af5613 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 23 Mar 2023 22:46:50 +0800 Subject: [PATCH 4/9] chore(deps): bump go.mongodb.org/mongo-driver from 1.11.2 to 1.11.3 (#3054) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 18a364152015..d79c680fa30c 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/stretchr/testify v1.8.2 go.etcd.io/etcd/api/v3 v3.5.7 go.etcd.io/etcd/client/v3 v3.5.7 - go.mongodb.org/mongo-driver v1.11.2 + go.mongodb.org/mongo-driver v1.11.3 go.opentelemetry.io/otel v1.14.0 go.opentelemetry.io/otel/exporters/jaeger v1.14.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 diff --git a/go.sum b/go.sum index ced61ed13d91..4fbede590223 100644 --- a/go.sum +++ b/go.sum @@ -388,8 +388,8 @@ go.etcd.io/etcd/client/pkg/v3 v3.5.7 h1:y3kf5Gbp4e4q7egZdn5T7W9TSHUvkClN6u+Rq9mE go.etcd.io/etcd/client/pkg/v3 v3.5.7/go.mod h1:o0Abi1MK86iad3YrWhgUsbGx1pmTS+hrORWc2CamuhY= go.etcd.io/etcd/client/v3 v3.5.7 h1:u/OhpiuCgYY8awOHlhIhmGIGpxfBU/GZBUP3m/3/Iz4= go.etcd.io/etcd/client/v3 v3.5.7/go.mod h1:sOWmj9DZUMyAngS7QQwCyAXXAL6WhgTOPLNS/NabQgw= -go.mongodb.org/mongo-driver v1.11.2 h1:+1v2rDQUWNcGW7/7E0Jvdz51V38XXxJfhzbV17aNHCw= -go.mongodb.org/mongo-driver v1.11.2/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8= +go.mongodb.org/mongo-driver v1.11.3 h1:Ql6K6qYHEzB6xvu4+AU0BoRoqf9vFPcc4o7MUIdPW8Y= +go.mongodb.org/mongo-driver v1.11.3/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= From 9c6b516bb879f1117678c0462d1e330d10a87c36 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Thu, 23 Mar 2023 23:45:57 +0800 Subject: [PATCH 5/9] fix: #3058 (#3059) --- core/conf/config_test.go | 18 +++++++++++++ core/mapping/unmarshaler.go | 13 ++++----- core/mapping/unmarshaler_test.go | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/core/conf/config_test.go b/core/conf/config_test.go index 723925113e42..a0d151408db1 100644 --- a/core/conf/config_test.go +++ b/core/conf/config_test.go @@ -123,6 +123,24 @@ d = "abcd" } } +func TestConfigWithLower(t *testing.T) { + text := `a = "foo" +b = 1 +` + tmpfile, err := createTempFile(".toml", text) + assert.Nil(t, err) + defer os.Remove(tmpfile) + + var val struct { + A string `json:"a"` + b int + } + if assert.NoError(t, Load(tmpfile, &val)) { + assert.Equal(t, "foo", val.A) + assert.Equal(t, 0, val.b) + } +} + func TestConfigJsonCanonical(t *testing.T) { text := []byte(`{"a": "foo", "B": "bar"}`) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index d19469f38a06..e64a7675feb8 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -695,6 +695,10 @@ func (u *Unmarshaler) processFieldWithEnvValue(fieldType reflect.Type, value ref func (u *Unmarshaler) processNamedField(field reflect.StructField, value reflect.Value, m valuerWithParent, fullName string) error { + if !field.IsExported() { + return nil + } + key, opts, err := u.parseOptionsWithContext(field, m, fullName) if err != nil { return err @@ -869,12 +873,9 @@ func (u *Unmarshaler) unmarshalWithFullName(m valuerWithParent, v any, fullName numFields := baseType.NumField() for i := 0; i < numFields; i++ { - field := baseType.Field(i) - if !field.IsExported() { - continue - } - - if err := u.processField(field, valElem.Field(i), m, fullName); err != nil { + typeField := baseType.Field(i) + valueField := valElem.Field(i) + if err := u.processField(typeField, valueField, m, fullName); err != nil { return err } } diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 4b6148a2f718..1f72326304fe 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -53,6 +53,52 @@ func TestUnmarshalWithoutTagName(t *testing.T) { } } +func TestUnmarshalWithLowerField(t *testing.T) { + type ( + Lower struct { + value int `key:"lower"` + } + + inner struct { + Lower + Optional bool `key:",optional"` + } + ) + m := map[string]any{ + "Optional": true, + "lower": 1, + } + + var in inner + if assert.NoError(t, UnmarshalKey(m, &in)) { + assert.True(t, in.Optional) + assert.Equal(t, 0, in.value) + } +} + +func TestUnmarshalWithLowerAnonymousStruct(t *testing.T) { + type ( + lower struct { + Value int `key:"lower"` + } + + inner struct { + lower + Optional bool `key:",optional"` + } + ) + m := map[string]any{ + "Optional": true, + "lower": 1, + } + + var in inner + if assert.NoError(t, UnmarshalKey(m, &in)) { + assert.True(t, in.Optional) + assert.Equal(t, 1, in.Value) + } +} + func TestUnmarshalWithoutTagNameWithCanonicalKey(t *testing.T) { type inner struct { Name string `key:"name"` From fe85e7cb428d48f215ef8f1a0b11adb8624d913c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 24 Mar 2023 10:16:45 +0800 Subject: [PATCH 6/9] chore(deps): bump k8s.io/client-go from 0.26.2 to 0.26.3 (#3052) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 6 +++--- go.sum | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index d79c680fa30c..bad0744dfc06 100644 --- a/go.mod +++ b/go.mod @@ -42,9 +42,9 @@ require ( gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/h2non/gock.v1 v1.1.2 gopkg.in/yaml.v2 v2.4.0 - k8s.io/api v0.26.2 - k8s.io/apimachinery v0.26.2 - k8s.io/client-go v0.26.2 + k8s.io/api v0.26.3 + k8s.io/apimachinery v0.26.3 + k8s.io/client-go v0.26.3 k8s.io/utils v0.0.0-20230115233650-391b47cb4029 ) diff --git a/go.sum b/go.sum index 4fbede590223..228628ec64ed 100644 --- a/go.sum +++ b/go.sum @@ -766,12 +766,12 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -k8s.io/api v0.26.2 h1:dM3cinp3PGB6asOySalOZxEG4CZ0IAdJsrYZXE/ovGQ= -k8s.io/api v0.26.2/go.mod h1:1kjMQsFE+QHPfskEcVNgL3+Hp88B80uj0QtSOlj8itU= -k8s.io/apimachinery v0.26.2 h1:da1u3D5wfR5u2RpLhE/ZtZS2P7QvDgLZTi9wrNZl/tQ= -k8s.io/apimachinery v0.26.2/go.mod h1:ats7nN1LExKHvJ9TmwootT00Yz05MuYqPXEXaVeOy5I= -k8s.io/client-go v0.26.2 h1:s1WkVujHX3kTp4Zn4yGNFK+dlDXy1bAAkIl+cFAiuYI= -k8s.io/client-go v0.26.2/go.mod h1:u5EjOuSyBa09yqqyY7m3abZeovO/7D/WehVVlZ2qcqU= +k8s.io/api v0.26.3 h1:emf74GIQMTik01Aum9dPP0gAypL8JTLl/lHa4V9RFSU= +k8s.io/api v0.26.3/go.mod h1:PXsqwPMXBSBcL1lJ9CYDKy7kIReUydukS5JiRlxC3qE= +k8s.io/apimachinery v0.26.3 h1:dQx6PNETJ7nODU3XPtrwkfuubs6w7sX0M8n61zHIV/k= +k8s.io/apimachinery v0.26.3/go.mod h1:ats7nN1LExKHvJ9TmwootT00Yz05MuYqPXEXaVeOy5I= +k8s.io/client-go v0.26.3 h1:k1UY+KXfkxV2ScEL3gilKcF7761xkYsSD6BC9szIu8s= +k8s.io/client-go v0.26.3/go.mod h1:ZPNu9lm8/dbRIPAgteN30RSXea6vrCpFvq+MateTUuQ= k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4= k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E= From ed4d5e5813e4ba78860e5083c14a436197b06c33 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 24 Mar 2023 11:44:26 +0800 Subject: [PATCH 7/9] chore(deps): bump google.golang.org/grpc from 1.53.0 to 1.54.0 (#3060) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index bad0744dfc06..c7d2f159e34a 100644 --- a/go.mod +++ b/go.mod @@ -37,7 +37,7 @@ require ( golang.org/x/sys v0.6.0 golang.org/x/time v0.3.0 google.golang.org/genproto v0.0.0-20230123190316-2c411cf9d197 - google.golang.org/grpc v1.53.0 + google.golang.org/grpc v1.54.0 google.golang.org/protobuf v1.30.0 gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/h2non/gock.v1 v1.1.2 diff --git a/go.sum b/go.sum index 228628ec64ed..feaf94436881 100644 --- a/go.sum +++ b/go.sum @@ -714,8 +714,8 @@ google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQ google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= -google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc= -google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= +google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag= +google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= From 992a56e90bddf1e9cc844c3a7f06c0ac9e138636 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:14:44 +0800 Subject: [PATCH 8/9] chore(deps): bump google.golang.org/grpc from 1.53.0 to 1.54.0 in /tools/goctl (#3061) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- tools/goctl/go.mod | 8 ++++---- tools/goctl/go.sum | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tools/goctl/go.mod b/tools/goctl/go.mod index d2abfc2767ce..7d8546940a90 100644 --- a/tools/goctl/go.mod +++ b/tools/goctl/go.mod @@ -16,7 +16,7 @@ require ( github.com/zeromicro/ddl-parser v1.0.4 github.com/zeromicro/go-zero v1.5.0 golang.org/x/text v0.8.0 - google.golang.org/grpc v1.53.0 + google.golang.org/grpc v1.54.0 google.golang.org/protobuf v1.30.0 ) @@ -89,10 +89,10 @@ require ( go.uber.org/multierr v1.9.0 // indirect go.uber.org/zap v1.24.0 // indirect golang.org/x/crypto v0.6.0 // indirect - golang.org/x/net v0.7.0 // indirect + golang.org/x/net v0.8.0 // indirect golang.org/x/oauth2 v0.4.0 // indirect - golang.org/x/sys v0.5.0 // indirect - golang.org/x/term v0.5.0 // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/term v0.6.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230123190316-2c411cf9d197 // indirect diff --git a/tools/goctl/go.sum b/tools/goctl/go.sum index eaea2adc802f..e231219d2924 100644 --- a/tools/goctl/go.sum +++ b/tools/goctl/go.sum @@ -474,8 +474,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= -golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -541,12 +541,12 @@ golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -683,8 +683,8 @@ google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTp google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= -google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc= -google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= +google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag= +google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= From 04434646eb38a713fc5d429290eb9b546f10576f Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 25 Mar 2023 20:40:21 +0800 Subject: [PATCH 9/9] chore: refactor zrpc setup (#3064) --- zrpc/server.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/zrpc/server.go b/zrpc/server.go index 5327b83e473a..158d810930f3 100644 --- a/zrpc/server.go +++ b/zrpc/server.go @@ -109,30 +109,38 @@ func SetServerSlowThreshold(threshold time.Duration) { serverinterceptors.SetSlowThreshold(threshold) } -func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Metrics) error { +func setupAuthInterceptors(svr internal.Server, c RpcServerConf) error { + rds, err := redis.NewRedis(c.Redis.RedisConf) + if err != nil { + return err + } + + authenticator, err := auth.NewAuthenticator(rds, c.Redis.Key, c.StrictControl) + if err != nil { + return err + } + + svr.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator)) + svr.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator)) + + return nil +} + +func setupInterceptors(svr internal.Server, c RpcServerConf, metrics *stat.Metrics) error { if c.CpuThreshold > 0 { shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold)) - server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics)) + svr.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics)) } if c.Timeout > 0 { - server.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor( + svr.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor( time.Duration(c.Timeout) * time.Millisecond)) } if c.Auth { - rds, err := redis.NewRedis(c.Redis.RedisConf) - if err != nil { - return err - } - - authenticator, err := auth.NewAuthenticator(rds, c.Redis.Key, c.StrictControl) - if err != nil { + if err := setupAuthInterceptors(svr, c); err != nil { return err } - - server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator)) - server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator)) } return nil