package anidb
import (
- "encoding/gob"
"github.com/Kovensky/go-anidb/udp"
"log"
"sync"
"time"
)
-func init() {
- gob.RegisterName("*github.com/Kovensky/go-anidb.banCache", &banCache{})
-}
-
const banDuration = 30*time.Minute + 1*time.Second
-type banCache struct{ time.Time }
-
-func (c *banCache) Touch() {
- c.Time = time.Now()
-}
-func (c *banCache) IsStale() bool {
- return time.Now().Sub(c.Time) > banDuration
-}
-
// Returns whether the last UDP API access returned a 555 BANNED message.
func Banned() bool {
- var banTime banCache
- cache.Get(&banTime, "banned")
+ stat, err := Cache.Stat("banned")
+ if err != nil {
+ return false
+ }
- stale := banTime.IsStale()
- if stale {
- cache.Delete("banned")
+ switch ts := stat.ModTime(); {
+ case ts.IsZero():
+ return false
+ case time.Now().Sub(ts) > banDuration:
+ return false
+ default:
+ return true
}
- return !stale
}
func setBanned() {
- cache.Set(&banCache{}, "banned")
+ Cache.Touch("banned")
}
type paramSet struct {
credLock sync.Mutex
credentials *credentials
connected bool
+
+ user *User
}
func newUDPWrap() *udpWrap {
}
func (udp *udpWrap) sendQueue() {
- initialWait := 6 * time.Second
+ initialWait := 5 * time.Second
wait := initialWait
for set := range udp.sendQueueCh {
Retry:
if reply.Error() == udpapi.TimeoutError {
// retry
- wait = (wait * 15) / 10
+ wait = wait * 2
if wait > time.Minute {
wait = time.Minute
}
}
}
-type errorReply struct {
- udpapi.APIReply
- err error
-}
-
-func (r *errorReply) Code() int {
- return 999
-}
-func (r *errorReply) Text() string {
- return r.err.Error()
-}
-func (r *errorReply) Error() error {
- return r.err
-}
-
func (udp *udpWrap) SendRecv(cmd string, params paramMap) <-chan udpapi.APIReply {
ch := make(chan udpapi.APIReply, 1)