20602: Prioritize backend traffic from keep-web as interactive.
[arvados.git] / services / keep-web / cache.go
index db06d635092a3ee36daed2d3b72fe1b1b738a909..c44a2eb73985ba5fd0494e99d27b6792c402838d 100644 (file)
@@ -5,6 +5,7 @@
 package keepweb
 
 import (
+       "errors"
        "net/http"
        "sync"
        "sync/atomic"
@@ -140,6 +141,11 @@ func (c *cache) GetSession(token string) (arvados.CustomFileSystem, *cachedSessi
                        return nil, nil, nil, err
                }
                sess.client.AuthToken = token
+               // A non-empty origin header tells controller to
+               // prioritize our traffic as interactive, which is
+               // true most of the time.
+               origin := c.cluster.Services.WebDAVDownload.ExternalURL
+               sess.client.SendHeader = http.Header{"Origin": {origin.Scheme + "://" + origin.Host}}
                sess.arvadosclient, err = arvadosclient.New(sess.client)
                if err != nil {
                        return nil, nil, nil, err
@@ -168,7 +174,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