|
1 | 1 | package state |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "log/slog" |
| 6 | + "os" |
4 | 7 | "reflect" |
5 | 8 | "time" |
6 | 9 |
|
@@ -255,3 +258,105 @@ func ReconcileState(fileState State, rateCache, botCache, verifiedCache *lru.Cac |
255 | 258 | } |
256 | 259 | } |
257 | 260 | } |
| 261 | + |
| 262 | +// SaveStateToFile saves state to a file with locking and optional reconciliation. |
| 263 | +// When reconcile is true, it reads and merges existing file state before saving. |
| 264 | +// Returns timing metrics for debugging. |
| 265 | +func SaveStateToFile( |
| 266 | + filePath string, |
| 267 | + reconcile bool, |
| 268 | + rateCache, botCache, verifiedCache *lru.Cache, |
| 269 | + log *slog.Logger, |
| 270 | +) (lockMs, readMs, reconcileMs, marshalMs, writeMs, totalMs int64, err error) { |
| 271 | + startTime := time.Now() |
| 272 | + |
| 273 | + lock, err := NewFileLock(filePath + ".lock") |
| 274 | + if err != nil { |
| 275 | + return 0, 0, 0, 0, 0, 0, err |
| 276 | + } |
| 277 | + defer lock.Close() |
| 278 | + |
| 279 | + if err := lock.Lock(); err != nil { |
| 280 | + return 0, 0, 0, 0, 0, 0, err |
| 281 | + } |
| 282 | + lockDuration := time.Since(startTime) |
| 283 | + |
| 284 | + var readDuration, reconcileDuration, marshalDuration, writeDuration time.Duration |
| 285 | + |
| 286 | + // Reconcile with existing file state if enabled |
| 287 | + if reconcile { |
| 288 | + readStart := time.Now() |
| 289 | + fileContent, readErr := os.ReadFile(filePath) |
| 290 | + readDuration = time.Since(readStart) |
| 291 | + |
| 292 | + if readErr == nil && len(fileContent) > 0 { |
| 293 | + reconcileStart := time.Now() |
| 294 | + var fileState State |
| 295 | + if unmarshalErr := json.Unmarshal(fileContent, &fileState); unmarshalErr == nil { |
| 296 | + log.Debug("Reconciling state before save", "fileBytes", len(fileContent)) |
| 297 | + ReconcileState(fileState, rateCache, botCache, verifiedCache) |
| 298 | + } |
| 299 | + reconcileDuration = time.Since(reconcileStart) |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + // Marshal current state |
| 304 | + marshalStart := time.Now() |
| 305 | + currentState := GetState(rateCache.Items(), botCache.Items(), verifiedCache.Items()) |
| 306 | + jsonData, err := json.Marshal(currentState) |
| 307 | + marshalDuration = time.Since(marshalStart) |
| 308 | + |
| 309 | + if err != nil { |
| 310 | + return lockDuration.Milliseconds(), readDuration.Milliseconds(), |
| 311 | + reconcileDuration.Milliseconds(), marshalDuration.Milliseconds(), |
| 312 | + 0, 0, err |
| 313 | + } |
| 314 | + |
| 315 | + // Write to disk |
| 316 | + writeStart := time.Now() |
| 317 | + err = os.WriteFile(filePath, jsonData, 0644) |
| 318 | + writeDuration = time.Since(writeStart) |
| 319 | + |
| 320 | + if err != nil { |
| 321 | + return lockDuration.Milliseconds(), readDuration.Milliseconds(), |
| 322 | + reconcileDuration.Milliseconds(), marshalDuration.Milliseconds(), |
| 323 | + writeDuration.Milliseconds(), 0, err |
| 324 | + } |
| 325 | + |
| 326 | + totalDuration := time.Since(startTime) |
| 327 | + return lockDuration.Milliseconds(), readDuration.Milliseconds(), |
| 328 | + reconcileDuration.Milliseconds(), marshalDuration.Milliseconds(), |
| 329 | + writeDuration.Milliseconds(), totalDuration.Milliseconds(), nil |
| 330 | +} |
| 331 | + |
| 332 | +// LoadStateFromFile loads state from a file with locking. |
| 333 | +func LoadStateFromFile( |
| 334 | + filePath string, |
| 335 | + rateCache, botCache, verifiedCache *lru.Cache, |
| 336 | +) error { |
| 337 | + lock, err := NewFileLock(filePath + ".lock") |
| 338 | + if err != nil { |
| 339 | + return err |
| 340 | + } |
| 341 | + defer lock.Close() |
| 342 | + |
| 343 | + if err := lock.Lock(); err != nil { |
| 344 | + return err |
| 345 | + } |
| 346 | + |
| 347 | + fileContent, err := os.ReadFile(filePath) |
| 348 | + if err != nil || len(fileContent) == 0 { |
| 349 | + return err |
| 350 | + } |
| 351 | + |
| 352 | + var loadedState State |
| 353 | + err = json.Unmarshal(fileContent, &loadedState) |
| 354 | + if err != nil { |
| 355 | + return err |
| 356 | + } |
| 357 | + |
| 358 | + // Use SetState which properly handles expiration times |
| 359 | + SetState(loadedState, rateCache, botCache, verifiedCache) |
| 360 | + |
| 361 | + return nil |
| 362 | +} |
0 commit comments