1 Star 0 Fork 0

非空非零/jupiter

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.go 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
renzhentao 提交于 2021-06-21 15:02 +08:00 . 1
package yell
import (
"context"
"errors"
"gitee.com/nonull/eden/pkg/util/xtest/proto/testproto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"time"
)
// FooServer ...
type FooServer struct {
name string
hook func(context.Context)
}
// SetName ...
func (s *FooServer) SetName(f string) {
s.name = f
}
// SetHook ...
func (s *FooServer) SetHook(f func(context.Context)) {
s.hook = f
}
// ErrFoo ...
var ErrFoo = errors.New("error foo")
// RespFantasy ...
var RespFantasy = &testproto.HelloReply{Message: "fantasy"}
// RespBye ...
var RespBye = &testproto.HelloReply{Message: "bye"}
// StatusFoo ...
var StatusFoo = status.Errorf(codes.DataLoss, ErrFoo.Error())
// SayHello ...
func (s *FooServer) SayHello(ctx context.Context, in *testproto.HelloRequest) (out *testproto.HelloReply, err error) {
// sleep to test cost time
time.Sleep(20 * time.Millisecond)
switch in.Name {
case "traceHook":
s.hook(ctx)
err = StatusFoo
case "needErr":
err = StatusFoo
case "slow":
time.Sleep(500 * time.Millisecond)
out = RespFantasy
case "needPanic":
panic("go dead!")
default:
out = RespFantasy
}
return
}
// StreamHello ...
func (s *FooServer) StreamHello(ss testproto.Greeter_StreamHelloServer) (err error) {
for {
in, _ := ss.Recv()
switch in.Name {
case "bye":
return ss.Send(RespBye)
case "needErr":
return StatusFoo
default:
return ss.Send(RespFantasy)
}
}
}
// StreamHello ...
func (s *FooServer) WhoServer(ctx context.Context, in *testproto.WhoServerReq) (out *testproto.WhoServerReply, err error) {
return &testproto.WhoServerReply{Message: s.name}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nonull/eden.git
git@gitee.com:nonull/eden.git
nonull
eden
jupiter
v0.3.1

搜索帮助