Skip to content

Instantly share code, notes, and snippets.

@ryochin
Last active September 17, 2018 03:08
Show Gist options
  • Save ryochin/88f6ebc3058757988f52443c0d7fa0d4 to your computer and use it in GitHub Desktop.
Save ryochin/88f6ebc3058757988f52443c0d7fa0d4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Main
require 'net/http'
require 'fileutils'
require 'thwait'
def initialize
@dir = './chunked_data'
FileUtils.rm_rf @dir
FileUtils.mkdir_p @dir
@size_limit = 2 * 1000 ** 2
end
def run
http = Net::HTTP.new('ftp.yz.yamagata-u.ac.jp')
http.read_timeout = 5
pos = 1
chunk = ''
threads = []
http.request_get('/pub/network/apache/httpd/httpd-2.4.34.tar.bz2') do |response|
response.read_body do |data|
chunk += data
# split & save content (async)
if chunk.size > @size_limit
threads << Thread.new(pos, chunk) do |p, c|
save_content p, c
end
chunk = ''
pos += 1
end
end
end
# save the remnant (sync)
if chunk.size.positive?
save_content pos, chunk
end
# wait all threads done
ThreadsWait.new(*threads)
puts 'done'
end
def save_content(pos, chunk)
sleep 2
File.open file(pos), 'wb' do |io|
io.write chunk
end
puts pos
end
def file(pos)
File.join(@dir, '%02d.bin' % pos)
end
end
Main.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment