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() }
func main() { // Initialise our configuration from flags var fbConnect = flag.String("forestbus", REQUIRED, "Connection string for Forest Bus cluster in ClusterID#Topic@host:port,host:port... format.") var startIndex = flag.Int64("index", 1, "Index to start reading messages from") var quantity = flag.Int("quantity", 1, "Target number of messages to retrieve. A larger or smaller number may be returned") var waitForMessages = flag.Bool("wait", false, "Wait for messages to be available") var enableDebug = flag.Bool("debug", false, "Enable debug logging") var tail = flag.Bool("tail", false, "Keeps reading all available messages, implies -wait=true") flag.Parse() if flag.Lookup("help") != nil || flag.Lookup("h") != nil { flag.PrintDefaults() return } if *fbConnect == REQUIRED { Print("forestbus connection string missing.\n") flag.PrintDefaults() return } clusterID, topic, peers := forestbus.ParseConnectionString(*fbConnect) if len(peers) == 0 { Print("At least one node name is required.\n") } if topic == "" { fmt.Printf("Topic is missing.\n") flag.PrintDefaults() return } var client *forestbus.Client if *enableDebug { client = forestbus.NewClient(clusterID, peers, forestbus.ClientEnableDebug()) } else { client = forestbus.NewClient(clusterID, peers) } maxMsgID, err := client.GetTopicMaxAvailableIndex(topic) if err != nil { Print("Error determining maximum available message ID: %v\n", err) return } if maxMsgID == 0 { Print("Commit index is zero due to a full cluster restart.)\n") return } if *tail { *waitForMessages = true } looping := true recievedCount := 0 firstIndex := *startIndex for looping { msgs, nextID, err := client.GetMessages(topic, *startIndex, *quantity, *waitForMessages) if err != nil { Print("Error getting messages: %v\n", err) return } if len(msgs) == 0 { if nextID == 0 { Print("Commit index is zero due to a full cluster restart.)\n") return } else if nextID == (*startIndex + 1) { // Nothing came back - are we planning to wait for more? if !*waitForMessages { looping = false } } else if nextID == *startIndex { // Index 1 not available due to clean up firstIndex = nextID } } for _, msg := range msgs { os.Stdout.Write(msg) os.Stdout.Write(NEWLINE) recievedCount++ if recievedCount == *quantity && !*tail { looping = false break } } *startIndex = nextID } maxMsgID, err = client.GetTopicMaxAvailableIndex(topic) if err != nil { Print("Error determining maximum available message ID: %v\n", err) return } Print("Recieved %v messages from index %v\n", recievedCount, firstIndex) Print("Maximum available message index: %v\n", maxMsgID) }