etcd: replace -proxy-mode with -proxy

release-2.0
Brian Waldon 2014-09-18 07:50:52 -07:00
parent fcf50e756d
commit afce2948d2
3 changed files with 60 additions and 7 deletions

View File

@ -2,4 +2,4 @@
etcd1: ./etcd -id 0x1 -bind-addr 127.0.0.1:4001 -peer-bind-addr :7001 -peers '0x1=localhost:7001&0x2=localhost:7002&0x3=localhost:7003'
etcd2: ./etcd -id 0x2 -bind-addr 127.0.0.1:4002 -peer-bind-addr :7002 -peers '0x1=localhost:7001&0x2=localhost:7002&0x3=localhost:7003'
etcd3: ./etcd -id 0x3 -bind-addr 127.0.0.1:4003 -peer-bind-addr :7003 -peers '0x1=localhost:7001&0x2=localhost:7002&0x3=localhost:7003'
proxy: ./etcd -proxy-mode -bind-addr 127.0.0.1:8080 -peers '0x1=localhost:7001&0x2=localhost:7002&0x3=localhost:7003'
proxy: ./etcd -proxy=on -bind-addr 127.0.0.1:8080 -peers '0x1=localhost:7001&0x2=localhost:7002&0x3=localhost:7003'

42
main.go
View File

@ -24,6 +24,9 @@ import (
const (
// the owner can make/remove files inside the directory
privateDirMode = 0700
proxyFlagValueOff = "off"
proxyFlagValueOn = "on"
)
var (
@ -31,18 +34,25 @@ var (
timeout = flag.Duration("timeout", 10*time.Second, "Request Timeout")
paddr = flag.String("peer-bind-addr", ":7001", "Peer service address (e.g., ':7001')")
dir = flag.String("data-dir", "", "Path to the data directory")
proxyMode = flag.Bool("proxy-mode", false, "Forward HTTP requests to peers, do not participate in raft.")
snapCount = flag.Int64("snapshot-count", etcdserver.DefaultSnapCount, "Number of committed transactions to trigger a snapshot")
peers = &etcdhttp.Peers{}
addrs = &Addrs{}
peers = &etcdhttp.Peers{}
addrs = &Addrs{}
proxyFlag = new(ProxyFlag)
proxyFlagValues = []string{
proxyFlagValueOff,
proxyFlagValueOn,
}
)
func init() {
flag.Var(peers, "peers", "your peers")
flag.Var(addrs, "bind-addr", "List of HTTP service addresses (e.g., '127.0.0.1:4001,10.0.0.1:8080')")
flag.Var(proxyFlag, "proxy", fmt.Sprintf("Valid values include %s", strings.Join(proxyFlagValues, ", ")))
peers.Set("0x1=localhost:8080")
addrs.Set("127.0.0.1:4001")
proxyFlag.Set(proxyFlagValueOff)
}
func main() {
@ -50,10 +60,10 @@ func main() {
setFlagsFromEnv()
if *proxyMode {
startProxy()
} else {
if string(*proxyFlag) == proxyFlagValueOff {
startEtcd()
} else {
startProxy()
}
// Block indefinitely
@ -201,6 +211,26 @@ func (as *Addrs) String() string {
return strings.Join(*as, ",")
}
// ProxyFlag implements the flag.Value interface.
type ProxyFlag string
// Set verifies the argument to be a valid member of proxyFlagValues
// before setting the underlying flag value.
func (pf *ProxyFlag) Set(s string) error {
for _, v := range proxyFlagValues {
if s == v {
*pf = ProxyFlag(s)
return nil
}
}
return errors.New("invalid value")
}
func (pf *ProxyFlag) String() string {
return string(*pf)
}
// setFlagsFromEnv parses all registered flags in the global flagset,
// and if they are not already set it attempts to set their values from
// environment variables. Environment variables take the name of the flag but

View File

@ -41,3 +41,26 @@ func TestSetFlagsFromEnv(t *testing.T) {
}
}
}
func TestProxyFlagSet(t *testing.T) {
tests := []struct {
val string
pass bool
}{
// known values
{"on", true},
{"off", true},
// unrecognized values
{"foo", false},
{"", false},
}
for i, tt := range tests {
pf := new(ProxyFlag)
err := pf.Set(tt.val)
if tt.pass != (err == nil) {
t.Errorf("#%d: want pass=%t, but got err=%v", i, tt.pass, err)
}
}
}