| | |
| | | |
| | | import java.time.Duration; |
| | | import java.util.ArrayList; |
| | | import java.util.Comparator; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | |
| | | |
| | | private volatile List<LlmRouteConfig> allRouteCache = Collections.emptyList(); |
| | | private volatile long cacheExpireAt = 0L; |
| | | private static final Comparator<LlmRouteConfig> ROUTE_ORDER = (a, b) -> { |
| | | int pa = a == null || a.getPriority() == null ? Integer.MAX_VALUE : a.getPriority(); |
| | | int pb = b == null || b.getPriority() == null ? Integer.MAX_VALUE : b.getPriority(); |
| | | if (pa != pb) return Integer.compare(pa, pb); |
| | | long ia = a == null || a.getId() == null ? Long.MAX_VALUE : a.getId(); |
| | | long ib = b == null || b.getId() == null ? Long.MAX_VALUE : b.getId(); |
| | | return Long.compare(ia, ib); |
| | | }; |
| | | |
| | | public void evictCache() { |
| | | cacheExpireAt = 0L; |
| | |
| | | } |
| | | if (result.isEmpty() && !coolingRoutes.isEmpty()) { |
| | | // 避免所有路由都处于冷却时系统完全不可用,降级允许使用冷却路由 |
| | | coolingRoutes.sort(ROUTE_ORDER); |
| | | log.warn("LLM 路由均处于冷却,降级启用冷却路由。cooling={}, total={}", coolingRoutes.size(), total); |
| | | return coolingRoutes; |
| | | } |
| | | result.sort(ROUTE_ORDER); |
| | | if (result.isEmpty()) { |
| | | log.warn("未找到可用 LLM 路由。total={}, disabled={}, invalid={}", total, disabled, invalid); |
| | | } |
| | |
| | | EntityWrapper<LlmRouteConfig> wrapper = new EntityWrapper<>(); |
| | | wrapper.orderBy("priority", true).orderBy("id", true); |
| | | List<LlmRouteConfig> list = llmRouteConfigService.selectList(wrapper); |
| | | allRouteCache = list == null ? Collections.emptyList() : list; |
| | | if (list == null) { |
| | | allRouteCache = Collections.emptyList(); |
| | | } else { |
| | | list.sort(ROUTE_ORDER); |
| | | allRouteCache = list; |
| | | } |
| | | cacheExpireAt = System.currentTimeMillis() + CACHE_TTL_MS; |
| | | return allRouteCache; |
| | | } |