func CreateExecutor()

in protocols/udp/udp.go [38:93]


func CreateExecutor(timeout time.Duration, validator ResponseValidator, hosts ...string) bender.RequestExecutor {
	var i int

	return func(_ int64, request interface{}) (interface{}, error) {
		datagram, ok := request.(*Datagram)
		if !ok {
			return nil, fmt.Errorf("%w, want: *Datagram, got: %T", ErrInvalidType, request)
		}

		addr := net.JoinHostPort(hosts[i], strconv.Itoa(datagram.Port))
		i = (i + 1) % len(hosts)

		// Setup connection
		conn, err := net.Dial("udp", addr)
		if err != nil {
			//nolint:wrapcheck
			return nil, err
		}

		defer func() {
			if err = conn.Close(); err != nil {
				log.Errorf("Error closing connection: %v\n", err)
			}
		}()

		if err = conn.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
			//nolint:wrapcheck
			return nil, err
		}

		_, err = conn.Write(datagram.Data)
		if err != nil {
			//nolint:wrapcheck
			return nil, err
		}

		buffer := make([]byte, MaxResponseSize)

		if err = conn.SetReadDeadline(time.Now().Add(timeout)); err != nil {
			//nolint:wrapcheck
			return nil, err
		}

		n, err := conn.Read(buffer)
		if err != nil {
			//nolint:wrapcheck
			return nil, err
		}

		if err = validator(datagram, buffer[:n]); err != nil {
			return nil, err
		}

		return buffer[:n], nil
	}
}