Cap the redirect_cache size to prevent memory abuse
authorMatt Robenolt <matt@ydekproductions.com>
Fri, 24 Oct 2014 03:49:18 +0000 (20:49 -0700)
committerMatt Robenolt <matt@ydekproductions.com>
Mon, 27 Oct 2014 16:20:06 +0000 (09:20 -0700)
requests/sessions.py

index d701ff2ef8af82bf1a48a283ca2a68898a727a32..75be1da4971c46fb38da52fdde2e3ca640d22b32 100644 (file)
@@ -21,6 +21,7 @@ from .hooks import default_hooks, dispatch_hook
 from .utils import to_key_val_list, default_headers, to_native_string
 from .exceptions import (
     TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)
+from .packages.urllib3._collections import RecentlyUsedContainer
 from .structures import CaseInsensitiveDict
 
 from .adapters import HTTPAdapter
@@ -327,7 +328,8 @@ class Session(SessionRedirectMixin):
         self.mount('https://', HTTPAdapter())
         self.mount('http://', HTTPAdapter())
 
-        self.redirect_cache = {}
+        # Only store 1000 redirects to prevent using infinite memory
+        self.redirect_cache = RecentlyUsedContainer(1000)
 
     def __enter__(self):
         return self