Debian Codegraph @

pkg/storage/cacher/util.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package cacher
18
19 import (
20 "math"
21 "strings"
22 "time"
23 )
24
25 // hasPathPrefix returns true if the string matches pathPrefix exactly, or if is prefixed with pathPrefix at a path segment boundary
26 func hasPathPrefix(s, pathPrefix string) bool {
27 // Short circuit if s doesn't contain the prefix at all
28 if !strings.HasPrefix(s, pathPrefix) {
29 return false
30 }
31
32 pathPrefixLength := len(pathPrefix)
33
34 if len(s) == pathPrefixLength {
35 // Exact match
36 return true
37 }
38 if strings.HasSuffix(pathPrefix, "/") {
39 // pathPrefix already ensured a path segment boundary
40 return true
41 }
42 if s[pathPrefixLength:pathPrefixLength+1] == "/" {
43 // The next character in s is a path segment boundary
44 // Check this instead of normalizing pathPrefix to avoid allocating on every call
45 return true
46 }
47 return false
48 }
49
50 // calculateRetryAfterForUnreadyCache calculates the retry duration based on the cache downtime.
51 func calculateRetryAfterForUnreadyCache(downtime time.Duration) int {
52 factor := 0.06
53 result := math.Exp(factor * downtime.Seconds())
54 result = math.Min(30, math.Max(1, result))
55 return int(result)
56 }