Example #1
0
// Creates a sentinel client. Connects to the given sentinel instance, pulls the
// information for the masters of the given names, and creates an intial pool of
// connections for each master. The client will automatically replace the pool
// for any master should sentinel decide to fail the master over. The returned
// error is a *ClientError.
func NewClient(
	network, address string, poolSize int, names ...string,
) (
	*Client, error,
) {

	// We use this to fetch initial details about masters before we upgrade it
	// to a pubsub client
	client, err := redis.Dial(network, address)
	if err != nil {
		return nil, &ClientError{err: err}
	}

	masterPools := map[string]*pool.Pool{}
	for _, name := range names {
		r := client.Cmd("SENTINEL", "MASTER", name)
		l, err := r.List()
		if err != nil {
			return nil, &ClientError{err: err, SentinelErr: true}
		}
		addr := l[3] + ":" + l[5]
		pool, err := pool.NewPool("tcp", addr, poolSize)
		if err != nil {
			return nil, &ClientError{err: err}
		}
		masterPools[name] = pool
	}

	subClient := pubsub.NewSubClient(client)
	r := subClient.Subscribe("+switch-master")
	if r.Err != nil {
		return nil, &ClientError{err: r.Err, SentinelErr: true}
	}

	c := &Client{
		poolSize:       poolSize,
		masterPools:    masterPools,
		subClient:      subClient,
		getCh:          make(chan *getReq),
		putCh:          make(chan *putReq),
		closeCh:        make(chan struct{}),
		alwaysErrCh:    make(chan *ClientError),
		switchMasterCh: make(chan *switchMaster),
	}

	go c.subSpin()
	go c.spin()
	return c, nil
}
Example #2
0
func main() {
	var data string

	flag.Parse()
	// minimum amount of requests
	if *requests < 1000 {
		*requests = 1000
	}

	if *cpuprof != "" {
		f, err := os.Create(*cpuprof)
		if err != nil {
			fmt.Println(err)
			return
		}

		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	for i := 0; i < *dsize; i++ {
		data += "x"
	}

	c, err := redis.Dial("tcp", "127.0.0.1:6379")
	errHndlr(err)

	// select database
	r := c.Cmd("select", *database)
	errHndlr(err)

	r = c.Cmd("flushdb")
	errHndlr(r.Err)

	fmt.Printf(
		"Requests: %d, Payload: %d byte \n\n",
		*requests,
		*dsize)

	args := flag.Args()
	if len(args) == 0 {
		// run all tests by default
		args = []string{
			"ping",
			"set",
			"get",
			"incr",
			"lpush",
			"lpop",
			"sadd",
			"spop",
			"lrange",
			"lrange_100",
			"lrange_300",
			"lrange_450",
			"lrange_600",
			"mset",
		}
	}

	if testIsSelected(args, "ping") {
		benchmark(c, "PING", "ping")
	}
	if testIsSelected(args, "set") {
		benchmark(c, "SET", "set", "foo:rand:000000000000", data)
	}
	if testIsSelected(args, "get") {
		benchmark(c, "GET", "get", "foo:rand:000000000000")
	}
	if testIsSelected(args, "incr") {
		benchmark(c, "INCR", "incr", "counter:rand:000000000000")
	}
	if testIsSelected(args, "lpush") {
		benchmark(c, "LPUSH", "lpush", "mylist", data)
	}
	if testIsSelected(args, "lpop") {
		benchmark(c, "LPOP", "lpop", "mylist")
	}
	if testIsSelected(args, "sadd") {
		benchmark(c, "SADD", "sadd", "myset", "counter:rand:000000000000")
	}
	if testIsSelected(args, "spop") {
		benchmark(c, "SPOP", "spop", "myset")
	}
	if testIsSelected(args, "lrange") ||
		testIsSelected(args, "lrange_100") ||
		testIsSelected(args, "lrange_300") ||
		testIsSelected(args, "lrange_450") ||
		testIsSelected(args, "lrange_600") {
		benchmark(c, "LPUSH (needed to benchmark LRANGE)", "lpush", "mylist", data)
	}
	if testIsSelected(args, "lrange") || testIsSelected(args, "lrange_100") {
		benchmark(c, "LRANGE_100", "lrange", "mylist", 0, 99)
	}
	if testIsSelected(args, "lrange") || testIsSelected(args, "lrange_300") {
		benchmark(c, "LRANGE_300", "lrange", "mylist", 0, 299)
	}
	if testIsSelected(args, "lrange") || testIsSelected(args, "lrange_450") {
		benchmark(c, "LRANGE_450", "lrange", "mylist", 0, 449)
	}
	if testIsSelected(args, "lrange") || testIsSelected(args, "lrange_600") {
		benchmark(c, "LRANGE_600", "lrange", "mylist", 0, 599)
	}
	if testIsSelected(args, "mset") {
		args := make([]interface{}, 20)
		for i := 0; i < 20; i += 2 {
			args[i] = "foo:rand:000000000000"
			args[i+1] = data
		}
		benchmark(c, "MSET", "mset", args...)
	}

	r = c.Cmd("flushdb")
	errHndlr(r.Err)
}