Rails(ActiveRecord)ã§ãã¼ã¿ãã¼ã¹ã¸ã®ã³ãã¯ã·ã§ã³ãã¼ãªã³ã°ããããªããã
ActiveRecordã使ã£ãRailsã¢ããªã¯ï¼ããã©ã«ãã§ãã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶ããã¼ã«ããããã«ãªã£ã¦ãã¾ãï¼
ActiveRecordã¦ã¼ã¶ã¨ãã¦ã¯å¾
ã¡ãã³ããï¼çãªæ©è½ãããã®ã§ããï¼è¨å®çã§ãããdisableãããã¨ãåºæ¥ãï¼LVS+keepalivedãä»ããå ´åã«ã¯ãã¼ããã©ã³ã·ã³ã°ãæåã®æ¥ç¶æã«ããçºãããããªããªãåä»ã§ãï¼
対çã¨ãã¦æãã¤ããã®ã¯
- ãã¼ã«ããæ¥ç¶ãæ©ãå¨æã§æ¨ã¦ã
- LVS+keepalivedã§ã¯ãªãï¼MySQL Proxyã§ãã©ã³ã·ã³ã°ãã(Proxyã¸ã®æ¥ç¶ã¯ãã¼ã«)
- ããããæ¥ç¶ããã¼ã«ãããªã
ãããã§ããï¼
ã©ãããã®ãã»ãªãªã¼ãªã®ãã¨èª¿ã¹ã¦ã¿ãã¨
ãªã©4å¹´è¿ãåã«è°è«ããã¦ãã¦ï¼"ãã¼ï¼é«ééè·¯ãããªã"ã¨ï¼ã³ãã¯ã·ã§ã³ç¢ºç«ã®ã³ã¹ãã調ã¹ãåã«ããã¼ã«ãããªããã¨ããé¸æãåºæ¥ã¦ãã¾ãã¾ããï¼æ¬æ¥ã¯æ ¹ã£ããã調ã¹ãã¹ãã ã¨ã¯æãã¾ããï¼ä»åã¯ãµã¼ãã¹ã¬ãã«ã§ä½ãå¤ãããã¨ããã®ã端çã«èª¿ã¹ããã¨ã«ãã¾ããï¼
ãã ï¼ActiveRecordã®ã³ãã¯ã·ã§ã³ãã¼ãªã³ã°ã«ã¤ãã¦ã¯ï¼å®è£ ããããã¨ãåãã§ãã人ã¯ãããã§å¤ãè¦ã¤ããã®ã§ããï¼ãã®æ©è½ãæ½°ãããã¨æã人ã®å£°ã«ã¯ãªããªãå·¡ãä¼ãã¾ããã§ããï¼å¤§ããªRailsãµã¤ãã¯ãã£ã±ãããã¨äºãã®ã«...ããããAR使ãã®ï¼ã¨ãMySQLProxyåã¾ãããã£ã¦ãã¨ãªã®ã§ããããï¼
æåã¯database.ymlã¨ãã§
pool: 0
ã¨ã
wait_timeout: 0
ã¨ããã£ããåºæ¥ãããããªããã¨æ¥½ãããã¤ããã§è©¦ã¿ã¾ãããï¼ã¨ã©ã¼ã ããã«ãªã£ã¦ãã¾ãã¾ããï¼ï¼ã¡ãªã¿ã«ã©ã¡ããããã©ã«ãå¤ã¯5ã§ãï¼
èªããï¼ã³ã¼ãï¼
ARã§ã³ãã¯ã·ã§ã³ãã¼ãªã³ã°ãè¡ã£ã¦ããç®æ
ActiveRecord-2.3.5ã ã¨
activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb
ã®
- checkout
- checkin
ã¨ãã£ãã¡ã½ããã§ããï¼ã³ã¡ã³ãã¢ã¦ãã®èª¬æã«ãcheckinã§poolã«ã³ãã¯ã·ã§ã³ã®ãªãã¸ã§ã¯ãconnãå
¥ãã¦ï¼checkoutã§åãåºãã¦ãããã¨æ¸ãã¦ããã¾ããï¼
å®æã§ã¯ããã¾ããï¼checkinã®ã¨ãã«ãã¼ã«ããã«ã³ãã¯ã·ã§ã³ãæ¨ã¦ã¦ï¼checkoutã§æ°ããã³ãã¯ã·ã§ã³ã確ç«ãããã°ãã¼ãªã³ã°ãæ©è½ãããªãã¦æ¸ã¿ããã§ãï¼
def checkout ActiveRecord::Base.send(spec.adapter_method, spec.config.dup) # same def new_connection end def checkin(conn) conn.disconnect! end
ã¿ãããªæãï¼
ã¡ãã£ã¨ä¸å®ã ãªã¨æã£ã¦ãããgithubã§ãå·¡ãä¼ãã¾ããï¼ä½è¨ãªã¡ã½ããæ½°ãããããã¦ããã£ãããã¾ããï¼
verify!(activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb)ã§ï¼æ¥ç¶ããã¦ãããã確èªãï¼ããã§ãªãã£ããåæ¥ç¶ãããã¦ãã¾ãï¼
å®æ½
$RAILS_HOME/lib以ä¸ã«disable_connection_pooling.rbã¨ã§ããã¦ä¸ã®ã³ã¼ããç½®ãã¦ï¼$RAILS_HOME/config/environment.rbã§requireããã¾ããï¼
é©ç¨å¾
åæã«åå¨ããMySQLã®ã¹ã¬ããæ°ãæ¿æ¸ãã¾ããï¼Passengerã§Railsã¢ããªãè¤æ°ã®ããã»ã¹åããã¦ï¼ããããã5ãã¼ã«ãã¤æã£ã¦ãã¦ï¼ãããã¾ãè¤æ°ã®ãµã¼ãã§DBã«æ¥ç¶ãã¦ããç¶æ
ããã®å¤åãªã®ã§ããªãã¨ã³ã§ããï¼
PCãµã¤ãã ã¨ã³ãã¯ã·ã§ã³ç¢ºç«ã®ã³ã¹ãããããªã«æ°ã«ãããã®ã§ããªãï¼ãµã¼ãã¹ã®ã¬ã¹ãã³ã¹ã¿ã¤ã ã«å½±é¿ã¯ããã¾ããã§ããï¼ãã ï¼ã¢ãã¤ã«ã®å ´åã¯å
¨ä½ã®å¦çããããªã«å¤ããªãçºãï¼ã¢ã¯ã»ã¹ãéä¸ããæé帯ãªã©ã§ã¯ã¬ã¹ãã³ã¹ã¿ã¤ã ãæªåãã¦ãã¾ãã¾ããï¼