From ed537658ac71a70abf18a92a44f8be65770ec61c Mon Sep 17 00:00:00 2001
From: Ben Wilson <ben@registrationmax.com>
Date: Sat, 17 Feb 2024 20:12:09 -0500
Subject: [PATCH] misc

---
 src/ORM/address-settings/address-settings.service.ts | 7 ++++++-
 src/models/StratumV1ClientStatistics.ts              | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/ORM/address-settings/address-settings.service.ts b/src/ORM/address-settings/address-settings.service.ts
index 9df9273..460a208 100644
--- a/src/ORM/address-settings/address-settings.service.ts
+++ b/src/ORM/address-settings/address-settings.service.ts
@@ -17,7 +17,12 @@ export class AddressSettingsService {
     public async getSettings(address: string, createIfNotFound: boolean) {
         const settings = await this.addressSettingsRepository.findOne({ where: { address } });
         if (createIfNotFound == true && settings == null) {
-            return await this.createNew(address);
+            // It's possible to have a race condition here so if we get a PK violation, fetch it
+            try {
+                return await this.createNew(address);
+            } catch (e) {
+                return await this.addressSettingsRepository.findOne({ where: { address } });
+            }
         }
         return settings;
     }
diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts
index 31704c3..7f3eb2e 100644
--- a/src/models/StratumV1ClientStatistics.ts
+++ b/src/models/StratumV1ClientStatistics.ts
@@ -63,6 +63,7 @@ export class StratumV1ClientStatistics {
             this.shares = 0;
             this.acceptedCount = 0;
             this.currentTimeSlot = timeSlot;
+            this.lastSave = new Date().getTime();
         } else if ((date.getTime() - 60 * 1000) > this.lastSave) {
             await this.clientStatisticsService.update({
                 time: this.currentTimeSlot,