File tree Expand file tree Collapse file tree 3 files changed +63
-16
lines changed Expand file tree Collapse file tree 3 files changed +63
-16
lines changed Original file line number Diff line number Diff line change 6
6
class BoardLinuxfr < Goliath ::API
7
7
plugin RedisPlugin
8
8
9
- def initialize
10
- Redis . new
11
- end
12
-
13
9
def response ( env )
14
- env . logger . debug "New client"
15
- env [ 'subscription' ] = status [ :channel ] . subscribe do |msg |
16
- env . stream_send ( "data: #{ msg } \n \n " )
17
- end
10
+ env . logger . debug "New client: #{ env [ 'PATH_INFO' ] } "
11
+ send_msg = -> ( msg ) {
12
+ env . logger . debug " -> #{ msg } "
13
+ env . stream_send ( "data: #{ msg } \n id: #{ msg [ :id ] } \n \n " )
14
+ }
15
+ event_id = env [ 'HTTP_LAST_EVENT_ID' ]
16
+ chan_name = env [ 'PATH_INFO' ] . delete ( '/b/' )
17
+ env [ 'cache' ] = status [ :cache ] [ chan_name ]
18
+ env [ 'chan' ] = status [ :channels ] [ chan_name ]
19
+ env [ 'sid' ] = env [ 'chan' ] . subscribe &send_msg
20
+ env [ 'timer' ] = EM . add_periodic_timer ( 15 ) { env . stream_send "::\n \n " }
21
+ EM . next_tick { env [ 'cache' ] . from ( event_id , &send_msg ) } if event_id
18
22
streaming_response ( 200 , { 'Content-Type' => 'text/event-stream' } )
19
23
end
20
24
21
- def on_close
22
- return unless env [ 'subscription ' ]
23
- status [ :channel ] . unsubscribe ( env [ 'subscription' ] )
25
+ def on_close ( env )
26
+ env [ 'chan' ] . unsubscribe env [ 'sid' ] if env [ 'sid ' ]
27
+ env [ 'timer' ] . cancel if env [ 'timer' ]
24
28
end
25
29
end
Original file line number Diff line number Diff line change
1
+ require "goliath/api"
2
+
3
+
4
+ class BoardLinuxfr < Goliath ::API
5
+ class Cache
6
+ CAPACITY = 10
7
+
8
+ class Entry < Array
9
+ def push ( item )
10
+ super item
11
+ shift if size > CAPACITY
12
+ end
13
+
14
+ def from ( id , &blk )
15
+ found = false
16
+ each do |item |
17
+ blk . call item if found
18
+ found ||= item [ :id ] == id
19
+ end
20
+ end
21
+ end
22
+
23
+ def initialize
24
+ @keys = Entry . new
25
+ @vals = Entry . new
26
+ end
27
+
28
+ def []( key )
29
+ n = @keys . index ( key )
30
+ unless n
31
+ @keys . push key
32
+ @vals . push Entry . new
33
+ n = @keys . length - 1
34
+ end
35
+ @vals [ n ]
36
+ end
37
+ end
38
+ end
Original file line number Diff line number Diff line change 2
2
require "em-synchrony"
3
3
require "redis"
4
4
require "redis/connection/synchrony"
5
+ require "board-linuxfr/cache"
5
6
6
7
7
8
class BoardLinuxfr < Goliath ::API
8
9
class RedisPlugin
10
+
9
11
def initialize ( port , config , status , logger )
10
12
logger . info "Initializing the Redis plugin"
11
- @logger = logger
12
- @channel = status [ :channel ] = EM ::Channel . new
13
- @redis = Redis . new
13
+ @logger = logger
14
+ @chans = status [ :channels ] = Hash . new { |h , k | h [ k ] = EM ::Channel . new }
15
+ @cache = status [ :cache ] = Cache . new
16
+ @redis = Redis . new
14
17
end
15
18
16
19
def run
@@ -22,8 +25,10 @@ def run
22
25
23
26
on . pmessage do |pattern , chan , msg |
24
27
_ , chan , id , kind = *chan . split ( '/' )
25
- @channel . push ( chan : chan , id : id , kind : kind , msg : msg )
26
- @logger . info "New message: #{ chan } #{ id } #{ kind } #{ msg } "
28
+ @logger . info "New message: [#{ chan } ] #{ id } . #{ kind } > #{ msg } "
29
+ [ @chans , @cache ] . each do |storage |
30
+ storage [ chan ] . push ( id : id , kind : kind , msg : msg )
31
+ end
27
32
end
28
33
29
34
on . punsubscribe do |pattern , total |
You can’t perform that action at this time.
0 commit comments