ä¸è¨ã®ã¨ã³ããªã§è¨åãã¦ããã¡ã¼ã«ã®åä¿¡ãµã¼ãdovecotãmrubyã§å¶å¾¡ããpluginãæ¦ãå®æãã¾ããã®ã§ç´¹ä»ãã¾ããã¨ããã®ããä¸æåãããã«ã¯ã§ãã¦ããã®ã§ãããã¿ãã¿ãã¦ããããã°ã«ã§ãã¦ãã¾ããã§ããã
dovecot-mruby-pluginã¯ã¡ã¼ã«åä¿¡ã®IMAPãµã¼ãã¨ãã¦åãdovecotãmrubyã§è²ã å¶å¾¡ãããã¨ãã§ãã¾ããä»æ¥ã¯ãã®å¶å¾¡ã®ä¾ãå¹¾ã¤ãç´¹ä»ãã¾ãã
mrubyã§IMAPã³ãã³ããä½ã
dovecot-mruby-pluginã§ã¯ãdovecotã®IMAPãããã³ã«ã§ä½¿ãã³ãã³ããmrubyã§ä½ããã¨ãã§ãã¾ããä¾ãã°ä»¥ä¸ã®ããã«Rubyã®ã³ã¼ããæ¸ããdovecotã«èªã¿è¾¼ã¾ãã¾ãã
# Register new commands %w( matsumotory test ).each do |cmd| Dovecot::IMAP.command_register(cmd) do |args| Dovecot::IMAP.send_line "Hi, #{Dovecot::IMAP.username}" if cmd == Dovecot::IMAP.username Dovecot::IMAP.send_line "You are me." else Dovecot::IMAP.send_line "I am #{cmd}. Not you with #{args}" end end end
ãã®ä¸ã§ã以ä¸ã®ããã«dovecot.confã«dovecotä¸ã§æå¹ãªç°å¢å¤æ°ãè¨å®ãã¾ãã
import_environment = DOVECOT_MRUBY_INIT_PATH
ããã¦ãdovecotã«ä¸è¨ã®Rubyã³ã¼ããç°å¢å¤æ°çµç±ã§æ¸¡ãã¦èµ·åããã¾ãã
DOVECOT_MRUBY_INIT_PATH=/path/to/command_register.rb ./dovecot/target/sbin/dovecot \ -c ./dovecot/configuration/dovecot.conf
ããã¦å®éã«telnetã§IMAPãµã¼ãã«ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãããã
$ telnet 127.0.0.1 6070 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. * OK [CAPABILITY ...] Dovecot ready. 1 login test testPassword 1 OK [CAPABILITY ...] Logged in 1 matsumotory hoge 1 * matsumotory Hi, test * matsumotory I am matsumotory. Not you with ["hoge", "1"] 1 OK matsumotory completed (0.001 + 0.000 secs). 1 test * test Hi, test * test You are me. 1 OK test completed (0.001 + 0.000 secs). 1 matsumotory * matsumotory Hi, test * matsumotory I am matsumotory. Not you with [] 1 OK matsumotory completed (0.001 + 0.000 secs).
ãªãã¨ãããã¨ã§ããããtestã³ãã³ããmatsumotoryã³ãã³ããæå¹ã¨ãªãããã®å¼æ°ãRubyã³ã¼ãä¸ã§åãã¦ä½ãããå¦çããã³ãã³ããåãã¦ãããã¨ããããã¾ããããã§ãªã¬ãªã¬ã³ãã³ããã³ãã³ãã®æ¡å¼µãªã©ããdovecotå´ã®ã³ã¼ããå¼ããã¨ãç°¡åã«å®è£ ã§ããããã«ãªãã¾ããããªãã¨ãããã¨ã§ãããï¼
æ¢åã®IMAPã³ãã³ãã®aliasã³ãã³ããä½ã
dovecot-mruby-pluginã¯æ¢åã®IMAPã³ãã³ãã®aliasãä½ããã¨ãã§ãã¾ããä¾ãã°ä»¥ä¸ã®ããã«ã³ã¼ããæ¸ãã¾ãã
# Register CAP command which run CAPABILITY command Dovecot::IMAP.alias_command_register("CAP") do Dovecot::IMAP.send_line "execute CAPABILITY commands" Dovecot::IMAP.capability end Dovecot::IMAP.alias_command_register("LIST_ALIAS") do Dovecot::IMAP.send_line "alias LIST commands" Dovecot::IMAP.list end
ããã¦ãä¸è¿°ããèªã¿è¾¼ã¿æ¹ã§dovecotã«Rubyã³ã¼ããèªã¿è¾¼ã¾ãã¾ãããã®å¾ãtelnetã§ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãããã
1 cap * cap execute CAPABILITY commands * CAPABILITY IMAP4rev1 LITERAL+ SASL-IR ... 1 OK Capability completed (0.001 + 0.000 secs). 1 list "" * * LIST (\HasNoChildren) "/" INBOX 1 OK List completed (0.001 + 0.000 secs). 1 list_alias "" * * list_alias alias LIST commands * LIST (\HasNoChildren) "/" INBOX 1 OK List completed (0.001 + 0.000 secs).
ããããªãã¨ãããã¨ã§ãããï¼capability
ã³ãã³ãã®aliasã§ããcap
ã³ãã³ããã¡ããã¨åãã¦ãããlist
ã³ãã³ãã§ããaliasã®list_alias
ãæå¹ã«ãªã£ã¦ããã§ã¯ããã¾ãããããªãã¨ãããã¨ã§ãããã
IMAPã³ãã³ãã®åå¾ã§Rubyã³ã¼ããããã¯ãã
ããã«ãåIMAPã³ãã³ãã®åå¾ã«Rubyã³ã¼ããããã¯ãããã¨ãã§ãã¾ãã
ä¾ãã°ãlist
ã³ãã³ããé常ã«CPUè² è·ãæ¸ãã¦ãã¾ãå ´åã«ããã®å¦çãæå¦ãããä¸æãããããã®ã§ã¯ãªããCPU使ç¨çã®å²ãå½ã¦ã30%ã«æããããã¨ãããã¨ãç°¡åã«å®ç¾ãããã¨ãã§ãã¾ãã
以ä¸ã®ããã«ãã³ãã³ãã®åã«å¦çãããpre_list.rb
ã¨ãã³ãã³ãå®è¡å¾ã«å¦çãããpost_list.rb
ãç¨æãã¾ãã
pre_list.rb
rate = Cgroup::CPU.new "test" # limit cpu 30% usage rate.cfs_quota_us = 30000 rate.create if Dovecot::IMAP.username == "test" && Dovecot::IMAP.command_name == "LIST" rate.attach end
post_list.rb
rate = Cgroup::CPU.new "test" if Dovecot::IMAP.username == "test" && Dovecot::IMAP.command_name == "LIST" rate.detach end
ããã¦ãä»®æ³çã«list
ã³ãã³ããCPUã使ãåãç¶æ³ãåç¾ããããã«ãä¸è¨ã§è§£èª¬ããããã«ãæ°ãã«Rubyã§list
ã³ãã³ããåå®ç¾©ããã«ã¼ããããå¦çã«list
ã³ãã³ããä¸æ¸ããã¾ãã
Dovecot::IMAP.command_register("LIST") do |args| 100000000.times {} Dovecot::IMAP.send_line "done!" end
ããã¦ã以ä¸ã®ããã«dovecot.confã«pre_list.rb
ã¨post_list.rb
ãç»é²ãã¦ããã¾ãã
- 95-mruby.conf
protocol imap { mail_plugins = $mail_plugins imap_mruby } plugin { mruby_pre_list_path = /path/to/pre_list.rb mruby_post_list_path = /path/to/post_list.rb }
ããã¦ãå®éã«telnetã§ã¢ã¯ã»ã¹ããlist
ã³ãã³ããå®è¡ãã¦ã¿ã¾ããããããã¨ã
29906 ubuntu 20 0 87640 9508 7532 R 29.9 0.1 0:04.46 imap
ãªãã¨ãããã¨ã§ãããï¼
ã¾ã¨ã
以ä¸ã®ããã«ãdovecot-mruby-pluginã使ãã°ãdovecotã®IMAPãµã¼ããé常ã«å®¹æã«æ¡å¼µãããã¨ãã§ãã¾ããã¾ããdovecotã¯1ã»ãã·ã§ã³ã«å¯¾ãã¦1ããã»ã¹ãçæç ´æ£ããCGIçããã»ã¹ã¢ãã«ãæ¡ç¨ãã¦ãããããmrubyã®ã¤ã³ã¿ããªã¿ãããã»ã¹åä½ã§èªã¿è¾¼ãã§ãããåã»ãã·ã§ã³éã§mrubyã®æ å ±ãå ±æãããã¨ããªãå®å ¨ã§ãã
ããã¾ã§ã®ããã«ã¦ã§ã¢å®è£ ã®çµé¨ä¸ãã»ãã·ã§ã³åä½ã§ããã»ã¹ãçæç ´æ£ããã¢ãã«ã¯ãã»ãã·ã§ã³æ¯ã«mrubyã¤ã³ã¿ããªã¿ãä½ã£ãã¨ãã¦ããæ§è½çã«ã¯ããã»ã¹ã®çæç ´æ£ããªã¼ãã¼ãããã«ãªããã¨ãããã£ã¦ããï¼ããã»ã¹çæç ´æ£ã1ç§éã«500ã»ãã·ã§ã³å¦çã§ãããã·ã³ã§ãmrubyã®ã¤ã³ã¿ããªã¿ã¯1ç§éã«2000åã¤ã³ã¿ããªã¿ãä½ããç¨åº¦ã®ã³ã¹ãï¼ãããé常ãæ§è½é¢ã§ãçµã¿è¾¼ã¿ã¢ã¼ããã¯ãã£ãåé¡ã«ã¯ãªããã¨ãªãã§ãããããããããã¨ããããå®è£ ã«ã¤ãã¦ã¯é常ã«ç°¡åã§ããã
ã¨ã¯ãããdovecotã®ã³ã¼ããèªãã§dovecotã®ã¢ã¼ããã¯ãã£ãå®è£ ãç解ããä¸ã§pluginã®ä»æ§ã«åºãã¦å®è£ ãããã¨ãããã¨ã«ã¤ãã¦ã¯ãä¾ã®ãã¨ãããã¥ã¡ã³ãããªãCè¨èªã§æ¸ãããç¡ãã®ã§ããããªãã«æ·å± ãé«ãæéããããã¨æããã¾ãã®ã§ããããmrubyã§wrapãã¦Rubyã§æ¯è¼ç誰ã§ãæ¡å¼µã§ãããããªç¶æ³ã«ãªãã¨è¯ããªã¨æãä½ãã¾ããããã¤ãã®ã¢ããã¼ã·ã§ã³ã§ããã
ã¨ãããã¨ã§ãæ¯éã¾ãã¯è²ã éãã§é ãã¦ããã®å¾å種ç¨éã«ãæ´»ç¨é ããã¨å¹¸ãã§ããã¾ããæ¨æ¥ã«ã¤ãã¦ã¯æä½éã§ãã®ã§ã追å æ©è½ãã¡ã½ããã«ã¤ãã¦ã¯issueã§é£çµ¡é ããã¨ç©æ¥µçã«å®è£ ãããã¨èãã¦ããã¾ãã®ã§ãæ°è»½ã«ãåãåãããã ããã