prefab-cloud-ruby 0.0.7 → 0.0.8
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 +4 -4
- data/VERSION +1 -1
- data/lib/prefab/client.rb +4 -14
- data/lib/prefab/config_client.rb +39 -15
- data/lib/prefab/config_resolver.rb +19 -4
- data/lib/prefab/ratelimit_client.rb +14 -4
- data/lib/prefab/retry.rb +26 -0
- data/lib/prefab-cloud-ruby.rb +1 -0
- data/prefab-cloud-ruby.gemspec +4 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b7a0a43eadc51e6ebfbd7573865ba48dd139bd1
|
4
|
+
data.tar.gz: 30e99adf8c06969e29034fbff3a711838b471e0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29262f1a91dd5c7322152e27fe4352c4a6974f17d891f29bdaed79aedda4c258c13ba4576337936d6b5fcf7b22a3a0eb793e1a4ce9b1b1185dac95e9f88bb352
|
7
|
+
data.tar.gz: cf12cc1441b1a8786f40cf309128129570fe061b9eb41a1c59ed8cc0159a54376cd3711809e4e76f52edefecccb5409b59c3d37ce7997bd38d1ee5da7801c1ad
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.8
|
data/lib/prefab/client.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Prefab
|
2
2
|
class Client
|
3
|
-
attr_reader :account_id, :shared_cache, :stats, :namespace, :logger
|
3
|
+
attr_reader :account_id, :shared_cache, :stats, :namespace, :logger, :creds, :channel, :interceptor
|
4
4
|
|
5
5
|
def initialize(api_key:,
|
6
6
|
logger: nil,
|
@@ -26,22 +26,12 @@ module Prefab
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
def config_client(timeout:
|
30
|
-
@config_client ||= Prefab::ConfigClient.new(
|
31
|
-
@creds,
|
32
|
-
channel_override: @channel,
|
33
|
-
timeout: timeout,
|
34
|
-
interceptors: [@interceptor]),
|
35
|
-
self)
|
29
|
+
def config_client(timeout: 5.0)
|
30
|
+
@config_client ||= Prefab::ConfigClient.new(self, timeout)
|
36
31
|
end
|
37
32
|
|
38
33
|
def ratelimit_client(timeout: 5.0)
|
39
|
-
@ratelimit_client ||= Prefab::RateLimitClient.new(
|
40
|
-
@creds,
|
41
|
-
channel_override: @channel,
|
42
|
-
timeout: timeout,
|
43
|
-
interceptors: [@interceptor]),
|
44
|
-
self)
|
34
|
+
@ratelimit_client ||= Prefab::RateLimitClient.new(self, timeout)
|
45
35
|
end
|
46
36
|
|
47
37
|
private
|
data/lib/prefab/config_client.rb
CHANGED
@@ -1,37 +1,61 @@
|
|
1
1
|
module Prefab
|
2
2
|
class ConfigClient
|
3
|
-
|
3
|
+
RECONNECT_WAIT = 5
|
4
|
+
|
5
|
+
def initialize(client, timeout)
|
4
6
|
@client = client
|
5
|
-
@
|
7
|
+
@timeout = timeout
|
6
8
|
@config_resolver = EzConfig::ConfigResolver.new(client)
|
7
|
-
boot_resolver
|
9
|
+
boot_resolver
|
8
10
|
end
|
9
11
|
|
10
12
|
def get(prop)
|
11
13
|
@config_resolver.get(prop)
|
12
14
|
end
|
13
15
|
|
14
|
-
def set(
|
15
|
-
|
16
|
+
def set(config_delta)
|
17
|
+
Retry.it method(:stub_with_timout), :upsert, config_delta, @timeout
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
@config_resolver.to_s
|
16
22
|
end
|
17
23
|
|
18
24
|
private
|
19
25
|
|
20
|
-
def
|
21
|
-
|
26
|
+
def stub
|
27
|
+
Prefab::ConfigService::Stub.new(nil,
|
28
|
+
nil,
|
29
|
+
channel_override: @client.channel,
|
30
|
+
interceptors: [@client.interceptor])
|
31
|
+
end
|
32
|
+
|
33
|
+
def stub_with_timout
|
34
|
+
Prefab::ConfigService::Stub.new(nil,
|
35
|
+
nil,
|
36
|
+
channel_override: @client.channel,
|
37
|
+
timeout: @timeout,
|
38
|
+
interceptors: [@client.interceptor])
|
39
|
+
end
|
40
|
+
|
41
|
+
def boot_resolver
|
42
|
+
config_req = Prefab::ConfigServicePointer.new(account_id: @client.account_id,
|
22
43
|
start_at_id: 0)
|
23
44
|
|
24
45
|
Thread.new do
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
46
|
+
while true do
|
47
|
+
begin
|
48
|
+
resp = stub.get_config(config_req)
|
49
|
+
resp.each do |r|
|
50
|
+
r.deltas.each do |delta|
|
51
|
+
@config_resolver.set(delta)
|
52
|
+
end
|
53
|
+
@config_resolver.update
|
30
54
|
end
|
31
|
-
|
55
|
+
rescue => e
|
56
|
+
sleep(RECONNECT_WAIT)
|
57
|
+
@client.logger.info("config client encountered #{e.message} pausing #{RECONNECT_WAIT}")
|
32
58
|
end
|
33
|
-
rescue => e
|
34
|
-
@client.logger.warn(e)
|
35
59
|
end
|
36
60
|
end
|
37
61
|
end
|
@@ -6,19 +6,34 @@ module EzConfig
|
|
6
6
|
@local_store = {}
|
7
7
|
@namespace = client.namespace
|
8
8
|
@config_loader = EzConfig::ConfigLoader.new(client.logger)
|
9
|
+
@logger = client.logger
|
9
10
|
make_local
|
10
11
|
end
|
11
12
|
|
13
|
+
def to_s
|
14
|
+
str = ""
|
15
|
+
@lock.with_read_lock do
|
16
|
+
@local_store.each do |k, v|
|
17
|
+
value = v[:value]
|
18
|
+
case value.type
|
19
|
+
when :string then
|
20
|
+
str << "#{k} #{value.string}"
|
21
|
+
when :int then
|
22
|
+
str << "#{k} #{value.int}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
str
|
27
|
+
end
|
28
|
+
|
12
29
|
def get(property)
|
13
30
|
value = @lock.with_read_lock do
|
14
31
|
@local_store[property][:value]
|
15
32
|
end
|
16
33
|
case value.type
|
17
34
|
when :string then
|
18
|
-
puts "SSString"
|
19
35
|
value.string
|
20
36
|
when :int then
|
21
|
-
puts "INT"
|
22
37
|
value.int
|
23
38
|
end
|
24
39
|
end
|
@@ -53,12 +68,12 @@ module EzConfig
|
|
53
68
|
store[property] = { namespace: namespace, value: value }
|
54
69
|
end
|
55
70
|
end
|
56
|
-
|
57
|
-
puts "prop #{property} namespace #{namespace} value #{value}"
|
58
71
|
end
|
59
72
|
@lock.with_write_lock do
|
60
73
|
@local_store = store
|
61
74
|
end
|
75
|
+
|
76
|
+
@logger.info "Updated to #{to_s}"
|
62
77
|
end
|
63
78
|
end
|
64
79
|
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module Prefab
|
2
2
|
class RateLimitClient
|
3
|
-
|
4
|
-
|
3
|
+
|
4
|
+
def initialize(client, timeout)
|
5
|
+
@timeout = timeout
|
5
6
|
@client = client
|
6
7
|
end
|
7
8
|
|
@@ -25,10 +26,10 @@ module Prefab
|
|
25
26
|
allow_partial_response: allow_partial_response
|
26
27
|
)
|
27
28
|
|
28
|
-
result =
|
29
|
+
result = Retry.it(method(:stub), :limit_check, req, @timeout)
|
29
30
|
|
30
31
|
reset = result.limit_reset_at
|
31
|
-
@client.shared_cache.write(expiry_cache_key, reset) unless reset < 1
|
32
|
+
@client.shared_cache.write(expiry_cache_key, reset) unless reset < 1 # protobuf default int to 0
|
32
33
|
|
33
34
|
@client.stats.increment("prefab.ratelimit.limitcheck", tags: ["policy_group:#{result.policy_group}", "pass:#{result.passed}"])
|
34
35
|
|
@@ -40,8 +41,17 @@ module Prefab
|
|
40
41
|
|
41
42
|
private
|
42
43
|
|
44
|
+
def stub
|
45
|
+
Prefab::RateLimitService::Stub.new(nil,
|
46
|
+
nil,
|
47
|
+
channel_override: @client.channel,
|
48
|
+
timeout: @timeout,
|
49
|
+
interceptors: [@client.interceptor])
|
50
|
+
end
|
51
|
+
|
43
52
|
def handle_error(e, on_error, groups)
|
44
53
|
@client.stats.increment("prefab.ratelimit.error", tags: ["type:limit"])
|
54
|
+
|
45
55
|
message = "ratelimit for #{groups} error: #{e.message}"
|
46
56
|
case on_error
|
47
57
|
when :log_and_pass
|
data/lib/prefab/retry.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class Retry
|
2
|
+
DEFAULT_TIMEOUT = 10
|
3
|
+
MAX_SLEEP_SEC = 10
|
4
|
+
BASE_SLEEP_SEC = 0.5
|
5
|
+
|
6
|
+
# GRPC Generally handles timeouts for us
|
7
|
+
# but if the connection is broken we want to retry up until the timeout
|
8
|
+
def self.it(stub_factory, rpc, req, timeout)
|
9
|
+
attempts = 0
|
10
|
+
start_time = Time.now
|
11
|
+
|
12
|
+
begin
|
13
|
+
attempts += 1
|
14
|
+
|
15
|
+
return stub_factory.call.send(rpc, req)
|
16
|
+
rescue => exception
|
17
|
+
raise exception if Time.now - start_time > timeout
|
18
|
+
|
19
|
+
sleep_seconds = [BASE_SLEEP_SEC * (2 ** (attempts - 1)), MAX_SLEEP_SEC].min
|
20
|
+
sleep_seconds = sleep_seconds * (0.5 * (1 + rand()))
|
21
|
+
sleep_seconds = [BASE_SLEEP_SEC, sleep_seconds].max
|
22
|
+
sleep sleep_seconds
|
23
|
+
retry
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/prefab-cloud-ruby.rb
CHANGED
data/prefab-cloud-ruby.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: prefab-cloud-ruby 0.0.
|
5
|
+
# stub: prefab-cloud-ruby 0.0.8 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "prefab-cloud-ruby".freeze
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.8"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["Jeff Dwyer".freeze]
|
14
|
-
s.date = "2017-12-
|
14
|
+
s.date = "2017-12-22"
|
15
15
|
s.description = "RateLimits & Config as a service".freeze
|
16
16
|
s.email = "jdwyer@prefab.cloud".freeze
|
17
17
|
s.extra_rdoc_files = [
|
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
|
|
34
34
|
"lib/prefab/noop_cache.rb",
|
35
35
|
"lib/prefab/noop_stats.rb",
|
36
36
|
"lib/prefab/ratelimit_client.rb",
|
37
|
+
"lib/prefab/retry.rb",
|
37
38
|
"lib/prefab/store.rb",
|
38
39
|
"lib/prefab_pb.rb",
|
39
40
|
"lib/prefab_services_pb.rb",
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prefab-cloud-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Dwyer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grpc
|
@@ -151,6 +151,7 @@ files:
|
|
151
151
|
- lib/prefab/noop_cache.rb
|
152
152
|
- lib/prefab/noop_stats.rb
|
153
153
|
- lib/prefab/ratelimit_client.rb
|
154
|
+
- lib/prefab/retry.rb
|
154
155
|
- lib/prefab/store.rb
|
155
156
|
- lib/prefab_pb.rb
|
156
157
|
- lib/prefab_services_pb.rb
|