configure_semian 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d47a27158e20da560941a9b5dd4757d8dc2b03c3
4
+ data.tar.gz: 07eb8ca0eb264e72938a9f00f663feaaba823547
5
+ SHA512:
6
+ metadata.gz: 004c300a99db4a8050948bec7a597d2ed3a180489af2407854d46ce9c0e563d17559016902f9d0787e512b32910b798f150881c526aa31c5866c4490d21d9776
7
+ data.tar.gz: b356ea59cf6eef8b66599bf8565f41e7a2025aff2f6d1614ccdac4681cc6bf74ee1076f4d5601b247a21ea8a5c34f9694df732d93dbac9f2d9ebf50bca21ef46
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /lib/configure_semian/.byebug_history
10
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+ ruby "~> 2.4.1"
3
+
4
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
+
6
+ # Specify your gem's dependencies in configure_semian.gemspec
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 supantha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # configure_semian
2
+ A layer to help start using semian in an easy and configurable way
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'configure_semian'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install configure_semian
19
+
20
+ ## Usage
21
+
22
+ Provides functionality to define parameters for Semian. Additionality, provides read timeout to all HTTP calls.
23
+ Semian parameters can be customized for each host the http call is made to, while the timeouts can be configured at path level also.
24
+
25
+ Configuring Gem:
26
+
27
+ Define a module in config/initializers and there define the configurations for the gem as follows:
28
+
29
+ ConfigureSemian::SemianConfiguration.configure_client{ |ob|
30
+ ob.app_server = (true if bulkheading to be enabled by default false otherwise)
31
+ ob.service_configs = (hash defining the default alongwith various host and path based configurations for the service)
32
+ ob.free_hosts = (array of hosts to be free from semian)
33
+ ob.track_exceptions = (array of exception classes to be tracked by semian)
34
+ ob.service_name = (name of the service using this gem, this name is prepended to the name of the host to which the http call is being made to create semian resource name)
35
+ }
36
+
37
+ Service Configs:
38
+ The complete set of parameters that can be defined in service configs alongwith their default values are
39
+
40
+ quota: 0.75,
41
+ success_threshold: 2,
42
+ error_threshold: 3,
43
+ error_timeout: 10,
44
+ timeout: 10,
45
+ bulkhead: true if app_server is true, false otherwise
46
+
47
+ The service configs hash provided during configuration can edit as many of these parameters as required. For the parameters for which no definition is provided during configuration takes the gem default value as defined above.
48
+ The structure of the configs hash should be as below:
49
+
50
+ {
51
+ default:{gem parameters to be overridden for this whole service provided as parameters-values hash},
52
+ hostname1: {default: {the resulting service parameters to be overridden for this host provided as parameters-values hash},
53
+ path1: hash with timeout as key and its value to override its hosts default read timeout value for this path}
54
+ }
55
+ Example Definition:
56
+ Suppose during configuration service provides app_server as true and service configs as:
57
+
58
+ {
59
+ default: {quota: 0.5, timeout: 16},
60
+ 'host.example.com': {
61
+ default: {timeout: 10, bulkhead: false},
62
+ '/example/index': {timeout: 20}
63
+ }
64
+ }
65
+
66
+ Then, in this example parameters values in default for the service would be
67
+
68
+ {quota: 0.5, success_threshold: 2,error_threshold: 3,error_timeout: 10,timeout: 16, bulkhead: true},
69
+ while for 'host.example.com' would be
70
+
71
+ {quota: 0.5, success_threshold: 2,error_threshold: 3,error_timeout: 10,timeout: 10, bulkhead: false}
72
+ and for the path '/example/index', the read timeout will be 20s.
73
+
74
+ Note: Only read timeout can be configures at path level, all the other parameters are same as that of its host's.
75
+ A suggested way to define the service_configs hash would be to define a yml file, and load that file while configuring.
76
+
77
+ ## Development
78
+
79
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
80
+
81
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/configure_semian.
86
+
87
+ ## License
88
+
89
+ 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,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "configure_semian"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "configure_semian/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "configure_semian"
8
+ spec.version = ConfigureSemian::VERSION
9
+ spec.authors = ["supantha"]
10
+ spec.email = ["supantha.samanta@gmail.com"]
11
+ spec.files = Dir["{lib}/**/*"] + ["README.md"]
12
+ spec.summary = "Configure Semian"
13
+ spec.description = "Semian Connector"
14
+ spec.homepage = "https://github.com/elarahq/configure_semian"
15
+ spec.license = "MIT"
16
+ spec.add_dependency "rails", ">=4.0.2"
17
+ spec.add_dependency "semian"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ end
@@ -0,0 +1,4 @@
1
+ module ConfigureSemian
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,25 @@
1
+ require 'net/http'
2
+ module Net
3
+ class HTTP
4
+
5
+ alias_method :old_request, :request
6
+ alias_method :old_post, :post
7
+
8
+ def request(req, body = nil, &block)
9
+ timeout = get_request_timeout_value(self.address, req.path)
10
+ self.read_timeout = timeout
11
+ old_request(req, body, &block)
12
+ end
13
+
14
+ def get_request_timeout_value(host, path)
15
+ if !ConfigureSemian::SemianConfiguration.service_configs[host.intern].nil?
16
+ specs = ConfigureSemian::SemianConfiguration.service_configs[host.intern][path.intern]
17
+ specs ||= ConfigureSemian::SemianConfiguration.service_configs[host.intern][:default]
18
+ end
19
+ specs ||= ConfigureSemian::SemianConfiguration.service_configs[:default]
20
+ timeout = specs[:timeout] || specs['timeout']
21
+ return timeout
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,103 @@
1
+ require 'semian'
2
+ require 'semian/net_http'
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ module ConfigureSemian
5
+ class SemianConfiguration
6
+ attr_accessor :host, :port, :path, :data
7
+
8
+ # Singleton Object associated with Semian Configuration that has various specifications for Semian initialization
9
+ class SemianParameters
10
+ attr_accessor :app_server, :service_configs, :free_hosts, :track_exceptions, :service_name
11
+
12
+ SEMIAN_PARAMETERS = {
13
+ semian_default: {
14
+ quota: 0.5,
15
+ success_threshold: 2,
16
+ error_threshold: 3,
17
+ error_timeout: 10,
18
+ timeout: 10,
19
+ bulkhead: false
20
+ }
21
+ }
22
+
23
+ def initialize
24
+ @app_server = false
25
+ @service_configs = SEMIAN_PARAMETERS.with_indifferent_access
26
+ @free_hosts = []
27
+ @track_exceptions = []
28
+ @service_name = nil
29
+ end
30
+
31
+ # Passed true only for app server so that bulkheading is disabled in worker servers
32
+ def app_server=value
33
+ @app_server = value
34
+ self.service_configs[:semian_default][:bulkhead] = (value || false)
35
+ end
36
+
37
+ # semian options alongwith the ones defined by the service
38
+ def service_configs=value
39
+ value.with_indifferent_access
40
+ self.service_configs.merge!(value)
41
+ end
42
+
43
+ # exceptions to be tracked defined by the service
44
+ def track_exceptions=value
45
+ self.track_exceptions |= value
46
+ end
47
+
48
+ # initial computations
49
+ def generate_specifications
50
+ # Define exceptions to be tracked by Semian
51
+ Semian::NetHTTP.exceptions |= self.track_exceptions
52
+ # Create the complete host,path driven semian options
53
+ semian_default = self.service_configs.delete(:semian_default)
54
+ service_default = semian_default.merge(self.service_configs.delete(:default) || {})
55
+ service_default.delete(:quota) if !service_default[:tickets].nil?
56
+ self.service_configs.each do |host, specs|
57
+ host_default = service_default.merge(specs.delete(:default) || {})
58
+ host_default.delete(:quota) if !host_default[:tickets].nil?
59
+ specs.each do |path, path_specs|
60
+ self.service_configs[host][path] = host_default.merge(path_specs || {})
61
+ end
62
+ self.service_configs[host][:default] = host_default
63
+ end
64
+ self.service_configs.merge!({semian_default: semian_default, default: service_default})
65
+ end
66
+
67
+ end
68
+
69
+ @@semian_parameters = SemianParameters.new
70
+
71
+ ::Semian::NetHTTP.semian_configuration = proc do |host, port|
72
+ if !self.free_hosts.include?(host)
73
+ semian_options = get_semian_parameters(host, port)
74
+ semian_options
75
+ else
76
+ nil
77
+ end
78
+ end
79
+
80
+ def self.get_semian_parameters(host, port)
81
+ resource_name = "#{self.service_name}_#{host}"
82
+ parameters = self.service_configs[host.intern].nil? ? self.service_configs[:default] : self.service_configs[host.intern][:default]
83
+ parameters.merge!({name: resource_name})
84
+ semian_parameters = parameters.dup
85
+ semian_parameters.delete(:timeout)
86
+ semian_parameters
87
+ end
88
+
89
+ def self.configure_client
90
+ yield(@@semian_parameters)
91
+ raise "Service Name not specified for Semian Configuration" if self.service_name.nil?
92
+ @@semian_parameters.generate_specifications
93
+ end
94
+
95
+ @@semian_parameters.instance_variables.each do |variable|
96
+ attribute = variable.to_s.delete('@')
97
+ self.define_singleton_method(attribute) do
98
+ return @@semian_parameters.instance_variable_get(variable)
99
+ end
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module ConfigureSemian
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "configure_semian/version"
2
+
3
+ module ConfigureSemian
4
+ require 'configure_semian/semian_configuration'
5
+ require 'configure_semian/net_http'
6
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configure_semian
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - supantha
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-03 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: 4.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: semian
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Semian Connector
84
+ email:
85
+ - supantha.samanta@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - configure_semian.gemspec
98
+ - lib/configure_semian.rb
99
+ - lib/configure_semian/engine.rb
100
+ - lib/configure_semian/net_http.rb
101
+ - lib/configure_semian/semian_configuration.rb
102
+ - lib/configure_semian/version.rb
103
+ homepage: https://github.com/elarahq/configure_semian
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.11
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Configure Semian
127
+ test_files: []