Class: Gel::TailFile
- Inherits:
-
Object
- Object
- Gel::TailFile
- Defined in:
- lib/gel/tail_file.rb
Constant Summary collapse
- MAXIMUM_CHAIN =
The number of redirects etc we’ll follow before giving up
8
- CONTENT_OVERLAP =
When attempting a partial file download, we’ll back up by this many bytes to ensure the preceding content matches our local file
100
- PARTIAL_MINIMUM =
Only bother trying for a partial download if we have at least this many bytes of local content. If we *don’t* make a partial request, 1) we’re guaranteed not to need a second request due to bad overlap, and 2) the response can be gzipped.
65536
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#pinboard ⇒ Object
Returns the value of attribute pinboard.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #debug(message) ⇒ Object
- #done? ⇒ Boolean
-
#initialize(uri, pinboard, httpool: Gel::Httpool.new) ⇒ TailFile
constructor
A new instance of TailFile.
- #update(force_reset: false) ⇒ Object
Constructor Details
#initialize(uri, pinboard, httpool: Gel::Httpool.new) ⇒ TailFile
Returns a new instance of TailFile.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gel/tail_file.rb', line 24 def initialize(uri, pinboard, httpool: Gel::Httpool.new) @uri = uri @pinboard = pinboard @httpool = httpool @filename = pinboard.filename(uri) @etag = pinboard.etag(uri) @done = false end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
23 24 25 |
# File 'lib/gel/tail_file.rb', line 23 def filename @filename end |
#pinboard ⇒ Object
Returns the value of attribute pinboard.
23 24 25 |
# File 'lib/gel/tail_file.rb', line 23 def pinboard @pinboard end |
#uri ⇒ Object
Returns the value of attribute uri.
23 24 25 |
# File 'lib/gel/tail_file.rb', line 23 def uri @uri end |
Instance Method Details
#debug(message) ⇒ Object
202 203 204 |
# File 'lib/gel/tail_file.rb', line 202 def debug() #$stderr.puts message if $DEBUG end |
#done? ⇒ Boolean
35 36 37 |
# File 'lib/gel/tail_file.rb', line 35 def done? @done end |
#update(force_reset: false) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/gel/tail_file.rb', line 39 def update(force_reset: false) uri = self.uri File.open(@filename, "a+b") do |f| f.seek(0, IO::SEEK_END) MAXIMUM_CHAIN.times do f.seek(0, IO::SEEK_SET) if force_reset request = Net::HTTP::Get.new(uri) requested_from = 0 if f.pos > PARTIAL_MINIMUM requested_from = f.pos - CONTENT_OVERLAP request["Range"] = "bytes=#{requested_from}-" request["Accept-Encoding"] = "identity" else f.pos = 0 end request["If-None-Match"] = @etag if @etag response = @httpool.request(uri, request) case response when Net::HTTPNotModified @done = true pinboard.updated(uri, response["ETag"], false) return # no-op when Net::HTTPRequestedRangeNotSatisfiable # This should never happen, but sometimes does: either the # file has been rebuilt, and we should do a full fetch, or # we're ahead of the server (because an intermediate is # caching too aggressively, say), and we should do nothing. if response["Content-Range"] =~ /\Abytes \*\/(\d+)\z/ current_length = $1.to_i debug "File is smaller than we expected: only #{current_length} bytes" # Back up a bit, and check whether the end matches what we # already have f.pos = current_length next else force_reset = true next end when Net::HTTPOK f.pos = 0 f.truncate(0) f.write response.body f.flush @done = true pinboard.updated(uri, response["ETag"], true) return # Done when Net::HTTPPartialContent if response["Content-Range"] =~ /\Abytes (\d+)-(\d+)\/(\d+)\z/ from, to, size = $1.to_i, $2.to_i, $3.to_i else # Not what we asked for debug "Bad response range" force_reset = true next end if from != requested_from # Server didn't give us what we asked for debug "Incorrect response range" force_reset = true next end if to - from + 1 != response.body.size # Server didn't give us what it claimed to debug "Bad response length" force_reset = true next end debug "Current file size is #{File.size(@filename)}" debug "Remote size is #{size}" f.pos = from if to < f.size # No new content, but check the overlap in case the file's # been reset overlap = f.read(to - from + 1) if response.body == overlap # Good overlap, but nothing new debug "Overlap is good, but no new content" @done = true pinboard.updated(uri, @etag, false) # keep old etag return # Done else # Bad overlap debug "Bad overlap on short response" force_reset = true next end else overlap = f.read if response.body[0, overlap.size] == overlap # Good overlap; use rest rest = response.body[overlap.size..-1] debug "#{overlap.size} byte overlap is okay" debug "Using remaining #{rest.size} bytes" f.write(rest) f.flush @done = true pinboard.updated(uri, response["ETag"], true) return # Done else # Bad overlap debug "Bad overlap on long response" force_reset = true next end end when Net::HTTPRedirection uri = URI(response["Location"]) next when Net::HTTPTooManyRequests # https://github.com/rubygems/rubygems-infrastructure/blob/adc0668c1f1539f281ffe86c6e0ad12743c5c8bd/cookbooks/rubygems-balancer/templates/default/site.conf.erb#L12 debug "Rate limited" sleep 1 + rand next else # splat response.value raise "Unexpected HTTP success code" end end raise "Giving up after #{MAXIMUM_CHAIN} requests" end end |