Created
February 24, 2017 21:48
-
-
Save slackpad/3729243ac729673625a646be356f930d to your computer and use it in GitHub Desktop.
Consul ephemeral key example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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