Esempio n. 1
0
func TestPublishEvent(t *testing.T) {
	db := redisdb.New(dburl)
	if err := db.Connect(); err != nil {
		t.Fatalf("Failed to connect to redis: %s", err.Error())
	}

	c, err := redis.Dial("tcp", dburl)
	if err != nil {
		t.Fatalf("Failed to connect to redis2: %s", err.Error())
	}

	ps := pubsub.NewSubClient(c)
	ps.Subscribe("thechan")

	wg := sync.WaitGroup{}
	wg.Add(1)

	go func() {
		resp := ps.Receive()
		if resp.Message != "moar" {
			t.Errorf("Expected moar but got %s", resp.Message)
		}
		wg.Done()
	}()

	go func() {
		buf := bytes.NewBufferString("moar")
		if err := db.PublishEvent("thechan", buf); err != nil {
			t.Fatalf("Should have have errored publishing: %s", err.Error())
		}
	}()
	wg.Wait()
}
Esempio n. 2
0
// NewClientCustom is the same as NewClient, except it takes in a DialFunc which
// will be used to create all new connections to the master instances. This can
// be used to implement authentication, custom timeouts, etc...
func NewClientCustom(
	network, address string, poolSize int, df DialFunc, 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.NewCustom("tcp", addr, poolSize, (pool.DialFunc)(df))
		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,
		dialFunc:       (pool.DialFunc)(df),
		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
}
Esempio n. 3
0
func (t *Transport) subscribe(channel chan *api.Message) {
	conn, _ := t.getConnection() // todo handle messages better
	defer conn.Close()

	psc := pubsub.NewSubClient(conn)
	subClient := psc.Subscribe(t.InputBinding)
	log.Debugf("Processor subscribing to: %s", subClient)
	defer psc.Unsubscribe(t.InputBinding)

	for {
		resp := psc.Receive()
		//log.Debugln("after: ", resp)

		if resp.Err != nil {
			channel <- api.NewMessageFromRawBytes([]byte(resp.Err.Error()))
		} else {
			channel <- api.NewMessageFromRawBytes([]byte(resp.Message))
		}
	}
}