Skip to content

Commit bfa27f3

Browse files
authored
fix(server): support unique method lookup when service name lookup fails (cloudwego#1873)
1 parent 5c6c29d commit bfa27f3

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

server/service.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,8 @@ func (s *services) SearchService(svcName, methodName string, strict bool, codecT
276276
if svc := s.knownSvcMap[svcName]; svc != nil {
277277
return svc.svcInfo
278278
}
279-
if svcName == serviceinfo.GenericService || svcName == serviceinfo.CombineServiceName {
280-
// Maybe combine or generic service name,
281-
// because Kitex client will write these two service name if the version is between v0.9.0-v0.9.1
282-
// TODO: remove this logic if this version range is converged
283-
if svcInfo := s.searchByMethodName(methodName); svcInfo != nil {
284-
return svcInfo
285-
}
279+
if svcInfo := s.searchUniqueByMethodName(methodName); svcInfo != nil {
280+
return svcInfo
286281
}
287282
}
288283
}
@@ -292,6 +287,28 @@ func (s *services) SearchService(svcName, methodName string, strict bool, codecT
292287
return nil
293288
}
294289

290+
// SearchUniqueByMethodName searches for a unique service by method name.
291+
// It returns nil if there are multiple services with the same method name.
292+
func (s *services) searchUniqueByMethodName(methodName string) (unique *serviceinfo.ServiceInfo) {
293+
if s.fallbackSvc != nil {
294+
// check whether fallback service has the method
295+
if mi := s.fallbackSvc.svcInfo.MethodInfo(notAllowBinaryGenericCtx, methodName); mi != nil {
296+
unique = s.fallbackSvc.svcInfo
297+
}
298+
}
299+
// match other services
300+
for _, svc := range s.nonFallbackSvcs {
301+
if mi := svc.svcInfo.MethodInfo(notAllowBinaryGenericCtx, methodName); mi != nil {
302+
if unique == nil {
303+
unique = svc.svcInfo
304+
} else {
305+
return nil
306+
}
307+
}
308+
}
309+
return unique
310+
}
311+
295312
// getTargetSvcInfo returns the service info if there is only one service registered.
296313
func (s *services) getTargetSvcInfo() *serviceinfo.ServiceInfo {
297314
if len(s.knownSvcMap) != 1 {

server/service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func TestSearchService(t *testing.T) {
156156
},
157157
serviceName: serviceinfo.GenericService,
158158
methodName: mocks.MockMethod,
159-
expectSvcInfo: mocks.ServiceInfo(),
159+
expectSvcInfo: nil,
160160
},
161161
{
162162
svcs: []svc{
@@ -170,7 +170,7 @@ func TestSearchService(t *testing.T) {
170170
},
171171
serviceName: "xxxxxx",
172172
methodName: mocks.MockExceptionMethod,
173-
expectSvcInfo: nil,
173+
expectSvcInfo: mocks.ServiceInfo(),
174174
},
175175
{
176176
svcs: []svc{

0 commit comments

Comments
 (0)