Compare commits

...

5 Commits

Author SHA1 Message Date
Kevin Wan
f7f3730e1a update goctl version to 1.1.10 (#874) 2021-08-04 19:29:40 +08:00
Kevin Wan
0ee7654407 fix #792 (#873) 2021-08-04 18:45:05 +08:00
neosu
16cc990fdd fix context missing (#872)
Co-authored-by: suzhenpeng <suzhenpeng@ecoplants.tech>
2021-08-04 17:46:51 +08:00
neosu
00061c2e5b add goctl rpc template home flag (#871)
Co-authored-by: suzhenpeng <suzhenpeng@ecoplants.tech>
2021-08-04 15:54:43 +08:00
Kevin Wan
6793f7a1de fix bug that proc.SetTimeToForceQuit not working in windows (#869) 2021-08-04 11:31:33 +08:00
9 changed files with 105 additions and 7 deletions

View File

@@ -14,5 +14,5 @@ func AddWrapUpListener(fn func()) func() {
return fn
}
func SetTimeoutToForceQuit(duration time.Duration) {
func SetTimeToForceQuit(duration time.Duration) {
}

View File

@@ -32,7 +32,7 @@ import (
)
var (
buildVersion = "1.1.9-pre"
buildVersion = "1.1.10"
commands = []cli.Command{
{
Name: "upgrade",
@@ -367,6 +367,10 @@ var (
Name: "out, o",
Usage: "the target path of proto",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
},
},
Action: rpc.RPCTemplate,
},

View File

@@ -79,6 +79,12 @@ func RPCNew(c *cli.Context) error {
// RPCTemplate is the entry for generate rpc template
func RPCTemplate(c *cli.Context) error {
protoFile := c.String("o")
home := c.String("home")
if len(home) > 0 {
util.RegisterGoctlHome(home)
}
if len(protoFile) == 0 {
return errors.New("missing -o")
}

View File

@@ -73,11 +73,20 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
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{}{
"head": head,
"server": stringx.From(service.Name).ToCamel(),
"imports": strings.Join(imports.KeysStr(), util.NL),
"funcs": strings.Join(funcList, util.NL),
"head": head,
"server": stringx.From(service.Name).ToCamel(),
"imports": strings.Join(imports.KeysStr(), util.NL),
"funcs": strings.Join(funcList, util.NL),
"notStream": notStream,
}, serverFile, true)
return err
}

View File

@@ -69,8 +69,8 @@ func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
WithUnaryClientInterceptors(
clientinterceptors.TracingInterceptor,
clientinterceptors.DurationInterceptor,
clientinterceptors.BreakerInterceptor,
clientinterceptors.PrometheusInterceptor,
clientinterceptors.BreakerInterceptor,
clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
),
}

View File

@@ -1,6 +1,8 @@
package codes
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
@@ -10,6 +12,17 @@ func Acceptable(err error) bool {
switch status.Code(err) {
case codes.DeadlineExceeded, codes.Internal, codes.Unavailable, codes.DataLoss:
return false
case codes.Unknown:
return acceptableUnknown(err)
default:
return true
}
}
func acceptableUnknown(err error) bool {
switch err {
case context.DeadlineExceeded:
return false
default:
return true
}

View File

@@ -58,10 +58,12 @@ func (s *rpcServer) Start(register RegisterFn) error {
serverinterceptors.UnaryCrashInterceptor(),
serverinterceptors.UnaryStatInterceptor(s.metrics),
serverinterceptors.UnaryPrometheusInterceptor(),
serverinterceptors.UnaryBreakerInterceptor(),
}
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
streamInterceptors := []grpc.StreamServerInterceptor{
serverinterceptors.StreamCrashInterceptor,
serverinterceptors.StreamBreakerInterceptor,
}
streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),

View 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
}
}

View 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)
}