diff --git a/storage/redis.go b/storage/redis.go index 490c5ed..7af50bb 100644 --- a/storage/redis.go +++ b/storage/redis.go @@ -8,6 +8,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" redis "gopkg.in/redis.v3" @@ -1843,12 +1844,17 @@ func (r *RedisClient) getSharesStatus(login string, id string) (int64, int64, in } -// lets try to fuck without understanding and see if it works -func (r *RedisClient) WriteWorkerShareStatus(login string, id string, valid bool, stale bool, invalid bool) { +var deletionLock sync.Mutex +var deletionDone bool +// WriteWorkerShareStatus updates the worker's share status in Redis. +// It takes the worker's login, ID, and status flags for valid, stale, and invalid shares. +func (r *RedisClient) WriteWorkerShareStatus(login string, id string, valid bool, stale bool, invalid bool) { valid_int := 0 stale_int := 0 invalid_int := 0 + + // Convert boolean flags to integer values. if valid { valid_int = 1 } @@ -1859,38 +1865,42 @@ func (r *RedisClient) WriteWorkerShareStatus(login string, id string, valid bool invalid_int = 1 } - // var after = time.Now().AddDate(0, 0, -1).Unix() - // var now = time.Now().Unix() - // if(now >= after){ - // tx.HDel(r.formatKey("minerShare", login, id)) - // } t := time.Now().Local() - if t.Format("15:04:05") >= "23:59:00" { - tx := r.client.Multi() - defer tx.Close() - tx.Exec(func() error { - //tx.Del(r.formatKey("minerShare", login, id)) - tx.HSet(r.formatKey("minerShare", login, id), "valid", strconv.FormatInt(0, 10)) - tx.HSet(r.formatKey("minerShare", login, id), "stale", strconv.FormatInt(0, 10)) - tx.HSet(r.formatKey("minerShare", login, id), "invalid", strconv.FormatInt(0, 10)) - return nil - }) + formattedTime := t.Format("15:04:05") // Time in 24-hour format + + if formattedTime >= "23:59:00" && !deletionDone { + // Lock to ensure only one deletion occurs. + deletionLock.Lock() + defer deletionLock.Unlock() + + if !deletionDone { + tx := r.client.Multi() + defer tx.Close() + + // Reset share status to 0 for the next day. + tx.Exec(func() error { + tx.HSet(r.formatKey("minerShare", login, id), "valid", strconv.FormatInt(0, 10)) + tx.HSet(r.formatKey("minerShare", login, id), "stale", strconv.FormatInt(0, 10)) + tx.HSet(r.formatKey("minerShare", login, id), "invalid", strconv.FormatInt(0, 10)) + return nil + }) + + deletionDone = true + } } else { - // So, we need to initiate the tx object tx := r.client.Multi() defer tx.Close() + // Increment share counts. tx.Exec(func() error { - // OK, good, no need to read reset and add if i use Hset and HGet shit tx.HIncrBy(r.formatKey("minerShare", login, id), "valid", int64(valid_int)) tx.HIncrBy(r.formatKey("minerShare", login, id), "stale", int64(stale_int)) tx.HIncrBy(r.formatKey("minerShare", login, id), "invalid", int64(invalid_int)) tx.HIncrBy(r.formatKey("chartsNum", "share", login), "valid", int64(valid_int)) - tx.HIncrBy(r.formatKey("chartsNum", "share", login), "stale", int64(stale_int)) // Would that work? - + tx.HIncrBy(r.formatKey("chartsNum", "share", login), "stale", int64(stale_int)) return nil }) - } //end else + } } func (r *RedisClient) NumberStratumWorker(count int) {