| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package internal | 5 package internal |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "io/ioutil" | 9 "io/ioutil" |
| 10 "os" | 10 "os" |
| 11 "path/filepath" | 11 "path/filepath" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
| 15 "golang.org/x/oauth2" | 15 "golang.org/x/oauth2" |
| 16 | 16 |
| 17 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
| 18 "github.com/luci/luci-go/common/errors" | |
| 19 "github.com/luci/luci-go/common/logging" | 18 "github.com/luci/luci-go/common/logging" |
| 20 "github.com/luci/luci-go/common/retry" | 19 "github.com/luci/luci-go/common/retry" |
| 21 ) | 20 ) |
| 22 | 21 |
| 23 const ( | 22 const ( |
| 24 // GCAccessTokenMaxAge defines when to remove unused access tokens from
the | 23 // GCAccessTokenMaxAge defines when to remove unused access tokens from
the |
| 25 // disk cache. | 24 // disk cache. |
| 26 // | 25 // |
| 27 // We define "an access token" as an instance of oauth2.Token with | 26 // We define "an access token" as an instance of oauth2.Token with |
| 28 // RefreshToken set to "". | 27 // RefreshToken set to "". |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 } | 190 } |
| 192 | 191 |
| 193 // Note that TempFile creates the file in 0600 mode already, so we don't
need | 192 // Note that TempFile creates the file in 0600 mode already, so we don't
need |
| 194 // to chmod it. | 193 // to chmod it. |
| 195 // | 194 // |
| 196 // On Windows Rename may fail with sharing violation error if some other | 195 // On Windows Rename may fail with sharing violation error if some other |
| 197 // process has opened the file. We treat it as transient error, to trigg
er | 196 // process has opened the file. We treat it as transient error, to trigg
er |
| 198 // a retry in updateCacheFile. | 197 // a retry in updateCacheFile. |
| 199 if err = os.Rename(tmp.Name(), c.absPath()); err != nil { | 198 if err = os.Rename(tmp.Name(), c.absPath()); err != nil { |
| 200 cleanup() | 199 cleanup() |
| 201 » » return errors.WrapTransient(err) | 200 » » return retry.Tag.Apply(err) |
| 202 } | 201 } |
| 203 return nil | 202 return nil |
| 204 } | 203 } |
| 205 | 204 |
| 206 // updateCacheFile reads the token cache file, calls the callback, writes the fi
le | 205 // updateCacheFile reads the token cache file, calls the callback, writes the fi
le |
| 207 // back if the callback returns 'true'. | 206 // back if the callback returns 'true'. |
| 208 // | 207 // |
| 209 // It retries a bunch of times when encountering sharing violation errors on | 208 // It retries a bunch of times when encountering sharing violation errors on |
| 210 // Windows. | 209 // Windows. |
| 211 // | 210 // |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 return c.updateCacheFile(func(cache *cacheFile, now time.Time) bool { | 278 return c.updateCacheFile(func(cache *cacheFile, now time.Time) bool { |
| 280 for i, entry := range cache.Cache { | 279 for i, entry := range cache.Cache { |
| 281 if EqualCacheKeys(&entry.Key, key) { | 280 if EqualCacheKeys(&entry.Key, key) { |
| 282 cache.Cache = append(cache.Cache[:i], cache.Cach
e[i+1:]...) | 281 cache.Cache = append(cache.Cache[:i], cache.Cach
e[i+1:]...) |
| 283 return true | 282 return true |
| 284 } | 283 } |
| 285 } | 284 } |
| 286 return false // not there, this is fine, skip writing the file | 285 return false // not there, this is fine, skip writing the file |
| 287 }) | 286 }) |
| 288 } | 287 } |
| OLD | NEW |