package args_test import ( "fmt" "net/http" "time" "github.com/kandoo/beehive/Godeps/_workspace/src/github.com/soheilhy/args" ) // port is an integer argument that its default value is read from // the "-example.typed.port" flag. var port = args.NewInt(args.Flag("example.typed.port", 1234, "the port")) // roundTripper is a generic argument that its default value is // http.DefaultTransport. var roundTripper = args.New(args.Default(http.DefaultTransport)) // timeout is a duration argument. var timeout = args.NewDuration() type ServerOpt args.V // Port, RoundTripper, and Timeout respectively wrap port, roundTripper, // and timeout to return ServerOpt instead of args.V. func Port(p int) ServerOpt { return ServerOpt(port(p)) } func RoundTripper(r http.RoundTripper) ServerOpt { return ServerOpt(roundTripper(r)) } func Timeout(d time.Duration) ServerOpt { return ServerOpt(timeout(d)) } func MyServer(opts ...ServerOpt) { port := port.Get(opts)
} func (h *ConnHandler) Stop(ctx beehive.RcvContext) { h.conn.Close() <-h.done } func (h *ConnHandler) Rcv(msg beehive.Msg, ctx beehive.RcvContext) error { res := msg.Data().(writerTo) if err := res.writeTo(h.w); err != nil { return err } return h.w.Flush() } var repl = args.NewInt(args.Flag("taskq.repl", 3, "replication factor of taskq")) var addr = args.NewString(args.Flag("taskq.addr", ":7979", "listening address of taskq server")) var rate = args.NewUint64(args.Flag("taskq.maxrate", uint64(bucket.Unlimited), "maximum message rate of each client")) // Option represents TaskQ server options. type Option args.V // ReplicationFactor is an option represeting the replication factor of TaskQ. func ReplicationFactor(f int) Option { return Option(repl(f)) } // Address is an option representing a TaskQ address. func Address(a string) Option { return Option(addr(a)) } // MaxRate is an option represeting the maximum rate of messages a client
package args_test import ( "fmt" "github.com/kandoo/beehive/Godeps/_workspace/src/github.com/soheilhy/args" ) var ListenOn = args.NewInt(args.Default(8080)) var BufferSize = args.NewUint64(args.Default(uint64(1024 * 1024))) var StateDir = args.NewString(args.Flag("test.state.dir", "/tmp", "state dir")) func Server(opts ...args.V) { port := ListenOn.Get(opts) bufs := BufferSize.Get(opts) sdir := StateDir.Get(opts) fmt.Printf("port=%d buf=%d state=%s\n", port, bufs, sdir) } func Example() { Server() Server(ListenOn(80), BufferSize(2048*1024), StateDir("/tmp2")) // Output: //port=8080 buf=1048576 state=/tmp //port=80 buf=2097152 state=/tmp2 }
// RaftTickDelta represents the random tick to add to the main raft tick. func RaftTickDelta(d time.Duration) HiveOption { return HiveOption(raftTickDelta(d)) } var raftFsyncTick = args.NewDuration(args.Flag("raftfsync", 1*time.Second, "the frequency of raft fsync. 0 means always sync immidiately")) // RaftFsyncTick represents when the hive should call fsync on written entires. // 0 means immidiately after each write. func RaftFsyncTick(t time.Duration) HiveOption { return HiveOption(raftFsyncTick(t)) } var raftElectTicks = args.NewInt(args.Flag("raftelectionticks", 5, "number of raft ticks to start an election (ie, election timeout)")) // RaftElectTicks represents the number of ticks to start a new raft election. func RaftElectTicks(e int) HiveOption { return HiveOption(raftElectTicks(e)) } var raftHbeatTicks = args.NewInt(args.Flag("rafthbticks", 1, "number of raft ticks to fire a heartbeat (ie, heartbeat timeout)")) // RaftHbeatTicks represents the number of ticks to send a new raft heartbeat. func RaftHbeatTicks(h int) HiveOption { return HiveOption(raftHbeatTicks(h)) } // TODO(soheil): use a better set of default values. var raftInFlights = args.NewInt(args.Flag("raftmaxinflights", 4096/8, "maximum number of inflight raft append messages")) // RaftInFlights represents the maximum number of raft messages in flight.