ensureHostCapacityLocked bounds the number of stored hosts by evicting expired entries first and then one remaining host if the jar is still full.
(key string, now time.Time)
| 358 | // ensureHostCapacityLocked bounds the number of stored hosts by evicting |
| 359 | // expired entries first and then one remaining host if the jar is still full. |
| 360 | func (cj *CookieJar) ensureHostCapacityLocked(key string, now time.Time) { |
| 361 | if _, ok := cj.hostCookies[key]; ok || len(cj.hostCookies) < maxCookieJarHosts { |
| 362 | return |
| 363 | } |
| 364 | |
| 365 | for host, cookies := range cj.hostCookies { |
| 366 | kept := cookies[:0] |
| 367 | for _, sc := range cookies { |
| 368 | if !sc.cookie.Expire().Equal(fasthttp.CookieExpireUnlimited) && sc.cookie.Expire().Before(now) { |
| 369 | fasthttp.ReleaseCookie(sc.cookie) |
| 370 | continue |
| 371 | } |
| 372 | kept = append(kept, sc) |
| 373 | } |
| 374 | if len(kept) == 0 { |
| 375 | delete(cj.hostCookies, host) |
| 376 | if len(cj.hostCookies) < maxCookieJarHosts { |
| 377 | return |
| 378 | } |
| 379 | continue |
| 380 | } |
| 381 | cj.hostCookies[host] = kept |
| 382 | } |
| 383 | |
| 384 | var evictHost string |
| 385 | for host := range cj.hostCookies { |
| 386 | if evictHost == "" || host < evictHost { |
| 387 | evictHost = host |
| 388 | } |
| 389 | } |
| 390 | if evictHost != "" { |
| 391 | releaseStoredCookies(cj.hostCookies[evictHost]) |
| 392 | delete(cj.hostCookies, evictHost) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // releaseStoredCookies releases pooled cookies for an evicted host entry. |
| 397 | func releaseStoredCookies(cookies []storedCookie) { |