func newConn(c net.Conn) *conn { reader := bufio.NewReaderSize(deadline.NewDeadlineReader(c, requestPDTimeout), readBufferSize) writer := bufio.NewWriterSize(deadline.NewDeadlineWriter(c, requestPDTimeout), writeBufferSize) return &conn{ Conn: c, quit: make(chan struct{}), ConnChan: make(chan *conn, 1), ReadWriter: bufio.NewReadWriter(reader, writer), } }
func NewConnection(addr string, netTimeout int) (*Conn, error) { conn, err := net.DialTimeout("tcp", addr, time.Duration(netTimeout)*time.Second) if err != nil { return nil, err } return &Conn{ addr: addr, Conn: conn, r: bufio.NewReaderSize(conn, 512*1024), w: bufio.NewWriterSize(deadline.NewDeadlineWriter(conn, time.Duration(netTimeout)*time.Second), 512*1024), netTimeout: netTimeout, }, nil }
func NewConnectionWithSize(addr string, netTimeout int, readSize int, writeSize int) (*Conn, error) { conn, err := net.DialTimeout("tcp", addr, time.Duration(netTimeout)*time.Second) if err != nil { return nil, err } return &Conn{ addr: addr, nc: conn, closed: false, r: bufio.NewReaderSize(conn, readSize), w: bufio.NewWriterSize(deadline.NewDeadlineWriter(conn, time.Duration(netTimeout)*time.Second), writeSize), netTimeout: netTimeout, }, nil }
func (w *rpcWorker) work() { defer w.wg.Done() RECONNECT: log.Infof("[pd] connect to pd server %v", w.addr) conn, err := rpcConnect(w.addr) if err != nil { log.Warnf("[pd] failed connect pd server: %v, will retry later", err) select { case <-time.After(netIOTimeout): goto RECONNECT case <-w.quit: return } } reader := bufio.NewReaderSize(deadline.NewDeadlineReader(conn, netIOTimeout), readBufferSize) writer := bufio.NewWriterSize(deadline.NewDeadlineWriter(conn, netIOTimeout), writeBufferSize) readwriter := bufio.NewReadWriter(reader, writer) for { var pending []interface{} select { case req := <-w.requests: pending = append(pending, req) POP_ALL: for { select { case req := <-w.requests: pending = append(pending, req) default: break POP_ALL } } if ok := w.handleRequests(pending, readwriter); !ok { conn.Close() goto RECONNECT } case <-w.quit: conn.Close() return } } }