Пример #1
0
// NewMongoPool returns a new usable and running pool
func NewMongoPool(use *mgo.Session, dbname string, workers int) *MongoPool {
	obj := &MongoPool{
		pool: handyman.NewPool(),
		sess: use,
		name: dbname,
	}

	go obj.pool.Monitor(workers, nil)
	return obj
}
Пример #2
0
// Startup initializes package getting configuration from environment and
// connecting with chat cluster.
func Startup(prefix string) error {

	// Get configuration fron env
	err := envconfig.Process(prefix, &spec)
	if err != nil {
		return err
	}

	// Validate both senders and receivers num
	if spec.Senders == 0 {
		return ErrZeroSenders
	}

	if spec.Receivers == 0 {
		return ErrZeroReceivers
	}

	// Connect with NATS cluster
	nc, err := nats.Connect(spec.NatsHosts)
	if err != nil {
		return err
	}

	// Use JSON encoder
	natsConn, err = nats.NewEncodedConn(nc, nats.JSON_ENCODER)
	if err != nil {
		return err
	}

	// Initialize pools
	recvPool = handyman.NewPool()
	go recvPool.Monitor(spec.Receivers)

	sendPool = handyman.NewPool()
	go sendPool.Monitor(spec.Senders)

	// Subscribe all events
	subscribeEvents()

	return nil
}