pezra / filequeue Goto Github PK
View Code? Open in Web Editor NEWsimple file based queue in Ruby
Home Page: http://test.rubygems.org/gems/filequeue
License: MIT License
simple file based queue in Ruby
Home Page: http://test.rubygems.org/gems/filequeue
License: MIT License
The length method doesn't protect from a file that doesn't exist yet:
$ irb
irb(main):001:0> require 'filequeue'
=> true
irb(main):002:0> queue = FileQueue.new 'bob.txt'
=> #<FileQueue:0x007fb2cc2c10b0 @delimiter="\n", @file_name="bob.txt">
irb(main):003:0> queue.empty?
Errno::ENOENT: No such file or directory - bob.txt
from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:51:in initialize' from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:51:in
open'
from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:51:in safe_open' from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:35:in
length'
from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:42:in empty?' from (irb):3 from /usr/bin/irb:12:in
I may be wrong but it seems like the following code has a potential race condition:
safe_open 'r' do |file|
value = file.gets @delimiter
rest = file.read
end
safe_open 'w+' do |file|
file.write rest
end
e.g:
A obtains lock; A reads line; A caches rest
B obtains lock; B reads line; B caches rest
B obtains lock; B writes rest
A obtains lock; A writes rest
Fixed by not releasing the lock until rest
is written back to the file:
FileQueue.class_eval do
def pop
value = nil
safe_open 'r+' do |file|
value = file.gets @delimiter
rest = file.read
file.rewind
file.truncate(0)
file.write(rest)
end
value ? value[0..-(@delimiter.length) - 1] : nil
end
end
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.