Пример #1
0
func NewConsumer(amqpURI, exchange string, qos int) (*Consumer, error) {
	queueName := utils.GetLocalIP()
	ctag := queueName + "_tag"
	c := &Consumer{
		conn:    nil,
		channel: nil,
		queue:   queueName,
		tag:     ctag,
		done:    make(chan error),
	}

	var err error

	log.Infof("dialing %q", amqpURI)
	c.conn, err = amqp.Dial(amqpURI)
	if err != nil {
		return nil, fmt.Errorf("Dial: %s", err)
	}

	/*go func() {
		log.Infof("closing: %s", <-c.conn.NotifyClose(make(chan *amqp.Error)))
	}()*/

	log.Infof("got Connection, getting Channel")
	c.channel, err = c.conn.Channel()
	if err != nil {
		return nil, fmt.Errorf("Channel: %s", err)
	}

	queue, err := c.channel.QueueDeclare(
		queueName, // name of the queue
		true,      // durable
		false,     // delete when usused
		false,     // exclusive
		false,     // noWait
		nil,       // arguments
	)
	if err != nil {
		return nil, fmt.Errorf("Queue Declare: %s", err)
	}

	log.Infof("declared Queue (%q %d messages, %d consumers), binding to Exchange (%s)",
		queue.Name, queue.Messages, queue.Consumers, exchange)

	if err = c.channel.QueueBind(
		queue.Name, // name of the queue
		"",         // bindingKey
		exchange,   // sourceExchange
		false,      // noWait
		nil,        // arguments
	); err != nil {
		return nil, fmt.Errorf("Queue Bind: %s", err)
	}

	c.channel.Qos(qos, 0, false)
	return c, nil
}
Пример #2
0
func NewServer(ato uint32, rto uint32, wto uint32, hto uint32, maxBodyLen uint32, maxClients uint32, srcnt int) *Server {
	return &Server{
		Name:          utils.GetLocalIP(),
		ctrl:          make(chan bool),
		lock:          new(sync.Mutex),
		wg:            &sync.WaitGroup{},
		funcMap:       make(map[uint8]MsgHandler),
		acceptTimeout: time.Duration(ato),
		readTimeout:   time.Duration(rto),
		writeTimeout:  time.Duration(wto),
		hbTimeout:     time.Duration(hto),
		maxBodyLen:    maxBodyLen,
		maxClients:    maxClients,
		clientCount:   0,
		workerCount:   srcnt,
		workers:       make(map[int]*Worker),
		HbInterval:    DEFAULT_HB_INTERVAL,
		ReconnTime:    DEFAULT_RECONN_TIME,
	}
}
Пример #3
0
Файл: zk.go Проект: houcy/push
func InitZk(config *conf.ConfigStruct) error {
	conn, err := Connect(config.ZooKeeper.Addr, config.ZooKeeper.Timeout*time.Second)
	if err != nil {
		return err
	}
	_, err = conn.Create(config.ZooKeeper.Path, []byte(""), 0, zk.WorldACL(zk.PermAll))
	if err != nil && err != zk.ErrNodeExists {
		return err
	}
	ip := utils.GetLocalIP()
	port := config.Comet.Port
	addr := ip + ":" + port
	if port == "20000" {
		addr = ip
	}
	data, _ := json.Marshal(addr)
	err = Register(conn, config.ZooKeeper.Path, data)
	if err != nil {
		return err
	}
	return nil
}