4 "github.com/Kovensky/go-anidb/udp"
10 var banTimeLock sync.Mutex
12 const banDuration = 30*time.Minute + 1*time.Second
14 // Returns whether the last UDP API access returned a 555 BANNED message.
23 return time.Now().Sub(banTime) > banDuration
26 type paramSet struct {
29 ch chan udpapi.APIReply
35 sendQueueCh chan paramSet
37 credentials *credentials
41 func newUDPWrap() *udpWrap {
43 AniDBUDP: udpapi.NewAniDBUDP(),
44 sendQueueCh: make(chan paramSet, 10),
50 type paramMap udpapi.ParamMap // shortcut
52 type bannedAPIReply struct {
56 func (r *bannedAPIReply) Code() int {
59 func (r *bannedAPIReply) Text() string {
62 func (r *bannedAPIReply) Error() error {
63 return &udpapi.APIError{Code: 555, Desc: "BANNED"}
66 var bannedReply udpapi.APIReply = &bannedAPIReply{}
68 func (udp *udpWrap) sendQueue() {
69 for set := range udp.sendQueueCh {
70 reply := <-udp.AniDBUDP.SendRecv(set.cmd, udpapi.ParamMap(set.params))
72 if reply.Error() == udpapi.TimeoutError {
74 go func(set paramSet) { udp.sendQueueCh <- set }(set)
79 case 403, 501, 506: // not logged in, or session expired
80 if err := udp.ReAuth(); err == nil {
82 go func(set paramSet) { udp.sendQueueCh <- set }(set)
85 case 503, 504: // client library rejected
87 case 555: // IP (and user, possibly client) temporarily banned
99 func (udp *udpWrap) SendRecv(cmd string, params paramMap) <-chan udpapi.APIReply {
100 ch := make(chan udpapi.APIReply, 1)
103 defer banTimeLock.Unlock()
105 banTime = time.Time{}
112 udp.sendQueueCh <- paramSet{