Commit 32fbe618 authored by Shinya Maeda's avatar Shinya Maeda

Fix HTTP::IO

parent 0e8a9c43
...@@ -65,7 +65,7 @@ module Gitlab ...@@ -65,7 +65,7 @@ module Gitlab
def read def read
stream = Gitlab::Ci::Trace::Stream.new do stream = Gitlab::Ci::Trace::Stream.new do
if trace_artifact&.exists? if trace_artifact
trace_artifact.open trace_artifact.open
elsif current_path elsif current_path
File.open(current_path, "rb") File.open(current_path, "rb")
...@@ -92,7 +92,7 @@ module Gitlab ...@@ -92,7 +92,7 @@ module Gitlab
end end
def erase! def erase!
trace_artifact&.destory trace_artifact&.destroy
paths.each do |trace_path| paths.each do |trace_path|
FileUtils.rm(trace_path, force: true) FileUtils.rm(trace_path, force: true)
......
require 'net/http'
## ##
#
# This class is compatible with IO class (https://ruby-doc.org/core-2.3.1/IO.html) # This class is compatible with IO class (https://ruby-doc.org/core-2.3.1/IO.html)
# source: https://gitlab.com/snippets/1685610 # source: https://gitlab.com/snippets/1685610
module Gitlab module Gitlab
module Ci module Ci
class Trace class Trace
class HTTP_IO class HTTP_IO
BUFFER_SIZE = 128*1024 BUFFER_SIZE = 128.kilobytes
attr_reader :uri, :size attr_reader :uri, :size
attr_reader :tell attr_reader :tell
attr_reader :chunk, :chunk_range attr_reader :chunk, :chunk_range
alias_method :pos, :tell
def initialize(url, size) def initialize(url, size)
@uri = URI(url) @uri = URI(url)
@size = size @size = size
...@@ -24,13 +23,14 @@ module Gitlab ...@@ -24,13 +23,14 @@ module Gitlab
end end
def binmode def binmode
# no-op
end end
def path def path
@uri.to_s @uri.to_s
end end
def seek(pos, where) def seek(pos, where=IO::SEEK_SET)
new_pos = new_pos =
case where case where
when IO::SEEK_END when IO::SEEK_END
...@@ -54,38 +54,43 @@ module Gitlab ...@@ -54,38 +54,43 @@ module Gitlab
def each_line def each_line
while !eof? do while !eof? do
line = read_line line = readline
yield(line) yield(line)
end end
end end
def read def read(length = nil)
out = "" out = ""
loop do
while length.nil? || out.length < length
data = get_chunk data = get_chunk
break if data.empty? break if data.empty?
out += data out += data
@tell += data.bytesize @tell += data.bytesize
end end
if length && out.length > length
extra = out.length - length
out = out[0..-extra]
end
out out
end end
def readline def readline
out = "" out = ""
data = get_chunk while !eof? do
while !data.empty? do data = get_chunk
new_line = data.index('\n') new_line = data.index("\n")
if !new_line.nil? if !new_line.nil?
out = data[0..new_line] out += data[0..new_line]
@tell += new_line+1 @tell += new_line+1
break break
elsif data.empty?
break
else else
out = data out += data
@tell += data.bytesize @tell += data.bytesize
data = get_chunk
end end
end end
...@@ -110,6 +115,9 @@ module Gitlab ...@@ -110,6 +115,9 @@ module Gitlab
private private
##
# The below methods are not implemented in IO class
#
def in_range? def in_range?
@chunk_range&.include?(tell) @chunk_range&.include?(tell)
end end
...@@ -129,7 +137,6 @@ module Gitlab ...@@ -129,7 +137,6 @@ module Gitlab
def request def request
Net::HTTP::Get.new(uri).tap do |request| Net::HTTP::Get.new(uri).tap do |request|
puts "Requesting chunk: #{chunk_start}-#{chunk_end}"
request.set_range(chunk_start, BUFFER_SIZE) request.set_range(chunk_start, BUFFER_SIZE)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment