Add an atomic bool to track if a watch is stopped
Saw a watch channel close once without actually calling watch.Stop. Need to investigate this a bit and handle it if necessary.
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"k8s.io/client-go/1.5/kubernetes"
|
"k8s.io/client-go/1.5/kubernetes"
|
||||||
"k8s.io/client-go/1.5/pkg/api"
|
"k8s.io/client-go/1.5/pkg/api"
|
||||||
@@ -43,6 +44,7 @@ type (
|
|||||||
watchSubscription struct {
|
watchSubscription struct {
|
||||||
fission.Watch
|
fission.Watch
|
||||||
kubeWatch watch.Interface
|
kubeWatch watch.Interface
|
||||||
|
stopped *int32
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeWatcherRequest struct {
|
kubeWatcherRequest struct {
|
||||||
@@ -159,9 +161,11 @@ func (kw *KubeWatcher) addWatch(w *fission.Watch) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
var stopped int32 = 0
|
||||||
ws := &watchSubscription{
|
ws := &watchSubscription{
|
||||||
Watch: *w,
|
Watch: *w,
|
||||||
kubeWatch: wi,
|
kubeWatch: wi,
|
||||||
|
stopped: &stopped,
|
||||||
}
|
}
|
||||||
kw.watches[w.Metadata.Uid] = *ws
|
kw.watches[w.Metadata.Uid] = *ws
|
||||||
go ws.eventDispatchLoop(kw.poster)
|
go ws.eventDispatchLoop(kw.poster)
|
||||||
@@ -175,8 +179,9 @@ func (kw *KubeWatcher) removeWatch(w *fission.Watch) error {
|
|||||||
return fission.MakeError(fission.ErrorNotFound,
|
return fission.MakeError(fission.ErrorNotFound,
|
||||||
fmt.Sprintf("watch doesn't exist: %v", w.Metadata))
|
fmt.Sprintf("watch doesn't exist: %v", w.Metadata))
|
||||||
}
|
}
|
||||||
ws.kubeWatch.Stop()
|
|
||||||
delete(kw.watches, w.Metadata.Uid)
|
delete(kw.watches, w.Metadata.Uid)
|
||||||
|
atomic.StoreInt32(ws.stopped, 1)
|
||||||
|
ws.kubeWatch.Stop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +191,7 @@ func (ws *watchSubscription) eventDispatchLoop(poster *Poster) {
|
|||||||
ev, more := <-ws.kubeWatch.ResultChan()
|
ev, more := <-ws.kubeWatch.ResultChan()
|
||||||
if !more {
|
if !more {
|
||||||
log.Println("Watch stopped", ws.Watch.Metadata.Name)
|
log.Println("Watch stopped", ws.Watch.Metadata.Name)
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
@@ -196,4 +201,8 @@ func (ws *watchSubscription) eventDispatchLoop(poster *Poster) {
|
|||||||
}
|
}
|
||||||
poster.Post(string(ev.Type), ws.Watch.ObjType, ws.Watch.Url, &buf)
|
poster.Post(string(ev.Type), ws.Watch.ObjType, ws.Watch.Url, &buf)
|
||||||
}
|
}
|
||||||
|
if !atomic.LoadInt32(ws.stopped) {
|
||||||
|
// TODO re-watch. What about resource version?
|
||||||
|
log.Panicf("Watch channel closed unexpectedly")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user