turbo-replay 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d51efac005c1642592ebccb9753657e2be702141014bc0ebae2e7d4757aee362
4
+ data.tar.gz: ae00e5f49f8f106ef525e5efa7f3f6abe01b2676553b64f2c8bbfe9f943e23f1
5
+ SHA512:
6
+ metadata.gz: 6ff54759f20fdd6cbb6c6a23f926297d9f12eb5d86ae96617b212a98eb0c53d421d2c676020cf89778ef56795ef7cc8a2a213f8f12b710e5d198948995508e8a
7
+ data.tar.gz: 1cd690000760467b1ded8d168d26621d69fd08c255ca1d688c5e83d43c8d0df12038cb0e41a94b4af45a47f84cd2663eced22f3edc1c6ca0129ba18c1e5a3343
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Luiz Paulo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Turbo::Replay
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "turbo-replay"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install turbo-replay
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+ require "bundler/gem_tasks"
3
+ require "standard/rake"
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :turbo_replay do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,47 @@
1
+ module Turbo::Replay
2
+ module Message
3
+ extend self
4
+
5
+ def get_current_sequence_number(broadcasting:)
6
+ Turbo::Replay.configuration.repo.get_current_sequence_number(broadcasting: broadcasting)
7
+ end
8
+
9
+ def get_after_sequence_number(broadcasting:, sequence_number:)
10
+ messages =
11
+ Turbo::Replay.configuration.repo
12
+ .get_all_messages(broadcasting: broadcasting)
13
+ .sort_by(&BySequenceNumber)
14
+
15
+ return :unrecoverable if IsUnrecoverable.call(sequence_number, messages)
16
+
17
+ messages.filter(&AfterSequenceNumber[sequence_number])
18
+ end
19
+
20
+ def insert(broadcasting:, content:)
21
+ Turbo::Replay.configuration.repo.insert_message(
22
+ broadcasting: broadcasting,
23
+ content: content,
24
+ retention: Turbo::Replay.configuration.retention
25
+ )
26
+ end
27
+
28
+ private
29
+
30
+ BySequenceNumber =
31
+ ->(content_with_sequence_number) {
32
+ content_with_sequence_number[:sequence_number]
33
+ }
34
+
35
+ IsUnrecoverable =
36
+ ->(sequence_number, messages) {
37
+ return false if messages.empty?
38
+
39
+ sequence_number < messages.first[:sequence_number] - 1
40
+ }
41
+
42
+ AfterSequenceNumber =
43
+ ->(sequence_number, content_with_sequence_number) {
44
+ content_with_sequence_number[:sequence_number] > sequence_number
45
+ }.curry
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ # Is this needed?
2
+ module Turbo
3
+ module Replay
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module Turbo::Replay
2
+ module Repo
3
+ class Base
4
+ def get_current_sequence_number(broadcasting:)
5
+ raise NotImplementedError
6
+ end
7
+
8
+ def get_all_messages(broadcasting:)
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def insert_message(broadcasting:, content:, retention:)
13
+ raise NotImplementedError
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,62 @@
1
+ module Turbo::Replay
2
+ module Repo
3
+ class Memory < Base
4
+ def initialize
5
+ @mutex = Mutex.new
6
+ @counters = {}
7
+ @messages = {}
8
+ @ttl = {}
9
+ end
10
+
11
+ def get_current_sequence_number(broadcasting:)
12
+ synchronize(broadcasting) do
13
+ @counters.fetch(broadcasting, 0)
14
+ end
15
+ end
16
+
17
+ def get_all_messages(broadcasting:)
18
+ synchronize(broadcasting) do
19
+ @messages.fetch(broadcasting, [])
20
+ end
21
+ end
22
+
23
+ def insert_message(broadcasting:, content:, retention:)
24
+ synchronize(broadcasting) do
25
+ @ttl[broadcasting] =
26
+ Time.current + retention.ttl
27
+
28
+ next_sequence_number =
29
+ (@counters[broadcasting] = @counters.fetch(broadcasting, 0) + 1)
30
+
31
+ content_with_sequence_number =
32
+ {sequence_number: next_sequence_number, content: content}
33
+
34
+ (@messages[broadcasting] ||= []).tap do |messages|
35
+ messages << content_with_sequence_number
36
+ messages.shift if messages.length > retention.size
37
+ end
38
+
39
+ content_with_sequence_number
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def synchronize(broadcasting)
46
+ @mutex.synchronize do
47
+ delete_cached_data_if_expired(broadcasting)
48
+
49
+ yield
50
+ end
51
+ end
52
+
53
+ def delete_cached_data_if_expired(broadcasting)
54
+ return if @ttl[broadcasting].nil? || @ttl[broadcasting].after?(Time.current)
55
+
56
+ @ttl.delete(broadcasting)
57
+ @counters.delete(broadcasting)
58
+ @messages.delete(broadcasting)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,69 @@
1
+ module Turbo::Replay
2
+ module Repo
3
+ class Redis < Base
4
+ attr_reader :client
5
+
6
+ def initialize(client:)
7
+ @client = client
8
+ end
9
+
10
+ def get_current_sequence_number(broadcasting:)
11
+ counter_key =
12
+ FormatCounterKey.call(broadcasting)
13
+
14
+ @client.get(counter_key)&.to_i || 0
15
+ end
16
+
17
+ def get_all_messages(broadcasting:)
18
+ messages_key =
19
+ FormatMessagesKey.call(broadcasting)
20
+
21
+ @client.lrange(messages_key, 0, -1)
22
+ .map(&SafeParseJson)
23
+ .compact
24
+ .reverse
25
+ end
26
+
27
+ def insert_message(broadcasting:, content:, retention:)
28
+ counter_key =
29
+ FormatCounterKey.call(broadcasting)
30
+
31
+ messages_key =
32
+ FormatMessagesKey.call(broadcasting)
33
+
34
+ next_sequence_number =
35
+ @client.incr(counter_key)
36
+
37
+ content_with_sequence_number =
38
+ {sequence_number: next_sequence_number, content: content}
39
+
40
+ @client.lpush(messages_key, content_with_sequence_number.to_json)
41
+ @client.ltrim(messages_key, 0, retention.size - 1)
42
+
43
+ @client.expire(counter_key, retention.ttl)
44
+ @client.expire(messages_key, retention.ttl)
45
+
46
+ content_with_sequence_number
47
+ end
48
+
49
+ private
50
+
51
+ PREFIX = "replay"
52
+
53
+ FormatCounterKey =
54
+ ->(broadcasting) { "#{PREFIX}:#{broadcasting}:counter" }
55
+
56
+ FormatMessagesKey =
57
+ ->(broadcasting) { "#{PREFIX}:#{broadcasting}:messages" }
58
+
59
+ SafeParseJson =
60
+ ->(encoded) do
61
+ begin
62
+ JSON.parse(encoded).symbolize_keys
63
+ rescue
64
+ nil
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Turbo::Replay
2
+ Retention = Struct.new(:ttl, :size, keyword_init: true)
3
+ end
@@ -0,0 +1,5 @@
1
+ module Turbo
2
+ module Replay
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ require "turbo/replay/version"
2
+ require "turbo/replay/railtie"
3
+
4
+ require_relative "./replay/retention"
5
+ require_relative "./replay/message"
6
+ require_relative "./replay/repo/base"
7
+ require_relative "./replay/repo/memory"
8
+ require_relative "./replay/repo/redis"
9
+
10
+ module Turbo
11
+ module Replay
12
+ include ActiveSupport::Autoload
13
+
14
+ class Configuration
15
+ attr_accessor :repo, :retention
16
+ end
17
+
18
+ mattr_accessor :configuration
19
+ self.configuration = Configuration.new
20
+
21
+ def self.configure = yield(configuration)
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turbo-replay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luiz Vasconcellos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: standard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Turbo-Replay caches events sent to channels and re-sends them in order
70
+ when clients have flaky network.
71
+ email:
72
+ - luizpvasc@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - lib/tasks/turbo/replay_tasks.rake
81
+ - lib/turbo/replay.rb
82
+ - lib/turbo/replay/message.rb
83
+ - lib/turbo/replay/railtie.rb
84
+ - lib/turbo/replay/repo/base.rb
85
+ - lib/turbo/replay/repo/memory.rb
86
+ - lib/turbo/replay/repo/redis.rb
87
+ - lib/turbo/replay/retention.rb
88
+ - lib/turbo/replay/version.rb
89
+ homepage: https://github.com/luizpvas/turbo-replay
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ homepage_uri: https://github.com/luizpvas/turbo-replay
94
+ source_code_uri: https://github.com/luizpvas/turbo-replay
95
+ changelog_uri: https://github.com/luizpvas/turbo-replay
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.3.7
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Never miss a single websocket event ever again.
115
+ test_files: []