Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix: zeromicro#2672

* chore: fix more cases

* chore: update deps

* chore: update deps

* chore: refactor

* chore: refactor

* chore: refactor
  • Loading branch information
kevwan authored Dec 10, 2022
1 parent ef22042 commit fdc57d0
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 65 deletions.
10 changes: 5 additions & 5 deletions core/breaker/nopbreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ func (b noOpBreaker) Do(req func() error) error {
return req()
}

func (b noOpBreaker) DoWithAcceptable(req func() error, acceptable Acceptable) error {
func (b noOpBreaker) DoWithAcceptable(req func() error, _ Acceptable) error {
return req()
}

func (b noOpBreaker) DoWithFallback(req func() error, fallback func(err error) error) error {
func (b noOpBreaker) DoWithFallback(req func() error, _ func(err error) error) error {
return req()
}

func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error,
acceptable Acceptable) error {
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, _ func(err error) error,
_ Acceptable) error {
return req()
}

Expand All @@ -38,5 +38,5 @@ type nopPromise struct{}
func (p nopPromise) Accept() {
}

func (p nopPromise) Reject(reason string) {
func (p nopPromise) Reject(_ string) {
}
12 changes: 12 additions & 0 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,16 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter
default:
switch v := keythData.(type) {
case bool:
if dereffedElemKind != reflect.Bool {
return emptyValue, errTypeMismatch
}

targetValue.SetMapIndex(key, reflect.ValueOf(v))
case string:
if dereffedElemKind != reflect.String {
return emptyValue, errTypeMismatch
}

targetValue.SetMapIndex(key, reflect.ValueOf(v))
case json.Number:
target := reflect.New(dereffedElemType)
Expand All @@ -746,6 +754,10 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter

targetValue.SetMapIndex(key, target.Elem())
default:
if dereffedElemKind != keythValue.Kind() {
return emptyValue, errTypeMismatch
}

targetValue.SetMapIndex(key, keythValue)
}
}
Expand Down
65 changes: 65 additions & 0 deletions core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3563,6 +3563,71 @@ func TestGoogleUUID(t *testing.T) {
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c2", val.Uidp.String())
}

func TestUnmarshalJsonReaderWithTypeMismatchBool(t *testing.T) {
var req struct {
Params map[string]bool `json:"params"`
}
body := `{"params":{"a":"123"}}`
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
}

func TestUnmarshalJsonReaderWithTypeMismatchString(t *testing.T) {
var req struct {
Params map[string]string `json:"params"`
}
body := `{"params":{"a":{"a":123}}}`
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
}

func TestUnmarshalJsonReaderWithMismatchType(t *testing.T) {
type Req struct {
Params map[string]string `json:"params"`
}

var req Req
body := `{"params":{"a":{"a":123}}}`
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
}

func TestUnmarshalJsonReaderWithMismatchTypeBool(t *testing.T) {
type Req struct {
Params map[string]bool `json:"params"`
}

tests := []struct {
name string
input string
}{
{
name: "int",
input: `{"params":{"a":123}}`,
},
{
name: "int",
input: `{"params":{"a":"123"}}`,
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
var req Req
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(test.input), &req))
})
}
}

func TestUnmarshalJsonReaderWithMismatchTypeBoolMap(t *testing.T) {
var req struct {
Params map[string]string `json:"params"`
}
assert.Equal(t, errTypeMismatch, UnmarshalJsonMap(map[string]interface{}{
"params": map[string]interface{}{
"a": true,
},
}, &req))
}

func BenchmarkDefaultValue(b *testing.B) {
for i := 0; i < b.N; i++ {
var a struct {
Expand Down
9 changes: 8 additions & 1 deletion core/mapping/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ func ValidatePtr(v *reflect.Value) error {
func convertType(kind reflect.Kind, str string) (interface{}, error) {
switch kind {
case reflect.Bool:
return str == "1" || strings.ToLower(str) == "true", nil
switch strings.ToLower(str) {
case "1", "true":
return true, nil
case "0", "false":
return false, nil
default:
return false, errTypeMismatch
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intValue, err := strconv.ParseInt(str, 10, 64)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion core/rescue/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "github.com/zeromicro/go-zero/core/logx"

// Recover is used with defer to do cleanup on panics.
// Use it like:
// defer Recover(func() {})
//
// defer Recover(func() {})
func Recover(cleanups ...func()) {
for _, cleanup := range cleanups {
cleanup()
Expand Down
1 change: 0 additions & 1 deletion core/stat/internal/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func initialize() {

cores = uint64(len(cpus))
quota = float64(len(cpus))

cq, err := cpuQuota()
if err == nil {
if cq != -1 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/stretchr/testify v1.8.1
go.etcd.io/etcd/api/v3 v3.5.5
go.etcd.io/etcd/client/v3 v3.5.5
go.mongodb.org/mongo-driver v1.11.0
go.mongodb.org/mongo-driver v1.11.1
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/exporters/jaeger v1.10.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,8 @@ go.etcd.io/etcd/client/pkg/v3 v3.5.5 h1:9S0JUVvmrVl7wCF39iTQthdaaNIiAaQbmK75ogO6
go.etcd.io/etcd/client/pkg/v3 v3.5.5/go.mod h1:ggrwbk069qxpKPq8/FKkQ3Xq9y39kbFR4LnKszpRXeQ=
go.etcd.io/etcd/client/v3 v3.5.5 h1:q++2WTJbUgpQu4B6hCuT7VkdwaTP7Qz6Daak3WzbrlI=
go.etcd.io/etcd/client/v3 v3.5.5/go.mod h1:aApjR4WGlSumpnJ2kloS75h6aHUmAyaPLjHMxpc7E7c=
go.mongodb.org/mongo-driver v1.11.0 h1:FZKhBSTydeuffHj9CBjXlR8vQLee1cQyTWYPA6/tqiE=
go.mongodb.org/mongo-driver v1.11.0/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8=
go.mongodb.org/mongo-driver v1.11.1 h1:QP0znIRTuL0jf1oBQoAoM0C6ZJfBK4kx0Uumtv1A7w8=
go.mongodb.org/mongo-driver v1.11.1/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8=
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=
Expand Down
7 changes: 2 additions & 5 deletions internal/devserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import (

"github.com/felixge/fgprof"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/threading"
"github.com/zeromicro/go-zero/internal/health"
)

var (
once sync.Once
)
var once sync.Once

// Server is inner http server, expose some useful observability information of app.
// For example health check, metrics and pprof.
Expand Down Expand Up @@ -68,7 +65,7 @@ func (s *Server) StartAsync() {
s.addRoutes()
threading.GoSafe(func() {
addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
logx.Infof("Starting inner http server at %s", addr)
logx.Infof("Starting dev http server at %s", addr)
if err := http.ListenAndServe(addr, s.server); err != nil {
logx.Error(err)
}
Expand Down
35 changes: 20 additions & 15 deletions internal/health/health.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package health

import (
"fmt"
"net/http"
"strings"
"sync"

"github.com/zeromicro/go-zero/core/syncx"
Expand Down Expand Up @@ -41,6 +43,18 @@ func AddProbe(probe Probe) {
defaultHealthManager.addProbe(probe)
}

// CreateHttpHandler create health http handler base on given probe.
func CreateHttpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
if defaultHealthManager.IsReady() {
_, _ = w.Write([]byte("OK"))
} else {
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(),
http.StatusServiceUnavailable)
}
}
}

// NewHealthManager returns a new healthManager.
func NewHealthManager(name string) Probe {
return &healthManager{
Expand Down Expand Up @@ -102,22 +116,24 @@ func (p *comboHealthManager) IsReady() bool {
return false
}
}

return true
}

func (p *comboHealthManager) verboseInfo() string {
p.mu.Lock()
defer p.mu.Unlock()

var info string
var info strings.Builder
for _, probe := range p.probes {
if probe.IsReady() {
info += probe.Name() + " is ready; \n"
info.WriteString(fmt.Sprintf("%s is ready\n", probe.Name()))
} else {
info += probe.Name() + " is not ready; \n"
info.WriteString(fmt.Sprintf("%s is not ready\n", probe.Name()))
}
}
return info

return info.String()
}

// addProbe add components probe to comboHealthManager.
Expand All @@ -127,14 +143,3 @@ func (p *comboHealthManager) addProbe(probe Probe) {

p.probes = append(p.probes, probe)
}

// CreateHttpHandler create health http handler base on given probe.
func CreateHttpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
if defaultHealthManager.IsReady() {
_, _ = w.Write([]byte("OK"))
} else {
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(), http.StatusServiceUnavailable)
}
}
}
20 changes: 10 additions & 10 deletions internal/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,36 @@ func TestComboHealthManager(t *testing.T) {
})

t.Run("concurrent add probes", func(t *testing.T) {
chm2 := newComboHealthManager()
chm := newComboHealthManager()

var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
hm := NewHealthManager(probeName)
hm.MarkReady()
chm2.addProbe(hm)
chm.addProbe(hm)
wg.Done()
}()
}
wg.Wait()
assert.True(t, chm2.IsReady())
assert.True(t, chm.IsReady())
})

t.Run("markReady and markNotReady", func(t *testing.T) {
chm2 := newComboHealthManager()
chm := newComboHealthManager()

for i := 0; i < 10; i++ {
hm := NewHealthManager(probeName)
chm2.addProbe(hm)
chm.addProbe(hm)
}
assert.False(t, chm2.IsReady())
assert.False(t, chm.IsReady())

chm2.MarkReady()
assert.True(t, chm2.IsReady())
chm.MarkReady()
assert.True(t, chm.IsReady())

chm2.MarkNotReady()
assert.False(t, chm2.IsReady())
chm.MarkNotReady()
assert.False(t, chm.IsReady())
})
}

Expand Down
35 changes: 21 additions & 14 deletions rest/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,41 @@ func New(middlewares ...Middleware) Chain {

// Append extends a chain, adding the specified middlewares as the last ones in the request flow.
//
// c := chain.New(m1, m2)
// c.Append(m3, m4)
// // requests in c go m1 -> m2 -> m3 -> m4
// c := chain.New(m1, m2)
// c.Append(m3, m4)
// // requests in c go m1 -> m2 -> m3 -> m4
func (c chain) Append(middlewares ...Middleware) Chain {
return chain{middlewares: join(c.middlewares, middlewares)}
}

// Prepend extends a chain by adding the specified chain as the first one in the request flow.
//
// c := chain.New(m3, m4)
// c1 := chain.New(m1, m2)
// c.Prepend(c1)
// // requests in c go m1 -> m2 -> m3 -> m4
// c := chain.New(m3, m4)
// c1 := chain.New(m1, m2)
// c.Prepend(c1)
// // requests in c go m1 -> m2 -> m3 -> m4
func (c chain) Prepend(middlewares ...Middleware) Chain {
return chain{middlewares: join(middlewares, c.middlewares)}
}

// Then chains the middleware and returns the final http.Handler.
// New(m1, m2, m3).Then(h)
//
// New(m1, m2, m3).Then(h)
//
// is equivalent to:
// m1(m2(m3(h)))
//
// m1(m2(m3(h)))
//
// When the request comes in, it will be passed to m1, then m2, then m3
// and finally, the given handler
// (assuming every middleware calls the following one).
//
// A chain can be safely reused by calling Then() several times.
// stdStack := chain.New(ratelimitHandler, csrfHandler)
// indexPipe = stdStack.Then(indexHandler)
// authPipe = stdStack.Then(authHandler)
//
// stdStack := chain.New(ratelimitHandler, csrfHandler)
// indexPipe = stdStack.Then(indexHandler)
// authPipe = stdStack.Then(authHandler)
//
// Note that middlewares are called on every call to Then() or ThenFunc()
// and thus several instances of the same middleware will be created
// when a chain is reused in this way.
Expand All @@ -88,8 +94,9 @@ func (c chain) Then(h http.Handler) http.Handler {
// a HandlerFunc instead of a Handler.
//
// The following two statements are equivalent:
// c.Then(http.HandlerFunc(fn))
// c.ThenFunc(fn)
//
// c.Then(http.HandlerFunc(fn))
// c.ThenFunc(fn)
//
// ThenFunc provides all the guarantees of Then.
func (c chain) ThenFunc(fn http.HandlerFunc) http.Handler {
Expand Down
Loading

0 comments on commit fdc57d0

Please sign in to comment.