+
+func (c *Client) HandleTransactions() error {
+ // Create a new scanner for parsing incoming bytes into transaction tokens
+ scanner := bufio.NewScanner(c.Connection)
+ scanner.Split(transactionScanner)
+
+ // Scan for new transactions and handle them as they come in.
+ for scanner.Scan() {
+ // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the
+ // scanner re-uses the buffer for subsequent scans.
+ buf := make([]byte, len(scanner.Bytes()))
+ copy(buf, scanner.Bytes())
+
+ var t Transaction
+ _, err := t.Write(buf)
+ if err != nil {
+ break
+ }
+ if err := c.HandleTransaction(&t); err != nil {
+ c.Logger.Errorw("Error handling transaction", "err", err)
+ }
+ }
+
+ if scanner.Err() == nil {
+ return scanner.Err()
+ }
+ return nil
+}