mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
boom
This commit is contained in:
parent
c87f1d3924
commit
cb7b321240
@ -10,6 +10,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -31,6 +32,35 @@ func (ps Peers) Pick(id int64) string {
|
|||||||
return fmt.Sprintf("http://%s", addrs[rand.Intn(len(addrs))])
|
return fmt.Sprintf("http://%s", addrs[rand.Intn(len(addrs))])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set parses command line sets of names to ips formatted like:
|
||||||
|
// a=1.1.1.1&a=1.1.1.2&b=2.2.2.2
|
||||||
|
func (ps Peers) Set(s string) error {
|
||||||
|
v, err := url.ParseQuery(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for k, v := range v {
|
||||||
|
id, err := strconv.ParseInt(k, 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ps[id] = v
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ps Peers) String() string {
|
||||||
|
return "todo"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ps Peers) Ids() []int64 {
|
||||||
|
var ids []int64
|
||||||
|
for id, _ := range ps {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
var errClosed = errors.New("etcdhttp: client closed connection")
|
var errClosed = errors.New("etcdhttp: client closed connection")
|
||||||
|
|
||||||
const DefaultTimeout = 500 * time.Millisecond
|
const DefaultTimeout = 500 * time.Millisecond
|
||||||
|
29
main.go
29
main.go
@ -4,6 +4,8 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
etcdserver "github.com/coreos/etcd/etcdserver2"
|
etcdserver "github.com/coreos/etcd/etcdserver2"
|
||||||
"github.com/coreos/etcd/etcdserver2/etcdhttp"
|
"github.com/coreos/etcd/etcdserver2/etcdhttp"
|
||||||
@ -12,25 +14,34 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
id = flag.String("id", "default", "The id of this server")
|
fid = flag.String("id", "default", "Id of this server")
|
||||||
|
timeout = flag.Duration("timeout", 10*time.Second, "Request Timeout")
|
||||||
|
laddr = flag.String("l", ":8080", "HTTP service address (e.g., ':8080')")
|
||||||
|
|
||||||
|
peers = etcdhttp.Peers{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func init() {
|
||||||
const V2Prefix = "/v2"
|
flag.Var(peers, "peers", "your peers")
|
||||||
|
}
|
||||||
|
|
||||||
peers := etcdhttp.Discover(V2Prefix, peerips)
|
func main() {
|
||||||
n := raft.Start(*id, peers.Ids())
|
id, err := strconv.ParseInt(*fid, 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
n := raft.Start(id, peers.Ids())
|
||||||
s := &etcdserver.Server{
|
s := &etcdserver.Server{
|
||||||
Node: n,
|
Node: n,
|
||||||
Save: func(st raftpb.State, ents []raftpb.Entry) {}, // TODO: use wal
|
Save: func(st raftpb.State, ents []raftpb.Entry) {}, // TODO: use wal
|
||||||
Send: etcdhttp.Sender(V2Prefix),
|
Send: etcdhttp.Sender(peers),
|
||||||
}
|
}
|
||||||
etcdserver.Start(s)
|
etcdserver.Start(s)
|
||||||
h := &etcdhttp.Handler{
|
h := &etcdhttp.Handler{
|
||||||
Timeout: timeout,
|
Timeout: *timeout,
|
||||||
Server: s,
|
Server: s,
|
||||||
Peers: peers,
|
|
||||||
}
|
}
|
||||||
http.Handle(V2Prefix, http.StripPrefix(V2Prefix, h))
|
http.Handle("/", h)
|
||||||
log.Fatal(http.ListenAndServe(*laddr, nil))
|
log.Fatal(http.ListenAndServe(*laddr, nil))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user