Class for generating unique file names for new messages
Return a thread-safe increasing counter
# File lib/maildir/unique_name.rb, line 7 def counter @counter_mutex ||= Mutex.new @counter_mutex.synchronize do @counter = @counter.to_i + 1 end end
# File lib/maildir/unique_name.rb, line 14 def create self.new.to_s end
Return a unique file name based on strategy
# File lib/maildir/unique_name.rb, line 20 def initialize # Use the same time object @now = Time.now end
Return the name as a string
# File lib/maildir/unique_name.rb, line 26 def to_s [left, middle, right].join(".") end
# File lib/maildir/unique_name.rb, line 56 def delivery_count self.class.counter.to_s end
The left part of the unique name is the number of seconds from since the UNIX epoch
# File lib/maildir/unique_name.rb, line 33 def left @now.to_i.to_s end
# File lib/maildir/unique_name.rb, line 48 def microsecond @now.usec.to_s end
The middle part contains the microsecond, the process id, and a per-process incrementing counter
# File lib/maildir/unique_name.rb, line 39 def middle "M#{'%06d' % microsecond}P#{process_id}Q#{delivery_count}" end
# File lib/maildir/unique_name.rb, line 52 def process_id Process.pid.to_s end
The right part is the hostname
# File lib/maildir/unique_name.rb, line 44 def right Socket.gethostname end