Пример #1
0
func clientSend(topic string, wg *sync.WaitGroup, client *forestbus.Client, numBatches int, batchSize int, messageSize int, msgTemplate string, waitForCommit bool) {
	count := 0
	var byteMsgTemplate []byte
	if msgTemplate != "" {
		if messageSize < len(msgTemplate) {
			byteMsgTemplate = []byte(msgTemplate[:messageSize])
		} else {
			byteMsgTemplate = []byte(msgTemplate)
		}
	}
	for batch := 0; batch < numBatches; batch++ {
		msgs := make([][]byte, 0, batchSize)
		for batchContent := 0; batchContent < batchSize; batchContent++ {
			msg := make([]byte, messageSize)
			if msgTemplate != "" {
				msgs = append(msgs, byteMsgTemplate)
			} else {
				if messageSize >= len(time.StampMilli) {
					copy(msg, []byte(time.Now().Format(time.StampMilli)))
				}
				msgs = append(msgs, msg)
			}
		}
		// Send the batch
		_, err := client.SendMessages(topic, msgs, waitForCommit)
		if err != nil {
			fmt.Printf("Client experienced error sending message to cluster: %v after %v messages\n", err, count)
			wg.Done()
			return
		}
		count++
	}
	wg.Done()
}