Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop watchers before reconnecting to free their resources #14

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func (c *KubernetesClient) connect() (err error) {
ResourceVersion: c.lastResourceVersion,
}

defer func() {
if err != nil {
c.stopWatchers()
}
}()

ni := c.clientset.Core().Nodes()
c.nodeWatcher, err = ni.Watch(options)
if err != nil {
Expand All @@ -124,6 +130,18 @@ func (c *KubernetesClient) connect() (err error) {
return
}

func (c *KubernetesClient) stopWatchers() {
if c.nodeWatcher != nil {
c.nodeWatcher.Stop()
}
if c.serviceWatcher != nil {
c.serviceWatcher.Stop()
}
if c.endpointsWatcher != nil {
c.endpointsWatcher.Stop()
}
}

func (c *KubernetesClient) AddNotifier(n Notifier) {
c.notifiers = append(c.notifiers, n)
}
Expand Down Expand Up @@ -321,6 +339,7 @@ func (c *KubernetesClient) Watch() error {
}

if !more {
c.stopWatchers()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this Watch function, should it be added a defer func() (as in connect function) to stop watchers too?

Copy link
Contributor Author

@jsoriano jsoriano Jun 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, probably the first call to something like the connect method should be made here in Watch (and not in the constructor) as at the end it the only stateful things it creates are the watchers.

I'll do another PR with some refactorizations about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

log.Printf("Connection closed, trying to reconnect...")
timeout := time.Duration(reconnectTimeoutSeconds) * time.Second
err := wait.Poll(5*time.Second, timeout, func() (bool, error) {
Expand Down