{"action":"created","comment":{"url":"https://api.github.com/repos/shazow/urllib3/pulls/comments/88235800","pull_request_review_id":8814130,"id":88235800,"diff_hunk":"@@ -0,0 +1,223 @@\n+import errno\n+import socket\n+from urllib3.util.selectors import (\n+    DefaultSelector,\n+    EVENT_WRITE\n+)\n+from urllib3.util.timeout import current_time\n+\n+\n+_HAPPY_EYEBALLS_CACHE = {}\n+_HAPPY_EYEBALLS_CACHE_TIME = 60 * 10  # 10 minutes according to RFC 6555\n+_ASYNC_ERROR_NUMBERS = set([errno.EINPROGRESS,\n+                            errno.EAGAIN,\n+                            errno.EWOULDBLOCK])\n+if hasattr(errno, \"WSAEWOULDBLOCK\"):  # Platform-specific: Windows\n+    _ASYNC_ERROR_NUMBERS.add(errno.WSAEWOULDBLOCK)\n+\n+_IP_FAMILIES = set([socket.AF_INET])\n+if hasattr(socket, \"AF_INET6\"):\n+    _IP_FAMILIES.add(socket.AF_INET6)\n+\n+\n+class _HappyEyeballs(object):\n+    def __init__(self, address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,\n+                 source_address=None, socket_options=None):\n+        self._address = address\n+        self._error = None\n+        self._timeout = timeout\n+        self._source_address = source_address\n+        self._socket_options = socket_options\n+        self._sockets = []\n+        self._selector = DefaultSelector()\n+        self._start_time = None\n+\n+    def connect(self):\n+        \"\"\" Given the setup of the algorithm, attempt to\n+        use a cached value and if it doesn't work then try\n+        to use a \"\"\"\n+        self._start_time = current_time()\n+        try:\n+            sock = None\n+            if self._address in _HAPPY_EYEBALLS_CACHE:\n+                family, expires = _HAPPY_EYEBALLS_CACHE[self._address]\n+\n+                # Invalidate the cache if it's expired.\n+                if expires > current_time():\n+                    del _HAPPY_EYEBALLS_CACHE[self._address]\n+                else:\n+                    try:\n+                        sock = self._algorithm(family)\n+                    # Invalidate the cache if connecting doesn't work.\n+                    except (OSError, socket.error):\n+                        del _HAPPY_EYEBALLS_CACHE[self._address]\n+\n+            if sock is None:\n+                sock = self._algorithm()\n+\n+                # Add a new entry to the cache if successful.\n+                cache_entry = (sock.family,\n+                               current_time() + _HAPPY_EYEBALLS_CACHE_TIME)\n+                _HAPPY_EYEBALLS_CACHE[self._address] = cache_entry\n+\n+            return sock\n+        finally:\n+            self._cleanup()\n+\n+    def _algorithm(self, family=None):","path":"urllib3/util/rfc6555.py","position":67,"original_position":67,"commit_id":"82bdc31482fdcd536645fdf609e0e8add1f5add3","original_commit_id":"82bdc31482fdcd536645fdf609e0e8add1f5add3","user":{"login":"SethMichaelLarson","id":18519037,"avatar_url":"https://avatars.githubusercontent.com/u/18519037?v=3","gravatar_id":"","url":"https://api.github.com/users/SethMichaelLarson","html_url":"https://github.com/SethMichaelLarson","followers_url":"https://api.github.com/users/SethMichaelLarson/followers","following_url":"https://api.github.com/users/SethMichaelLarson/following{/other_user}","gists_url":"https://api.github.com/users/SethMichaelLarson/gists{/gist_id}","starred_url":"https://api.github.com/users/SethMichaelLarson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SethMichaelLarson/subscriptions","organizations_url":"https://api.github.com/users/SethMichaelLarson/orgs","repos_url":"https://api.github.com/users/SethMichaelLarson/repos","events_url":"https://api.github.com/users/SethMichaelLarson/events{/privacy}","received_events_url":"https://api.github.com/users/SethMichaelLarson/received_events","type":"User","site_admin":false},"body":"Neither do I :P I'll think of a better one :)","created_at":"2016-11-16T13:30:50Z","updated_at":"2016-11-16T13:30:51Z","html_url":"https://github.com/shazow/urllib3/pull/1000#discussion_r88235800","pull_request_url":"https://api.github.com/repos/shazow/urllib3/pulls/1000","_links":{"self":{"href":"https://api.github.com/repos/shazow/urllib3/pulls/comments/88235800"},"html":{"href":"https://github.com/shazow/urllib3/pull/1000#discussion_r88235800"},"pull_request":{"href":"https://api.github.com/repos/shazow/urllib3/pulls/1000"}}},"pull_request":{"url":"https://api.github.com/repos/shazow/urllib3/pulls/1000","id":89931135,"html_url":"https://github.com/shazow/urllib3/pull/1000","diff_url":"https://github.com/shazow/urllib3/pull/1000.diff","patch_url":"https://github.com/shazow/urllib3/pull/1000.patch","issue_url":"https://api.github.com/repos/shazow/urllib3/issues/1000","number":1000,"state":"open","locked":false,"title":"Happy Eyeballs Algorithm","user":{"login":"SethMichaelLarson","id":18519037,"avatar_url":"https://avatars.githubusercontent.com/u/18519037?v=3","gravatar_id":"","url":"https://api.github.com/users/SethMichaelLarson","html_url":"https://github.com/SethMichaelLarson","followers_url":"https://api.github.com/users/SethMichaelLarson/followers","following_url":"https://api.github.com/users/SethMichaelLarson/following{/other_user}","gists_url":"https://api.github.com/users/SethMichaelLarson/gists{/gist_id}","starred_url":"https://api.github.com/users/SethMichaelLarson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SethMichaelLarson/subscriptions","organizations_url":"https://api.github.com/users/SethMichaelLarson/orgs","repos_url":"https://api.github.com/users/SethMichaelLarson/repos","events_url":"https://api.github.com/users/SethMichaelLarson/events{/privacy}","received_events_url":"https://api.github.com/users/SethMichaelLarson/received_events","type":"User","site_admin":false},"body":"Hello, this PR is for issue #797 and adds upon the work and takes the recommendations of @Lukasa in order to implement a subset of the Happy Eyeballs Algorithm ([RFC 6555](https://tools.ietf.org/html/rfc6555)) to decrease the performance decrease that dual-stack clients feel when connecting to a server with advertised IPv6 addresses but for whatever reason cannot be connected to via IPv6. Without this protocol in place, clients attempting to connect to a service with broken IPv6 will see a long timeout for IPv6 before their system will try to connect via IPv4 which punishes dual-stack users.\n\nThe suggestion from @Lukasa was to break this into its own helper which this PR accomplishes. This PR also has support for caching Happy Eyes results for faster use.\n\nCurrently, there are two tests failing on my machine (Receiving a ProxyError rather than a ConnectionTimeoutError) but because I don't have very much knowledge about urllib3's code-base I needed some help from other maintainers debugging them. \n\nI currently have not added any tests to this PR because I want to know the best way to add tests that are for an optional protocol for creating a connection such as this, with suggestions on the best way to test this protocol as well as the `create_connection` method without the Happy Eyes protocol enabled. Criticism welcome.\n","created_at":"2016-10-19T02:18:11Z","updated_at":"2016-11-16T13:30:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d8e65e6f4d6f8d41c509c10c7df8b6a6f6e6e73a","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/shazow/urllib3/pulls/1000/commits","review_comments_url":"https://api.github.com/repos/shazow/urllib3/pulls/1000/comments","review_comment_url":"https://api.github.com/repos/shazow/urllib3/pulls/comments{/number}","comments_url":"https://api.github.com/repos/shazow/urllib3/issues/1000/comments","statuses_url":"https://api.github.com/repos/shazow/urllib3/statuses/82bdc31482fdcd536645fdf609e0e8add1f5add3","head":{"label":"SethMichaelLarson:happy_eyes","ref":"happy_eyes","sha":"82bdc31482fdcd536645fdf609e0e8add1f5add3","user":{"login":"SethMichaelLarson","id":18519037,"avatar_url":"https://avatars.githubusercontent.com/u/18519037?v=3","gravatar_id":"","url":"https://api.github.com/users/SethMichaelLarson","html_url":"https://github.com/SethMichaelLarson","followers_url":"https://api.github.com/users/SethMichaelLarson/followers","following_url":"https://api.github.com/users/SethMichaelLarson/following{/other_user}","gists_url":"https://api.github.com/users/SethMichaelLarson/gists{/gist_id}","starred_url":"https://api.github.com/users/SethMichaelLarson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SethMichaelLarson/subscriptions","organizations_url":"https://api.github.com/users/SethMichaelLarson/orgs","repos_url":"https://api.github.com/users/SethMichaelLarson/repos","events_url":"https://api.github.com/users/SethMichaelLarson/events{/privacy}","received_events_url":"https://api.github.com/users/SethMichaelLarson/received_events","type":"User","site_admin":false},"repo":{"id":71281542,"name":"urllib3","full_name":"SethMichaelLarson/urllib3","owner":{"login":"SethMichaelLarson","id":18519037,"avatar_url":"https://avatars.githubusercontent.com/u/18519037?v=3","gravatar_id":"","url":"https://api.github.com/users/SethMichaelLarson","html_url":"https://github.com/SethMichaelLarson","followers_url":"https://api.github.com/users/SethMichaelLarson/followers","following_url":"https://api.github.com/users/SethMichaelLarson/following{/other_user}","gists_url":"https://api.github.com/users/SethMichaelLarson/gists{/gist_id}","starred_url":"https://api.github.com/users/SethMichaelLarson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SethMichaelLarson/subscriptions","organizations_url":"https://api.github.com/users/SethMichaelLarson/orgs","repos_url":"https://api.github.com/users/SethMichaelLarson/repos","events_url":"https://api.github.com/users/SethMichaelLarson/events{/privacy}","received_events_url":"https://api.github.com/users/SethMichaelLarson/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/SethMichaelLarson/urllib3","description":"Python HTTP library with thread-safe connection pooling, file post support, sanity friendly, and more.","fork":true,"url":"https://api.github.com/repos/SethMichaelLarson/urllib3","forks_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/forks","keys_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/teams","hooks_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/hooks","issue_events_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/issues/events{/number}","events_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/events","assignees_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/assignees{/user}","branches_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/branches{/branch}","tags_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/tags","blobs_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/git/refs{/sha}","trees_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/statuses/{sha}","languages_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/languages","stargazers_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/stargazers","contributors_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/contributors","subscribers_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/subscribers","subscription_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/subscription","commits_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/commits{/sha}","git_commits_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/git/commits{/sha}","comments_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/comments{/number}","issue_comment_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/issues/comments{/number}","contents_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/contents/{+path}","compare_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/merges","archive_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/downloads","issues_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/issues{/number}","pulls_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/pulls{/number}","milestones_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/milestones{/number}","notifications_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/labels{/name}","releases_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/releases{/id}","deployments_url":"https://api.github.com/repos/SethMichaelLarson/urllib3/deployments","created_at":"2016-10-18T18:59:43Z","updated_at":"2016-10-18T18:59:44Z","pushed_at":"2016-11-16T05:41:53Z","git_url":"git://github.com/SethMichaelLarson/urllib3.git","ssh_url":"git@github.com:SethMichaelLarson/urllib3.git","clone_url":"https://github.com/SethMichaelLarson/urllib3.git","svn_url":"https://github.com/SethMichaelLarson/urllib3","homepage":"https://urllib3.readthedocs.io/","size":3055,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"shazow:master","ref":"master","sha":"fe918c67359876dc9ff600e10be6103c64b0b7d4","user":{"login":"shazow","id":6292,"avatar_url":"https://avatars.githubusercontent.com/u/6292?v=3","gravatar_id":"","url":"https://api.github.com/users/shazow","html_url":"https://github.com/shazow","followers_url":"https://api.github.com/users/shazow/followers","following_url":"https://api.github.com/users/shazow/following{/other_user}","gists_url":"https://api.github.com/users/shazow/gists{/gist_id}","starred_url":"https://api.github.com/users/shazow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shazow/subscriptions","organizations_url":"https://api.github.com/users/shazow/orgs","repos_url":"https://api.github.com/users/shazow/repos","events_url":"https://api.github.com/users/shazow/events{/privacy}","received_events_url":"https://api.github.com/users/shazow/received_events","type":"User","site_admin":false},"repo":{"id":2410676,"name":"urllib3","full_name":"shazow/urllib3","owner":{"login":"shazow","id":6292,"avatar_url":"https://avatars.githubusercontent.com/u/6292?v=3","gravatar_id":"","url":"https://api.github.com/users/shazow","html_url":"https://github.com/shazow","followers_url":"https://api.github.com/users/shazow/followers","following_url":"https://api.github.com/users/shazow/following{/other_user}","gists_url":"https://api.github.com/users/shazow/gists{/gist_id}","starred_url":"https://api.github.com/users/shazow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shazow/subscriptions","organizations_url":"https://api.github.com/users/shazow/orgs","repos_url":"https://api.github.com/users/shazow/repos","events_url":"https://api.github.com/users/shazow/events{/privacy}","received_events_url":"https://api.github.com/users/shazow/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shazow/urllib3","description":"Python HTTP library with thread-safe connection pooling, file post support, sanity friendly, and more.","fork":false,"url":"https://api.github.com/repos/shazow/urllib3","forks_url":"https://api.github.com/repos/shazow/urllib3/forks","keys_url":"https://api.github.com/repos/shazow/urllib3/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shazow/urllib3/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shazow/urllib3/teams","hooks_url":"https://api.github.com/repos/shazow/urllib3/hooks","issue_events_url":"https://api.github.com/repos/shazow/urllib3/issues/events{/number}","events_url":"https://api.github.com/repos/shazow/urllib3/events","assignees_url":"https://api.github.com/repos/shazow/urllib3/assignees{/user}","branches_url":"https://api.github.com/repos/shazow/urllib3/branches{/branch}","tags_url":"https://api.github.com/repos/shazow/urllib3/tags","blobs_url":"https://api.github.com/repos/shazow/urllib3/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shazow/urllib3/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shazow/urllib3/git/refs{/sha}","trees_url":"https://api.github.com/repos/shazow/urllib3/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shazow/urllib3/statuses/{sha}","languages_url":"https://api.github.com/repos/shazow/urllib3/languages","stargazers_url":"https://api.github.com/repos/shazow/urllib3/stargazers","contributors_url":"https://api.github.com/repos/shazow/urllib3/contributors","subscribers_url":"https://api.github.com/repos/shazow/urllib3/subscribers","subscription_url":"https://api.github.com/repos/shazow/urllib3/subscription","commits_url":"https://api.github.com/repos/shazow/urllib3/commits{/sha}","git_commits_url":"https://api.github.com/repos/shazow/urllib3/git/commits{/sha}","comments_url":"https://api.github.com/repos/shazow/urllib3/comments{/number}","issue_comment_url":"https://api.github.com/repos/shazow/urllib3/issues/comments{/number}","contents_url":"https://api.github.com/repos/shazow/urllib3/contents/{+path}","compare_url":"https://api.github.com/repos/shazow/urllib3/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shazow/urllib3/merges","archive_url":"https://api.github.com/repos/shazow/urllib3/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shazow/urllib3/downloads","issues_url":"https://api.github.com/repos/shazow/urllib3/issues{/number}","pulls_url":"https://api.github.com/repos/shazow/urllib3/pulls{/number}","milestones_url":"https://api.github.com/repos/shazow/urllib3/milestones{/number}","notifications_url":"https://api.github.com/repos/shazow/urllib3/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shazow/urllib3/labels{/name}","releases_url":"https://api.github.com/repos/shazow/urllib3/releases{/id}","deployments_url":"https://api.github.com/repos/shazow/urllib3/deployments","created_at":"2011-09-18T18:08:28Z","updated_at":"2016-11-15T22:25:22Z","pushed_at":"2016-11-16T13:03:25Z","git_url":"git://github.com/shazow/urllib3.git","ssh_url":"git@github.com:shazow/urllib3.git","clone_url":"https://github.com/shazow/urllib3.git","svn_url":"https://github.com/shazow/urllib3","homepage":"https://urllib3.readthedocs.io/","size":5278,"stargazers_count":1157,"watchers_count":1157,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":391,"mirror_url":null,"open_issues_count":120,"forks":391,"open_issues":120,"watchers":1157,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/shazow/urllib3/pulls/1000"},"html":{"href":"https://github.com/shazow/urllib3/pull/1000"},"issue":{"href":"https://api.github.com/repos/shazow/urllib3/issues/1000"},"comments":{"href":"https://api.github.com/repos/shazow/urllib3/issues/1000/comments"},"review_comments":{"href":"https://api.github.com/repos/shazow/urllib3/pulls/1000/comments"},"review_comment":{"href":"https://api.github.com/repos/shazow/urllib3/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/shazow/urllib3/pulls/1000/commits"},"statuses":{"href":"https://api.github.com/repos/shazow/urllib3/statuses/82bdc31482fdcd536645fdf609e0e8add1f5add3"}}}}
{"actor":{"display_login":"SethMichaelLarson"}}