Example #1
0
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)
Example #2
0
}

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
Example #3
0
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
}
Example #4
0
// RaftTick*RaftElectTicks.
func (c HiveConfig) RaftElectTimeout() time.Duration {
	return time.Duration(c.RaftElectTicks) * (c.RaftTick + c.RaftTickDelta)
}

// RaftHBTimeout returns the raft heartbeat timeout as RaftTick*RaftHBTicks.
func (c HiveConfig) RaftHBTimeout() time.Duration {
	return time.Duration(c.RaftHBTicks) * (c.RaftTick + c.RaftTickDelta)
}

var raftLogOnce sync.Once

// HiveOption represents a configuration option of a hive.
type HiveOption args.V

var addr = args.NewString(args.Flag("addr", "localhost:7677",
	"the server listening address used for both RPC and HTTP"))

// Addr represents the listening address of the hive used for both inter-hive
// RPC and its HTTP/web interface.
func Addr(a string) HiveOption { return HiveOption(addr(a)) }

var paddrs = args.NewString(args.Flag("paddrs", "",
	"address of peers. Seperate entries with a comma"))

// PeerAddrs represents the peer addresses of hive.
func PeerAddrs(pa ...string) HiveOption {
	return HiveOption(paddrs(strings.Join(pa, ",")))
}

var dataChBufSize = args.NewUint(args.Flag("chsize", uint(1024),
	"buffer size of data channels"))