Class: RubyPitaya::NatsConnector
- Inherits:
-
Object
- Object
- RubyPitaya::NatsConnector
- Defined in:
- lib/rubypitaya/core/nats_connector.rb
Constant Summary collapse
- REQUEST_TYPE_SYS =
0
- REQUEST_TYPE_USER =
1
- MESSAGE_TYPE_REQUEST =
0
- MESSAGE_TYPE_NOTIFY =
1
- MESSAGE_TYPE_RESPONSE =
2
- MESSAGE_TYPE_PUSH =
3
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
- #get_frontend_topic(frontend_id) ⇒ Object
- #get_user_message_topic(uid) ⇒ Object
-
#initialize(nats_address, server_uuid, server_name, n_threads, log) ⇒ NatsConnector
constructor
A new instance of NatsConnector.
- #push_to_frontend(session, message_route, payload) ⇒ Object
- #push_to_user(uid, message_route, payload) ⇒ Object
- #subscribe_topic ⇒ Object
Constructor Details
#initialize(nats_address, server_uuid, server_name, n_threads, log) ⇒ NatsConnector
Returns a new instance of NatsConnector.
18 19 20 21 22 23 24 25 26 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 18 def initialize(nats_address, server_uuid, server_name, n_threads, log) @nats_address = nats_address @server_uuid = server_uuid @server_name = server_name @n_threads = n_threads @log = log @nats = nil @subscribe = nil end |
Instance Method Details
#connect ⇒ Object
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 58 59 60 61 62 63 64 65 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 28 def connect self.disconnect @nats = NATS.connect(servers: [@nats_address]) @subscribe = @nats.subscribe(subscribe_topic) @queue = Thread::Queue.new @threads = [] @n_threads.times do |i| @threads << Thread.new do loop do data, reply = @queue.pop request = NatsRequest.decode(data).to_h response = yield request nats_response = NatsResponse.new(data: response.force_encoding('ascii-8bit')) nats_response_encoded = NatsResponse.encode(nats_response) @nats.publish(reply, nats_response_encoded) rescue Exception => error @log.error "NATS ERROR: #{error.}} \n #{error.backtrace.join("\n")}" end end end loop do = @subscribe.pending_queue.pop @subscribe.synchronize do @subscribe.pending_size -= .data.size end @queue << [.data, .reply] end end |
#disconnect ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 67 def disconnect @threads.map(&:kill) unless @threads.nil? @queue.close unless @queue.nil? @subscribe.unsubscribe unless @subscribe.nil? @nats.close unless @nats.nil? @threads = nil @queue = nil @subscribe = nil @nats = nil end |
#get_frontend_topic(frontend_id) ⇒ Object
130 131 132 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 130 def get_frontend_topic(frontend_id) "pitaya/servers/connector/#{frontend_id}" end |
#get_user_message_topic(uid) ⇒ Object
134 135 136 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 134 def (uid) "pitaya/connector/user/#{uid}/push" end |
#push_to_frontend(session, message_route, payload) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 79 def push_to_frontend(session, , payload) frontend_topic = get_frontend_topic(session.frontend_id) = NatsMessage.new( route: , data: payload, reply: @server_uuid, type: MESSAGE_TYPE_REQUEST, ) nats_session = NatsSession.new( id: session.id, uid: session.uid.to_s, data: session.data.to_json, ) nats_request = NatsRequest.new( type: REQUEST_TYPE_USER, session: nats_session, msg: , frontendID: session.frontend_id, metadata: session..to_json, ) request = NatsRequest.encode(nats_request) nats_response = @nats.request(frontend_topic, request) response = NatsResponse.decode(nats_response.data).to_h response end |
#push_to_user(uid, message_route, payload) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 112 def push_to_user(uid, , payload) user_topic = (uid) nats_push = NatsPush.new( route: , uid: uid, data: payload.to_json, ) @nats.publish(user_topic, nats_push) 0 end |
#subscribe_topic ⇒ Object
126 127 128 |
# File 'lib/rubypitaya/core/nats_connector.rb', line 126 def subscribe_topic "pitaya/servers/#{@server_name}/#{@server_uuid}" end |