This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
crystal-http-test/src/file-uploader-crystal.cr
Fijxu 3529e01f19
Some checks failed
File-uploader-crystal CI / build (push) Failing after 14s
a
2025-04-07 00:52:47 -04:00

94 lines
2.1 KiB
Crystal

require "http"
require "kemal"
require "db"
require "./routing"
Kemal.config.port = 9999
Kemal.config.host_binding = "0.0.0.0"
Kemal.config.shutdown_message = false
YTIMG_POOLS = {} of String => YoutubeConnectionPool
struct YoutubeConnectionPool
property! url : URI
property! capacity : Int32
property! timeout : Float64
property pool : DB::Pool(HTTP::Client)
def initialize(url : URI, @capacity = 5, @timeout = 5.0)
@url = url
@pool = build_pool()
end
def size()
return @pool.size
end
def client(&)
conn = pool.checkout
begin
response = yield conn
rescue ex
puts "CLOSING CON: #{ex.message} + #{ex.inspect}"
conn.close
conn = make_client(url, force_resolve: true)
response = yield conn
ensure
pool.release(conn)
end
response
end
def get_pool()
return @pool
end
private def build_pool
options = DB::Pool::Options.new(
initial_pool_size: 0,
max_pool_size: capacity,
max_idle_pool_size: capacity,
checkout_timeout: timeout
)
DB::Pool(HTTP::Client).new(options) do
next make_client(url, force_resolve: true)
end
end
end
def make_client(url : URI, region = nil, force_resolve : Bool = false, force_youtube_headers : Bool = false, use_http_proxy : Bool = true)
client = HTTP::Client.new(url)
client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds
return client
end
# Fetches a HTTP pool for the specified subdomain of ytimg.com
#
# Creates a new one when the specified pool for the subdomain does not exist
def get_ytimg_pool(subdomain)
if pool = YTIMG_POOLS[subdomain]?
return pool
else
puts "ytimg_pool: Creating a new HTTP pool for \"https://#{subdomain}.ytimg.com\""
pool = YoutubeConnectionPool.new(URI.parse("https://#{subdomain}.ytimg.com"), capacity: ENV.fetch("POOL_SIZE", 100).to_i)
YTIMG_POOLS[subdomain] = pool
return pool
end
end
Routing.register_all
{% if flag?(:release) || flag?(:production) %}
Kemal.config.env = "production" if !ENV.has_key?("KEMAL_ENV")
{% end %}
Kemal.run