Module: MicroserviceDSL
- Defined in:
- lib/microservice_dsl.rb,
lib/microservice_dsl/railtie.rb,
lib/microservice_dsl/version.rb,
lib/microservice_dsl/instrument.rb,
lib/microservice_dsl/middleware.rb
Defined Under Namespace
Modules: Instrument
Classes: Middleware, Railtie
Constant Summary
collapse
- VERSION =
'0.3.5'.freeze
Class Method Summary
collapse
Class Method Details
.cache_key(url, req) ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'lib/microservice_dsl.rb', line 90
def self.cache_key(url, req)
@@taggers ||= nil
url = url.tr(':', '|')
if @@taggers
@@taggers.map { |t| Digest::MD5.hexdigest(req.options[:headers][t]) }.join('/') + '-' + url
else
url
end
end
|
.cache_taggers=(taggers) ⇒ Object
123
124
125
126
|
# File 'lib/microservice_dsl.rb', line 123
def self.cache_taggers=(taggers)
raise unless taggers.is_a?(Array)
@@taggers = taggers
end
|
.caching? ⇒ Boolean
67
68
69
70
71
72
73
|
# File 'lib/microservice_dsl.rb', line 67
def self.caching?
if ENV['MS_CACHE_URL']
true
else
false
end
end
|
.current_hop ⇒ Object
148
149
150
151
|
# File 'lib/microservice_dsl.rb', line 148
def self.current_hop
Thread.current[:microservice_dsl_current_hop] = '1' unless Thread.current[:microservice_dsl_current_hop]
Thread.current[:microservice_dsl_current_hop]
end
|
.current_hop=(hop) ⇒ Object
144
145
146
|
# File 'lib/microservice_dsl.rb', line 144
def self.current_hop=(hop)
Thread.current[:microservice_dsl_current_hop] = hop
end
|
136
137
138
|
# File 'lib/microservice_dsl.rb', line 136
def self.
Thread.current[:microservice_dsl_default_headers] || {}
end
|
140
141
142
|
# File 'lib/microservice_dsl.rb', line 140
def self.( = {})
Thread.current[:microservice_dsl_default_headers] =
end
|
.get_cache(url, req) ⇒ Object
112
113
114
115
116
117
|
# File 'lib/microservice_dsl.rb', line 112
def self.get_cache(url, req)
return nil unless caching?
if val = redis.get([redis_hash_name, cache_key(url, req)].join(':'))
JSON.parse(val, symbolize_names: true)
end
end
|
.get_etag(url, req) ⇒ Object
100
101
102
103
104
105
|
# File 'lib/microservice_dsl.rb', line 100
def self.get_etag(url, req)
return nil unless caching?
if val = get_cache(url, req)
val[:etag]
end
end
|
.has_cache?(url, etag, req) ⇒ Boolean
107
108
109
110
|
# File 'lib/microservice_dsl.rb', line 107
def self.has_cache?(url, etag, req)
return nil unless caching?
(stored_etag = get_etag(url, req)) && (stored_etag == etag) && stored_etag
end
|
.hashable_string_for(obj) ⇒ Object
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/microservice_dsl.rb', line 166
def self.hashable_string_for(obj)
case obj
when Hash
hashable_string_for(obj.sort_by { |sub_obj| sub_obj.first.to_s })
when Array
obj.map { |sub_obj| hashable_string_for(sub_obj) }.to_s
else
obj.to_s
end
end
|
128
129
130
|
# File 'lib/microservice_dsl.rb', line 128
def self.
'X-Hop-Count'
end
|
.hop_string ⇒ Object
162
163
164
|
# File 'lib/microservice_dsl.rb', line 162
def self.hop_string
[current_hop, next_hop].join('.')
end
|
.next_hop ⇒ Object
157
158
159
160
|
# File 'lib/microservice_dsl.rb', line 157
def self.next_hop
Thread.current[:microservice_dsl_next_hop] = 0 unless Thread.current[:microservice_dsl_next_hop]
Thread.current[:microservice_dsl_next_hop] += 1
end
|
.next_hop=(hop) ⇒ Object
153
154
155
|
# File 'lib/microservice_dsl.rb', line 153
def self.next_hop=(hop)
Thread.current[:microservice_dsl_next_hop] = hop
end
|
132
133
134
|
# File 'lib/microservice_dsl.rb', line 132
def self.
"HTTP_#{.upcase.tr('-', '_')}"
end
|
.redis ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/microservice_dsl.rb', line 59
def self.redis
return nil unless caching?
splitted = ENV['MS_CACHE_URL'].split(':')
host = splitted[0]
port = splitted[1] || '6379'
@@redis ||= Redis.new(host: host, port: port)
end
|
.redis_hash_name ⇒ Object
119
120
121
|
# File 'lib/microservice_dsl.rb', line 119
def self.redis_hash_name
ENV['MS_CACHE_HASH'] || 'msdsl'
end
|
.redis_reset! ⇒ Object
55
56
57
|
# File 'lib/microservice_dsl.rb', line 55
def self.redis_reset!
@@redis = nil
end
|
.set_cache(url, etag, res) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/microservice_dsl.rb', line 75
def self.set_cache(url, etag, res)
return nil unless caching?
puts "Etag: #{etag}"
data = {
etag: etag,
response: {
body: res.body,
code: res.code,
headers: res.
}
}
puts data
redis.set([redis_hash_name, cache_key(url, res.request)].join(':'), JSON.dump(data))
end
|