mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
change command api to support expiration feature
This commit is contained in:
parent
e832f143db
commit
e4eb808434
@ -26,6 +26,7 @@ type Command interface {
|
|||||||
type SetCommand struct {
|
type SetCommand struct {
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
|
ExpireTime time.Time `json:"expireTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// The name of the command in the log
|
// The name of the command in the log
|
||||||
@ -35,7 +36,7 @@ func (c *SetCommand) CommandName() string {
|
|||||||
|
|
||||||
// Set the value of key to value
|
// Set the value of key to value
|
||||||
func (c *SetCommand) Apply(server *raft.Server) ([]byte, error) {
|
func (c *SetCommand) Apply(server *raft.Server) ([]byte, error) {
|
||||||
res := s.Set(c.Key, c.Value, time.Unix(0, 0))
|
res := s.Set(c.Key, c.Value, c.ExpireTime)
|
||||||
return json.Marshal(res)
|
return json.Marshal(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
handlers.go
19
handlers.go
@ -7,6 +7,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"time"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -103,7 +106,21 @@ func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
command := &SetCommand{}
|
command := &SetCommand{}
|
||||||
command.Key = key
|
command.Key = key
|
||||||
command.Value = string(content)
|
values := strings.Split(string(content), " ")
|
||||||
|
|
||||||
|
command.Value = values[0]
|
||||||
|
|
||||||
|
if len(values) == 2 {
|
||||||
|
duration, err := strconv.Atoi(values[1])
|
||||||
|
if err != nil {
|
||||||
|
warn("raftd: Bad duration: %v", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
command.ExpireTime = time.Now().Add(time.Second * (time.Duration)(duration))
|
||||||
|
} else {
|
||||||
|
command.ExpireTime = time.Unix(0,0)
|
||||||
|
}
|
||||||
|
|
||||||
Dispatch(server, command, w)
|
Dispatch(server, command, w)
|
||||||
}
|
}
|
||||||
|
2
store.go
2
store.go
@ -82,7 +82,7 @@ func (s *Store) Set(key string, value string, expireTime time.Time) Response {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response{SET, key, node.Value, value, true, time.Unix(0, 0)}
|
return Response{SET, key, node.Value, value, true, expireTime}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
update := make(chan time.Time)
|
update := make(chan time.Time)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user