<commit_before>package nsq

import (
	"net"
	"time"
)

// LookupPeer is a low-level type for connecting/reading/writing to nsqlookupd
type LookupPeer struct {
	tcpAddr         *net.TCPAddr
	conn            net.Conn
	state           int32
	connectCallback func(*LookupPeer)
}

// NewLookupPeer creates a new LookupPeer instance
func NewLookupPeer(tcpAddr *net.TCPAddr, connectCallback func(*LookupPeer)) *LookupPeer {
	return &LookupPeer{
		tcpAddr:         tcpAddr,
		state:           StateDisconnected,
		connectCallback: connectCallback,
	}
}

func (lp *LookupPeer) Connect() error {
	conn, err := net.DialTimeout("tcp", lp.tcpAddr.String(), time.Second)
	if err != nil {
		return err
	}
	lp.conn = conn
	return nil
}

func (lp *LookupPeer) String() string {
	return lp.tcpAddr.String()
}

func (lp *LookupPeer) Read(data []byte) (int, error) {
	return lp.conn.Read(data)
}

func (lp *LookupPeer) Write(data []byte) (int, error) {
	return lp.conn.Write(data)
}

func (lp *LookupPeer) Close() error {
	return lp.conn.Close()
}

func (lp *LookupPeer) Command(cmd *Command) ([]byte, error) {
	initialState := lp.state
	if lp.state != StateConnected {
		err := lp.Connect()
		if err != nil {
			return nil, err
		}
		lp.state = StateConnected
		lp.Write(MagicV1)
		if initialState == StateDisconnected {
			lp.connectCallback(lp)
		}
	}
	err := SendCommand(lp, cmd)
	if err != nil {
		lp.Close()
		lp.state = StateDisconnected
		return nil, err
	}
	resp, err := ReadResponse(lp)
	if err != nil {
		lp.Close()
		lp.state = StateDisconnected
		return nil, err
	}
	return resp, nil
}
<commit_msg>add read/write deadline to lookup peer<commit_after>package nsq

import (
	"net"
	"time"
)

// LookupPeer is a low-level type for connecting/reading/writing to nsqlookupd
type LookupPeer struct {
	tcpAddr         *net.TCPAddr
	conn            net.Conn
	state           int32
	connectCallback func(*LookupPeer)
}

// NewLookupPeer creates a new LookupPeer instance
func NewLookupPeer(tcpAddr *net.TCPAddr, connectCallback func(*LookupPeer)) *LookupPeer {
	return &LookupPeer{
		tcpAddr:         tcpAddr,
		state:           StateDisconnected,
		connectCallback: connectCallback,
	}
}

func (lp *LookupPeer) Connect() error {
	conn, err := net.DialTimeout("tcp", lp.tcpAddr.String(), time.Second)
	if err != nil {
		return err
	}
	lp.conn = conn
	return nil
}

func (lp *LookupPeer) String() string {
	return lp.tcpAddr.String()
}

func (lp *LookupPeer) Read(data []byte) (int, error) {
	lp.conn.SetReadDeadline(time.Now().Add(time.Second))
	return lp.conn.Read(data)
}

func (lp *LookupPeer) Write(data []byte) (int, error) {
	lp.conn.SetWriteDeadline(time.Now().Add(time.Second))
	return lp.conn.Write(data)
}

func (lp *LookupPeer) Close() error {
	return lp.conn.Close()
}

func (lp *LookupPeer) Command(cmd *Command) ([]byte, error) {
	initialState := lp.state
	if lp.state != StateConnected {
		err := lp.Connect()
		if err != nil {
			return nil, err
		}
		lp.state = StateConnected
		lp.Write(MagicV1)
		if initialState == StateDisconnected {
			lp.connectCallback(lp)
		}
	}
	err := SendCommand(lp, cmd)
	if err != nil {
		lp.Close()
		lp.state = StateDisconnected
		return nil, err
	}
	resp, err := ReadResponse(lp)
	if err != nil {
		lp.Close()
		lp.state = StateDisconnected
		return nil, err
	}
	return resp, nil
}

{'fraction_non_alphanumeric': 0.12756332931242462, 'fraction_numerical': 0.0018094089264173703, 'mean_word_length': 3.550068587105624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1955752', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1153, 'n_words': 307}