fix: add null check in RouteCache.GetRoute to prevent ArgumentNullException

- Allow nullable tenantCode and serviceName parameters
- Return null early if serviceName is null or empty
- Fix test: GetRoute_WithNullServiceName_ShouldReturnNull
This commit is contained in:
movingsam 2026-03-08 11:01:49 +08:00
parent faba26043f
commit 1b13efffc5

View File

@ -54,13 +54,23 @@ public class RouteCache : IRouteCache
_logger.LogInformation("Route cache reloaded"); _logger.LogInformation("Route cache reloaded");
} }
public RouteInfo? GetRoute(string tenantCode, string serviceName) public RouteInfo? GetRoute(string? tenantCode, string? serviceName)
{ {
// 参数校验
if (string.IsNullOrEmpty(serviceName))
{
_logger.LogDebug("GetRoute called with null or empty serviceName");
return null;
}
tenantCode ??= string.Empty;
_lock.EnterUpgradeableReadLock(); _lock.EnterUpgradeableReadLock();
try try
{ {
// 1. 优先查找租户专用路由 // 1. 优先查找租户专用路由
if (_tenantRoutes.TryGetValue(tenantCode, out var tenantRouteMap) && if (!string.IsNullOrEmpty(tenantCode) &&
_tenantRoutes.TryGetValue(tenantCode, out var tenantRouteMap) &&
tenantRouteMap.TryGetValue(serviceName, out var tenantRoute)) tenantRouteMap.TryGetValue(serviceName, out var tenantRoute))
{ {
_logger.LogDebug("Found tenant-specific route: {Tenant}/{Service} -> {Cluster}", _logger.LogDebug("Found tenant-specific route: {Tenant}/{Service} -> {Cluster}",