github.com/luckypickle/go-ethereum-vet@v1.14.2/p2p/simulations/adapters/ws.go (about)

     1  package adapters
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"io"
     7  	"regexp"
     8  	"strings"
     9  	"time"
    10  )
    11  
    12  // wsAddrPattern is a regex used to read the WebSocket address from the node's
    13  // log
    14  var wsAddrPattern = regexp.MustCompile(`ws://[\d.:]+`)
    15  
    16  func matchWSAddr(str string) (string, bool) {
    17  	if !strings.Contains(str, "WebSocket endpoint opened") {
    18  		return "", false
    19  	}
    20  
    21  	return wsAddrPattern.FindString(str), true
    22  }
    23  
    24  // findWSAddr scans through reader r, looking for the log entry with
    25  // WebSocket address information.
    26  func findWSAddr(r io.Reader, timeout time.Duration) (string, error) {
    27  	ch := make(chan string)
    28  
    29  	go func() {
    30  		s := bufio.NewScanner(r)
    31  		for s.Scan() {
    32  			addr, ok := matchWSAddr(s.Text())
    33  			if ok {
    34  				ch <- addr
    35  			}
    36  		}
    37  		close(ch)
    38  	}()
    39  
    40  	var wsAddr string
    41  	select {
    42  	case wsAddr = <-ch:
    43  		if wsAddr == "" {
    44  			return "", errors.New("empty result")
    45  		}
    46  	case <-time.After(timeout):
    47  		return "", errors.New("timed out")
    48  	}
    49  
    50  	return wsAddr, nil
    51  }