19620: Remove goamz dependency in non-testing code.
[arvados.git] / services / keep-web / cache.go
index 3ba3e60abdad599bc5622e9cf5583f2422e81786..c73191103e01e0916a08f1314e19d02f53cb61b3 100644 (file)
@@ -5,6 +5,7 @@
 package keepweb
 
 import (
+       "errors"
        "net/http"
        "sync"
        "sync/atomic"
@@ -168,7 +169,7 @@ func (c *cache) GetSession(token string) (arvados.CustomFileSystem, *cachedSessi
        if user == nil || expired {
                user = new(arvados.User)
                err := sess.client.RequestAndDecode(user, "GET", "/arvados/v1/users/current", nil, nil)
-               if statusErr, ok := err.(interface{ HTTPStatus() int }); ok && statusErr.HTTPStatus() == http.StatusForbidden {
+               if he := errorWithHTTPStatus(nil); errors.As(err, &he) && he.HTTPStatus() == http.StatusForbidden {
                        // token is OK, but "get user id" api is out
                        // of scope -- return nil, signifying unknown
                        // user
@@ -185,9 +186,10 @@ func (c *cache) GetSession(token string) (arvados.CustomFileSystem, *cachedSessi
 // until approximate remaining size <= maxsize/2
 func (c *cache) pruneSessions() {
        now := time.Now()
-       var size int64
        keys := c.sessions.Keys()
-       for _, token := range keys {
+       sizes := make([]int64, len(keys))
+       var size int64
+       for i, token := range keys {
                ent, ok := c.sessions.Peek(token)
                if !ok {
                        continue
@@ -198,27 +200,17 @@ func (c *cache) pruneSessions() {
                        continue
                }
                if fs, ok := s.fs.Load().(arvados.CustomFileSystem); ok {
-                       size += fs.MemorySize()
+                       sizes[i] = fs.MemorySize()
+                       size += sizes[i]
                }
        }
        // Remove tokens until reaching size limit, starting with the
        // least frequently used entries (which Keys() returns last).
-       for i := len(keys) - 1; i >= 0; i-- {
-               token := keys[i]
-               if size <= c.cluster.Collections.WebDAVCache.MaxCollectionBytes {
-                       break
-               }
-               ent, ok := c.sessions.Peek(token)
-               if !ok {
-                       continue
-               }
-               s := ent.(*cachedSession)
-               fs, _ := s.fs.Load().(arvados.CustomFileSystem)
-               if fs == nil {
-                       continue
+       for i := len(keys) - 1; i >= 0 && size > c.cluster.Collections.WebDAVCache.MaxCollectionBytes; i-- {
+               if sizes[i] > 0 {
+                       c.sessions.Remove(keys[i])
+                       size -= sizes[i]
                }
-               c.sessions.Remove(token)
-               size -= fs.MemorySize()
        }
 }