Skip to content

Commit

Permalink
add get actual method name function
Browse files Browse the repository at this point in the history
  • Loading branch information
luky116 committed Dec 19, 2021
1 parent 2924a2a commit 2a61862
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 19 deletions.
3 changes: 1 addition & 2 deletions cluster/cluster/base/cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,9 @@ func isInvoked(selectedInvoker protocol.Invoker, invoked []protocol.Invoker) boo
return false
}

func GetLoadBalance(invoker protocol.Invoker, invocation protocol.Invocation) loadbalance.LoadBalance {
func GetLoadBalance(invoker protocol.Invoker, methodName string) loadbalance.LoadBalance {
url := invoker.GetURL()

methodName := invocation.MethodName()
// Get the service loadbalance config
lb := url.GetParam(constant.LoadbalanceKey, constant.DefaultLoadBalance)

Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster/failfast/cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (invoker *failfastClusterInvoker) Invoke(ctx context.Context, invocation pr
return &protocol.RPCResult{Err: err}
}

loadbalance := base.GetLoadBalance(invokers[0], invocation)
loadbalance := base.GetLoadBalance(invokers[0], invocation.ActualMethodName())

err = invoker.CheckWhetherDestroyed()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cluster/cluster/failover/cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func (invoker *failoverClusterInvoker) Invoke(ctx context.Context, invocation pr
return &protocol.RPCResult{Err: err}
}

methodName := invocation.MethodName()
methodName := invocation.ActualMethodName()
retries := getRetries(invokers, methodName)
loadBalance := base.GetLoadBalance(invokers[0], invocation)
loadBalance := base.GetLoadBalance(invokers[0], methodName)

for i := 0; i <= retries; i++ {
// Reselect before retry to avoid a change of candidate `invokers`.
Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster/forking/cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (invoker *forkingClusterInvoker) Invoke(ctx context.Context, invocation pro
if forks < 0 || forks > len(invokers) {
selected = invokers
} else {
loadBalance := base.GetLoadBalance(invokers[0], invocation)
loadBalance := base.GetLoadBalance(invokers[0], invocation.ActualMethodName())
for i := 0; i < forks; i++ {
if ivk := invoker.DoSelect(loadBalance, invocation, invokers, selected); ivk != nil {
selected = append(selected, ivk)
Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster/zoneaware/cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (invoker *zoneawareClusterInvoker) Invoke(ctx context.Context, invocation p
}

// load balance among all registries, with registry weight count in.
loadBalance := base.GetLoadBalance(invokers[0], invocation)
loadBalance := base.GetLoadBalance(invokers[0], invocation.ActualMethodName())
ivk := invoker.DoSelect(loadBalance, invocation, invokers, nil)
if ivk != nil && ivk.IsAvailable() {
return ivk.Invoke(ctx, invocation)
Expand Down
4 changes: 2 additions & 2 deletions filter/generic/service_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newGenericServiceFilter() filter.Filter {
return serviceGeneric
}
func (f *genericServiceFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
if !isGenericInvocation(invocation) {
if !invocation.IsGenericInvocation() {
return invoker.Invoke(ctx, invocation)
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func (f *genericServiceFilter) Invoke(ctx context.Context, invoker protocol.Invo
}

func (f *genericServiceFilter) OnResponse(_ context.Context, result protocol.Result, _ protocol.Invoker, invocation protocol.Invocation) protocol.Result {
if isGenericInvocation(invocation) && result.Result() != nil {
if invocation.IsGenericInvocation() && result.Result() != nil {
// get generic info from attachments of invocation, the default value is "true"
generic := invocation.AttachmentsByKey(constant.GenericKey, constant.GenericSerializationDefault)
// get generalizer according to value in the `generic`
Expand Down
12 changes: 2 additions & 10 deletions filter/generic/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
// isCallingToGenericService check if it calls to a generic service
func isCallingToGenericService(invoker protocol.Invoker, invocation protocol.Invocation) bool {
return isGeneric(invoker.GetURL().GetParam(constant.GenericKey, "")) &&
(invocation.MethodName() == constant.Generic ||
invocation.MethodName() == constant.GenericAsync)
invocation.MethodName() != constant.Generic &&
invocation.MethodName() != constant.GenericAsync
}

// isMakingAGenericCall check if it is making a generic call to a generic service
Expand All @@ -50,14 +50,6 @@ func isGeneric(generic string) bool {
return lowerGeneric == constant.GenericSerializationDefault
}

// isGenericInvocation determines if the invocation has generic format
func isGenericInvocation(invocation protocol.Invocation) bool {
return (invocation.MethodName() == constant.Generic ||
invocation.MethodName() == constant.GenericAsync) &&
invocation.Arguments() != nil &&
len(invocation.Arguments()) == 3
}

// toUnexport is to lower the first letter
func toUnexport(a string) string {
return strings.ToLower(a[:1]) + a[1:]
Expand Down
4 changes: 4 additions & 0 deletions protocol/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
type Invocation interface {
// MethodName gets invocation method name.
MethodName() string
// ActualMethodName gets actual invocation method name. It returns the method name been called if it's a generic call
ActualMethodName() string
// ParameterTypeNames gets invocation parameter type names.
ParameterTypeNames() []string
// ParameterTypes gets invocation parameter types.
Expand All @@ -48,4 +50,6 @@ type Invocation interface {
SetAttachments(key string, value interface{})
// Invoker gets the invoker in current context.
Invoker() Invoker
// IsGenericInvocation gets if this is a generic invocation
IsGenericInvocation() bool
}
17 changes: 17 additions & 0 deletions protocol/invocation/rpcinvocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ func (r *RPCInvocation) MethodName() string {
return r.methodName
}

// ActualMethodName gets actual invocation method name. It returns the method name been called if it's a generic call
func (r *RPCInvocation) ActualMethodName() string {
if r.IsGenericInvocation() {
return r.Arguments()[0].(string)
} else {
return r.MethodName()
}
}

// IsGenericInvocation gets if this is a generic invocation
func (r *RPCInvocation) IsGenericInvocation() bool {
return (r.MethodName() == constant.Generic ||
r.MethodName() == constant.GenericAsync) &&
r.Arguments() != nil &&
len(r.Arguments()) == 3
}

// ParameterTypes gets RPC invocation parameter types.
func (r *RPCInvocation) ParameterTypes() []reflect.Type {
return r.parameterTypes
Expand Down

0 comments on commit 2a61862

Please sign in to comment.