Sha256: 6d2cd8bde40e6ed15af65e1ad7dcd15e349b1859bbfed8ec4682b11842482ed2

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

require "thread"
require "mizuno"
Mizuno.require_jars(%w(jetty-client jetty-http jetty-io jetty-util))
require "mizuno/client_exchange"

module Mizuno
    class Client
        java_import "org.eclipse.jetty.client.HttpClient"
        java_import "org.eclipse.jetty.util.thread.QueuedThreadPool"

        @lock = Mutex.new

        def Client.request(*args, &block)
            @lock.synchronize { @root ||= new }
            @root.request(*args, &block)
        end

        def Client.stop
            @lock.synchronize do
                return unless @root
                @root.stop
                @root = nil
            end
        end

        def initialize(options = {})
            defaults = { :timeout => 60 }
            options = defaults.merge(options)
            @client = HttpClient.new
            @client.setConnectorType(HttpClient::CONNECTOR_SELECT_CHANNEL)
            @client.setMaxConnectionsPerAddress(100)
            @client.setThreadPool(QueuedThreadPool.new(50))
            @client.setTimeout(options[:timeout] * 1000)
            @client.start
            @lock = Mutex.new
            @exchanges = []
        end

        def stop(wait = true)
            wait and @lock.synchronize do
                @exchanges.each { |e| e.waitForDone }
                @exchanges.clear
            end
            @client.stop
        end

        def clear(exchange)
            return unless @lock.try_lock
            @exchanges.delete(exchange)
            @lock.unlock
        end

        def request(url, options = {}, &block)
            exchange = ClientExchange.new(self)
            @lock.synchronize { @exchanges << exchange }
            exchange.setup(url, options, &block)
            @client.send(exchange)
            return(exchange)
        end
    end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
mizuno-aspace-9.4.44 lib/mizuno/client.rb
mizuno-0.6.11 lib/mizuno/client.rb
mizuno-0.6.10 lib/mizuno/client.rb
mizuno-0.6.9 lib/mizuno/client.rb
mizuno-0.6.8 lib/mizuno/client.rb
mizuno-0.6.7 lib/mizuno/client.rb