func handleConnection(conn *connection.Conn, auth bool, timeout time.Duration) { var host string connCnt++ // this maybe not accurate, but should be enough if connCnt-nextLogConnCnt >= 0 { // XXX There's no xadd in the atomic package, so it's difficult to log // the message only once with low cost. Also note nextLogConnCnt maybe // added twice for current peak connection number level. log.Printf("Number of client connections reaches %d\n", nextLogConnCnt) nextLogConnCnt += logCntDelta } // function arguments are always evaluated, so surround debug statement // with if statement glog.V(5).Infof("new client %s->%s\n", conn.RemoteAddr().String(), conn.LocalAddr()) closed := false defer func() { glog.V(5).Infof("closed pipe %s<->%s\n", conn.RemoteAddr(), host) connCnt-- if !closed { conn.Close() } }() host, ota, err := getRequest(conn, auth, timeout) if err != nil { glog.Errorf("error getting request %v<->%v err:%v", conn.RemoteAddr(), conn.LocalAddr(), err) return } glog.V(5).Infof("connection host:%v ota:%v \r\n", host, ota) remote, err := net.Dial("tcp", host) if err != nil { if ne, ok := err.(*net.OpError); ok && (ne.Err == syscall.EMFILE || ne.Err == syscall.ENFILE) { // log too many open file error // EMFILE is process reaches open file limits, ENFILE is system limit glog.Errorf("dial error:%v\r\n", err) } else { glog.Errorf(" connecting to:%v occur err:%v", host, err) } return } defer func() { if !closed { remote.Close() } }() glog.V(5).Infof("piping %s<->%s ota=%v connOta=%v", conn.RemoteAddr(), host, ota, conn.IsOta()) if ota { go connection.PipeThenCloseOta(conn, remote, timeout) } else { go connection.PipeThenClose(conn, remote, timeout) } connection.PipeThenClose(remote, conn, timeout) closed = true return }