feat: implement advanced settings for user preferences, allowing configuration of max concurrent requests, reader prefetch count, and circuit breaker settings
This commit is contained in:
@@ -16,8 +16,10 @@ import { NavigationBar } from "./components/NavigationBar";
|
||||
import { EndOfSeriesModal } from "./components/EndOfSeriesModal";
|
||||
import { PageDisplay } from "./components/PageDisplay";
|
||||
import { ReaderContainer } from "./components/ReaderContainer";
|
||||
import { usePreferences } from "@/contexts/PreferencesContext";
|
||||
|
||||
export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderProps) {
|
||||
const { preferences } = usePreferences();
|
||||
const [showControls, setShowControls] = useState(false);
|
||||
const [showThumbnails, setShowThumbnails] = useState(false);
|
||||
const lastClickTimeRef = useRef<number>(0);
|
||||
@@ -30,7 +32,7 @@ export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderP
|
||||
const { loadedImages, imageBlobUrls, prefetchPages, prefetchNextBook, handleForceReload, getPageUrl, prefetchCount } = useImageLoader({
|
||||
bookId: book.id,
|
||||
pages,
|
||||
prefetchCount: 5,
|
||||
prefetchCount: preferences.readerPrefetchCount,
|
||||
nextBook: nextBook ? { id: nextBook.id, pages: [] } : null
|
||||
});
|
||||
const { currentPage, showEndMessage, navigateToPage, handlePreviousPage, handleNextPage } = usePageNavigation({
|
||||
|
||||
@@ -10,11 +10,11 @@ type ImageKey = number | string; // Support both numeric pages and prefixed keys
|
||||
interface UseImageLoaderProps {
|
||||
bookId: string;
|
||||
pages: number[];
|
||||
prefetchCount?: number; // Nombre de pages à précharger (défaut: 2)
|
||||
prefetchCount?: number; // Nombre de pages à précharger (défaut: 5)
|
||||
nextBook?: { id: string; pages: number[] } | null; // Livre suivant pour prefetch
|
||||
}
|
||||
|
||||
export function useImageLoader({ bookId, pages: _pages, prefetchCount = 2, nextBook }: UseImageLoaderProps) {
|
||||
export function useImageLoader({ bookId, pages: _pages, prefetchCount = 5, nextBook }: UseImageLoaderProps) {
|
||||
const [loadedImages, setLoadedImages] = useState<Record<ImageKey, ImageDimensions>>({});
|
||||
const [imageBlobUrls, setImageBlobUrls] = useState<Record<ImageKey, string>>({});
|
||||
const loadedImagesRef = useRef(loadedImages);
|
||||
|
||||
325
src/components/settings/AdvancedSettings.tsx
Normal file
325
src/components/settings/AdvancedSettings.tsx
Normal file
@@ -0,0 +1,325 @@
|
||||
import { useTranslate } from "@/hooks/useTranslate";
|
||||
import { usePreferences } from "@/contexts/PreferencesContext";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card";
|
||||
import { Activity, ImageIcon, Shield, Info } from "lucide-react";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
|
||||
export function AdvancedSettings() {
|
||||
const { t } = useTranslate();
|
||||
const { toast } = useToast();
|
||||
const { preferences, updatePreferences } = usePreferences();
|
||||
|
||||
const handleMaxConcurrentChange = async (value: number) => {
|
||||
try {
|
||||
await updatePreferences({
|
||||
komgaMaxConcurrentRequests: value,
|
||||
});
|
||||
toast({
|
||||
title: t("settings.title"),
|
||||
description: t("settings.komga.messages.configSaved"),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur:", error);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: t("settings.error.title"),
|
||||
description: t("settings.error.message"),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handlePrefetchChange = async (value: number) => {
|
||||
try {
|
||||
await updatePreferences({
|
||||
readerPrefetchCount: value,
|
||||
});
|
||||
toast({
|
||||
title: t("settings.title"),
|
||||
description: t("settings.komga.messages.configSaved"),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur:", error);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: t("settings.error.title"),
|
||||
description: t("settings.error.message"),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleCircuitBreakerChange = async (field: string, value: number) => {
|
||||
try {
|
||||
await updatePreferences({
|
||||
circuitBreakerConfig: {
|
||||
...preferences.circuitBreakerConfig,
|
||||
[field]: value,
|
||||
},
|
||||
});
|
||||
toast({
|
||||
title: t("settings.title"),
|
||||
description: t("settings.komga.messages.configSaved"),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur:", error);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: t("settings.error.title"),
|
||||
description: t("settings.error.message"),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Performance Settings */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<Activity className="h-5 w-5 text-primary" />
|
||||
<CardTitle className="text-lg">Performance</CardTitle>
|
||||
</div>
|
||||
<CardDescription>
|
||||
Optimisez les performances et la réactivité de l'application
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
{/* Concurrent Requests */}
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="space-y-1 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<Label htmlFor="maxConcurrentRequests" className="text-base">
|
||||
{t("settings.advanced.maxConcurrentRequests.label")}
|
||||
</Label>
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{preferences.komgaMaxConcurrentRequests}
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t("settings.advanced.maxConcurrentRequests.description")}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
<Input
|
||||
id="maxConcurrentRequests"
|
||||
type="range"
|
||||
min="1"
|
||||
max="10"
|
||||
value={preferences.komgaMaxConcurrentRequests}
|
||||
onChange={(e) => handleMaxConcurrentChange(parseInt(e.target.value))}
|
||||
className="flex-1 cursor-pointer"
|
||||
/>
|
||||
<Input
|
||||
type="number"
|
||||
min="1"
|
||||
max="10"
|
||||
value={preferences.komgaMaxConcurrentRequests}
|
||||
onChange={(e) => handleMaxConcurrentChange(parseInt(e.target.value) || 1)}
|
||||
className="w-20"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="border-t" />
|
||||
|
||||
{/* Reader Prefetch Count */}
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="space-y-1 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<ImageIcon className="h-4 w-4 text-muted-foreground" />
|
||||
<Label htmlFor="prefetchCount" className="text-base">
|
||||
{t("settings.advanced.prefetchCount.label")}
|
||||
</Label>
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{preferences.readerPrefetchCount}
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t("settings.advanced.prefetchCount.description")}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
<Input
|
||||
id="prefetchCount"
|
||||
type="range"
|
||||
min="0"
|
||||
max="20"
|
||||
value={preferences.readerPrefetchCount}
|
||||
onChange={(e) => handlePrefetchChange(parseInt(e.target.value))}
|
||||
className="flex-1 cursor-pointer"
|
||||
/>
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
max="20"
|
||||
value={preferences.readerPrefetchCount}
|
||||
onChange={(e) => handlePrefetchChange(parseInt(e.target.value) || 0)}
|
||||
className="w-20"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Circuit Breaker Configuration */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<Shield className="h-5 w-5 text-primary" />
|
||||
<CardTitle className="text-lg">{t("settings.advanced.circuitBreaker.title")}</CardTitle>
|
||||
</div>
|
||||
<CardDescription>
|
||||
{t("settings.advanced.circuitBreaker.description")}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
{/* Threshold */}
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="space-y-1 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<Label htmlFor="cbThreshold" className="text-base">
|
||||
{t("settings.advanced.circuitBreaker.threshold.label")}
|
||||
</Label>
|
||||
<Badge variant="destructive" className="text-xs">
|
||||
{preferences.circuitBreakerConfig.threshold}
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t("settings.advanced.circuitBreaker.threshold.description")}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
<Input
|
||||
id="cbThreshold"
|
||||
type="range"
|
||||
min="1"
|
||||
max="20"
|
||||
value={preferences.circuitBreakerConfig.threshold}
|
||||
onChange={(e) =>
|
||||
handleCircuitBreakerChange("threshold", parseInt(e.target.value))
|
||||
}
|
||||
className="flex-1 cursor-pointer"
|
||||
/>
|
||||
<Input
|
||||
type="number"
|
||||
min="1"
|
||||
max="20"
|
||||
value={preferences.circuitBreakerConfig.threshold}
|
||||
onChange={(e) =>
|
||||
handleCircuitBreakerChange("threshold", parseInt(e.target.value) || 5)
|
||||
}
|
||||
className="w-20"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="border-t" />
|
||||
|
||||
{/* Timeout */}
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="space-y-1 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<Label htmlFor="cbTimeout" className="text-base">
|
||||
{t("settings.advanced.circuitBreaker.timeout.label")}
|
||||
</Label>
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{(preferences.circuitBreakerConfig.timeout ?? 30000) / 1000}s
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t("settings.advanced.circuitBreaker.timeout.description")}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
<Input
|
||||
id="cbTimeout"
|
||||
type="range"
|
||||
min="1000"
|
||||
max="120000"
|
||||
step="1000"
|
||||
value={preferences.circuitBreakerConfig.timeout}
|
||||
onChange={(e) =>
|
||||
handleCircuitBreakerChange("timeout", parseInt(e.target.value))
|
||||
}
|
||||
className="flex-1 cursor-pointer"
|
||||
/>
|
||||
<Input
|
||||
type="number"
|
||||
min="1"
|
||||
max="120"
|
||||
value={(preferences.circuitBreakerConfig.timeout ?? 30000) / 1000}
|
||||
onChange={(e) =>
|
||||
handleCircuitBreakerChange("timeout", (parseInt(e.target.value) || 30) * 1000)
|
||||
}
|
||||
className="w-20"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center gap-1 text-xs text-muted-foreground">
|
||||
<Info className="h-3 w-3" />
|
||||
<span>{t("settings.advanced.circuitBreaker.timeout.unit")}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="border-t" />
|
||||
|
||||
{/* Reset Timeout */}
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="space-y-1 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<Label htmlFor="cbResetTimeout" className="text-base">
|
||||
{t("settings.advanced.circuitBreaker.resetTimeout.label")}
|
||||
</Label>
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{(preferences.circuitBreakerConfig.resetTimeout ?? 60000) / 1000}s
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t("settings.advanced.circuitBreaker.resetTimeout.description")}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
<Input
|
||||
id="cbResetTimeout"
|
||||
type="range"
|
||||
min="10000"
|
||||
max="600000"
|
||||
step="1000"
|
||||
value={preferences.circuitBreakerConfig.resetTimeout ?? 60000}
|
||||
onChange={(e) =>
|
||||
handleCircuitBreakerChange("resetTimeout", parseInt(e.target.value))
|
||||
}
|
||||
className="flex-1 cursor-pointer"
|
||||
/>
|
||||
<Input
|
||||
type="number"
|
||||
min="10"
|
||||
max="600"
|
||||
value={(preferences.circuitBreakerConfig.resetTimeout ?? 60000) / 1000}
|
||||
onChange={(e) =>
|
||||
handleCircuitBreakerChange("resetTimeout", (parseInt(e.target.value) || 60) * 1000)
|
||||
}
|
||||
className="w-20"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center gap-1 text-xs text-muted-foreground">
|
||||
<Info className="h-3 w-3" />
|
||||
<span>{t("settings.advanced.circuitBreaker.resetTimeout.unit")}</span>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { DisplaySettings } from "./DisplaySettings";
|
||||
import { KomgaSettings } from "./KomgaSettings";
|
||||
import { CacheSettings } from "./CacheSettings";
|
||||
import { BackgroundSettings } from "./BackgroundSettings";
|
||||
import { AdvancedSettings } from "./AdvancedSettings";
|
||||
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
|
||||
import { Monitor, Network, HardDrive } from "lucide-react";
|
||||
|
||||
@@ -44,6 +45,7 @@ export function ClientSettings({ initialConfig, initialTTLConfig }: ClientSettin
|
||||
|
||||
<TabsContent value="connection" className="mt-6 space-y-6">
|
||||
<KomgaSettings initialConfig={initialConfig} />
|
||||
<AdvancedSettings />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="cache" className="mt-6 space-y-6">
|
||||
|
||||
27
src/components/ui/separator.tsx
Normal file
27
src/components/ui/separator.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import * as React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface SeparatorProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
orientation?: "horizontal" | "vertical";
|
||||
decorative?: boolean;
|
||||
}
|
||||
|
||||
const Separator = React.forwardRef<HTMLDivElement, SeparatorProps>(
|
||||
({ className, orientation = "horizontal", decorative = true, ...props }, ref) => (
|
||||
<div
|
||||
ref={ref}
|
||||
role={decorative ? "none" : "separator"}
|
||||
aria-orientation={orientation}
|
||||
className={cn(
|
||||
"shrink-0 bg-border",
|
||||
orientation === "horizontal" ? "h-[1px] w-full" : "h-full w-[1px]",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
);
|
||||
Separator.displayName = "Separator";
|
||||
|
||||
export { Separator };
|
||||
|
||||
16
src/components/ui/skeleton.tsx
Normal file
16
src/components/ui/skeleton.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
function Skeleton({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) {
|
||||
return (
|
||||
<div
|
||||
className={cn("animate-pulse rounded-md bg-muted", className)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export { Skeleton };
|
||||
|
||||
@@ -102,6 +102,37 @@
|
||||
"save": "Save"
|
||||
}
|
||||
},
|
||||
"advanced": {
|
||||
"title": "Advanced Settings",
|
||||
"description": "Configure advanced performance and reliability settings.",
|
||||
"save": "Save settings",
|
||||
"maxConcurrentRequests": {
|
||||
"label": "Max Concurrent Requests",
|
||||
"description": "Maximum number of simultaneous requests to Komga server (1-10)"
|
||||
},
|
||||
"prefetchCount": {
|
||||
"label": "Reader Prefetch Count",
|
||||
"description": "Number of pages to preload in the reader (0-20)"
|
||||
},
|
||||
"circuitBreaker": {
|
||||
"title": "Circuit Breaker",
|
||||
"description": "Automatic protection against server overload",
|
||||
"threshold": {
|
||||
"label": "Failure Threshold",
|
||||
"description": "Number of consecutive failures before opening the circuit (1-20)"
|
||||
},
|
||||
"timeout": {
|
||||
"label": "Request Timeout",
|
||||
"description": "Maximum wait time for a request before considering it failed",
|
||||
"unit": "milliseconds (1000ms = 1 second)"
|
||||
},
|
||||
"resetTimeout": {
|
||||
"label": "Reset Timeout",
|
||||
"description": "Time to wait before attempting to close the circuit",
|
||||
"unit": "milliseconds (1000ms = 1 second)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"title": "Error",
|
||||
"message": "An error occurred while updating preferences"
|
||||
|
||||
@@ -102,6 +102,37 @@
|
||||
"save": "Enregistrer"
|
||||
}
|
||||
},
|
||||
"advanced": {
|
||||
"title": "Paramètres avancés",
|
||||
"description": "Configurez les paramètres avancés de performance et de fiabilité.",
|
||||
"save": "Enregistrer les paramètres",
|
||||
"maxConcurrentRequests": {
|
||||
"label": "Requêtes simultanées max",
|
||||
"description": "Nombre maximum de requêtes simultanées vers le serveur Komga (1-10)"
|
||||
},
|
||||
"prefetchCount": {
|
||||
"label": "Préchargement du lecteur",
|
||||
"description": "Nombre de pages à précharger dans le lecteur (0-20)"
|
||||
},
|
||||
"circuitBreaker": {
|
||||
"title": "Disjoncteur",
|
||||
"description": "Protection automatique contre la surcharge du serveur",
|
||||
"threshold": {
|
||||
"label": "Seuil d'échec",
|
||||
"description": "Nombre d'échecs consécutifs avant ouverture du circuit (1-20)"
|
||||
},
|
||||
"timeout": {
|
||||
"label": "Délai d'expiration",
|
||||
"description": "Temps d'attente maximum pour une requête avant de la considérer comme échouée",
|
||||
"unit": "millisecondes (1000ms = 1 seconde)"
|
||||
},
|
||||
"resetTimeout": {
|
||||
"label": "Délai de réinitialisation",
|
||||
"description": "Temps d'attente avant de tenter de fermer le circuit",
|
||||
"unit": "millisecondes (1000ms = 1 seconde)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"title": "Erreur",
|
||||
"message": "Une erreur est survenue lors de la mise à jour des préférences"
|
||||
|
||||
@@ -9,6 +9,7 @@ import type { ServerCacheService } from "./server-cache.service";
|
||||
import { RequestMonitorService } from "./request-monitor.service";
|
||||
import { RequestQueueService } from "./request-queue.service";
|
||||
import { CircuitBreakerService } from "./circuit-breaker.service";
|
||||
import { PreferencesService } from "./preferences.service";
|
||||
|
||||
export type { CacheType };
|
||||
|
||||
@@ -23,7 +24,71 @@ interface KomgaUrlBuilder {
|
||||
}
|
||||
|
||||
export abstract class BaseApiService {
|
||||
private static requestQueueInitialized = false;
|
||||
private static circuitBreakerInitialized = false;
|
||||
|
||||
/**
|
||||
* Initialise le RequestQueueService avec les préférences de l'utilisateur
|
||||
*/
|
||||
private static async initializeRequestQueue(): Promise<void> {
|
||||
if (this.requestQueueInitialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Configurer le getter qui récupère dynamiquement la valeur depuis les préférences
|
||||
RequestQueueService.setMaxConcurrentGetter(async () => {
|
||||
try {
|
||||
const preferences = await PreferencesService.getPreferences();
|
||||
return preferences.komgaMaxConcurrentRequests;
|
||||
} catch (error) {
|
||||
console.error('Failed to get preferences for request queue:', error);
|
||||
return 5; // Valeur par défaut
|
||||
}
|
||||
});
|
||||
|
||||
this.requestQueueInitialized = true;
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize request queue:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise le CircuitBreakerService avec les préférences de l'utilisateur
|
||||
*/
|
||||
private static async initializeCircuitBreaker(): Promise<void> {
|
||||
if (this.circuitBreakerInitialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Configurer le getter qui récupère dynamiquement la config depuis les préférences
|
||||
CircuitBreakerService.setConfigGetter(async () => {
|
||||
try {
|
||||
const preferences = await PreferencesService.getPreferences();
|
||||
return preferences.circuitBreakerConfig;
|
||||
} catch (error) {
|
||||
console.error('Failed to get preferences for circuit breaker:', error);
|
||||
return {
|
||||
threshold: 5,
|
||||
timeout: 30000,
|
||||
resetTimeout: 60000,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
this.circuitBreakerInitialized = true;
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize circuit breaker:', error);
|
||||
}
|
||||
}
|
||||
|
||||
protected static async getKomgaConfig(): Promise<AuthConfig> {
|
||||
// Initialiser les services si ce n'est pas déjà fait
|
||||
await Promise.all([
|
||||
this.initializeRequestQueue(),
|
||||
this.initializeCircuitBreaker(),
|
||||
]);
|
||||
try {
|
||||
const config: KomgaConfig | null = await ConfigDBService.getConfig();
|
||||
if (!config) {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
* Circuit Breaker pour éviter de surcharger Komga quand il est défaillant
|
||||
* Évite l'effet avalanche en coupant les requêtes vers un service défaillant
|
||||
*/
|
||||
import type { CircuitBreakerConfig } from "@/types/preferences";
|
||||
|
||||
interface CircuitBreakerState {
|
||||
state: 'CLOSED' | 'OPEN' | 'HALF_OPEN';
|
||||
failureCount: number;
|
||||
@@ -17,13 +19,44 @@ class CircuitBreaker {
|
||||
nextAttemptTime: 0,
|
||||
};
|
||||
|
||||
private readonly config = {
|
||||
private config = {
|
||||
failureThreshold: 5, // Nombre d'échecs avant ouverture
|
||||
recoveryTimeout: 30000, // 30s avant tentative de récupération
|
||||
successThreshold: 3, // Nombre de succès pour fermer le circuit
|
||||
resetTimeout: 60000, // Délai de reset après échec
|
||||
};
|
||||
|
||||
private getConfigFromPreferences: (() => Promise<CircuitBreakerConfig>) | null = null;
|
||||
|
||||
/**
|
||||
* Configure une fonction pour récupérer dynamiquement la config depuis les préférences
|
||||
*/
|
||||
setConfigGetter(getter: () => Promise<CircuitBreakerConfig>): void {
|
||||
this.getConfigFromPreferences = getter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère la config actuelle, soit depuis les préférences, soit depuis les valeurs par défaut
|
||||
*/
|
||||
private async getCurrentConfig(): Promise<typeof this.config> {
|
||||
if (this.getConfigFromPreferences) {
|
||||
try {
|
||||
const prefConfig = await this.getConfigFromPreferences();
|
||||
return {
|
||||
failureThreshold: prefConfig.threshold ?? 5,
|
||||
recoveryTimeout: prefConfig.timeout ?? 30000,
|
||||
resetTimeout: prefConfig.resetTimeout ?? 60000,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting circuit breaker config from preferences:', error);
|
||||
return this.config;
|
||||
}
|
||||
}
|
||||
return this.config;
|
||||
}
|
||||
|
||||
async execute<T>(operation: () => Promise<T>): Promise<T> {
|
||||
const config = await this.getCurrentConfig();
|
||||
|
||||
if (this.state.state === 'OPEN') {
|
||||
if (Date.now() < this.state.nextAttemptTime) {
|
||||
throw new Error('Circuit breaker is OPEN - Komga service unavailable');
|
||||
@@ -36,7 +69,7 @@ class CircuitBreaker {
|
||||
this.onSuccess();
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.onFailure();
|
||||
await this.onFailure(config);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -50,14 +83,14 @@ class CircuitBreaker {
|
||||
}
|
||||
}
|
||||
|
||||
private onFailure(): void {
|
||||
private async onFailure(config: typeof this.config): Promise<void> {
|
||||
this.state.failureCount++;
|
||||
this.state.lastFailureTime = Date.now();
|
||||
|
||||
if (this.state.failureCount >= this.config.failureThreshold) {
|
||||
if (this.state.failureCount >= config.failureThreshold) {
|
||||
this.state.state = 'OPEN';
|
||||
this.state.nextAttemptTime = Date.now() + this.config.recoveryTimeout;
|
||||
console.warn(`[CIRCUIT-BREAKER] 🔴 Circuit OPEN - Komga failing (${this.state.failureCount} failures)`);
|
||||
this.state.nextAttemptTime = Date.now() + config.resetTimeout;
|
||||
console.warn(`[CIRCUIT-BREAKER] 🔴 Circuit OPEN - Komga failing (${this.state.failureCount} failures, reset in ${config.resetTimeout}ms)`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import prisma from "@/lib/prisma";
|
||||
import { getCurrentUser } from "../auth-utils";
|
||||
import { ERROR_CODES } from "../../constants/errorCodes";
|
||||
import { AppError } from "../../utils/errors";
|
||||
import type { UserPreferences, BackgroundPreferences } from "@/types/preferences";
|
||||
import type { UserPreferences, BackgroundPreferences, CircuitBreakerConfig } from "@/types/preferences";
|
||||
import { defaultPreferences } from "@/types/preferences";
|
||||
import type { User } from "@/types/komga";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
@@ -35,6 +35,9 @@ export class PreferencesService {
|
||||
showOnlyUnread: preferences.showOnlyUnread,
|
||||
displayMode: preferences.displayMode as UserPreferences["displayMode"],
|
||||
background: preferences.background as unknown as BackgroundPreferences,
|
||||
komgaMaxConcurrentRequests: preferences.komgaMaxConcurrentRequests,
|
||||
readerPrefetchCount: preferences.readerPrefetchCount,
|
||||
circuitBreakerConfig: preferences.circuitBreakerConfig as unknown as CircuitBreakerConfig,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof AppError) {
|
||||
@@ -55,6 +58,9 @@ export class PreferencesService {
|
||||
if (preferences.showOnlyUnread !== undefined) updateData.showOnlyUnread = preferences.showOnlyUnread;
|
||||
if (preferences.displayMode !== undefined) updateData.displayMode = preferences.displayMode;
|
||||
if (preferences.background !== undefined) updateData.background = preferences.background;
|
||||
if (preferences.komgaMaxConcurrentRequests !== undefined) updateData.komgaMaxConcurrentRequests = preferences.komgaMaxConcurrentRequests;
|
||||
if (preferences.readerPrefetchCount !== undefined) updateData.readerPrefetchCount = preferences.readerPrefetchCount;
|
||||
if (preferences.circuitBreakerConfig !== undefined) updateData.circuitBreakerConfig = preferences.circuitBreakerConfig;
|
||||
|
||||
const updatedPreferences = await prisma.preferences.upsert({
|
||||
where: { userId },
|
||||
@@ -66,9 +72,9 @@ export class PreferencesService {
|
||||
showOnlyUnread: preferences.showOnlyUnread ?? defaultPreferences.showOnlyUnread,
|
||||
displayMode: preferences.displayMode ?? defaultPreferences.displayMode,
|
||||
background: (preferences.background ?? defaultPreferences.background) as unknown as Prisma.InputJsonValue,
|
||||
circuitBreakerConfig: {},
|
||||
komgaMaxConcurrentRequests: 2,
|
||||
readerPrefetchCount: 5,
|
||||
circuitBreakerConfig: (preferences.circuitBreakerConfig ?? defaultPreferences.circuitBreakerConfig) as unknown as Prisma.InputJsonValue,
|
||||
komgaMaxConcurrentRequests: preferences.komgaMaxConcurrentRequests ?? 5,
|
||||
readerPrefetchCount: preferences.readerPrefetchCount ?? 5,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -78,6 +84,9 @@ export class PreferencesService {
|
||||
showOnlyUnread: updatedPreferences.showOnlyUnread,
|
||||
displayMode: updatedPreferences.displayMode as UserPreferences["displayMode"],
|
||||
background: updatedPreferences.background as unknown as BackgroundPreferences,
|
||||
komgaMaxConcurrentRequests: updatedPreferences.komgaMaxConcurrentRequests,
|
||||
readerPrefetchCount: updatedPreferences.readerPrefetchCount,
|
||||
circuitBreakerConfig: updatedPreferences.circuitBreakerConfig as unknown as CircuitBreakerConfig,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof AppError) {
|
||||
|
||||
@@ -13,11 +13,33 @@ class RequestQueue {
|
||||
private queue: QueuedRequest<any>[] = [];
|
||||
private activeCount = 0;
|
||||
private maxConcurrent: number;
|
||||
private getMaxConcurrent: (() => Promise<number>) | null = null;
|
||||
|
||||
constructor(maxConcurrent?: number) {
|
||||
// Lire depuis env ou utiliser la valeur par défaut
|
||||
const envValue = process.env.KOMGA_MAX_CONCURRENT_REQUESTS;
|
||||
this.maxConcurrent = maxConcurrent ?? (envValue ? parseInt(envValue, 10) : 5);
|
||||
// Valeur par défaut
|
||||
this.maxConcurrent = maxConcurrent ?? 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure une fonction pour récupérer dynamiquement le max concurrent depuis les préférences
|
||||
*/
|
||||
setMaxConcurrentGetter(getter: () => Promise<number>): void {
|
||||
this.getMaxConcurrent = getter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère la valeur de maxConcurrent, soit depuis les préférences, soit depuis la valeur fixe
|
||||
*/
|
||||
private async getCurrentMaxConcurrent(): Promise<number> {
|
||||
if (this.getMaxConcurrent) {
|
||||
try {
|
||||
return await this.getMaxConcurrent();
|
||||
} catch (error) {
|
||||
console.error('Error getting maxConcurrent from preferences, using default:', error);
|
||||
return this.maxConcurrent;
|
||||
}
|
||||
}
|
||||
return this.maxConcurrent;
|
||||
}
|
||||
|
||||
async enqueue<T>(execute: () => Promise<T>): Promise<T> {
|
||||
@@ -38,7 +60,8 @@ class RequestQueue {
|
||||
}
|
||||
|
||||
private async processQueue(): Promise<void> {
|
||||
if (this.activeCount >= this.maxConcurrent || this.queue.length === 0) {
|
||||
const maxConcurrent = await this.getCurrentMaxConcurrent();
|
||||
if (this.activeCount >= maxConcurrent || this.queue.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -77,10 +100,6 @@ class RequestQueue {
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance - Par défaut limite à 2 requêtes simultanées (configurable via KOMGA_MAX_CONCURRENT_REQUESTS)
|
||||
export const RequestQueueService = new RequestQueue(
|
||||
process.env.KOMGA_MAX_CONCURRENT_REQUESTS
|
||||
? parseInt(process.env.KOMGA_MAX_CONCURRENT_REQUESTS, 10)
|
||||
: 2
|
||||
);
|
||||
// Singleton instance - Par défaut limite à 5 requêtes simultanées
|
||||
export const RequestQueueService = new RequestQueue(5);
|
||||
|
||||
|
||||
@@ -9,6 +9,12 @@ export interface BackgroundPreferences {
|
||||
komgaLibraries?: string[]; // IDs des bibliothèques Komga sélectionnées
|
||||
}
|
||||
|
||||
export interface CircuitBreakerConfig {
|
||||
threshold?: number;
|
||||
timeout?: number;
|
||||
resetTimeout?: number;
|
||||
}
|
||||
|
||||
export interface UserPreferences {
|
||||
showThumbnails: boolean;
|
||||
cacheMode: "memory" | "file";
|
||||
@@ -18,6 +24,9 @@ export interface UserPreferences {
|
||||
itemsPerPage: number;
|
||||
};
|
||||
background: BackgroundPreferences;
|
||||
komgaMaxConcurrentRequests: number;
|
||||
readerPrefetchCount: number;
|
||||
circuitBreakerConfig: CircuitBreakerConfig;
|
||||
}
|
||||
|
||||
export const defaultPreferences: UserPreferences = {
|
||||
@@ -33,6 +42,13 @@ export const defaultPreferences: UserPreferences = {
|
||||
opacity: 10,
|
||||
blur: 0,
|
||||
},
|
||||
komgaMaxConcurrentRequests: 5,
|
||||
readerPrefetchCount: 5,
|
||||
circuitBreakerConfig: {
|
||||
threshold: 5,
|
||||
timeout: 30000,
|
||||
resetTimeout: 60000,
|
||||
},
|
||||
};
|
||||
|
||||
// Dégradés prédéfinis
|
||||
|
||||
Reference in New Issue
Block a user