Browse Source

Update redis.go

code and how the deletionLock and deletionDone variables are used to ensure the deletion action only occurs once per day.
master
yuriy0803 2 years ago
parent
commit
fb87db9641
  1. 54
      storage/redis.go

54
storage/redis.go

@ -8,6 +8,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
redis "gopkg.in/redis.v3" 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 var deletionLock sync.Mutex
func (r *RedisClient) WriteWorkerShareStatus(login string, id string, valid bool, stale bool, invalid bool) { 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 valid_int := 0
stale_int := 0 stale_int := 0
invalid_int := 0 invalid_int := 0
// Convert boolean flags to integer values.
if valid { if valid {
valid_int = 1 valid_int = 1
} }
@ -1859,38 +1865,42 @@ func (r *RedisClient) WriteWorkerShareStatus(login string, id string, valid bool
invalid_int = 1 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() t := time.Now().Local()
if t.Format("15:04:05") >= "23:59:00" { formattedTime := t.Format("15:04:05") // Time in 24-hour format
tx := r.client.Multi()
defer tx.Close() if formattedTime >= "23:59:00" && !deletionDone {
tx.Exec(func() error { // Lock to ensure only one deletion occurs.
//tx.Del(r.formatKey("minerShare", login, id)) deletionLock.Lock()
tx.HSet(r.formatKey("minerShare", login, id), "valid", strconv.FormatInt(0, 10)) defer deletionLock.Unlock()
tx.HSet(r.formatKey("minerShare", login, id), "stale", strconv.FormatInt(0, 10))
tx.HSet(r.formatKey("minerShare", login, id), "invalid", strconv.FormatInt(0, 10)) if !deletionDone {
return nil 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 { } else {
// So, we need to initiate the tx object
tx := r.client.Multi() tx := r.client.Multi()
defer tx.Close() defer tx.Close()
// Increment share counts.
tx.Exec(func() error { 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), "valid", int64(valid_int))
tx.HIncrBy(r.formatKey("minerShare", login, id), "stale", int64(stale_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("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), "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 return nil
}) })
} //end else }
} }
func (r *RedisClient) NumberStratumWorker(count int) { func (r *RedisClient) NumberStratumWorker(count int) {

Loading…
Cancel
Save