Class: Tus::Server::Goliath

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/tus/server/goliath.rb

Instance Method Summary collapse

Instance Method Details

#on_body(env, data) ⇒ Object

Called on each request body chunk received from the client.



19
20
21
22
23
24
25
# File 'lib/tus/server/goliath.rb', line 19

def on_body(env, data)
  # append data to the write end of the pipe if open, otherwise do nothing
  env["tus.input-writer"].write(data) unless env["tus.input-writer"].closed?
rescue Errno::EPIPE
  # read end of the pipe has been closed, so we close the write end as well
  env["tus.input-writer"].close
end

#on_close(env) ⇒ Object

Called at the end of the request (after #response is called), but also on client disconnect (in which case #response isn’t called), so we want to do the same finalization in both methods.



30
31
32
# File 'lib/tus/server/goliath.rb', line 30

def on_close(env)
  finalize(env)
end

#on_headers(env, headers) ⇒ Object

Called as soon as request headers are parsed.



9
10
11
12
13
14
15
16
# File 'lib/tus/server/goliath.rb', line 9

def on_headers(env, headers)
  # the write end of the pipe is written in #on_body, and the read end is read by Tus::Server
  env["tus.input-reader"], env["tus.input-writer"] = IO.pipe
  # use a thread so that request is being processed in parallel
  env["tus.request-thread"] = Thread.new do
    call_tus_server env.merge("rack.input" => env["tus.input-reader"])
  end
end

#response(env) ⇒ Object

Called after all the data has been received from the client.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tus/server/goliath.rb', line 35

def response(env)
  status, headers, body = finalize(env)

  env[STREAM_START].call(status, headers)

  operation = proc { body.each { |chunk| env.stream_send(chunk) } }
  callback  = proc { env.stream_close }

  EM.defer(operation, callback) # use an outside thread pool for streaming

  nil
end