{"action":"closed","number":13,"pull_request":{"url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/13","id":75096971,"html_url":"https://github.com/Meiqia/flask-sqlalchemy/pull/13","diff_url":"https://github.com/Meiqia/flask-sqlalchemy/pull/13.diff","patch_url":"https://github.com/Meiqia/flask-sqlalchemy/pull/13.patch","issue_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/issues/13","number":13,"state":"closed","locked":false,"title":"Manage one connector for each slave, and use slave randomly","user":{"login":"RussellLuo","id":5109942,"avatar_url":"https://avatars.githubusercontent.com/u/5109942?v=3","gravatar_id":"","url":"https://api.github.com/users/RussellLuo","html_url":"https://github.com/RussellLuo","followers_url":"https://api.github.com/users/RussellLuo/followers","following_url":"https://api.github.com/users/RussellLuo/following{/other_user}","gists_url":"https://api.github.com/users/RussellLuo/gists{/gist_id}","starred_url":"https://api.github.com/users/RussellLuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RussellLuo/subscriptions","organizations_url":"https://api.github.com/users/RussellLuo/orgs","repos_url":"https://api.github.com/users/RussellLuo/repos","events_url":"https://api.github.com/users/RussellLuo/events{/privacy}","received_events_url":"https://api.github.com/users/RussellLuo/received_events","type":"User","site_admin":false},"body":"## 1. 问题\r\n\r\n`SQLALCHEMY_DATABASE_SLAVE_URIS` 中有多个 URI （即有多个从库）时，由于 `_EngineConnector.get_uri()` 会随机返回一个 URI，进而导致访问从库时，在 `_EngineConnector.get_engine()` 中，下列判断不满足：\r\n\r\n``` python\r\nif (uri, echo) == self._connected_for:\r\n    return self._engine\r\n```\r\n\r\n进而会重复执行后面的逻辑：\r\n\r\n```python\r\nself._engine = rv = sqlalchemy.create_engine(info, **options)\r\n```\r\n\r\n简而言之，当多次访问从库的时候，由于 URI 的随机性，导致 `self._engine` 被复用的概率很低，进而会频繁 create_engine，所以导致了之前遇到的 Too many connections 的问题。\r\n\r\n## 2. 解决办法\r\n\r\n原来有问题的策略：\r\n\r\n一个 `bind='slaves'` 对应一个 `_EngineConnector`，又在一个 `_EngineConnector` 中随机返回 URI 以实现从库的随机读取，但是这种随机性会导致频繁 create_engine。\r\n\r\n修改以后的策略：\r\n\r\n结合 `SQLALCHEMY_DATABASE_SLAVE_URIS` 列表的下标，让每一个从库对应一个 `bind='slaves_<index>'`，进而再对应一个 `_EngineConnector`，这样就避免了原有策略的问题。随机性在 `_EngineConnector` 外面保证。","created_at":"2016-06-24T10:51:08Z","updated_at":"2016-07-07T06:51:16Z","closed_at":"2016-07-07T06:51:16Z","merged_at":null,"merge_commit_sha":"105afc59f131e256642355b95ec85dcbaa4cae99","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/13/commits","review_comments_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/13/comments","review_comment_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/issues/13/comments","statuses_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/statuses/f2f7c30e6b6421ecea7a6a7e12fec53270aabe1d","head":{"label":"RussellLuo:fix-recreate-many-engines-for-slaves","ref":"fix-recreate-many-engines-for-slaves","sha":"f2f7c30e6b6421ecea7a6a7e12fec53270aabe1d","user":{"login":"RussellLuo","id":5109942,"avatar_url":"https://avatars.githubusercontent.com/u/5109942?v=3","gravatar_id":"","url":"https://api.github.com/users/RussellLuo","html_url":"https://github.com/RussellLuo","followers_url":"https://api.github.com/users/RussellLuo/followers","following_url":"https://api.github.com/users/RussellLuo/following{/other_user}","gists_url":"https://api.github.com/users/RussellLuo/gists{/gist_id}","starred_url":"https://api.github.com/users/RussellLuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RussellLuo/subscriptions","organizations_url":"https://api.github.com/users/RussellLuo/orgs","repos_url":"https://api.github.com/users/RussellLuo/repos","events_url":"https://api.github.com/users/RussellLuo/events{/privacy}","received_events_url":"https://api.github.com/users/RussellLuo/received_events","type":"User","site_admin":false},"repo":{"id":61865128,"name":"flask-sqlalchemy","full_name":"RussellLuo/flask-sqlalchemy","owner":{"login":"RussellLuo","id":5109942,"avatar_url":"https://avatars.githubusercontent.com/u/5109942?v=3","gravatar_id":"","url":"https://api.github.com/users/RussellLuo","html_url":"https://github.com/RussellLuo","followers_url":"https://api.github.com/users/RussellLuo/followers","following_url":"https://api.github.com/users/RussellLuo/following{/other_user}","gists_url":"https://api.github.com/users/RussellLuo/gists{/gist_id}","starred_url":"https://api.github.com/users/RussellLuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RussellLuo/subscriptions","organizations_url":"https://api.github.com/users/RussellLuo/orgs","repos_url":"https://api.github.com/users/RussellLuo/repos","events_url":"https://api.github.com/users/RussellLuo/events{/privacy}","received_events_url":"https://api.github.com/users/RussellLuo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/RussellLuo/flask-sqlalchemy","description":"Adds SQLAlchemy support to Flask","fork":true,"url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy","forks_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/forks","keys_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/teams","hooks_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/hooks","issue_events_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/issues/events{/number}","events_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/events","assignees_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/assignees{/user}","branches_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/branches{/branch}","tags_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/tags","blobs_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/git/refs{/sha}","trees_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/statuses/{sha}","languages_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/languages","stargazers_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/stargazers","contributors_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/contributors","subscribers_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/subscribers","subscription_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/subscription","commits_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/commits{/sha}","git_commits_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/git/commits{/sha}","comments_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/comments{/number}","issue_comment_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/issues/comments{/number}","contents_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/contents/{+path}","compare_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/merges","archive_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/downloads","issues_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/issues{/number}","pulls_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/pulls{/number}","milestones_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/milestones{/number}","notifications_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/labels{/name}","releases_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/releases{/id}","deployments_url":"https://api.github.com/repos/RussellLuo/flask-sqlalchemy/deployments","created_at":"2016-06-24T07:21:52Z","updated_at":"2016-06-24T07:21:53Z","pushed_at":"2016-06-24T09:23:05Z","git_url":"git://github.com/RussellLuo/flask-sqlalchemy.git","ssh_url":"git@github.com:RussellLuo/flask-sqlalchemy.git","clone_url":"https://github.com/RussellLuo/flask-sqlalchemy.git","svn_url":"https://github.com/RussellLuo/flask-sqlalchemy","homepage":"http://flask-sqlalchemy.pocoo.org/","size":497,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"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":"Meiqia:master","ref":"master","sha":"00733198bf85a2ca16a36e8bdd61d2775517ce21","user":{"login":"Meiqia","id":8907899,"avatar_url":"https://avatars.githubusercontent.com/u/8907899?v=3","gravatar_id":"","url":"https://api.github.com/users/Meiqia","html_url":"https://github.com/Meiqia","followers_url":"https://api.github.com/users/Meiqia/followers","following_url":"https://api.github.com/users/Meiqia/following{/other_user}","gists_url":"https://api.github.com/users/Meiqia/gists{/gist_id}","starred_url":"https://api.github.com/users/Meiqia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Meiqia/subscriptions","organizations_url":"https://api.github.com/users/Meiqia/orgs","repos_url":"https://api.github.com/users/Meiqia/repos","events_url":"https://api.github.com/users/Meiqia/events{/privacy}","received_events_url":"https://api.github.com/users/Meiqia/received_events","type":"Organization","site_admin":false},"repo":{"id":55669556,"name":"flask-sqlalchemy","full_name":"Meiqia/flask-sqlalchemy","owner":{"login":"Meiqia","id":8907899,"avatar_url":"https://avatars.githubusercontent.com/u/8907899?v=3","gravatar_id":"","url":"https://api.github.com/users/Meiqia","html_url":"https://github.com/Meiqia","followers_url":"https://api.github.com/users/Meiqia/followers","following_url":"https://api.github.com/users/Meiqia/following{/other_user}","gists_url":"https://api.github.com/users/Meiqia/gists{/gist_id}","starred_url":"https://api.github.com/users/Meiqia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Meiqia/subscriptions","organizations_url":"https://api.github.com/users/Meiqia/orgs","repos_url":"https://api.github.com/users/Meiqia/repos","events_url":"https://api.github.com/users/Meiqia/events{/privacy}","received_events_url":"https://api.github.com/users/Meiqia/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Meiqia/flask-sqlalchemy","description":"Adds SQLAlchemy support to Flask","fork":true,"url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy","forks_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/forks","keys_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/teams","hooks_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/hooks","issue_events_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/issues/events{/number}","events_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/events","assignees_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/assignees{/user}","branches_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/branches{/branch}","tags_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/tags","blobs_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/git/refs{/sha}","trees_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/statuses/{sha}","languages_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/languages","stargazers_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/stargazers","contributors_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/contributors","subscribers_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/subscribers","subscription_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/subscription","commits_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/commits{/sha}","git_commits_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/git/commits{/sha}","comments_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/comments{/number}","issue_comment_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/issues/comments{/number}","contents_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/contents/{+path}","compare_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/merges","archive_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/downloads","issues_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/issues{/number}","pulls_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls{/number}","milestones_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/milestones{/number}","notifications_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/labels{/name}","releases_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/releases{/id}","deployments_url":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/deployments","created_at":"2016-04-07T06:31:29Z","updated_at":"2016-05-04T14:08:13Z","pushed_at":"2016-06-24T10:51:08Z","git_url":"git://github.com/Meiqia/flask-sqlalchemy.git","ssh_url":"git@github.com:Meiqia/flask-sqlalchemy.git","clone_url":"https://github.com/Meiqia/flask-sqlalchemy.git","svn_url":"https://github.com/Meiqia/flask-sqlalchemy","homepage":"http://flask-sqlalchemy.pocoo.org/","size":497,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":0,"forks":2,"open_issues":0,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/13"},"html":{"href":"https://github.com/Meiqia/flask-sqlalchemy/pull/13"},"issue":{"href":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/issues/13"},"comments":{"href":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/issues/13/comments"},"review_comments":{"href":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/13/comments"},"review_comment":{"href":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/pulls/13/commits"},"statuses":{"href":"https://api.github.com/repos/Meiqia/flask-sqlalchemy/statuses/f2f7c30e6b6421ecea7a6a7e12fec53270aabe1d"}},"merged":false,"mergeable":true,"mergeable_state":"clean","merged_by":null,"comments":4,"review_comments":0,"commits":1,"additions":13,"deletions":8,"changed_files":1}}
{"actor":{"display_login":"fatelei"}}