forked from decred/dcrstakepool
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
track timestamp usage in ticket auth routine
- Loading branch information
1 parent
e3b9ac7
commit a2f89e6
Showing
3 changed files
with
65 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package v3api | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type ticketChallengesCache struct { | ||
sync.Mutex | ||
data map[string]int64 // [timestamp]expiry | ||
} | ||
|
||
func newTicketChallengesCache() *ticketChallengesCache { | ||
cache := &ticketChallengesCache{ | ||
data: make(map[string]int64, 0), | ||
} | ||
|
||
go func() { | ||
for now := range time.Tick(time.Second) { | ||
cache.Lock() | ||
for timestampChallenge, challengeExpiry := range cache.data { | ||
if now.Unix() > challengeExpiry { | ||
delete(cache.data, timestampChallenge) | ||
} | ||
} | ||
cache.Unlock() | ||
} | ||
}() | ||
|
||
return cache | ||
} | ||
|
||
func (cache *ticketChallengesCache) addChallenge(timestamp string, expiresIn int64) { | ||
cache.Lock() | ||
if _, ok := cache.data[timestamp]; !ok { | ||
cache.data[timestamp] = time.Now().Unix() + expiresIn | ||
} | ||
cache.Unlock() | ||
} | ||
|
||
func (cache *ticketChallengesCache) containsChallenge(timestamp string) (exists bool) { | ||
cache.Lock() | ||
_, exists = cache.data[timestamp] | ||
cache.Unlock() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
package v3api | ||
|
||
import "github.com/decred/dcrstakepool/stakepooldclient" | ||
import ( | ||
"github.com/decred/dcrstakepool/stakepooldclient" | ||
) | ||
|
||
type V3API struct { | ||
stakepooldConnMan *stakepooldclient.StakepooldManager | ||
|
||
ticketChallengeMaxAge int64 | ||
processedTicketChallenges *ticketChallengesCache | ||
} | ||
|
||
func New(stakepooldConnMan *stakepooldclient.StakepooldManager, ticketChallengeMaxAge int64) *V3API { | ||
return &V3API{ | ||
stakepooldConnMan: stakepooldConnMan, | ||
ticketChallengeMaxAge: ticketChallengeMaxAge, | ||
processedTicketChallenges: newTicketChallengesCache(), | ||
} | ||
} |