]> git.lizzy.rs Git - go-anidb.git/blobdiff - udp.go
anidb: Delete invalid cache data
[go-anidb.git] / udp.go
diff --git a/udp.go b/udp.go
index abd8a3aa1856e7c4168f3f88ec7f7df044555805..84cd5c8124b0c0ad3b6e2f73e37c591738f4d4e9 100644 (file)
--- a/udp.go
+++ b/udp.go
@@ -63,6 +63,22 @@ func newUDPWrap() *udpWrap {
 
 type paramMap udpapi.ParamMap // shortcut
 
+type noauthAPIReply struct {
+       udpapi.APIReply
+}
+
+func (r *noauthAPIReply) Code() int {
+       return 501
+}
+
+func (r *noauthAPIReply) Text() string {
+       return "LOGIN FIRST"
+}
+
+func (r *noauthAPIReply) Error() error {
+       return &udpapi.APIError{Code: r.Code(), Desc: r.Text()}
+}
+
 type bannedAPIReply struct {
        udpapi.APIReply
 }
@@ -74,27 +90,34 @@ func (r *bannedAPIReply) Text() string {
        return "BANNED"
 }
 func (r *bannedAPIReply) Error() error {
-       return &udpapi.APIError{Code: 555, Desc: "BANNED"}
+       return &udpapi.APIError{Code: r.Code(), Desc: r.Text()}
 }
 
 var bannedReply udpapi.APIReply = &bannedAPIReply{}
 
 func (udp *udpWrap) sendQueue() {
+       initialWait := 6 * time.Second
+       wait := initialWait
        for set := range udp.sendQueueCh {
+       Retry:
                reply := <-udp.AniDBUDP.SendRecv(set.cmd, udpapi.ParamMap(set.params))
 
                if reply.Error() == udpapi.TimeoutError {
                        // retry
-                       go func(set paramSet) { udp.sendQueueCh <- set }(set)
-                       continue
+                       wait = (wait * 15) / 10
+                       if wait > time.Minute {
+                               wait = time.Minute
+                       }
+                       time.Sleep(wait)
+                       goto Retry
                }
+               wait = initialWait
 
                switch reply.Code() {
                case 403, 501, 506: // not logged in, or session expired
                        if err := udp.ReAuth(); err == nil {
                                // retry
-                               go func(set paramSet) { udp.sendQueueCh <- set }(set)
-                               continue
+                               goto Retry
                        }
                case 503, 504: // client library rejected
                        panic(reply.Error())
@@ -108,6 +131,11 @@ func (udp *udpWrap) sendQueue() {
 
 func (udp *udpWrap) SendRecv(cmd string, params paramMap) <-chan udpapi.APIReply {
        ch := make(chan udpapi.APIReply, 1)
+       if udp.credentials == nil {
+               ch <- &noauthAPIReply{}
+               close(ch)
+               return ch
+       }
 
        if Banned() {
                ch <- bannedReply