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
b0f4517e
Commit
b0f4517e
authored
Apr 22, 2016
by
Hongchao Deng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
etcd3/watcher: cancelling context shouldn't return error
parent
06c2db4f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
2 deletions
+34
-2
watcher.go
pkg/storage/etcd3/watcher.go
+15
-2
watcher_test.go
pkg/storage/etcd3/watcher_test.go
+19
-0
No files found.
pkg/storage/etcd3/watcher.go
View file @
b0f4517e
...
@@ -31,6 +31,8 @@ import (
...
@@ -31,6 +31,8 @@ import (
etcdrpc
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
etcdrpc
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
"github.com/golang/glog"
"github.com/golang/glog"
"golang.org/x/net/context"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
)
const
(
const
(
...
@@ -78,6 +80,12 @@ func (w *watcher) Watch(ctx context.Context, key string, rev int64, recursive bo
...
@@ -78,6 +80,12 @@ func (w *watcher) Watch(ctx context.Context, key string, rev int64, recursive bo
if
recursive
&&
!
strings
.
HasSuffix
(
key
,
"/"
)
{
if
recursive
&&
!
strings
.
HasSuffix
(
key
,
"/"
)
{
key
+=
"/"
key
+=
"/"
}
}
wc
:=
w
.
createWatchChan
(
ctx
,
key
,
rev
,
recursive
,
filter
)
go
wc
.
run
()
return
wc
,
nil
}
func
(
w
*
watcher
)
createWatchChan
(
ctx
context
.
Context
,
key
string
,
rev
int64
,
recursive
bool
,
filter
storage
.
FilterFunc
)
*
watchChan
{
wc
:=
&
watchChan
{
wc
:=
&
watchChan
{
watcher
:
w
,
watcher
:
w
,
key
:
key
,
key
:
key
,
...
@@ -89,8 +97,7 @@ func (w *watcher) Watch(ctx context.Context, key string, rev int64, recursive bo
...
@@ -89,8 +97,7 @@ func (w *watcher) Watch(ctx context.Context, key string, rev int64, recursive bo
errChan
:
make
(
chan
error
,
1
),
errChan
:
make
(
chan
error
,
1
),
}
}
wc
.
ctx
,
wc
.
cancel
=
context
.
WithCancel
(
ctx
)
wc
.
ctx
,
wc
.
cancel
=
context
.
WithCancel
(
ctx
)
go
wc
.
run
()
return
wc
return
wc
,
nil
}
}
func
(
wc
*
watchChan
)
run
()
{
func
(
wc
*
watchChan
)
run
()
{
...
@@ -276,6 +283,12 @@ func parseError(err error) *watch.Event {
...
@@ -276,6 +283,12 @@ func parseError(err error) *watch.Event {
}
}
func
(
wc
*
watchChan
)
sendError
(
err
error
)
{
func
(
wc
*
watchChan
)
sendError
(
err
error
)
{
// Context.canceled is an expected behavior.
// We should just stop all goroutines in watchChan without returning error.
// TODO: etcd client should return context.Canceled instead of grpc specific error.
if
grpc
.
Code
(
err
)
==
codes
.
Canceled
||
err
==
context
.
Canceled
{
return
}
select
{
select
{
case
wc
.
errChan
<-
err
:
case
wc
.
errChan
<-
err
:
case
<-
wc
.
ctx
.
Done
()
:
case
<-
wc
.
ctx
.
Done
()
:
...
...
pkg/storage/etcd3/watcher_test.go
View file @
b0f4517e
...
@@ -168,6 +168,25 @@ func TestWatchError(t *testing.T) {
...
@@ -168,6 +168,25 @@ func TestWatchError(t *testing.T) {
testCheckResult
(
t
,
0
,
watch
.
Error
,
w
,
nil
)
testCheckResult
(
t
,
0
,
watch
.
Error
,
w
,
nil
)
}
}
func
TestWatchContextCancel
(
t
*
testing
.
T
)
{
ctx
,
store
,
cluster
:=
testSetup
(
t
)
defer
cluster
.
Terminate
(
t
)
canceledCtx
,
cancel
:=
context
.
WithCancel
(
ctx
)
cancel
()
w
:=
store
.
watcher
.
createWatchChan
(
canceledCtx
,
"/abc"
,
0
,
false
,
storage
.
Everything
)
// When we do a client.Get with a canceled context, it will return error.
// Nonetheless, when we try to send it over internal errChan, we should detect
// it's context canceled and not send it.
err
:=
w
.
sync
()
w
.
ctx
=
ctx
w
.
sendError
(
err
)
select
{
case
err
:=
<-
w
.
errChan
:
t
.
Errorf
(
"cancelling context shouldn't return any error. Err: %v"
,
err
)
default
:
}
}
type
testWatchStruct
struct
{
type
testWatchStruct
struct
{
obj
*
api
.
Pod
obj
*
api
.
Pod
expectEvent
bool
expectEvent
bool
...
...
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