]> git.lizzy.rs Git - go-anidb.git/commitdiff
anidb: Make (*AniDB).User() try harder to return a user
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Fri, 26 Jul 2013 23:29:44 +0000 (20:29 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Fri, 26 Jul 2013 23:29:44 +0000 (20:29 -0300)
Will block for at least 2 seconds if the user ID is not cached. Any UDP
API access will cache the user ID.

anidb.go

index 7029a46f04d6b470407a5f170552297cd8eabfee..aa30395f52f349b8c04a0fc50b63794170a474cd 100644 (file)
--- a/anidb.go
+++ b/anidb.go
@@ -41,8 +41,13 @@ func (adb *AniDB) User() *User {
                if adb.udp.user != nil {
                        return adb.udp.user
                } else if adb.udp.credentials != nil {
-                       // see if we can get it from the cache
+                       // see if we can get it from the cache (we don't care if it's stale)
                        adb.udp.user = UserByName(decrypt(adb.udp.credentials.username))
+                       if adb.udp.user != nil {
+                               return adb.udp.user
+                       }
+                       // we have to go through the slow path
+                       adb.udp.user = <-adb.GetUserByName(decrypt(adb.udp.credentials.username))
                        return adb.udp.user
                }
        }