Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f7f3730e1a | ||
|
|
0ee7654407 | ||
|
|
16cc990fdd | ||
|
|
00061c2e5b | ||
|
|
6793f7a1de |
@@ -14,5 +14,5 @@ func AddWrapUpListener(fn func()) func() {
|
|||||||
return fn
|
return fn
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetTimeoutToForceQuit(duration time.Duration) {
|
func SetTimeToForceQuit(duration time.Duration) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
buildVersion = "1.1.9-pre"
|
buildVersion = "1.1.10"
|
||||||
commands = []cli.Command{
|
commands = []cli.Command{
|
||||||
{
|
{
|
||||||
Name: "upgrade",
|
Name: "upgrade",
|
||||||
@@ -367,6 +367,10 @@ var (
|
|||||||
Name: "out, o",
|
Name: "out, o",
|
||||||
Usage: "the target path of proto",
|
Usage: "the target path of proto",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "home",
|
||||||
|
Usage: "the goctl home path of the template",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: rpc.RPCTemplate,
|
Action: rpc.RPCTemplate,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -79,6 +79,12 @@ func RPCNew(c *cli.Context) error {
|
|||||||
// RPCTemplate is the entry for generate rpc template
|
// RPCTemplate is the entry for generate rpc template
|
||||||
func RPCTemplate(c *cli.Context) error {
|
func RPCTemplate(c *cli.Context) error {
|
||||||
protoFile := c.String("o")
|
protoFile := c.String("o")
|
||||||
|
home := c.String("home")
|
||||||
|
|
||||||
|
if len(home) > 0 {
|
||||||
|
util.RegisterGoctlHome(home)
|
||||||
|
}
|
||||||
|
|
||||||
if len(protoFile) == 0 {
|
if len(protoFile) == 0 {
|
||||||
return errors.New("missing -o")
|
return errors.New("missing -o")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,11 +73,20 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
notStream := false
|
||||||
|
for _, rpc := range service.RPC {
|
||||||
|
if !rpc.StreamsRequest && !rpc.StreamsReturns {
|
||||||
|
notStream = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||||
"head": head,
|
"head": head,
|
||||||
"server": stringx.From(service.Name).ToCamel(),
|
"server": stringx.From(service.Name).ToCamel(),
|
||||||
"imports": strings.Join(imports.KeysStr(), util.NL),
|
"imports": strings.Join(imports.KeysStr(), util.NL),
|
||||||
"funcs": strings.Join(funcList, util.NL),
|
"funcs": strings.Join(funcList, util.NL),
|
||||||
|
"notStream": notStream,
|
||||||
}, serverFile, true)
|
}, serverFile, true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
|
|||||||
WithUnaryClientInterceptors(
|
WithUnaryClientInterceptors(
|
||||||
clientinterceptors.TracingInterceptor,
|
clientinterceptors.TracingInterceptor,
|
||||||
clientinterceptors.DurationInterceptor,
|
clientinterceptors.DurationInterceptor,
|
||||||
clientinterceptors.BreakerInterceptor,
|
|
||||||
clientinterceptors.PrometheusInterceptor,
|
clientinterceptors.PrometheusInterceptor,
|
||||||
|
clientinterceptors.BreakerInterceptor,
|
||||||
clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
|
clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package codes
|
package codes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
@@ -10,6 +12,17 @@ func Acceptable(err error) bool {
|
|||||||
switch status.Code(err) {
|
switch status.Code(err) {
|
||||||
case codes.DeadlineExceeded, codes.Internal, codes.Unavailable, codes.DataLoss:
|
case codes.DeadlineExceeded, codes.Internal, codes.Unavailable, codes.DataLoss:
|
||||||
return false
|
return false
|
||||||
|
case codes.Unknown:
|
||||||
|
return acceptableUnknown(err)
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func acceptableUnknown(err error) bool {
|
||||||
|
switch err {
|
||||||
|
case context.DeadlineExceeded:
|
||||||
|
return false
|
||||||
default:
|
default:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,10 +58,12 @@ func (s *rpcServer) Start(register RegisterFn) error {
|
|||||||
serverinterceptors.UnaryCrashInterceptor(),
|
serverinterceptors.UnaryCrashInterceptor(),
|
||||||
serverinterceptors.UnaryStatInterceptor(s.metrics),
|
serverinterceptors.UnaryStatInterceptor(s.metrics),
|
||||||
serverinterceptors.UnaryPrometheusInterceptor(),
|
serverinterceptors.UnaryPrometheusInterceptor(),
|
||||||
|
serverinterceptors.UnaryBreakerInterceptor(),
|
||||||
}
|
}
|
||||||
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
|
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
|
||||||
streamInterceptors := []grpc.StreamServerInterceptor{
|
streamInterceptors := []grpc.StreamServerInterceptor{
|
||||||
serverinterceptors.StreamCrashInterceptor,
|
serverinterceptors.StreamCrashInterceptor,
|
||||||
|
serverinterceptors.StreamBreakerInterceptor,
|
||||||
}
|
}
|
||||||
streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
|
streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
|
||||||
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
|
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
|
||||||
|
|||||||
33
zrpc/internal/serverinterceptors/breakerinterceptor.go
Normal file
33
zrpc/internal/serverinterceptors/breakerinterceptor.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package serverinterceptors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/tal-tech/go-zero/core/breaker"
|
||||||
|
"github.com/tal-tech/go-zero/zrpc/internal/codes"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StreamBreakerInterceptor is an interceptor that acts as a circuit breaker.
|
||||||
|
func StreamBreakerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
|
||||||
|
handler grpc.StreamHandler) (err error) {
|
||||||
|
breakerName := info.FullMethod
|
||||||
|
return breaker.DoWithAcceptable(breakerName, func() error {
|
||||||
|
return handler(srv, stream)
|
||||||
|
}, codes.Acceptable)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnaryBreakerInterceptor is an interceptor that acts as a circuit breaker.
|
||||||
|
func UnaryBreakerInterceptor() grpc.UnaryServerInterceptor {
|
||||||
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
||||||
|
handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
|
breakerName := info.FullMethod
|
||||||
|
err = breaker.DoWithAcceptable(breakerName, func() error {
|
||||||
|
var err error
|
||||||
|
resp, err = handler(ctx, req)
|
||||||
|
return err
|
||||||
|
}, codes.Acceptable)
|
||||||
|
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
}
|
||||||
31
zrpc/internal/serverinterceptors/breakerinterceptor_test.go
Normal file
31
zrpc/internal/serverinterceptors/breakerinterceptor_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package serverinterceptors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStreamBreakerInterceptor(t *testing.T) {
|
||||||
|
err := StreamBreakerInterceptor(nil, nil, &grpc.StreamServerInfo{
|
||||||
|
FullMethod: "any",
|
||||||
|
}, func(
|
||||||
|
srv interface{}, stream grpc.ServerStream) error {
|
||||||
|
return status.New(codes.DeadlineExceeded, "any").Err()
|
||||||
|
})
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnaryBreakerInterceptor(t *testing.T) {
|
||||||
|
interceptor := UnaryBreakerInterceptor()
|
||||||
|
_, err := interceptor(nil, nil, &grpc.UnaryServerInfo{
|
||||||
|
FullMethod: "any",
|
||||||
|
}, func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return nil, status.New(codes.DeadlineExceeded, "any").Err()
|
||||||
|
})
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user