Class: Grenache::Http

Inherits:
Base
  • Object
show all
Defined in:
lib/grenache/http.rb,
lib/grenache/http/http_client.rb

Defined Under Namespace

Classes: HttpClient

Instance Method Summary collapse

Instance Method Details

#get(t_hash, params = {}) ⇒ Object



81
82
83
84
85
86
# File 'lib/grenache/http.rb', line 81

def get(t_hash, params={})
  resp = link.send('get', t_hash, params)
  return [nil, Oj.dump(resp)]
rescue Exception => e
  return [e, nil]
end

#listen(key, port, opts = {}, &block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/grenache/http.rb', line 14

def listen(key, port,  opts={}, &block)
  start_http_service(port,&block)

  announce(key, port, opts) do |res|
    puts "#{key} announced #{res}"
  end
end

#put(payload, params = {}) ⇒ Object



74
75
76
77
78
79
# File 'lib/grenache/http.rb', line 74

def put(payload, params={})
  resp = link.send('put', payload, params)
  return [nil, resp]
rescue Exception => e
  return [e, nil]
end

#request(key, payload, params = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/grenache/http.rb', line 59

def request(key, payload, params={})
  services = lookup(key)
  if services && services.size > 0
    json = ServiceMessage.new(payload,key).to_json
    service = get_random_service services
    resp = http_client.request service, json, params
    msg = ServiceMessage.parse(resp.body)
    return [msg.err, msg.payload]
  else
    return ["NoPeerFound",nil]
  end
rescue Exception => e
  return [e, nil]
end

#start_http_service(port, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/grenache/http.rb', line 22

def start_http_service(port, &block)
  EM.defer {
    app = -> (env) {
      req = ServiceMessage.parse(env['rack.input'].read)
      fingerprint = extract_fingerprint(env['rack.peer_cert'])
      e, payload = block.call(req, fingerprint)
      err = e.kind_of?(Exception) ? e.message : e
      [200,nil, ServiceMessage.new(payload, err, req.rid).to_json]
    }
    server = Thin::Server.new config.service_host, port, {signals: false}, app

    if config.thin_threaded
      server.threaded = true
      server.threadpool_size = config.thin_threadpool_size
    end

    # Configure thin
    server.timeout = config.thin_timeout
    server.maximum_connections = config.thin_max_conns
    server.maximum_persistent_connections = config.thin_max_persistent_conns
    server.no_epoll = config.thin_no_epoll

    if tls?
      server.ssl = true
      server.ssl_options = {
        private_key_file: config.key,
        cert_chain_file: config.cert_pem,
        ecdh_curve: config.cert_ecdh_curve,
        verify_peer: true
      }

      server.backend.ca_cert = config.ca
    end
    server.start
  }
end