Exemple #1
0
// Setup prepares the peer for testing.
func (c *Peer) Setup() {
	buffer := make([]*pubsub.Message, bufferSize)
	go func() {
		i := 0
		for {
			select {
			case msg := <-c.send:
				buffer[i] = &pubsub.Message{Data: msg}
				i++
				if i == bufferSize {
					if _, err := pubsub.Publish(c.context, topic, buffer...); err != nil {
						c.errors <- err
					}
					i = 0
				}
			case <-c.done:
				if i > 0 {
					if _, err := pubsub.Publish(c.context, topic, buffer[0:i]...); err != nil {
						c.errors <- err
					}
				}
				c.flush <- true
				return
			}
		}
	}()
}
Exemple #2
0
// PublishRaw will publish the message to GCP pubsub.
func (p publisher) PublishRaw(ctx context.Context, key string, m []byte) error {
	_, err := gpubsub.Publish(ctx, p.topic, &gpubsub.Message{
		Data:       m,
		Attributes: map[string]string{"key": key},
	})
	return err
}
func ExamplePublish() {
	ctx := Example_auth()

	msgIDs, err := pubsub.Publish(ctx, "topic1", &pubsub.Message{
		Data: []byte("hello world"),
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Published a message with a message id: %s\n", msgIDs[0])
}
Exemple #4
0
func publish(ctx context.Context, argv []string) {
	checkArgs(argv, 3)
	topic := argv[1]
	message := argv[2]
	msgIDs, err := pubsub.Publish(ctx, topic, &pubsub.Message{
		Data: []byte(message),
	})
	if err != nil {
		log.Fatalf("Publish failed, %v", err)
	}
	fmt.Printf("Message '%s' published to a topic %s and the message id is %s\n", message, topic, msgIDs[0])
}
func publish(message string) {
	// Publish "hello world" on topic1.
	msgIDs, err := pubsub.Publish(getCtx(), "livefeed", &pubsub.Message{
		Data: []byte(message),
	})
	if err != nil {
		log.Println(err)
		tracelog.Error(err, `Error publishing to pubsub`, `publish`)
	} else {
		tracelog.Info(msgIDs[0], "publish", "Message stored in pubsub")
	}
}
Exemple #6
0
func publishHandler(w http.ResponseWriter, r *http.Request) {
	msg := &pubsub.Message{
		Data: []byte(r.FormValue("payload")),
	}

	if _, err := pubsub.Publish(pubsubCtx, topic, msg); err != nil {
		http.Error(w, fmt.Sprintf("Could not publish message: %v", err), 500)
		return
	}

	fmt.Fprint(w, "Message published.")
}
Exemple #7
0
// publishUpdate notifies Pub/Sub subscribers that the book identified with
// the given ID has been added/modified.
func publishUpdate(bookID int64) {
	if !bookshelf.PubSubEnabled() {
		return
	}

	pubSubCtx, err := bookshelf.PubSubCtx()
	if err != nil {
		log.Print(err)
		return
	}

	b, err := json.Marshal(bookID)
	if err != nil {
		return
	}
	_, err = pubsub.Publish(pubSubCtx, bookshelf.PubSubTopic, &pubsub.Message{
		Data: b,
	})
	log.Printf("Published update to Pub/Sub for Book ID %d: %v", bookID, err)
}
Exemple #8
0
func publishLoop(ctx context.Context, topic string, workerid int, result chan<- int) {
	var r uint64
	for {
		msgs := make([]*pubsub.Message, *size)
		for i := 0; i < *size; i++ {
			msgs[i] = &pubsub.Message{
				Data: []byte(fmt.Sprintf("Worker: %d, Round: %d, Message: %d", workerid, r, i)),
			}
		}
		_, err := pubsub.Publish(ctx, topic, msgs...)
		if err != nil {
			log.Printf("Publish failed, %v\n", err)
			return
		}
		r++
		if *reportMPS {
			result <- *size
		}
	}
}