Esempio n. 1
0
File: main.go Progetto: vito/auction
func main() {
	flag.Parse()

	if *natsAddrs == "" {
		panic("need either nats addr")
	}

	client := yagnats.NewClient()

	clusterInfo := &yagnats.ConnectionCluster{}

	for _, addr := range strings.Split(*natsAddrs, ",") {
		clusterInfo.Members = append(clusterInfo.Members, &yagnats.ConnectionInfo{
			Addr: addr,
		})
	}

	err := client.Connect(clusterInfo)

	if err != nil {
		log.Fatalln("no nats:", err)
	}

	semaphore := make(chan bool, *maxConcurrent)

	repclient := repnatsclient.New(client, *timeout)

	client.SubscribeWithQueue("diego.auction", "auction-channel", func(msg *yagnats.Message) {
		semaphore <- true
		defer func() {
			<-semaphore
		}()

		var auctionRequest types.AuctionRequest
		err := json.Unmarshal(msg.Payload, &auctionRequest)
		if err != nil {
			client.Publish(msg.ReplyTo, errorResponse)
			return
		}

		auctionResult := auctioneer.Auction(repclient, auctionRequest)
		payload, _ := json.Marshal(auctionResult)

		client.Publish(msg.ReplyTo, payload)
	})

	fmt.Println("auctioneering")

	select {}
}
Esempio n. 2
0
	clusterInfo := &yagnats.ConnectionCluster{}

	for _, addr := range natsAddrs {
		clusterInfo.Members = append(clusterInfo.Members, &yagnats.ConnectionInfo{
			Addr: addr,
		})
	}

	err := natsClient.Connect(clusterInfo)
	Ω(err).ShouldNot(HaveOccurred())

	client = repnatsclient.New(natsClient, timeout)

	if auctioneerMode == "inprocess" {
		communicator = func(auctionRequest types.AuctionRequest) types.AuctionResult {
			return auctioneer.Auction(client, auctionRequest)
		}
	} else if auctioneerMode == "remote" {
		communicator = func(auctionRequest types.AuctionRequest) types.AuctionResult {
			return auctioneer.RemoteAuction(natsClient, auctionRequest)
		}
	} else {
		panic("wat?")
	}
})

var _ = BeforeEach(func() {
	for _, guid := range guids {
		client.Reset(guid)
	}