<commit_before>package nsq

import (
	"log"
	"net"
	"time"
)

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

// NewLookupPeer creates a new LookupPeer instance
func NewLookupPeer(addr string, connectCallback func(*LookupPeer)) *LookupPeer {
	return &LookupPeer{
		addr:            addr,
		state:           StateDisconnected,
		connectCallback: connectCallback,
	}
}

func (lp *LookupPeer) Connect() error {
	log.Printf("LOOKUP connecting to %s", lp.addr)
	conn, err := net.DialTimeout("tcp", lp.addr, time.Second)
	if err != nil {
		return err
	}
	lp.conn = conn
	return nil
}

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

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
}<commit_msg>lookup channels at topic creation time, against lookkupd<commit_after>package nsq

import (
	"log"
	"net"
	"time"
)

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

type PeerInfo struct {
	TcpPort  int    `json:"tcp_port"`
	HttpPort int    `json:"http_port"`
	Version  string `json:"version"`
	Address  string `json:"address"`
}

// NewLookupPeer creates a new LookupPeer instance
func NewLookupPeer(addr string, connectCallback func(*LookupPeer)) *LookupPeer {
	return &LookupPeer{
		addr:            addr,
		state:           StateDisconnected,
		connectCallback: connectCallback,
	}
}

func (lp *LookupPeer) Connect() error {
	log.Printf("LOOKUP connecting to %s", lp.addr)
	conn, err := net.DialTimeout("tcp", lp.addr, time.Second)
	if err != nil {
		return err
	}
	lp.conn = conn
	return nil
}

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

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)
		}
	}
	if cmd == nil {
		return nil, nil
	}
	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.1277056277056277, 'fraction_numerical': 0.0016233766233766235, 'mean_word_length': 3.5085365853658534, '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': '6561345', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1286, 'n_words': 346}