Exemplo n.º 1
0
func runPublisher(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs int, msgSize int) {
	nc, err := opts.Connect()
	if err != nil {
		log.Fatalf("Can't connect: %v\n", err)
	}
	defer nc.Close()
	startwg.Done()

	args := flag.Args()
	subj := args[0]
	var msg []byte
	if msgSize > 0 {
		msg = make([]byte, msgSize)
	}

	start := time.Now()

	for i := 0; i < numMsgs; i++ {
		nc.Publish(subj, msg)
	}
	nc.Flush()
	benchmark.AddPubSample(bench.NewSample(numMsgs, msgSize, start, time.Now(), nc))

	donewg.Done()
}
Exemplo n.º 2
0
func NewElectronConn(opt *nats.Options) (ec *ElectronConn, err error) {
	var nc *nats.Conn
	if opt == nil {
		nc, err = nats.Connect(nats.DefaultURL)
	} else {
		nc, err = opt.Connect()
	}
	if err != nil {
		return nil, err
	}

	ec = &ElectronConn{conn: nc}
	f := func(m *nats.Msg) {
		ID := atomic.AddUint32(&ec.genID, 1)
		if err := nc.Publish(m.Reply, []byte(fmt.Sprintf("%d", ID))); err != nil {
			logrus.Error(err)
			return
		}
		ec.Lock()
		ec.clients = append(ec.clients, ID)
		ec.Unlock()
	}
	if _, err = nc.Subscribe("register", f); err != nil {
		nc.Close()
		return nil, fmt.Errorf("nats: %v", err)
	}

	if ec.jsonConn, err = nats.NewEncodedConn(nc, "json"); err != nil {
		nc.Close()
		return nil, fmt.Errorf("nats: %v", err)
	}
	return
}
Exemplo n.º 3
0
// Create a new instance of the driver. The NATS encoded connection
// will use json and the options passed in.
func NewNatsRpc(opts *nats.Options) (*NatsRpcDriver, error) {
	nc, err := opts.Connect()
	ec, _ := nats.NewEncodedConn(nc, "json")
	if err != nil {
		return nil, err
	}
	return &NatsRpcDriver{ec: ec}, nil
}
Exemplo n.º 4
0
// Shows different ways to create a Conn
func ExampleConnect() {

	nats.Connect(nats.DefaultURL)
	nats.Connect("nats://*****:*****@nats.apcera.com:421")

	opts := nats.Options{
		AllowReconnect: true,
		MaxReconnect:   10,
		ReconnectWait:  5 * time.Second,
		Timeout:        1 * time.Second,
	}

	nc, _ := opts.Connect()
	nc.Close()
}
Exemplo n.º 5
0
// NewBasicQueue initializes BasicQueue
//
// NewBasicQueue accepts nats.Options and encoding type which must be supported by nats.io queue
// If no options are provided, BasicQueue will be initialized with DefaultOptions
// It returns error if the queue fails to be initialized.
func NewBasicQueue(options *nats.Options, encType string) (*BasicQueue, error) {
	conn, err := options.Connect()
	if err != nil {
		return nil, err
	}

	enconn, err := nats.NewEncodedConn(conn, encType)
	if err != nil {
		return nil, err
	}

	return &BasicQueue{
		enconn: enconn,
	}, nil
}
Exemplo n.º 6
0
func buildConns(count int, opts *nats.Options) error {
	var err error
	conns = nil
	err = nil

	// make a conn pool to use
	if count < 0 {
		return nil
	}
	conns = make([]*nats.Conn, count)
	for i := 0; i < count; i++ {
		opts.Name = "conn-" + string(i)
		conns[i], err = opts.Connect()
	}
	return err
}
Exemplo n.º 7
0
func runPublisher(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs int) {
	nc, err := opts.Connect()
	if err != nil {
		log.Fatalf("Can't connect: %v\n", err)
	}
	defer nc.Close()
	startwg.Done()

	args := flag.Args()
	subj, msg := args[0], []byte(args[1])

	for i := 0; i < numMsgs; i++ {
		nc.Publish(subj, msg)
		if i%HashModulo == 0 {
			fmt.Fprintf(os.Stderr, "#")
		}
	}
	nc.Flush()
	donewg.Done()
}
Exemplo n.º 8
0
// Shows different ways to create a Conn
func ExampleConnect() {

	nc, _ := nats.Connect(nats.DefaultURL)
	nc.Close()

	nc, _ = nats.Connect("nats://*****:*****@demo.nats.io:4222")
	nc.Close()

	nc, _ = nats.Connect("tls://*****:*****@demo.nats.io:4443")
	nc.Close()

	opts := nats.Options{
		AllowReconnect: true,
		MaxReconnect:   10,
		ReconnectWait:  5 * time.Second,
		Timeout:        1 * time.Second,
	}

	nc, _ = opts.Connect()
	nc.Close()
}
Exemplo n.º 9
0
func runSubscriber(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs int, msgSize int) {
	nc, err := opts.Connect()
	if err != nil {
		log.Fatalf("Can't connect: %v\n", err)
	}

	args := flag.Args()
	subj := args[0]

	received := 0
	start := time.Now()
	nc.Subscribe(subj, func(msg *nats.Msg) {
		received++
		if received >= numMsgs {
			benchmark.AddSubSample(bench.NewSample(numMsgs, msgSize, start, time.Now(), nc))
			donewg.Done()
			nc.Close()
		}
	})
	nc.Flush()
	startwg.Done()
}
Exemplo n.º 10
0
func runSubscriber(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs int) {
	nc, err := opts.Connect()
	if err != nil {
		log.Fatalf("Can't connect: %v\n", err)
	}

	args := flag.Args()
	subj := args[0]

	received := 0
	nc.Subscribe(subj, func(msg *nats.Msg) {
		received++
		if received%HashModulo == 0 {
			fmt.Fprintf(os.Stderr, "*")
		}
		if received >= numMsgs {
			donewg.Done()
			nc.Close()
		}
	})
	nc.Flush()
	startwg.Done()
}