<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tmty.jp &#187; Rails</title>
	<atom:link href="http://tmty.jp/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://tmty.jp</link>
	<description>tmtysk が前よりも、もうちょっとだけコマメに更新していくかもしれないブログ</description>
	<lastBuildDate>Tue, 08 Jun 2010 13:19:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<link rel="http://api.friendfeed.com/2008/03#sup" xmlns="http://www.w3.org/2005/Atom" type="application/json" href="http://friendfeed.com/api/public-sup.json#fdff53faaa"/>		<item>
		<title>デコメールの3キャリア間変換などをおこなう Rails プラグイン MbMail</title>
		<link>http://tmty.jp/2008/12/25/%e3%83%87%e3%82%b3%e3%83%a1%e3%83%bc%e3%83%ab%e3%81%ae3%e3%82%ad%e3%83%a3%e3%83%aa%e3%82%a2%e9%96%93%e5%a4%89%e6%8f%9b%e3%81%aa%e3%81%a9%e3%82%92%e3%81%8a%e3%81%93%e3%81%aa%e3%81%86-rails-%e3%83%97/</link>
		<comments>http://tmty.jp/2008/12/25/%e3%83%87%e3%82%b3%e3%83%a1%e3%83%bc%e3%83%ab%e3%81%ae3%e3%82%ad%e3%83%a3%e3%83%aa%e3%82%a2%e9%96%93%e5%a4%89%e6%8f%9b%e3%81%aa%e3%81%a9%e3%82%92%e3%81%8a%e3%81%93%e3%81%aa%e3%81%86-rails-%e3%83%97/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 16:07:47 +0000</pubDate>
		<dc:creator>tmtysk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[MbMail]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://wp.tmty.jp/2008/12/25/%e3%83%87%e3%82%b3%e3%83%a1%e3%83%bc%e3%83%ab%e3%81%ae3%e3%82%ad%e3%83%a3%e3%83%aa%e3%82%a2%e9%96%93%e5%a4%89%e6%8f%9b%e3%81%aa%e3%81%a9%e3%82%92%e3%81%8a%e3%81%93%e3%81%aa%e3%81%86-rails-%e3%83%97/</guid>
		<description><![CDATA[git とか GitHub とかいうのを使った事がなかったので、このたび初めて使ってみました。というわけで、標記の Rails プラグインを the MIT License にて公開します。
MbMail &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>git とか GitHub とかいうのを使った事がなかったので、このたび初めて使ってみました。というわけで、標記の Rails プラグインを the MIT License にて公開します。<br/><br/><br />
<a href="http://github.com/tmtysk/mbmail/tree/master">MbMail &#8211; GitHub</a><br/><br/><br />
MbMail は、<b>Rails で日本の携帯向けサービスをつくる際、 メール扱い周りで発生するちょっとした面倒を回避するための小物寄せ集めプラグイン</b>です。rails-2.0.2, tmail-1.1.0 環境下での動作を確認しています。また、rails プラグイン形式で配布していますが、MbMail::DMail は rails 環境に関係なく使えます。<br/><br/><br />
MbMail は以下の機能を提供しています。</p>
<ul>
<li>MbMail::DMail
<ul>
<li>キャリア間HTMLメール(デコメール, デコレーションメール, デコレメール)の相互変換</li>
</ul>
</li>
<li>MbMail::MbMailer
<ul>
<li>ActionMailer の拡張
<ul>
<li>標準で使用する charset (エンコーディング)を ISO-2022-JP に変更</li>
<li>文字コード変換器に NKF を使用するように変更 (機種依存文字の送信を可能に)</li>
<li>日本語ヘッダを作成するためのメソッド base64 を追加 (Mailer 内で日本語Subjectや日本語Fromを簡単に記述)</li>
<li>RFC違反のドットが連続するアドレスが扱えるよう、TMail::Parser を置き換え</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>たとえば、以下のように au デコレーションメール形式のファイルがある場合、これを load し to_docomo_format とすれば、docomo のデコメールフォーマットが得られます。</p>
<pre class="prettyprint">
au = MbMail::DMail.load("au_decoration_mail.eml")
docomo = au.to_docomo_format
puts docomo.encoded # => docomo デコメールフォーマット文字列
</pre>
<p>その他、使用方法サンプルは、git リポジトリの中の spec ファイルたちをご参考ください。<br/><br/><br />
パッケージに含まれるクラスのうち、MbMail::MbMailer は以前、こちらのエントリでも取り上げた「<a href="http://blog.tmty.jp/archives/54977725.html">ドット連続アドレス問題</a>」などに対応するものです。ただし、MbMailer を使用する事により TMail::Parser を書き換えてしまいますので、ご注意ください(MbMailer を使用するに当たって、ActionMailer の使い勝手を残しつつ TMail の一部だけ書き換える、ウマい方法が思いつかず。。誰か助けて)。<br/><br/><br />
MbMail::DMail も MbMail::MbMailer も個人的な利用実績がありますので、基本的な部分はそこそこ使えると思います。が、プラグイン形式に切り出したのはこれが初めてですので、何かと不都合があるかもしれません。(絵文字の変換辺りで微妙にオカシイところがあるかも。)あと、git の使い方がよく分かってないうちは、何かと不慣れなことをしでかすかもしれません。諸々ご理解の上、ご利用いただければ幸いです。<br/><br/><br />
なお、再掲を含みますが、上記のプラグイン提供に当たりお世話になった方々をご紹介しておきます。勉強させていただきました。ありがとうございました。</p>
<ul>
<li>携帯メールを扱うに当たっての base64 メソッドの実装や NKF 利用のアイデアとして、以下のサイト様を参考にさせていただきました
<ul>
<li><a href="http://www.kbmj.com/~shinya/rails_seminar/slides/#(33)">http://www.kbmj.com/~shinya/rails_seminar/slides/#(33)</a></li>
<li><a href="http://wiki.fdiary.net/rails/?ActionMailer">http://wiki.fdiary.net/rails/?ActionMailer</a></li>
</ul>
</li>
<li>DMail の絵文字相互変換に Jpmobile の絵文字変換テーブルを使用させていただいています</li>
<li>ActionMailer の拡張時に TMail の幾つかのクラスを書き換え利用させていただいています</li>
<li>サンプルで使用している GIF 画像に 素材屋イチゴアポロ の素材を利用させていただいています</li>
</ul>
<p>ついでに、以前こちらで紹介した「<a href="http://blog.tmty.jp/archives/54984497.html">ケータイサイトで機種情報を取得する Rails プラグイン mbterm_db</a>」も GitHub に引っ越しました。ついでに更新しようと思ったけど、更新の仕方を忘れたので、そのうちメンテします。<br/><br/><br />
<a href="http://github.com/tmtysk/mbtermdb/tree/master">MbtermDb &#8211; GitHub</a><br/><br/><br />
それでは皆様、よいクリスマス＆よいお年をお迎えくださいませ。<br />
<br/><br/><strong>関連していそうなエントリ:</strong>
<ul class="similar-posts">
<li><a href="http://tmty.jp/2008/02/25/%e3%82%b1%e3%83%bc%e3%82%bf%e3%82%a4%e3%82%b5%e3%82%a4%e3%83%88%e3%81%a7%e6%a9%9f%e7%a8%ae%e6%83%85%e5%a0%b1%e3%82%92%e5%8f%96%e5%be%97%e3%81%99%e3%82%8b-rails-%e3%83%97%e3%83%a9%e3%82%b0%e3%82%a4/" rel="bookmark" title="2008 年 2 月 25 日">ケータイサイトで機種情報を取得する Rails プラグイン mbterm_db</a></li>
<li><a href="http://tmty.jp/2008/12/27/%e6%90%ba%e5%b8%af%e3%81%ab%e3%83%a1%e3%83%bc%e3%83%ab%e3%82%92%e5%8a%b9%e7%8e%87%e7%9a%84%e3%81%ab%e9%80%81%e4%bf%a1%e3%81%99%e3%82%8b%e3%81%8b%e3%82%82%e3%81%97%e3%82%8c%e3%81%aa%e3%81%84smtp/" rel="bookmark" title="2008 年 12 月 27 日">携帯にメールを効率的に送信する(かもしれない)SMTPクライアント MbSmtp</a></li>
<li><a href="http://tmty.jp/2008/02/19/tmail-%e3%81%a7%e6%90%ba%e5%b8%af%e3%83%a1%e3%83%bc%e3%83%ab%e3%82%a2%e3%83%89%e3%83%ac%e3%82%b9%e3%82%92-parse-%e3%81%99%e3%82%8b/" rel="bookmark" title="2008 年 2 月 19 日">TMail で携帯メールアドレスを parse する</a></li>
</ul>
<p><!-- Similar Posts took 42.234 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://tmty.jp/2008/12/25/%e3%83%87%e3%82%b3%e3%83%a1%e3%83%bc%e3%83%ab%e3%81%ae3%e3%82%ad%e3%83%a3%e3%83%aa%e3%82%a2%e9%96%93%e5%a4%89%e6%8f%9b%e3%81%aa%e3%81%a9%e3%82%92%e3%81%8a%e3%81%93%e3%81%aa%e3%81%86-rails-%e3%83%97/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>around_filter で reset_session するとメモリリーク(memory leak)する</title>
		<link>http://tmty.jp/2008/12/11/around_filter-%e3%81%a7-reset_session-%e3%81%99%e3%82%8b%e3%81%a8%e3%83%a1%e3%83%a2%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%afmemory-leak%e3%81%99%e3%82%8b/</link>
		<comments>http://tmty.jp/2008/12/11/around_filter-%e3%81%a7-reset_session-%e3%81%99%e3%82%8b%e3%81%a8%e3%83%a1%e3%83%a2%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%afmemory-leak%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 16:57:14 +0000</pubDate>
		<dc:creator>tmtysk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Trouble]]></category>

		<guid isPermaLink="false">http://wp.tmty.jp/2008/12/11/around_filter-%e3%81%a7-reset_session-%e3%81%99%e3%82%8b%e3%81%a8%e3%83%a1%e3%83%a2%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%afmemory-leak%e3%81%99%e3%82%8b/</guid>
		<description><![CDATA[以下のように around_filter 内で reset_session するアクションに対し、大量のアクセスを重ねていくと、メモリ使用量ががつがつ増大していく。

class FugaController < App [...]]]></description>
			<content:encoded><![CDATA[<p>以下のように around_filter 内で reset_session するアクションに対し、大量のアクセスを重ねていくと、メモリ使用量ががつがつ増大していく。<br/></p>
<pre class="prettyprint">
class FugaController < ApplicationController
around_filter :foo
def index
end
protected
def foo
reset_session
yield
end
end
</pre>
<p>こいつを script/server -e production で起動し、ab -n3000 http://localhost:3000/fuga/ などにより多量のリクエストを送ると、メモリ使用量が 80MB を超えるまでに増大し、リクエスト完了後もまったく解放されない。foo 内で yield しなくても同様。セッションストレージにデフォルトの CookieStore を使った場合のほか、:active_record_store, :mem_cache_store を使っても同じ。session_options[:expires] の設定も関係無し。<br/><br />
なお、before_filter や after_filter で reset_session した場合は、問題は起こらないようだ。また、rails-2.0.2 環境で発見したが、rails-2.2.2 でも再現する。<br />
<br/><br/><br />
状況解析のため、<a href="http://scottstuff.net/blog/articles/2006/08/17/memory-leak-profiling-with-rails">こちらのエントリ(Memory leak profiling with Rails)</a> を参考にメモリ利用状況をプロファイルしてみたところ、around_filter を使った場合は String のインスタンス数が一気に膨れ上がった後、一向に解放されないことがわかった。(before_filter を使った場合は、リクエスト処理完了後、それなりに解放された。)<br />
<br/><br/><br />
次に、この String の正体を見るために、上記の MemoryProfiler の :string_debug オプションを使って ObjectSpace 中の String をダンプしてみたところ、session_id などセッションデータと思しき文字列が大量に入っていた。リクエスト終了後は、CgiRequest や CGI::Session などのリクエストやセッションに絡むオブジェクトはすぐに解放されていた事から、何らかの原因でセッション内文字列のみ GC 対象にならない状態になっていると思われる。<br />
<br/><br/><br />
さらに、上記の String がどの時点で生成されているのかを追ってみた。<br />
<br/><br />
<b>actionpack-2.0.2/lib/action_controller/cgi_process.rb L.150-</b><br/></p>
<pre class="prettyprint">
private
# Delete an old session if it exists then create a new one.
def new_session
if @session_options == false
Hash.new
else
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => false)).delete rescue nil
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => true))
end
end
</pre>
<p>どうも上記の CgiRequest#new_session の CGI::Session.new で確保されているようだ。何が原因なのか追いたいが、CGI::Session は ActionPack で拡張されており、どうも切り分けが難しい。いろいろ書き換えながら試してみたところ、オリジナルの CGI::Session 中:<br />
<br/><br />
<b>lib/ruby/1.8/cgi/session.rb L.299</b><br/></p>
<pre class="prettyprint">
ObjectSpace::define_finalizer(self, Session::callback(@dbprot))
</pre>
<p>ここのファイナライザ定義があることで、around_filter でのみリークするようだ(これを試しに無効にすると、リークしない。謎)。かといって、こんなところを無効にするわけにはいかず.. と、今度は around_filter と before_filter とで、何が変わってきているのか、を追ってみた。<br />
<br/><br />
<b>actionpack-2.0.2/lib/action_controller/filters.rb L.709-</b><br/></p>
<pre class="prettyprint">
def run_before_filters(chain, index, nesting)
while chain[index]
filter, index = skip_excluded_filters(chain, index)
break unless filter # end of call chain reached
case filter.type
when :before
filter.run(self)  # invoke before filter
index = index.next
break if @before_filter_chain_aborted
when :around
yielded = false
filter.call(self) do
yielded = true
# all remaining before and around filters will be run in this call
index = call_filters(chain, index.next, nesting.next)
end
halt_filter_chain(filter, :did_not_yield) unless yielded
break
else
break  # no before or around filters left
end
end
index
end
</pre>
<p>ソースを追う限り、filter 処理は適用順序に並べた配列形式に変換された後、before_filter も around_filter も上記の ActionController::Filters::InstanceMethods#run_before_filters で実際のフィルタ処理がおこなわれているように見える。で、around_filter では、上記の filter.call(self) へ渡しているブロックで index = call_filters している箇所があるが、いろいろ切り分けていってみるに、どうもここが怪しい。around_filter に入れ子になっている filter を再帰的に処理しているようだが、ここの中で CGI::Session.new するとセッション中の String のみが解放されない状態になってしまう.. ということだろうか。<br />
<br/><br/><br />
もうひといきな気がするんだが、ちょっとツラくなってきたので、とりあえずここまでで POST しておく。Ruby の GC の問題なのか、Rails の黒魔術のせいなのか、まだ微妙なところだ。とりあえず before_filter にしとけば問題は起こらないのだが、多数のフィルタ処理をサイトの全域で使うようなサービスの場合、個々のコントローラには around_filter をひとつ書いておいて、その filter 内に多数のフィルタメソッドを並べる、というのがラクチンだとおもう。うーむ、悔しいが before_filter で回避してしまおうか.. フィルタ内で、セッション検査をおこなった上で、不正セッションであれば reset_session して仕切り直す、みたいなのって、わりとあるような気がするんだけどなあ.. ハマってる人が見当たらない。</p>
<p><span id="more-79"></span><br />
<br/><br/><br />
(2008-12-24追記)<br/><br />
lighthouseapp にタレコんだところ、修正がコミットされたようです。<br/><br/><br />
<a href="http://rails.lighthouseapp.com/projects/8994/tickets/1558-memory-problem-on-reset_session-in-around_filter">http://rails.lighthouseapp.com/projects/8994/tickets/1558-memory-problem-on-reset_session-in-around_filter</a><br/><br/><br />
undefine_finalizer しちゃってるけど.. うーん、これでいいのかなあ、怖いなあ<br />
<br/><br/><strong>関連していそうなエントリ:</strong>
<ul class="similar-posts">
<li><a href="http://tmty.jp/2008/12/25/%e3%83%87%e3%82%b3%e3%83%a1%e3%83%bc%e3%83%ab%e3%81%ae3%e3%82%ad%e3%83%a3%e3%83%aa%e3%82%a2%e9%96%93%e5%a4%89%e6%8f%9b%e3%81%aa%e3%81%a9%e3%82%92%e3%81%8a%e3%81%93%e3%81%aa%e3%81%86-rails-%e3%83%97/" rel="bookmark" title="2008 年 12 月 25 日">デコメールの3キャリア間変換などをおこなう Rails プラグイン MbMail</a></li>
<li><a href="http://tmty.jp/2007/06/12/netbeans-60m9%e3%81%a7rubyonrails%e3%81%99%e3%82%8b%e3%81%a8generate%e3%81%a7%e3%82%b3%e3%82%b1%e3%82%8b/" rel="bookmark" title="2007 年 6 月 12 日">NetBeans-6.0M9でRubyOnRailsするとgenerateでコケる</a></li>
<li><a href="http://tmty.jp/2008/02/18/rails%e5%8b%89%e5%bc%b7%e4%bc%9a%e6%9d%b1%e4%ba%ac%e3%81%ab%e8%a1%8c%e3%81%a3%e3%81%a6%e3%81%8d%e3%81%9f080217/" rel="bookmark" title="2008 年 2 月 18 日">Rails勉強会(東京)に行ってきた(080217)</a></li>
</ul>
<p><!-- Similar Posts took 31.304 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://tmty.jp/2008/12/11/around_filter-%e3%81%a7-reset_session-%e3%81%99%e3%82%8b%e3%81%a8%e3%83%a1%e3%83%a2%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%afmemory-leak%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.013 seconds -->
