-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Consul Session TTLs #524
Merged
Merged
Consul Session TTLs #524
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4732c36
Consul Session TTLs
amalaviy 624c465
Added more tests. Also added return of 404 if the session id to renew…
amalaviy 87e9d85
Added more tests
amalaviy 5229f3b
Clean up code based on feedback from armon
amalaviy 3821f3c
Took out StateSnapshot SessionListTTL also
amalaviy 0f9723e
Remove hardcoded wait time in session TTL tests
amalaviy 6091562
Took out usage of snapshot SessionListTTL
amalaviy ac54010
Fixed clearSessionTimer, created invalidateSession, added invalid TTL…
amalaviy 31e461e
Add invalidateSession test
amalaviy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request) | |
Checks: []string{consul.SerfCheckID}, | ||
LockDelay: 15 * time.Second, | ||
Behavior: structs.SessionKeysRelease, | ||
TTL: "", | ||
}, | ||
} | ||
s.parseDC(req, &args.Datacenter) | ||
|
@@ -51,6 +52,21 @@ func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request) | |
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) | ||
return nil, nil | ||
} | ||
|
||
if args.Session.TTL != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test with an invalid TTL to check this code path? |
||
ttl, err := time.ParseDuration(args.Session.TTL) | ||
if err != nil { | ||
resp.WriteHeader(400) | ||
resp.Write([]byte(fmt.Sprintf("Request TTL decode failed: %v", err))) | ||
return nil, nil | ||
} | ||
|
||
if ttl < structs.SessionTTLMin || ttl > structs.SessionTTLMax { | ||
resp.WriteHeader(400) | ||
resp.Write([]byte(fmt.Sprintf("Request TTL '%s', must be between [%v-%v]", args.Session.TTL, structs.SessionTTLMin, structs.SessionTTLMax))) | ||
return nil, nil | ||
} | ||
} | ||
} | ||
|
||
// Create the session, get the ID | ||
|
@@ -130,6 +146,39 @@ func (s *HTTPServer) SessionDestroy(resp http.ResponseWriter, req *http.Request) | |
return true, nil | ||
} | ||
|
||
// SessionRenew is used to renew the TTL on an existing TTL session | ||
func (s *HTTPServer) SessionRenew(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
// Mandate a PUT request | ||
if req.Method != "PUT" { | ||
resp.WriteHeader(405) | ||
return nil, nil | ||
} | ||
|
||
args := structs.SessionSpecificRequest{} | ||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done { | ||
return nil, nil | ||
} | ||
|
||
// Pull out the session id | ||
args.Session = strings.TrimPrefix(req.URL.Path, "/v1/session/renew/") | ||
if args.Session == "" { | ||
resp.WriteHeader(400) | ||
resp.Write([]byte("Missing session")) | ||
return nil, nil | ||
} | ||
|
||
var out structs.IndexedSessions | ||
if err := s.agent.RPC("Session.Renew", &args, &out); err != nil { | ||
return nil, err | ||
} else if out.Sessions == nil { | ||
resp.WriteHeader(404) | ||
resp.Write([]byte(fmt.Sprintf("Session id '%s' not found", args.Session))) | ||
return nil, nil | ||
} | ||
|
||
return out.Sessions, nil | ||
} | ||
|
||
// SessionGet is used to get info for a particular session | ||
func (s *HTTPServer) SessionGet(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
args := structs.SessionSpecificRequest{} | ||
|
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If TTL is set, we should also validate the value here. Avoid a trip to the server if the input is invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added checks to parseDuration after we've decoded the body.