Exemplo n.º 1
0
func newTestCluster(size int, prefixAddr string) ([]*drax.Cluster, error) {
	var nodes []*drax.Cluster
	for i := 0; i < size; i++ {
		addr := prefixAddr + strconv.Itoa(i)
		home, err := ioutil.TempDir("", addr)
		if err != nil {
			return nil, err
		}
		l := sockets.NewInmemSocket(addr, 1)
		clusterDialers[addr] = l.Dial
		dialer := func(addr string, timeout time.Duration) (net.Conn, error) {
			h, ok := clusterDialers[addr]
			if !ok {
				return nil, fmt.Errorf("unreachable")
			}
			return h("inmem", addr)
		}
		var peer string
		if i > 0 {
			peer = nodes[i-1].Addr()
		}
		c, err := drax.New(l, dialer, home, addr, peer, nil)
		if err != nil {
			return nil, err
		}
		nodes = append(nodes, c)
	}
	return nodes, nil
}
Exemplo n.º 2
0
Arquivo: kv.go Projeto: cpuguy83/drax
func main() {
	flag.Parse()
	logrus.SetLevel(logrus.DebugLevel)

	var (
		tlsConfig *tls.Config
		l         net.Listener
		err       error
		dialer    rpc.DialerFn
	)

	if tlsConfig == nil {
		l, err = net.Listen("tcp", *flAddr)
		dialer = func(addr string, timeout time.Duration) (net.Conn, error) {
			return net.DialTimeout("tcp", addr, timeout)
		}
	} else {
		l, err = tls.Listen("tcp", *flAddr, tlsConfig)
		d := &net.Dialer{
			Timeout: 30 * time.Second,
		}
		dialer = func(addr string, timeout time.Duration) (net.Conn, error) {
			return tls.DialWithDialer(d, "tcp", *flAddr, tlsConfig)
		}
	}
	if err != nil {
		logrus.Fatalf("error setting up listener: %v", err)
	}

	cluster, err := drax.New(l, dialer, *flHome, l.Addr().String(), *flPeer, nil)
	if err != nil {
		logrus.Fatalf("Error setting up cluster: %v", err)
	}

	signal.Trap(func() {
		logrus.Warn("Shutting down")
		cluster.Shutdown()
	})

	for {
		select {
		case err := <-cluster.Errors():
			if err != nil {
				logrus.Error(err)
			}
		case <-cluster.ShutdownCh():
			return
		}
	}
}