5 "github.com/Kovensky/go-anidb/udp"
12 gob.RegisterName("*github.com/Kovensky/go-anidb.banCache", &banCache{})
15 const banDuration = 30*time.Minute + 1*time.Second
17 type banCache struct{ time.Time }
19 func (c *banCache) Touch() {
22 func (c *banCache) IsStale() bool {
23 return time.Now().Sub(c.Time) > banDuration
26 // Returns whether the last UDP API access returned a 555 BANNED message.
29 cache.Get(&banTime, "banned")
31 stale := banTime.IsStale()
33 cache.Delete("banned")
39 cache.Set(&banCache{}, "banned")
42 type paramSet struct {
45 ch chan udpapi.APIReply
52 sendQueueCh chan paramSet
55 credentials *credentials
59 func newUDPWrap() *udpWrap {
61 AniDBUDP: udpapi.NewAniDBUDP(),
62 sendQueueCh: make(chan paramSet, 10),
68 type paramMap udpapi.ParamMap // shortcut
70 type noauthAPIReply struct {
74 func (r *noauthAPIReply) Code() int {
78 func (r *noauthAPIReply) Text() string {
82 func (r *noauthAPIReply) Error() error {
83 return &udpapi.APIError{Code: r.Code(), Desc: r.Text()}
86 type bannedAPIReply struct {
90 func (r *bannedAPIReply) Code() int {
93 func (r *bannedAPIReply) Text() string {
96 func (r *bannedAPIReply) Error() error {
97 return &udpapi.APIError{Code: r.Code(), Desc: r.Text()}
100 var bannedReply udpapi.APIReply = &bannedAPIReply{}
102 func (udp *udpWrap) sendQueue() {
103 initialWait := 6 * time.Second
105 for set := range udp.sendQueueCh {
108 set.ch <- bannedReply
113 reply := <-udp.AniDBUDP.SendRecv(set.cmd, udpapi.ParamMap(set.params))
115 if reply.Error() == udpapi.TimeoutError {
117 wait = (wait * 15) / 10
118 if wait > time.Minute {
127 switch reply.Code() {
128 case 403, 501, 506: // not logged in, or session expired
129 if r := udp.ReAuth(); r.Error() == nil {
133 case 503, 504: // client library rejected
135 case 555: // IP (and user, possibly client) temporarily banned
143 type errorReply struct {
148 func (r *errorReply) Code() int {
151 func (r *errorReply) Text() string {
154 func (r *errorReply) Error() error {
158 func (udp *udpWrap) SendRecv(cmd string, params paramMap) <-chan udpapi.APIReply {
159 ch := make(chan udpapi.APIReply, 1)
162 defer udp.sendLock.Unlock()
171 if r := udp.ReAuth(); r.Error() != nil {
178 udp.sendQueueCh <- paramSet{