refactor(provider-pool): 将日期对象统一转换为ISO字符串格式
修改ProviderPoolManager中日期处理逻辑,将所有Date对象统一转换为ISO字符串格式存储,确保数据一致性并简化序列化过程。
This commit is contained in:
parent
7b0aee7959
commit
cec97ff3ae
1 changed files with 8 additions and 4 deletions
|
|
@ -30,9 +30,12 @@ export class ProviderPoolManager {
|
||||||
providerConfig.usageCount = providerConfig.usageCount !== undefined ? providerConfig.usageCount : 0;
|
providerConfig.usageCount = providerConfig.usageCount !== undefined ? providerConfig.usageCount : 0;
|
||||||
providerConfig.errorCount = providerConfig.errorCount !== undefined ? providerConfig.errorCount : 0;
|
providerConfig.errorCount = providerConfig.errorCount !== undefined ? providerConfig.errorCount : 0;
|
||||||
if (providerConfig.lastErrorTime && typeof providerConfig.lastErrorTime === 'string') {
|
if (providerConfig.lastErrorTime && typeof providerConfig.lastErrorTime === 'string') {
|
||||||
providerConfig.lastErrorTime = new Date(providerConfig.lastErrorTime);
|
// Keep as string (ISOString)
|
||||||
|
providerConfig.lastErrorTime = providerConfig.lastErrorTime;
|
||||||
} else if (providerConfig.lastErrorTime === undefined) {
|
} else if (providerConfig.lastErrorTime === undefined) {
|
||||||
providerConfig.lastErrorTime = null;
|
providerConfig.lastErrorTime = null;
|
||||||
|
} else if (providerConfig.lastErrorTime instanceof Date) {
|
||||||
|
providerConfig.lastErrorTime = providerConfig.lastErrorTime.toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.providerStatus[providerType].push({
|
this.providerStatus[providerType].push({
|
||||||
|
|
@ -75,7 +78,7 @@ export class ProviderPoolManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selected) {
|
if (selected) {
|
||||||
selected.config.lastUsed = new Date();
|
selected.config.lastUsed = new Date().toISOString();
|
||||||
selected.config.usageCount++; // Increment usage count
|
selected.config.usageCount++; // Increment usage count
|
||||||
|
|
||||||
console.log(`[ProviderPoolManager] Selected provider for ${providerType} (round-robin): ${JSON.stringify(selected.config)}`);
|
console.log(`[ProviderPoolManager] Selected provider for ${providerType} (round-robin): ${JSON.stringify(selected.config)}`);
|
||||||
|
|
@ -97,7 +100,7 @@ export class ProviderPoolManager {
|
||||||
const provider = pool.find(p => p.uuid === providerConfig.uuid);
|
const provider = pool.find(p => p.uuid === providerConfig.uuid);
|
||||||
if (provider) {
|
if (provider) {
|
||||||
provider.config.errorCount++;
|
provider.config.errorCount++;
|
||||||
provider.config.lastErrorTime = new Date(); // Update last error time in config
|
provider.config.lastErrorTime = new Date().toISOString(); // Update last error time in config
|
||||||
|
|
||||||
if (provider.config.errorCount >= this.maxErrorCount) {
|
if (provider.config.errorCount >= this.maxErrorCount) {
|
||||||
provider.config.isHealthy = false;
|
provider.config.isHealthy = false;
|
||||||
|
|
@ -142,7 +145,7 @@ export class ProviderPoolManager {
|
||||||
|
|
||||||
// Only attempt to health check unhealthy providers after a certain interval
|
// Only attempt to health check unhealthy providers after a certain interval
|
||||||
if (!providerStatus.config.isHealthy && providerStatus.config.lastErrorTime &&
|
if (!providerStatus.config.isHealthy && providerStatus.config.lastErrorTime &&
|
||||||
(now.getTime() - providerStatus.config.lastErrorTime.getTime() < this.healthCheckInterval)) {
|
(now.getTime() - new Date(providerStatus.config.lastErrorTime).getTime() < this.healthCheckInterval)) {
|
||||||
console.log(`[ProviderPoolManager] Skipping health check for ${JSON.stringify(providerConfig)} (${providerType}). Last error too recent.`);
|
console.log(`[ProviderPoolManager] Skipping health check for ${JSON.stringify(providerConfig)} (${providerType}). Last error too recent.`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -260,6 +263,7 @@ export class ProviderPoolManager {
|
||||||
|
|
||||||
if (this.providerStatus[providerTypeToUpdate]) {
|
if (this.providerStatus[providerTypeToUpdate]) {
|
||||||
currentPools[providerTypeToUpdate] = this.providerStatus[providerTypeToUpdate].map(p => {
|
currentPools[providerTypeToUpdate] = this.providerStatus[providerTypeToUpdate].map(p => {
|
||||||
|
// Convert Date objects to ISOString if they exist
|
||||||
if (p.config.lastUsed instanceof Date) {
|
if (p.config.lastUsed instanceof Date) {
|
||||||
p.config.lastUsed = p.config.lastUsed.toISOString();
|
p.config.lastUsed = p.config.lastUsed.toISOString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue