File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22require_relative "stencil/configuration"
33require_relative "stencil/constants"
44require_relative "stencil/client"
5+ require_relative "stencil/store"
56
67require "http"
7- require "concurrent"
8+ require "concurrent/timer_task"
9+ require "concurrent/mutable_struct"
810require "protobuf"
911
1012module Stencil
Original file line number Diff line number Diff line change @@ -5,14 +5,18 @@ def initialize
55 begin
66 @config = Stencil . configuration
77 validate_configuration ( @config )
8- @root = nil
8+
99 setup_http_client
10+
11+ @store = Store . new
1012 load_descriptors
13+ setup_store_update_job
1114 end
1215 end
1316
1417 def get_type ( proto_name )
15- @root . file . each do |file_desc |
18+ file_descriptor_set = @store . read ( @config . registry_url )
19+ file_descriptor_set . file . each do |file_desc |
1620 file_desc . message_type . each do |message |
1721 if proto_name == "#{ file_desc . options . java_package } .#{ message . name } "
1822 return message
@@ -22,6 +26,10 @@ def get_type(proto_name)
2226 raise InvalidProtoClass . new
2327 end
2428
29+ def close
30+ @task . shutdown
31+ end
32+
2533 private
2634
2735 def validate_configuration ( configuration )
@@ -42,7 +50,16 @@ def load_descriptors
4250 raise HTTPClientError . new ( e . message )
4351 end
4452
45- @root = Google ::Protobuf ::FileDescriptorSet . decode ( response . body )
53+ file_descriptor_set = Google ::Protobuf ::FileDescriptorSet . decode ( response . body )
54+ @store . write ( @config . registry_url , file_descriptor_set )
55+ end
56+
57+ def setup_store_update_job
58+ begin
59+ @task = Concurrent ::TimerTask . new ( execution_interval : @config . refresh_ttl_in_secs ) do
60+ load_descriptors
61+ end
62+ end
4663 end
4764 end
4865end
Original file line number Diff line number Diff line change 1+ module Stencil
2+ class Store
3+ def initialize
4+ @lock = Concurrent ::ReadWriteLock . new
5+ @data = Hash . new
6+ end
7+
8+ def write ( key , value )
9+ @lock . with_write_lock do
10+ @data . store ( key , value )
11+ end
12+ end
13+
14+ def read ( key )
15+ @data [ key ]
16+ end
17+ end
18+ end
Original file line number Diff line number Diff line change 1+ module Stencil
2+ RSpec . describe Store do
3+ context "#read" do
4+ let ( :sample_key ) { "sample_key" }
5+ let ( :sample_value ) { 123 }
6+
7+ it "should be able to handle concurrent reads of data" do
8+ store = Store . new
9+ store . write ( sample_key , sample_value )
10+
11+ 5 . times do
12+ Thread . start do
13+ expect ( store . read ( sample_key ) ) . to eq ( sample_value )
14+ end
15+ end
16+ end
17+ end
18+
19+ context "#write" do
20+ let ( :sample_key ) { "sample_key" }
21+ let ( :sample_value ) { 100 }
22+
23+ it "should be able to handle concurrent writes of data by locking data" do
24+ store = Store . new
25+ store . write ( sample_key , sample_value )
26+ expect ( store . read ( sample_key ) ) . to eq ( sample_value )
27+ end
28+ end
29+ end
30+ end
You can’t perform that action at this time.
0 commit comments