2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-06-16 22:35:15 +00:00
|
|
|
"errors"
|
2015-05-06 23:40:45 +00:00
|
|
|
"fmt"
|
2015-01-06 18:40:00 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-01-07 22:01:27 +00:00
|
|
|
const (
|
|
|
|
// SessionBehaviorRelease is the default behavior and causes
|
|
|
|
// all associated locks to be released on session invalidation.
|
|
|
|
SessionBehaviorRelease = "release"
|
|
|
|
|
|
|
|
// SessionBehaviorDelete is new in Consul 0.5 and changes the
|
|
|
|
// behavior to delete all associated locks on session invalidation.
|
|
|
|
// It can be used in a way similar to Ephemeral Nodes in ZooKeeper.
|
|
|
|
SessionBehaviorDelete = "delete"
|
|
|
|
)
|
|
|
|
|
2015-06-16 22:35:15 +00:00
|
|
|
var ErrSessionExpired = errors.New("session expired")
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// SessionEntry represents a session in consul
|
|
|
|
type SessionEntry struct {
|
|
|
|
CreateIndex uint64
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Node string
|
|
|
|
LockDelay time.Duration
|
|
|
|
Behavior string
|
|
|
|
TTL string
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace string `json:",omitempty"`
|
|
|
|
|
|
|
|
// Deprecated for Consul Enterprise in v1.7.0.
|
|
|
|
Checks []string
|
|
|
|
|
|
|
|
// NodeChecks and ServiceChecks are new in Consul 1.7.0.
|
|
|
|
// When associating checks with sessions, namespaces can be specified for service checks.
|
|
|
|
NodeChecks []string
|
|
|
|
ServiceChecks []ServiceCheck
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServiceCheck struct {
|
|
|
|
ID string
|
|
|
|
Namespace string
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Session can be used to query the Session endpoints
|
|
|
|
type Session struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session returns a handle to the session endpoints
|
|
|
|
func (c *Client) Session() *Session {
|
|
|
|
return &Session{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNoChecks is like Create but is used specifically to create
|
|
|
|
// a session with no associated health checks.
|
|
|
|
func (s *Session) CreateNoChecks(se *SessionEntry, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
body := make(map[string]interface{})
|
2019-12-10 02:26:41 +00:00
|
|
|
body["NodeChecks"] = []string{}
|
2015-01-06 18:40:00 +00:00
|
|
|
if se != nil {
|
|
|
|
if se.Name != "" {
|
|
|
|
body["Name"] = se.Name
|
|
|
|
}
|
|
|
|
if se.Node != "" {
|
|
|
|
body["Node"] = se.Node
|
|
|
|
}
|
|
|
|
if se.LockDelay != 0 {
|
|
|
|
body["LockDelay"] = durToMsec(se.LockDelay)
|
|
|
|
}
|
|
|
|
if se.Behavior != "" {
|
|
|
|
body["Behavior"] = se.Behavior
|
|
|
|
}
|
|
|
|
if se.TTL != "" {
|
|
|
|
body["TTL"] = se.TTL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.create(body, q)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create makes a new session. Providing a session entry can
|
|
|
|
// customize the session. It can also be nil to use defaults.
|
|
|
|
func (s *Session) Create(se *SessionEntry, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
var obj interface{}
|
|
|
|
if se != nil {
|
|
|
|
body := make(map[string]interface{})
|
|
|
|
obj = body
|
|
|
|
if se.Name != "" {
|
|
|
|
body["Name"] = se.Name
|
|
|
|
}
|
|
|
|
if se.Node != "" {
|
|
|
|
body["Node"] = se.Node
|
|
|
|
}
|
|
|
|
if se.LockDelay != 0 {
|
|
|
|
body["LockDelay"] = durToMsec(se.LockDelay)
|
|
|
|
}
|
|
|
|
if len(se.Checks) > 0 {
|
|
|
|
body["Checks"] = se.Checks
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
if len(se.NodeChecks) > 0 {
|
|
|
|
body["NodeChecks"] = se.NodeChecks
|
|
|
|
}
|
|
|
|
if len(se.ServiceChecks) > 0 {
|
|
|
|
body["ServiceChecks"] = se.ServiceChecks
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
if se.Behavior != "" {
|
|
|
|
body["Behavior"] = se.Behavior
|
|
|
|
}
|
|
|
|
if se.TTL != "" {
|
|
|
|
body["TTL"] = se.TTL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.create(obj, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) create(obj interface{}, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
var out struct{ ID string }
|
2015-02-11 22:01:29 +00:00
|
|
|
wm, err := s.c.write("/v1/session/create", obj, &out, q)
|
|
|
|
if err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return out.ID, wm, nil
|
|
|
|
}
|
|
|
|
|
2015-09-15 12:22:08 +00:00
|
|
|
// Destroy invalidates a given session
|
2015-01-06 18:40:00 +00:00
|
|
|
func (s *Session) Destroy(id string, q *WriteOptions) (*WriteMeta, error) {
|
2015-02-11 22:01:29 +00:00
|
|
|
wm, err := s.c.write("/v1/session/destroy/"+id, nil, nil, q)
|
2015-01-06 18:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renew renews the TTL on a given session
|
|
|
|
func (s *Session) Renew(id string, q *WriteOptions) (*SessionEntry, *WriteMeta, error) {
|
2015-06-16 22:35:15 +00:00
|
|
|
r := s.c.newRequest("PUT", "/v1/session/renew/"+id)
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
rtt, resp, err := s.c.doRequest(r)
|
2015-01-06 18:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-06-16 22:35:15 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return nil, wm, nil
|
|
|
|
} else if resp.StatusCode != 200 {
|
|
|
|
return nil, nil, fmt.Errorf("Unexpected response code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var entries []*SessionEntry
|
|
|
|
if err := decodeBody(resp, &entries); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to read response: %v", err)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
if len(entries) > 0 {
|
|
|
|
return entries[0], wm, nil
|
|
|
|
}
|
|
|
|
return nil, wm, nil
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:53:47 +00:00
|
|
|
// RenewPeriodic is used to periodically invoke Session.Renew on a
|
|
|
|
// session until a doneCh is closed. This is meant to be used in a long running
|
|
|
|
// goroutine to ensure a session stays valid.
|
2017-07-14 23:31:44 +00:00
|
|
|
func (s *Session) RenewPeriodic(initialTTL string, id string, q *WriteOptions, doneCh <-chan struct{}) error {
|
2017-07-15 00:30:08 +00:00
|
|
|
ctx := q.Context()
|
|
|
|
|
2015-01-20 02:53:47 +00:00
|
|
|
ttl, err := time.ParseDuration(initialTTL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-06 23:40:45 +00:00
|
|
|
|
|
|
|
waitDur := ttl / 2
|
|
|
|
lastRenewTime := time.Now()
|
|
|
|
var lastErr error
|
2015-01-20 02:53:47 +00:00
|
|
|
for {
|
2015-05-06 23:40:45 +00:00
|
|
|
if time.Since(lastRenewTime) > ttl {
|
|
|
|
return lastErr
|
|
|
|
}
|
2015-01-20 02:53:47 +00:00
|
|
|
select {
|
2015-05-06 23:40:45 +00:00
|
|
|
case <-time.After(waitDur):
|
2015-01-20 02:53:47 +00:00
|
|
|
entry, _, err := s.Renew(id, q)
|
|
|
|
if err != nil {
|
2015-05-06 23:40:45 +00:00
|
|
|
waitDur = time.Second
|
|
|
|
lastErr = err
|
|
|
|
continue
|
2015-01-20 02:53:47 +00:00
|
|
|
}
|
|
|
|
if entry == nil {
|
2015-06-16 22:35:15 +00:00
|
|
|
return ErrSessionExpired
|
2015-01-20 02:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the server updating the TTL
|
|
|
|
ttl, _ = time.ParseDuration(entry.TTL)
|
2015-05-06 23:40:45 +00:00
|
|
|
waitDur = ttl / 2
|
|
|
|
lastRenewTime = time.Now()
|
2015-01-20 02:53:47 +00:00
|
|
|
|
|
|
|
case <-doneCh:
|
|
|
|
// Attempt a session destroy
|
|
|
|
s.Destroy(id, q)
|
|
|
|
return nil
|
2017-07-15 00:30:08 +00:00
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Bail immediately since attempting the destroy would
|
|
|
|
// use the canceled context in q, which would just bail.
|
|
|
|
return ctx.Err()
|
2015-01-20 02:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// Info looks up a single session
|
|
|
|
func (s *Session) Info(id string, q *QueryOptions) (*SessionEntry, *QueryMeta, error) {
|
|
|
|
var entries []*SessionEntry
|
2015-02-11 21:53:33 +00:00
|
|
|
qm, err := s.c.query("/v1/session/info/"+id, &entries, q)
|
|
|
|
if err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if len(entries) > 0 {
|
|
|
|
return entries[0], qm, nil
|
|
|
|
}
|
|
|
|
return nil, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List gets sessions for a node
|
|
|
|
func (s *Session) Node(node string, q *QueryOptions) ([]*SessionEntry, *QueryMeta, error) {
|
|
|
|
var entries []*SessionEntry
|
2015-02-11 21:53:33 +00:00
|
|
|
qm, err := s.c.query("/v1/session/node/"+node, &entries, q)
|
|
|
|
if err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return entries, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List gets all active sessions
|
|
|
|
func (s *Session) List(q *QueryOptions) ([]*SessionEntry, *QueryMeta, error) {
|
|
|
|
var entries []*SessionEntry
|
2015-02-11 21:53:33 +00:00
|
|
|
qm, err := s.c.query("/v1/session/list", &entries, q)
|
|
|
|
if err != nil {
|
2015-01-06 18:40:00 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return entries, qm, nil
|
|
|
|
}
|