Skip to content

Commit

Permalink
adding routing delegate in dynamic channel support for routing throug…
Browse files Browse the repository at this point in the history
…h sidecar (#658)
  • Loading branch information
abhishekparwal committed Oct 22, 2019
1 parent 6d3b183 commit fc98106
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
10 changes: 7 additions & 3 deletions codegen/template_bundle/template_files.go

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

8 changes: 6 additions & 2 deletions codegen/templates/tchannel_client.tmpl
Expand Up @@ -162,9 +162,13 @@ func initializeDynamicChannel(deps *module.Dependencies, headerPatterns []string

ruleWrapper := ruleengine.RuleWrapper{}
for _, routingConfig := range alternateServiceDetail.RoutingConfigs {
ruleValue := []string{routingConfig.ServiceName}
rd := routingConfig.RoutingDelegate
if rd != nil {
ruleValue = append(ruleValue, *rd)
}
rawRule := ruleengine.RawRule{Patterns: []string{textproto.CanonicalMIMEHeaderKey(routingConfig.HeaderName),
strings.ToLower(routingConfig.HeaderValue)},
Value: routingConfig.ServiceName}
strings.ToLower(routingConfig.HeaderValue)}, Value: ruleValue}
headerPatterns = append(headerPatterns, textproto.CanonicalMIMEHeaderKey(routingConfig.HeaderName))
ruleWrapper.Rules = append(ruleWrapper.Rules, rawRule)

Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Expand Up @@ -33,9 +33,10 @@ type EnvConfig map[string]struct {
}

type RoutingConfig struct {
HeaderName string `yaml:"headerName" json:"headerName"`
HeaderValue string `yaml:"headerValue" json:"headerValue"`
ServiceName string `yaml:"serviceName" json:"serviceName"`
HeaderName string `yaml:"headerName" json:"headerName"`
HeaderValue string `yaml:"headerValue" json:"headerValue"`
ServiceName string `yaml:"serviceName" json:"serviceName"`
RoutingDelegate *string `yaml:"rd,omitempty" json:"rd,omitempty"`
}

type ServiceRouting struct {
Expand Down
8 changes: 6 additions & 2 deletions examples/example-gateway/build/clients/baz/baz.go

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

8 changes: 6 additions & 2 deletions examples/example-gateway/build/clients/corge/corge.go

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

1 change: 1 addition & 0 deletions examples/example-gateway/config/production.yaml
Expand Up @@ -72,6 +72,7 @@ clients.baz.alternates:
- headerName: x-container
headerValue: ^sandbox$
serviceName: basicSandbox
rd: reverse-proxy
- headerName: x-test-env
headerValue: ^sandbox$
serviceName: nomatch
Expand Down
17 changes: 10 additions & 7 deletions runtime/tchannel_client.go
Expand Up @@ -224,7 +224,7 @@ func (c *TChannelClient) call(
err = c.ch.RunWithRetry(ctx, func(ctx netContext.Context, rs *tchannel.RequestState) (cerr error) {
call.resHeaders, call.success = nil, false

sc := c.getDynamicChannelWithFallback(reqHeaders, c.sc)
sc, ctx := c.getDynamicChannelWithFallback(reqHeaders, c.sc, ctx)
call.call, cerr = sc.BeginCall(ctx, call.serviceMethod, &tchannel.CallOptions{
Format: tchannel.Thrift,
ShardKey: GetShardKeyFromCtx(ctx),
Expand Down Expand Up @@ -275,10 +275,10 @@ func (c *TChannelClient) call(

// first rule match, would be the choosen channel. if nothing matches fallback to default channel
func (c *TChannelClient) getDynamicChannelWithFallback(reqHeaders map[string]string,
sc *tchannel.SubChannel) *tchannel.SubChannel {
sc *tchannel.SubChannel, ctx netContext.Context) (*tchannel.SubChannel, netContext.Context) {
ch := sc
if c.ruleEngine == nil {
return ch
return ch, ctx
}
for _, headerPattern := range c.headerPatterns {
// this header is not present, so can't match a rule
Expand All @@ -291,11 +291,14 @@ func (c *TChannelClient) getDynamicChannelWithFallback(reqHeaders map[string]str
if !match {
continue
}
serviceName := val.(string)
serviceDetails := val.([]string)
// we know service has a channel, as this was constructed in c'tor
ch = c.altChannelMap[serviceName]
return ch
ch = c.altChannelMap[serviceDetails[0]]
if len(serviceDetails) > 1 {
ctx = WithRoutingDelegate(ctx, serviceDetails[1])
}
return ch, ctx
}
// if nothing matches return the default channel/**/
return ch
return ch, ctx
}

0 comments on commit fc98106

Please sign in to comment.