How to send an answer, but continue execution in Sinatra?

I am making a small application for Sinatra. It creates a pair of Redis calls, returns the data, and then makes the final Redis call to save the statistics.

While I want to complete the request earlier (based on input), I find that I can use halt :

# code code halt send_blank if is_blocked? SETTINGS, host # code code 

At the very end, I want to have something like this:

 response.body = JSON.generate(outgoing) # update user STATISTICS.hset('u:' + userID, 'data', JSON.generate({'ip' => request.ip, 'time' => Time.now.to_f.to_s})) 

Is it possible to send a response, and THEN do 5 ~ ms redis write so that the client does not have to wait? No matter where and how I put the final call to redis, it delays sending a response by a few ms - it cannot fool a sinatra.

This can easily be done in Node, but just write what I want to do and it will fire after the response is sent; as I understand it, the code here will block execution for about 5 ms before canceling the response.

I tried to use after .. make filters, and they will work fine, except that you can not pass them anything except the data in response.body - this means that you can not pass anything to the filter that will not be released! You can avoid this problem by assigning the variable outside the publishing area ("/"), however with 100+ queries per second, I hope you will see how switching data through the "global" kind can be a huge problem.

Something seems really simple, but I can’t find anything better than after .. doing filters in the documents.

Is it possible to create a stream or do something so that the redis.hset () process does not block, will this work? It seems like he cracked hardcore.

Thanks!

+6
source share
1 answer

Redis capture capture should work.

Something like that:

 response.body = JSON.generate(outgoing) fork do # update user redis = Redis.new(:host => "your_host_name", :port => your_port_number) redis.hset('u:' + userID, 'data', JSON.generate({'ip' => request.ip, 'time' => Time.now.to_f.to_s})) end 
+2
source

Source: https://habr.com/ru/post/924891/


All Articles