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
378cd81d
Commit
378cd81d
authored
Sep 08, 2016
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Split dispatching to watchers in Cacher into separate goroutine.
parent
d877967c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
10 deletions
+28
-10
cacher.go
pkg/storage/cacher.go
+28
-10
No files found.
pkg/storage/cacher.go
View file @
378cd81d
...
@@ -161,6 +161,9 @@ type Cacher struct {
...
@@ -161,6 +161,9 @@ type Cacher struct {
watcherIdx
int
watcherIdx
int
watchers
indexedWatchers
watchers
indexedWatchers
// Incoming events that should be dispatched to watchers.
incoming
chan
watchCacheEvent
// Handling graceful termination.
// Handling graceful termination.
stopLock
sync
.
RWMutex
stopLock
sync
.
RWMutex
stopped
bool
stopped
bool
...
@@ -197,6 +200,8 @@ func NewCacherFromConfig(config CacherConfig) *Cacher {
...
@@ -197,6 +200,8 @@ func NewCacherFromConfig(config CacherConfig) *Cacher {
allWatchers
:
make
(
map
[
int
]
*
cacheWatcher
),
allWatchers
:
make
(
map
[
int
]
*
cacheWatcher
),
valueWatchers
:
make
(
map
[
string
]
watchersMap
),
valueWatchers
:
make
(
map
[
string
]
watchersMap
),
},
},
// TODO: Figure out the correct value for the buffer size.
incoming
:
make
(
chan
watchCacheEvent
,
100
),
// We need to (potentially) stop both:
// We need to (potentially) stop both:
// - wait.Until go-routine
// - wait.Until go-routine
// - reflector.ListAndWatch
// - reflector.ListAndWatch
...
@@ -205,6 +210,7 @@ func NewCacherFromConfig(config CacherConfig) *Cacher {
...
@@ -205,6 +210,7 @@ func NewCacherFromConfig(config CacherConfig) *Cacher {
stopCh
:
make
(
chan
struct
{}),
stopCh
:
make
(
chan
struct
{}),
}
}
watchCache
.
SetOnEvent
(
cacher
.
processEvent
)
watchCache
.
SetOnEvent
(
cacher
.
processEvent
)
go
cacher
.
dispatchEvents
()
stopCh
:=
cacher
.
stopCh
stopCh
:=
cacher
.
stopCh
cacher
.
stopWg
.
Add
(
1
)
cacher
.
stopWg
.
Add
(
1
)
...
@@ -403,14 +409,26 @@ func (c *Cacher) triggerValues(event *watchCacheEvent) ([]string, bool) {
...
@@ -403,14 +409,26 @@ func (c *Cacher) triggerValues(event *watchCacheEvent) ([]string, bool) {
return
result
,
len
(
result
)
>
0
return
result
,
len
(
result
)
>
0
}
}
// TODO: Most probably splitting this method to a separate thread will visibily
// improve throughput of our watch machinery. So what we should do is to:
// - OnEvent handler simply put an element to channel
// - processEvent be another goroutine processing events from that channel
// Additionally, if we make this channel buffered, cacher will be more resistant
// to single watchers being slow - see cacheWatcher::add method.
func
(
c
*
Cacher
)
processEvent
(
event
watchCacheEvent
)
{
func
(
c
*
Cacher
)
processEvent
(
event
watchCacheEvent
)
{
triggerValues
,
supported
:=
c
.
triggerValues
(
&
event
)
c
.
incoming
<-
event
}
func
(
c
*
Cacher
)
dispatchEvents
()
{
for
{
select
{
case
event
,
ok
:=
<-
c
.
incoming
:
if
!
ok
{
return
}
c
.
dispatchEvent
(
&
event
)
case
<-
c
.
stopCh
:
return
}
}
}
func
(
c
*
Cacher
)
dispatchEvent
(
event
*
watchCacheEvent
)
{
triggerValues
,
supported
:=
c
.
triggerValues
(
event
)
c
.
Lock
()
c
.
Lock
()
defer
c
.
Unlock
()
defer
c
.
Unlock
()
...
@@ -614,10 +632,10 @@ func (c *cacheWatcher) stop() {
...
@@ -614,10 +632,10 @@ func (c *cacheWatcher) stop() {
var
timerPool
sync
.
Pool
var
timerPool
sync
.
Pool
func
(
c
*
cacheWatcher
)
add
(
event
watchCacheEvent
)
{
func
(
c
*
cacheWatcher
)
add
(
event
*
watchCacheEvent
)
{
// Try to send the event immediately, without blocking.
// Try to send the event immediately, without blocking.
select
{
select
{
case
c
.
input
<-
event
:
case
c
.
input
<-
*
event
:
return
return
default
:
default
:
}
}
...
@@ -636,7 +654,7 @@ func (c *cacheWatcher) add(event watchCacheEvent) {
...
@@ -636,7 +654,7 @@ func (c *cacheWatcher) add(event watchCacheEvent) {
defer
timerPool
.
Put
(
t
)
defer
timerPool
.
Put
(
t
)
select
{
select
{
case
c
.
input
<-
event
:
case
c
.
input
<-
*
event
:
stopped
:=
t
.
Stop
()
stopped
:=
t
.
Stop
()
if
!
stopped
{
if
!
stopped
{
// Consume triggered (but not yet received) timer event
// Consume triggered (but not yet received) timer event
...
...
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