package salts
import (
"sort"
"sync"
"time"
"github.com/gotd/td/internal/mt"
)
type Salts struct {
salts []mt .FutureSalt
saltsMux sync .Mutex
}
func (s *Salts ) Get (deadline time .Time ) (int64 , bool ) {
s .saltsMux .Lock ()
defer s .saltsMux .Unlock ()
check :
if len (s .salts ) < 1 {
return 0 , false
}
date := int (deadline .Unix ())
if salt := s .salts [len (s .salts )-1 ]; salt .ValidUntil > date {
return salt .Salt , true
}
n := 0
for _ , salt := range s .salts {
if salt .ValidUntil > date {
s .salts [n ] = salt
n ++
}
}
s .salts = s .salts [:n ]
goto check
}
type saltSlice []mt .FutureSalt
func (s saltSlice ) Len () int {
return len (s )
}
func (s saltSlice ) Less (i , j int ) bool {
return s [i ].ValidUntil > s [j ].ValidUntil
}
func (s saltSlice ) Swap (i , j int ) {
s [i ], s [j ] = s [j ], s [i ]
}
func (s *Salts ) Store (salts []mt .FutureSalt ) {
s .saltsMux .Lock ()
defer s .saltsMux .Unlock ()
s .salts = append (s .salts , salts ...)
n := 0
dedup := make (map [int64 ]struct {}, len (s .salts )+1 )
for _ , salt := range s .salts {
if _ , ok := dedup [salt .Salt ]; !ok {
dedup [salt .Salt ] = struct {}{}
s .salts [n ] = salt
n ++
}
}
s .salts = s .salts [:n ]
sort .Sort (saltSlice (s .salts ))
}
func (s *Salts ) Reset () {
s .saltsMux .Lock ()
s .salts = s .salts [:0 ]
s .saltsMux .Unlock ()
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .