8 "git.curoverse.com/arvados.git/sdk/go/arvados"
12 maxPermCacheAge = time.Hour
13 minPermCacheAge = 5 * time.Minute
16 type permChecker interface {
17 SetToken(token string)
18 Check(uuid string) (bool, error)
21 func NewPermChecker(ac arvados.Client) permChecker {
23 return &cachingPermChecker{
25 cache: make(map[string]time.Time),
30 type cachingPermChecker struct {
32 cache map[string]time.Time
36 func (pc *cachingPermChecker) SetToken(token string) {
37 pc.Client.AuthToken = token
40 func (pc *cachingPermChecker) Check(uuid string) (bool, error) {
42 if t, ok := pc.cache[uuid]; ok && time.Now().Sub(t) < maxPermCacheAge {
43 debugLogf("perm ok (cached): %+q %+q", pc.Client.AuthToken, uuid)
46 var buf map[string]interface{}
47 path, err := pc.PathForUUID("get", uuid)
51 err = pc.RequestAndDecode(&buf, "GET", path, nil, url.Values{
52 "select": {`["uuid"]`},
54 if err, ok := err.(arvados.TransactionError); ok && err.StatusCode == http.StatusNotFound {
55 debugLogf("perm err: %+q %+q: %s", pc.Client.AuthToken, uuid, err)
59 debugLogf("perm !ok: %+q %+q", pc.Client.AuthToken, uuid)
62 debugLogf("perm ok: %+q %+q", pc.Client.AuthToken, uuid)
63 pc.cache[uuid] = time.Now()
67 func (pc *cachingPermChecker) tidy() {
68 if len(pc.cache) <= pc.maxCurrent*2 {
71 tooOld := time.Now().Add(-minPermCacheAge)
72 for uuid, t := range pc.cache {
74 delete(pc.cache, uuid)
77 pc.maxCurrent = len(pc.cache)