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

Add TTL for etcd sd #413

Merged
merged 2 commits into from
Jan 9, 2017
Merged

Add TTL for etcd sd #413

merged 2 commits into from
Jan 9, 2017

Conversation

buptmiao
Copy link
Contributor

Add TTL for registering keys to etcd, with backward compatibility。

relevant issue: #343

Users can set TTL options like this:

       registrar := etcd.NewRegistrar(sdClient, etcd.Service{
		Key:   key,
		Value: value,
		TTL: etcd.NewTTLOption(time.Second, time.Second * 3),
	}, log.NewNopLogger())

Copy link
Member

@peterbourgon peterbourgon left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the submission, and I'm sorry it took me so long—I overlooked the notification when it came originally! This will be a welcome addition. There are several things that need attention, please let me know if any of them are unclear.

@@ -4,13 +4,19 @@ import (
etcd "github.com/coreos/etcd/client"

"github.com/go-kit/kit/log"
"time"
Copy link
Member

Choose a reason for hiding this comment

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

This should be grouped with the other stdlib imports.

)

const (
MinHeartBeatTime = time.Millisecond * 500
)
Copy link
Member

Choose a reason for hiding this comment

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

This constant should either be made private (I think that's preferable) or given a doc comment.

DeleteOptions *etcd.DeleteOptions
}

// TTLOption allow setting a key with a TTL, and regularly refreshes the lease with a goroutine
Copy link
Member

Choose a reason for hiding this comment

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

This comment describes the mechanics of what happens, but not what benefit it provides. Can you amend the comment to tell the user in which circumstances they would want to use this option? Also, please wrap the comment at 80 columns, and use proper punctuation (i.e. add a period to the end of the sentence).

type TTLOption struct {
Heartbeat time.Duration
TTL time.Duration
}
Copy link
Member

Choose a reason for hiding this comment

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

Given that a TTLOption is constructed with a constructor, I don't think either of these fields needs to be exported. Is that correct?

}

// NewTTLOption returns a TTLOption
func NewTTLOption(heartbeat, ttl time.Duration) *TTLOption {
Copy link
Member

Choose a reason for hiding this comment

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

The doc comment here is a little sparse. Could it please describe what each of the parameters are, and some good default values? It would also be good to describe the bounds-checking logic implemented in the function body.

}
}
}
}()
Copy link
Member

Choose a reason for hiding this comment

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

Please extract this to a loop method on the registrar type.

select {
case <-r.quit:
return
case <-time.After(r.service.TTL.Heartbeat):
Copy link
Member

Choose a reason for hiding this comment

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

This will generate a lot of unnecessary garbage. It's better to use a time.Ticker, constructed outside of the for loop.

if r.service.TTL == nil {
return
}
r.quit = make(chan struct{})
Copy link
Member

Choose a reason for hiding this comment

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

This mutates registrar state, which makes it unsafe for concurrent access from multiple goroutines. I think the registrar needs its internal state protected with a mutex, which should be taken with every public method.

@@ -53,4 +93,7 @@ func (r *Registrar) Deregister() {
} else {
r.logger.Log("action", "deregister")
}
if r.quit != nil {
close(r.quit)
}
Copy link
Member

Choose a reason for hiding this comment

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

It looks like r.quit should also be reset to nil.

if s.TTL != nil {
_, err := c.keysAPI.Set(c.ctx, s.Key, s.Value, &etcd.SetOptions{PrevExist: etcd.PrevIgnore, TTL: s.TTL.TTL})
return err
}
_, err := c.keysAPI.Create(c.ctx, s.Key, s.Value)
return err
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. What do you think about

var err error
if s.TTL != nil {
    _, err = c.keysAPI.Set(...)
} else {
    _, err = c.keysAPI.Create(...)
}
return err

@buptmiao
Copy link
Contributor Author

buptmiao commented Jan 7, 2017

Thank you for your attention and advice. I have fixed them.

@peterbourgon
Copy link
Member

Amazing, thank you! There are a couple of other small things, but I'll make the changes in master after the merge.

@peterbourgon peterbourgon merged commit 9f4e759 into go-kit:master Jan 9, 2017
jamesgist pushed a commit to jamesgist/kit that referenced this pull request Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants