Skip to content

Commit

Permalink
Merge cbd9dfb into b10ce88
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuntaoLu committed Apr 4, 2017
2 parents b10ce88 + cbd9dfb commit e197c4f
Show file tree
Hide file tree
Showing 25 changed files with 1,088 additions and 444 deletions.
35 changes: 35 additions & 0 deletions codegen/templates/endpoint_register.tmpl
Expand Up @@ -12,6 +12,9 @@ import (
{{end}}

"github.com/uber/zanzibar/runtime"

// TODO: (lu) remove this, to be generated
"github.com/uber/zanzibar/examples/example-gateway/endpoints/baz"
)

type handlerFn func(
Expand Down Expand Up @@ -88,4 +91,36 @@ func Register(g *zanzibar.Gateway, router *zanzibar.Router) {
),
)
{{end}}

// TODO: (lu) remove below, to be generated
router.Register(
"POST", "/baz/call-path",
makeEndpoint(
g,
"baz",
"call",
nil,
baz.HandleCallRequest,
),
)
router.Register(
"GET", "/baz/simple-path",
makeEndpoint(
g,
"baz",
"simple",
nil,
baz.HandleSimpleRequest,
),
)
router.Register(
"GET", "/baz/simple-future-path",
makeEndpoint(
g,
"baz",
"simpleFuture",
nil,
baz.HandleSimpleFutureRequest,
),
)
}
34 changes: 34 additions & 0 deletions examples/example-gateway/build/endpoints/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 45 additions & 123 deletions examples/example-gateway/clients/baz/baz.go
Expand Up @@ -5,14 +5,12 @@ package bazClient
import (
"context"
"errors"
"fmt"
"strconv"
"time"

"github.com/uber/tchannel-go"
"github.com/uber/zanzibar/runtime"

zt "github.com/uber/zanzibar/runtime/tchannel"
"go.uber.org/thriftrw/wire"

"github.com/uber/zanzibar/examples/example-gateway/build/gen-code/github.com/uber/zanzibar/clients/baz/baz"
)

Expand All @@ -25,7 +23,6 @@ type TChanBaz interface {

// NewClient returns a new http client for service Bar.
func NewClient(config *zanzibar.StaticConfig, gateway *zanzibar.Gateway) *BazClient {
// TODO: (lu) get serviceName from thrift
serviceName := "SimpleService"

sc := gateway.Channel.GetSubChannel(serviceName)
Expand All @@ -34,23 +31,42 @@ func NewClient(config *zanzibar.StaticConfig, gateway *zanzibar.Gateway) *BazCli
port := config.MustGetInt("clients.baz.port")
sc.Peers().Add(ip + ":" + strconv.Itoa(int(port)))

client := zt.NewClient(gateway.Channel, serviceName)
client := zanzibar.NewClient(gateway.Channel, serviceName)

// TODO: (lu) maybe set these at per method level
timeout := time.Duration(config.MustGetInt("clients.baz.timeout")) * time.Millisecond
timeoutPerAttempt := time.Duration(config.MustGetInt("clients.baz.timeoutPerAttempt")) * time.Millisecond

return &BazClient{
thriftService: serviceName,
client: client,
thriftService: serviceName,
client: client,
timeout: timeout,
timeoutPerAttempt: timeoutPerAttempt,
}
}

// BazClient is the client to talk to SimpleService backend.
type BazClient struct {
thriftService string
client zt.TChanClient
client zanzibar.TChanClient

timeout time.Duration
timeoutPerAttempt time.Duration
}

// Call ...
func (c *BazClient) Call(ctx context.Context, reqHeaders map[string]string, args *baz.SimpleService_Call_Args) (map[string]string, *baz.BazResponse, error) {
var result baz.SimpleService_Call_Result

retryOpts := &tchannel.RetryOptions{
TimeoutPerAttempt: c.timeoutPerAttempt,
}
ctx, cancel := tchannel.NewContextBuilder(c.timeout).
SetParentContext(ctx).
SetRetryOptions(retryOpts).
Build()
defer cancel()

respHeaders, success, err := c.client.Call(ctx, c.thriftService, "Call", reqHeaders, args, &result)
if err == nil && !success {
err = errors.New("received no result or unknown exception for Call")
Expand All @@ -67,6 +83,16 @@ func (c *BazClient) Call(ctx context.Context, reqHeaders map[string]string, args
// Simple ...
func (c *BazClient) Simple(ctx context.Context, reqHeaders map[string]string) (map[string]string, error) {
var result baz.SimpleService_Simple_Result

retryOpts := &tchannel.RetryOptions{
TimeoutPerAttempt: c.timeoutPerAttempt,
}
ctx, cancel := tchannel.NewContextBuilder(c.timeout).
SetParentContext(ctx).
SetRetryOptions(retryOpts).
Build()
defer cancel()

args := baz.SimpleService_Simple_Args{}
respHeaders, success, err := c.client.Call(ctx, c.thriftService, "Simple", reqHeaders, &args, &result)
if err == nil && !success {
Expand All @@ -84,6 +110,16 @@ func (c *BazClient) Simple(ctx context.Context, reqHeaders map[string]string) (m
// SimpleFuture ...
func (c *BazClient) SimpleFuture(ctx context.Context, reqHeaders map[string]string) (map[string]string, error) {
var result baz.SimpleService_SimpleFuture_Result

retryOpts := &tchannel.RetryOptions{
TimeoutPerAttempt: c.timeoutPerAttempt,
}
ctx, cancel := tchannel.NewContextBuilder(c.timeout).
SetParentContext(ctx).
SetRetryOptions(retryOpts).
Build()
defer cancel()

args := baz.SimpleService_SimpleFuture_Args{}
respHeaders, success, err := c.client.Call(ctx, c.thriftService, "SimpleFuture", reqHeaders, &args, &result)
if err == nil && !success {
Expand All @@ -99,117 +135,3 @@ func (c *BazClient) SimpleFuture(ctx context.Context, reqHeaders map[string]stri

return respHeaders, err
}

// SimpleServiceServer is the Baz backend service
type SimpleServiceServer struct {
handler TChanBaz
}

// NewSimpleServiceServer wraps a handler for Baz so it can be registered with a thrift server.
func NewSimpleServiceServer(handler TChanBaz) zt.TChanServer {
return &SimpleServiceServer{
handler,
}
}

// Service returns the service name.
func (s *SimpleServiceServer) Service() string {
return "SimpleService"
}

// Methods returns the method names handled by this server.
func (s *SimpleServiceServer) Methods() []string {
return []string{
"Call",
"Simple",
"SimpleFuture",
}
}

// Handle dispatches a method call to corresponding handler.
func (s *SimpleServiceServer) Handle(ctx context.Context, methodName string, reqHeaders map[string]string, wireValue *wire.Value) (bool, map[string]string, zt.RWTStruct, error) {
switch methodName {
case "Call":
return s.handleCall(ctx, reqHeaders, wireValue)
case "Simple":
return s.handleSimple(ctx, reqHeaders, wireValue)
case "SimpleFuture":
return s.handleSimpleFuture(ctx, reqHeaders, wireValue)

default:
return false, nil, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}

func (s *SimpleServiceServer) handleCall(ctx context.Context, reqHeaders map[string]string, wireValue *wire.Value) (bool, map[string]string, zt.RWTStruct, error) {
var req baz.SimpleService_Call_Args
var res baz.SimpleService_Call_Result

if err := req.FromWire(*wireValue); err != nil {
return false, nil, nil, err
}

respHeaders, r, err := s.handler.Call(ctx, reqHeaders, &req)

if err != nil {
return false, nil, nil, err
}

res.Success = r
return true, respHeaders, &res, nil
}

func (s *SimpleServiceServer) handleSimple(ctx context.Context, reqHeaders map[string]string, wireValue *wire.Value) (bool, map[string]string, zt.RWTStruct, error) {
var req baz.SimpleService_Simple_Args
var res baz.SimpleService_Simple_Result

if err := req.FromWire(*wireValue); err != nil {
return false, nil, nil, err
}

respHeaders, err := s.handler.Simple(ctx, reqHeaders)

if err != nil {
switch v := err.(type) {
case *baz.SimpleErr:
if v == nil {
return false, nil, nil, errors.New("Handler for simpleErr returned non-nil error type *SimpleErr but nil value")
}
res.SimpleErr = v
default:
return false, nil, nil, err
}
}

return err == nil, respHeaders, &res, nil
}

func (s *SimpleServiceServer) handleSimpleFuture(ctx context.Context, reqHeaders map[string]string, wireValue *wire.Value) (bool, map[string]string, zt.RWTStruct, error) {
var req baz.SimpleService_SimpleFuture_Args
var res baz.SimpleService_SimpleFuture_Result

if err := req.FromWire(*wireValue); err != nil {
return false, nil, nil, err
}

respHeaders, err := s.handler.SimpleFuture(ctx, reqHeaders)

if err != nil {
switch v := err.(type) {
case *baz.SimpleErr:
if v == nil {
return false, nil, nil, errors.New("Handler for simpleErr returned non-nil error type *SimpleErr but nil value")
}
res.SimpleErr = v
case *baz.NewErr:
if v == nil {
return false, nil, nil, errors.New("Handler for newErr returned non-nil error type *NewErr_ but nil value")
}
res.NewErr = v
default:
return false, nil, nil, err
}
}

return err == nil, respHeaders, &res, nil
}

0 comments on commit e197c4f

Please sign in to comment.