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
6e70853e
Commit
6e70853e
authored
Jan 09, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
genericize ip cache
parent
f2b82c6c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
32 deletions
+150
-32
ip_cache.go
pkg/master/ip_cache.go
+12
-32
time_cache.go
pkg/util/time_cache.go
+75
-0
time_cache_test.go
pkg/util/time_cache_test.go
+63
-0
No files found.
pkg/master/ip_cache.go
View file @
6e70853e
...
@@ -17,7 +17,6 @@ limitations under the License.
...
@@ -17,7 +17,6 @@ limitations under the License.
package
master
package
master
import
(
import
(
"sync"
"time"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
...
@@ -26,19 +25,6 @@ import (
...
@@ -26,19 +25,6 @@ import (
"github.com/golang/glog"
"github.com/golang/glog"
)
)
type
ipCacheEntry
struct
{
ip
string
lastUpdate
time
.
Time
}
type
ipCache
struct
{
clock
util
.
Clock
cloudProvider
cloudprovider
.
Interface
cache
map
[
string
]
ipCacheEntry
lock
sync
.
Mutex
ttl
time
.
Duration
}
// NewIPCache makes a new ip caching layer, which will get IP addresses from cp,
// NewIPCache makes a new ip caching layer, which will get IP addresses from cp,
// and use clock for deciding when to re-get an IP address.
// and use clock for deciding when to re-get an IP address.
// Thread-safe.
// Thread-safe.
...
@@ -47,30 +33,24 @@ type ipCache struct {
...
@@ -47,30 +33,24 @@ type ipCache struct {
// that could be produced from a template and a type via `go generate`.
// that could be produced from a template and a type via `go generate`.
func
NewIPCache
(
cp
cloudprovider
.
Interface
,
clock
util
.
Clock
,
ttl
time
.
Duration
)
*
ipCache
{
func
NewIPCache
(
cp
cloudprovider
.
Interface
,
clock
util
.
Clock
,
ttl
time
.
Duration
)
*
ipCache
{
return
&
ipCache
{
return
&
ipCache
{
clock
:
clock
,
cache
:
util
.
NewTimeCache
(
cloudProvider
:
cp
,
clock
,
cache
:
map
[
string
]
ipCacheEntry
{},
ttl
,
ttl
:
ttl
,
func
(
host
string
)
util
.
T
{
return
getInstanceIPFromCloud
(
cp
,
host
)
},
),
}
}
}
}
type
ipCache
struct
{
cache
util
.
TimeCache
}
// GetInstanceIP returns the IP address of host, from the cache
// GetInstanceIP returns the IP address of host, from the cache
// if possible, otherwise it asks the cloud provider.
// if possible, otherwise it asks the cloud provider.
func
(
c
*
ipCache
)
GetInstanceIP
(
host
string
)
string
{
func
(
c
*
ipCache
)
GetInstanceIP
(
host
string
)
string
{
c
.
lock
.
Lock
()
return
c
.
cache
.
Get
(
host
)
.
(
string
)
defer
c
.
lock
.
Unlock
()
data
,
ok
:=
c
.
cache
[
host
]
now
:=
c
.
clock
.
Now
()
if
!
ok
||
now
.
Sub
(
data
.
lastUpdate
)
>
c
.
ttl
{
ip
:=
getInstanceIPFromCloud
(
c
.
cloudProvider
,
host
)
data
=
ipCacheEntry
{
ip
:
ip
,
lastUpdate
:
now
,
}
c
.
cache
[
host
]
=
data
}
return
data
.
ip
}
}
func
getInstanceIPFromCloud
(
cloud
cloudprovider
.
Interface
,
host
string
)
string
{
func
getInstanceIPFromCloud
(
cloud
cloudprovider
.
Interface
,
host
string
)
string
{
...
...
pkg/util/time_cache.go
0 → 100644
View file @
6e70853e
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
util
import
(
"sync"
"time"
)
// T stands in for any type in TimeCache
// Should make it easy to use this as a template for an autogenerator
// if we ever start doing that.
type
T
interface
{}
type
TimeCache
interface
{
// Get will fetch an item from the cache if
// it is present and recent enough.
Get
(
key
string
)
T
}
type
timeCacheEntry
struct
{
item
T
lastUpdate
time
.
Time
}
type
timeCache
struct
{
clock
Clock
fillFunc
func
(
string
)
T
cache
map
[
string
]
timeCacheEntry
lock
sync
.
Mutex
ttl
time
.
Duration
}
// NewTimeCache returns a cache which calls fill to fill its entries, and
// forgets entries after ttl has passed.
func
NewTimeCache
(
clock
Clock
,
ttl
time
.
Duration
,
fill
func
(
key
string
)
T
)
TimeCache
{
return
&
timeCache
{
clock
:
clock
,
fillFunc
:
fill
,
cache
:
map
[
string
]
timeCacheEntry
{},
ttl
:
ttl
,
}
}
// Get returns the value of key from the cache, if it is present
// and recent enough; otherwise, it blocks while it gets the value.
func
(
c
*
timeCache
)
Get
(
key
string
)
T
{
c
.
lock
.
Lock
()
defer
c
.
lock
.
Unlock
()
data
,
ok
:=
c
.
cache
[
key
]
now
:=
c
.
clock
.
Now
()
if
!
ok
||
now
.
Sub
(
data
.
lastUpdate
)
>
c
.
ttl
{
data
=
timeCacheEntry
{
item
:
c
.
fillFunc
(
key
),
lastUpdate
:
now
,
}
c
.
cache
[
key
]
=
data
}
return
data
.
item
}
pkg/util/time_cache_test.go
0 → 100644
View file @
6e70853e
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
util
import
(
"testing"
"time"
)
func
TestCacheExpire
(
t
*
testing
.
T
)
{
calls
:=
map
[
string
]
int
{}
ff
:=
func
(
key
string
)
T
{
calls
[
key
]
++
;
return
key
}
clock
:=
&
FakeClock
{
time
.
Now
()}
c
:=
NewTimeCache
(
clock
,
60
*
time
.
Second
,
ff
)
c
.
Get
(
"foo"
)
c
.
Get
(
"bar"
)
// This call should hit the cache, so we expect no additional calls
c
.
Get
(
"foo"
)
// Advance the clock, this call should miss the cache, so expect one more call.
clock
.
Time
=
clock
.
Time
.
Add
(
61
*
time
.
Second
)
c
.
Get
(
"foo"
)
c
.
Get
(
"bar"
)
if
e
,
a
:=
2
,
calls
[
"foo"
];
e
!=
a
{
t
.
Errorf
(
"Wrong number of calls for foo: wanted %v, got %v"
,
e
,
a
)
}
if
e
,
a
:=
2
,
calls
[
"bar"
];
e
!=
a
{
t
.
Errorf
(
"Wrong number of calls for bar: wanted %v, got %v"
,
e
,
a
)
}
}
func
TestCacheNotExpire
(
t
*
testing
.
T
)
{
calls
:=
map
[
string
]
int
{}
ff
:=
func
(
key
string
)
T
{
calls
[
key
]
++
;
return
key
}
clock
:=
&
FakeClock
{
time
.
Now
()}
c
:=
NewTimeCache
(
clock
,
60
*
time
.
Second
,
ff
)
c
.
Get
(
"foo"
)
// This call should hit the cache, so we expect no additional calls to the cloud
clock
.
Time
=
clock
.
Time
.
Add
(
60
*
time
.
Second
)
c
.
Get
(
"foo"
)
if
e
,
a
:=
1
,
calls
[
"foo"
];
e
!=
a
{
t
.
Errorf
(
"Wrong number of calls for foo: wanted %v, got %v"
,
e
,
a
)
}
}
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