bfe icon indicating copy to clipboard operation
bfe copied to clipboard

bfe1.6版本bfe_http transponrt.go的putIdleConn细节处理

Open cherishman2005 opened this issue 1 year ago • 0 comments

bfe1.6版本bfe_http transponrt.go的putIdleConn细节处理是不是没处理好?

bfe1.6版本bfe_http transponrt.go的处理

func (t *Transport) putIdleConn(pconn *persistConn) bool {
	if t.DisableKeepAlives || t.MaxIdleConnsPerHost < 0 {
		pconn.close()
		return false
	}
	if pconn.isBroken() {
		return false
	}
	key := pconn.cacheKey
	max := t.MaxIdleConnsPerHost
	if max == 0 {
		max = DefaultMaxIdleConnsPerHost
	}
	t.idleMu.Lock()

	waitingDialer := t.idleConnCh[key]
	select {
	case waitingDialer <- pconn:
		// We're done with this pconn and somebody else is
		// currently waiting for a conn of this type (they're
		// actively dialing, but this conn is ready
		// first). Chrome calls this socket late binding.  See
		// https://insouciant.org/tech/connection-management-in-chromium/
		t.idleMu.Unlock()
		return true
	default:
		if waitingDialer != nil {
			// They had populated this, but their dial won
			// first, so we can clean up this map entry.
			delete(t.idleConnCh, key)
		}
	}
	if t.idleConn == nil {
		t.idleConn = make(map[string][]*persistConn)
	}
	// evict the oldest connection when idleConn exceed its limits.
	if len(t.idleConn[key]) >= max {
		oldest := t.idleConn[key][0]
		t.idleConn[key] = append(t.idleConn[key][1:], pconn)
		t.idleMu.Unlock()
		oldest.close()
		return true
	}
	for _, exist := range t.idleConn[key] {
		if exist == pconn {
			log.Logger.Error("dup idle pconn %p in freelist", pconn)
		}
	}
	t.idleConn[key] = append(t.idleConn[key], pconn)
	t.idleMu.Unlock()
	return true
}

在检测到相同的pconn时应该返回

func (t *Transport) putIdleConn(pconn *persistConn) bool {
	if t.DisableKeepAlives || t.MaxIdleConnsPerHost < 0 {
		pconn.close()
		return false
	}
	if pconn.isBroken() {
		return false
	}
	key := pconn.cacheKey
	max := t.MaxIdleConnsPerHost
	if max == 0 {
		max = DefaultMaxIdleConnsPerHost
	}
	t.idleMu.Lock()

	waitingDialer := t.idleConnCh[key]
	select {
	case waitingDialer <- pconn:
		// We're done with this pconn and somebody else is
		// currently waiting for a conn of this type (they're
		// actively dialing, but this conn is ready
		// first). Chrome calls this socket late binding.  See
		// https://insouciant.org/tech/connection-management-in-chromium/
		t.idleMu.Unlock()
		return true
	default:
		if waitingDialer != nil {
			// They had populated this, but their dial won
			// first, so we can clean up this map entry.
			delete(t.idleConnCh, key)
		}
	}
	if t.idleConn == nil {
		t.idleConn = make(map[string][]*persistConn)
	}
	// evict the oldest connection when idleConn exceed its limits.
	if len(t.idleConn[key]) >= max {
		oldest := t.idleConn[key][0]
		t.idleConn[key] = append(t.idleConn[key][1:], pconn)
		t.idleMu.Unlock()
		oldest.close()
		return true
	}
	for _, exist := range t.idleConn[key] {
		if exist == pconn {
			log.Logger.Error("dup idle pconn %p in freelist", pconn)
	                t.idleMu.Unlock()
	                return true  // 在检测到相同的pconn时应该返回
		}
	}
	t.idleConn[key] = append(t.idleConn[key], pconn)
	t.idleMu.Unlock()
	return true
}

cherishman2005 avatar Nov 09 '23 10:11 cherishman2005