Line data Source code
1 : import { useAlgoliaSearchProvider } from '@/services/providers/algoliaSearchProvider'
2 : import { useComfyRegistrySearchProvider } from '@/services/providers/registrySearchProvider'
3 : import type { SearchNodePacksParams } from '@/types/algoliaTypes'
4 : import type {
5 : NodePackSearchProvider,
6 : SearchPacksOutcome
7 : } from '@/types/searchServiceTypes'
8 : import { paramsToCacheKey } from '@/utils/formatUtil'
9 : import { toError } from '@comfyorg/shared-frontend-utils/errorUtil'
10 :
11 : interface ProviderState {
12 : provider: NodePackSearchProvider
13 : name: string
14 : isHealthy: boolean
15 : lastError?: Error
16 : lastAttempt?: Date
17 : consecutiveFailures: number
18 : }
19 :
20 : interface ListProvider {
21 : listKey: string
22 : providerState: ProviderState
23 : }
24 :
25 : export interface RegistrySearchGateway extends NodePackSearchProvider {
26 : /** Closes every provider's circuit so the next search tries them all again. */
27 : resetCircuits(): void
28 : }
29 :
30 2355 : const CIRCUIT_BREAKER_THRESHOLD = 3
31 2355 : const CIRCUIT_BREAKER_TIMEOUT_MS = 60_000
32 :
33 2355 : const toListKey = (
34 : query: string,
35 : { pageSize, restrictSearchableAttributes }: SearchNodePacksParams
36 : ) => paramsToCacheKey({ query, pageSize, restrictSearchableAttributes })
37 :
38 : /**
39 : * Searches node packs through the first available provider, failing over on
40 : * errors, with a circuit breaker per provider.
41 : */
42 2355 : export const useRegistrySearchGateway = (): RegistrySearchGateway => {
43 15 : const providers: ProviderState[] = []
44 15 : let activeProviderIndex = 0
45 15 : let firstPageProvider: ListProvider | undefined
46 :
47 15 : try {
48 15 : providers.push({
49 : provider: useAlgoliaSearchProvider(),
50 : name: 'Algolia',
51 : isHealthy: true,
52 : consecutiveFailures: 0
53 : })
54 : } catch (error) {
55 15 : console.warn('Failed to initialize Algolia provider:', error)
56 : }
57 :
58 15 : providers.push({
59 : provider: useComfyRegistrySearchProvider(),
60 : name: 'ComfyRegistry',
61 : isHealthy: true,
62 : consecutiveFailures: 0
63 : })
64 :
65 15 : const isCircuitClosed = (providerState: ProviderState): boolean => {
66 20 : if (providerState.consecutiveFailures < CIRCUIT_BREAKER_THRESHOLD) {
67 20 : return true
68 : }
69 :
70 0 : if (providerState.lastAttempt) {
71 : const timeSinceLastAttempt =
72 0 : Date.now() - providerState.lastAttempt.getTime()
73 : if (timeSinceLastAttempt > CIRCUIT_BREAKER_TIMEOUT_MS) {
74 0 : return true
75 : }
76 : }
77 :
78 0 : return false
79 : }
80 :
81 15 : const recordSuccess = (providerState: ProviderState) => {
82 20 : providerState.isHealthy = true
83 20 : providerState.consecutiveFailures = 0
84 20 : providerState.lastError = undefined
85 : }
86 :
87 15 : const recordFailure = (providerState: ProviderState, error: Error) => {
88 1 : providerState.consecutiveFailures++
89 1 : providerState.lastError = error
90 1 : providerState.lastAttempt = new Date()
91 :
92 1 : if (providerState.consecutiveFailures >= CIRCUIT_BREAKER_THRESHOLD) {
93 0 : providerState.isHealthy = false
94 0 : console.warn(
95 : `${providerState.name} provider circuit breaker opened after ${providerState.consecutiveFailures} failures`
96 : )
97 : }
98 : }
99 :
100 15 : const findUntriedProvider = (
101 : skipped: ReadonlySet<ProviderState>
102 : ): ProviderState | undefined => {
103 21 : const isAvailable = (providerState: ProviderState) =>
104 : !skipped.has(providerState) && isCircuitClosed(providerState)
105 21 : if (isAvailable(providers[activeProviderIndex])) {
106 20 : return providers[activeProviderIndex]
107 : }
108 1 : const index = providers.findIndex(isAvailable)
109 1 : if (index === -1) return undefined
110 0 : activeProviderIndex = index
111 0 : return providers[index]
112 : }
113 :
114 15 : const updateActiveProviderOnFailure = () => {
115 1 : if (activeProviderIndex < providers.length - 1) {
116 0 : activeProviderIndex++
117 : }
118 : }
119 :
120 15 : const providersOtherThanListProvider = (
121 : listKey: string,
122 : pageNumber: number
123 : ): Set<ProviderState> => {
124 20 : const listProvider =
125 : pageNumber > 0 && firstPageProvider?.listKey === listKey
126 : ? firstPageProvider.providerState
127 : : undefined
128 20 : return new Set(
129 : listProvider ? providers.filter((p) => p !== listProvider) : []
130 : )
131 : }
132 :
133 15 : const searchPacks = async (
134 : query: string,
135 : params: SearchNodePacksParams
136 : ): Promise<SearchPacksOutcome> => {
137 20 : const providerErrors: Error[] = []
138 20 : const listKey = toListKey(query, params)
139 20 : const skipped = providersOtherThanListProvider(listKey, params.pageNumber)
140 :
141 20 : for (
142 : let providerState = findUntriedProvider(skipped);
143 : providerState;
144 : providerState = findUntriedProvider(skipped)
145 : ) {
146 20 : skipped.add(providerState)
147 20 : const outcome = await providerState.provider.searchPacks(query, params)
148 20 : if (outcome.ok) {
149 19 : recordSuccess(providerState)
150 19 : if (params.pageNumber === 0) {
151 18 : firstPageProvider = { listKey, providerState }
152 : }
153 19 : return outcome
154 : }
155 1 : const error = toError(outcome.cause)
156 1 : providerErrors.push(error)
157 1 : recordFailure(providerState, error)
158 1 : console.warn(
159 : `${providerState.name} search provider failed (${providerState.consecutiveFailures} failures):`,
160 : outcome.cause
161 : )
162 1 : updateActiveProviderOnFailure()
163 : }
164 :
165 1 : const lastError = providerErrors.at(-1)
166 20 : return {
167 : ok: false,
168 : cause: new AggregateError(
169 : providerErrors,
170 : lastError
171 : ? `All search providers failed. Last error: ${lastError.message}`
172 : : 'No available search providers'
173 : )
174 : }
175 : }
176 :
177 15 : const resetCircuits = () => {
178 1 : for (const providerState of providers) recordSuccess(providerState)
179 1 : activeProviderIndex = 0
180 : }
181 :
182 15 : const clearSearchCache = () => {
183 0 : for (const providerState of providers) {
184 0 : try {
185 0 : providerState.provider.clearSearchCache()
186 : } catch (error) {
187 0 : console.warn(
188 : `Failed to clear cache for ${providerState.name} provider:`,
189 : error
190 : )
191 : }
192 : }
193 : }
194 :
195 15 : return {
196 : searchPacks,
197 : clearSearchCache,
198 : resetCircuits
199 : }
200 : }
|