hwt/hwt.go

45 lines
837 B
Go
Raw Normal View History

2018-01-16 20:57:04 -08:00
package hwt
import (
"context"
fmt "fmt"
2018-01-26 17:15:05 -08:00
"go/src/math/rand"
"time"
"github.com/twitchtv/twirp"
pb "mcquay.me/hwt/rpc/hwt"
2018-01-16 20:57:04 -08:00
)
2018-01-20 09:07:57 -08:00
type Server struct {
Hostname string
}
2018-01-16 20:57:04 -08:00
func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, error) {
if req.Subject == "" {
return nil, twirp.RequiredArgumentError("subject")
}
2018-01-20 09:07:57 -08:00
2018-01-21 20:18:28 -08:00
u, err := getUser(ctx)
if err != nil {
return nil, twirp.InternalErrorWith(err)
}
2018-01-26 17:15:05 -08:00
if rand.Int()%100 == 0 {
rest := time.Duration(rand.Intn(50)) * time.Millisecond
time.Sleep(rest)
}
2018-01-26 18:04:38 -08:00
switch rand.Int() % 100 {
case 0:
return nil, twirp.NewError(twirp.Internal, "bleeding")
case 1:
return nil, twirp.NewError(twirp.ResourceExhausted, "some exhaustion")
}
2018-01-20 09:07:57 -08:00
r := &pb.HelloResp{
2018-01-21 20:18:28 -08:00
Text: fmt.Sprintf("%s said: %v", u, req.Subject),
2018-01-20 09:07:57 -08:00
Hostname: s.Hostname,
}
return r, nil
2018-01-16 20:57:04 -08:00
}