Skip to content

Instantly share code, notes, and snippets.

@slackpad
Created February 24, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slackpad/3729243ac729673625a646be356f930d to your computer and use it in GitHub Desktop.
Save slackpad/3729243ac729673625a646be356f930d to your computer and use it in GitHub Desktop.
Consul ephemeral key example
package main
import (
"log"
"github.com/hashicorp/consul/api"
)
func main() {
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
log.Fatalf("Failed to create Consul client: %v", err)
}
sess := client.Session()
id, _, err := sess.Create(&api.SessionEntry{
Behavior: api.SessionBehaviorDelete,
TTL: "10s",
}, nil)
if err != nil {
log.Fatalf("Failed to create session: %v", err)
}
kv := client.KV()
ok, _, err := kv.Acquire(&api.KVPair{
Key: "ephemeral",
Session: id,
}, nil)
if err != nil {
log.Fatalf("Failed to create KV pair: %v", err)
}
if !ok {
log.Fatalf("Failed to acquire lock")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment