Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
bc226a2a
Commit
bc226a2a
authored
Mar 22, 2019
by
tiffany jernigan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated client-go expiration cache to take in expiration policies
parent
ab35bd06
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
24 deletions
+32
-24
expiration_cache.go
staging/src/k8s.io/client-go/tools/cache/expiration_cache.go
+25
-17
expiration_cache_fakes.go
...rc/k8s.io/client-go/tools/cache/expiration_cache_fakes.go
+1
-1
expiration_cache_test.go
...src/k8s.io/client-go/tools/cache/expiration_cache_test.go
+6
-6
No files found.
staging/src/k8s.io/client-go/tools/cache/expiration_cache.go
View file @
bc226a2a
...
...
@@ -48,7 +48,7 @@ type ExpirationCache struct {
// ExpirationPolicy dictates when an object expires. Currently only abstracted out
// so unittests don't rely on the system clock.
type
ExpirationPolicy
interface
{
IsExpired
(
obj
*
t
imestampedEntry
)
bool
IsExpired
(
obj
*
T
imestampedEntry
)
bool
}
// TTLPolicy implements a ttl based ExpirationPolicy.
...
...
@@ -63,26 +63,29 @@ type TTLPolicy struct {
// IsExpired returns true if the given object is older than the ttl, or it can't
// determine its age.
func
(
p
*
TTLPolicy
)
IsExpired
(
obj
*
t
imestampedEntry
)
bool
{
return
p
.
Ttl
>
0
&&
p
.
Clock
.
Since
(
obj
.
t
imestamp
)
>
p
.
Ttl
func
(
p
*
TTLPolicy
)
IsExpired
(
obj
*
T
imestampedEntry
)
bool
{
return
p
.
Ttl
>
0
&&
p
.
Clock
.
Since
(
obj
.
T
imestamp
)
>
p
.
Ttl
}
// timestampedEntry is the only type allowed in a ExpirationCache.
type
timestampedEntry
struct
{
obj
interface
{}
timestamp
time
.
Time
// TimestampedEntry is the only type allowed in a ExpirationCache.
// Keep in mind that it is not safe to share timestamps between computers.
// Behavior may be inconsistent if you get a timestamp from the API Server and
// use it on the client machine as part of your ExpirationCache.
type
TimestampedEntry
struct
{
Obj
interface
{}
Timestamp
time
.
Time
}
// getTimestampedEntry returns the
t
imestampedEntry stored under the given key.
func
(
c
*
ExpirationCache
)
getTimestampedEntry
(
key
string
)
(
*
t
imestampedEntry
,
bool
)
{
// getTimestampedEntry returns the
T
imestampedEntry stored under the given key.
func
(
c
*
ExpirationCache
)
getTimestampedEntry
(
key
string
)
(
*
T
imestampedEntry
,
bool
)
{
item
,
_
:=
c
.
cacheStorage
.
Get
(
key
)
if
tsEntry
,
ok
:=
item
.
(
*
t
imestampedEntry
);
ok
{
if
tsEntry
,
ok
:=
item
.
(
*
T
imestampedEntry
);
ok
{
return
tsEntry
,
true
}
return
nil
,
false
}
// getOrExpire retrieves the object from the
t
imestampedEntry if and only if it hasn't
// getOrExpire retrieves the object from the
T
imestampedEntry if and only if it hasn't
// already expired. It holds a write lock across deletion.
func
(
c
*
ExpirationCache
)
getOrExpire
(
key
string
)
(
interface
{},
bool
)
{
// Prevent all inserts from the time we deem an item as "expired" to when we
...
...
@@ -95,11 +98,11 @@ func (c *ExpirationCache) getOrExpire(key string) (interface{}, bool) {
return
nil
,
false
}
if
c
.
expirationPolicy
.
IsExpired
(
timestampedItem
)
{
klog
.
V
(
4
)
.
Infof
(
"Entry %v: %+v has expired"
,
key
,
timestampedItem
.
o
bj
)
klog
.
V
(
4
)
.
Infof
(
"Entry %v: %+v has expired"
,
key
,
timestampedItem
.
O
bj
)
c
.
cacheStorage
.
Delete
(
key
)
return
nil
,
false
}
return
timestampedItem
.
o
bj
,
true
return
timestampedItem
.
O
bj
,
true
}
// GetByKey returns the item stored under the key, or sets exists=false.
...
...
@@ -126,7 +129,7 @@ func (c *ExpirationCache) List() []interface{} {
list
:=
make
([]
interface
{},
0
,
len
(
items
))
for
_
,
item
:=
range
items
{
obj
:=
item
.
(
*
timestampedEntry
)
.
o
bj
obj
:=
item
.
(
*
TimestampedEntry
)
.
O
bj
if
key
,
err
:=
c
.
keyFunc
(
obj
);
err
!=
nil
{
list
=
append
(
list
,
obj
)
}
else
if
obj
,
exists
:=
c
.
getOrExpire
(
key
);
exists
{
...
...
@@ -151,7 +154,7 @@ func (c *ExpirationCache) Add(obj interface{}) error {
c
.
expirationLock
.
Lock
()
defer
c
.
expirationLock
.
Unlock
()
c
.
cacheStorage
.
Add
(
key
,
&
t
imestampedEntry
{
obj
,
c
.
clock
.
Now
()})
c
.
cacheStorage
.
Add
(
key
,
&
T
imestampedEntry
{
obj
,
c
.
clock
.
Now
()})
return
nil
}
...
...
@@ -184,7 +187,7 @@ func (c *ExpirationCache) Replace(list []interface{}, resourceVersion string) er
if
err
!=
nil
{
return
KeyError
{
item
,
err
}
}
items
[
key
]
=
&
t
imestampedEntry
{
item
,
ts
}
items
[
key
]
=
&
T
imestampedEntry
{
item
,
ts
}
}
c
.
expirationLock
.
Lock
()
defer
c
.
expirationLock
.
Unlock
()
...
...
@@ -199,10 +202,15 @@ func (c *ExpirationCache) Resync() error {
// NewTTLStore creates and returns a ExpirationCache with a TTLPolicy
func
NewTTLStore
(
keyFunc
KeyFunc
,
ttl
time
.
Duration
)
Store
{
return
NewExpirationStore
(
keyFunc
,
&
TTLPolicy
{
ttl
,
clock
.
RealClock
{}})
}
// NewExpirationStore creates and returns a ExpirationCache for a given policy
func
NewExpirationStore
(
keyFunc
KeyFunc
,
expirationPolicy
ExpirationPolicy
)
Store
{
return
&
ExpirationCache
{
cacheStorage
:
NewThreadSafeStore
(
Indexers
{},
Indices
{}),
keyFunc
:
keyFunc
,
clock
:
clock
.
RealClock
{},
expirationPolicy
:
&
TTLPolicy
{
ttl
,
clock
.
RealClock
{}}
,
expirationPolicy
:
expirationPolicy
,
}
}
staging/src/k8s.io/client-go/tools/cache/expiration_cache_fakes.go
View file @
bc226a2a
...
...
@@ -38,7 +38,7 @@ type FakeExpirationPolicy struct {
RetrieveKeyFunc
KeyFunc
}
func
(
p
*
FakeExpirationPolicy
)
IsExpired
(
obj
*
t
imestampedEntry
)
bool
{
func
(
p
*
FakeExpirationPolicy
)
IsExpired
(
obj
*
T
imestampedEntry
)
bool
{
key
,
_
:=
p
.
RetrieveKeyFunc
(
obj
)
return
!
p
.
NeverExpire
.
Has
(
key
)
}
...
...
staging/src/k8s.io/client-go/tools/cache/expiration_cache_test.go
View file @
bc226a2a
...
...
@@ -34,7 +34,7 @@ func TestTTLExpirationBasic(t *testing.T) {
&
FakeExpirationPolicy
{
NeverExpire
:
sets
.
NewString
(),
RetrieveKeyFunc
:
func
(
obj
interface
{})
(
string
,
error
)
{
return
obj
.
(
*
timestampedEntry
)
.
o
bj
.
(
testStoreObject
)
.
id
,
nil
return
obj
.
(
*
TimestampedEntry
)
.
O
bj
.
(
testStoreObject
)
.
id
,
nil
},
},
clock
.
RealClock
{},
...
...
@@ -67,7 +67,7 @@ func TestReAddExpiredItem(t *testing.T) {
exp
:=
&
FakeExpirationPolicy
{
NeverExpire
:
sets
.
NewString
(),
RetrieveKeyFunc
:
func
(
obj
interface
{})
(
string
,
error
)
{
return
obj
.
(
*
timestampedEntry
)
.
o
bj
.
(
testStoreObject
)
.
id
,
nil
return
obj
.
(
*
TimestampedEntry
)
.
O
bj
.
(
testStoreObject
)
.
id
,
nil
},
}
ttlStore
:=
NewFakeExpirationStore
(
...
...
@@ -130,7 +130,7 @@ func TestTTLList(t *testing.T) {
&
FakeExpirationPolicy
{
NeverExpire
:
sets
.
NewString
(
testObjs
[
1
]
.
id
),
RetrieveKeyFunc
:
func
(
obj
interface
{})
(
string
,
error
)
{
return
obj
.
(
*
timestampedEntry
)
.
o
bj
.
(
testStoreObject
)
.
id
,
nil
return
obj
.
(
*
TimestampedEntry
)
.
O
bj
.
(
testStoreObject
)
.
id
,
nil
},
},
clock
.
RealClock
{},
...
...
@@ -168,15 +168,15 @@ func TestTTLPolicy(t *testing.T) {
expiredTime
:=
fakeTime
.
Add
(
-
(
ttl
+
1
))
policy
:=
TTLPolicy
{
ttl
,
clock
.
NewFakeClock
(
fakeTime
)}
fakeTimestampedEntry
:=
&
timestampedEntry
{
obj
:
struct
{}{},
t
imestamp
:
exactlyOnTTL
}
fakeTimestampedEntry
:=
&
TimestampedEntry
{
Obj
:
struct
{}{},
T
imestamp
:
exactlyOnTTL
}
if
policy
.
IsExpired
(
fakeTimestampedEntry
)
{
t
.
Errorf
(
"TTL cache should not expire entries exactly on ttl"
)
}
fakeTimestampedEntry
.
t
imestamp
=
fakeTime
fakeTimestampedEntry
.
T
imestamp
=
fakeTime
if
policy
.
IsExpired
(
fakeTimestampedEntry
)
{
t
.
Errorf
(
"TTL Cache should not expire entries before ttl"
)
}
fakeTimestampedEntry
.
t
imestamp
=
expiredTime
fakeTimestampedEntry
.
T
imestamp
=
expiredTime
if
!
policy
.
IsExpired
(
fakeTimestampedEntry
)
{
t
.
Errorf
(
"TTL Cache should expire entries older than ttl"
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment