protein 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c6dfac5e16391f182c40d822c356770a07e2a1a
4
- data.tar.gz: fb8dda9c4fa2ad86351bab6478b9315edc6a0522
3
+ metadata.gz: '08b4bbb0ee5a01175aafd5859b0bc26135434114'
4
+ data.tar.gz: de4ab2913d794eb14b1779315e009e82eb4d6f7e
5
5
  SHA512:
6
- metadata.gz: e590ba3aa43770856f8e42b8713b02c0d6b2fb4fddd7bc49aeed7470b6465a511a84b740c3c2811d41fd981216f05d71f838bbfcc3b3ea6a742ee02f106d66c5
7
- data.tar.gz: de08e7f29a2bda074a13e99b06b3eabc9f18081ffe5ed63065c5f13e8116bd8968b7a8cfc11ed4b5b5edd4e9706cb8f1ca51604c2ebd7d5c08f8d71b954ee6c4
6
+ metadata.gz: 7f7f1b3d4996505ff18d68ae2aa8b634da35988a5fb8e8e0be898f1bbf0a7e20c29771774d5962833693a31275f5112a80df61772b9af10db39ba1329ecf5c50
7
+ data.tar.gz: 6d80c80bc0e95c9976e7c38288a628f14cb2cc5413307254cfff870e06013d8d809068b25d1d790f2e46a39be39c9894bf757bcd8b35ed94f77005fb712c6c66
@@ -2,6 +2,7 @@ require "protein/errors"
2
2
  require "protein/proto_compiler"
3
3
  require "protein/get_const"
4
4
 
5
+ require "protein/config"
5
6
  require "protein/router"
6
7
  require "protein/client"
7
8
  require "protein/server"
@@ -12,6 +13,35 @@ require "protein/pointer"
12
13
  require "protein/service_error"
13
14
  require "protein/service"
14
15
 
15
- require "protein/transport"
16
16
  require "protein/http_adapter"
17
17
  require "protein/amqp_adapter"
18
+ require "protein/transport"
19
+
20
+ require "google/protobuf"
21
+ begin
22
+ require "bunny"
23
+ rescue LoadError
24
+ end
25
+
26
+ module Protein
27
+ class << self
28
+ def configure(&block)
29
+ new_config = Config.new
30
+ block.call(new_config)
31
+ @config = new_config
32
+ @config
33
+ end
34
+
35
+ def config
36
+ @config ||= Config.new
37
+ end
38
+
39
+ def logger
40
+ @logger ||= begin
41
+ Logger.new($stdout).tap do |log|
42
+ log.progname = 'protein'
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,4 +1,5 @@
1
1
  require "bunny"
2
+ require "securerandom"
2
3
  require "thread"
3
4
 
4
5
  module Protein
@@ -74,7 +75,7 @@ class AMQPAdapter
74
75
  @q = @ch.queue(queue)
75
76
  @x = @ch.default_exchange
76
77
 
77
- Rails.logger.info "Connected to #{url}, serving RPC calls from #{queue}"
78
+ Protein.logger.info "Connected to #{url}, serving RPC calls from #{queue}"
78
79
 
79
80
  loop do
80
81
  begin
@@ -95,10 +96,16 @@ class AMQPAdapter
95
96
 
96
97
  @ch.ack(delivery_info.delivery_tag)
97
98
 
98
- raise(@error) if @error
99
+ if @error
100
+ error_logger = Protein.config.error_logger
101
+ error_logger.call(@error) if error_logger
102
+
103
+ raise(@error)
104
+ end
99
105
  end
100
106
  rescue StandardError => e
101
- Rails.logger.info "RPC server error: #{e.inspect}, restarting the server in 5s..."
107
+ Protein.logger.error "RPC server error: #{e.inspect}, restarting the server in 5s..."
108
+
102
109
  sleep 5
103
110
  end
104
111
  end
@@ -0,0 +1,5 @@
1
+ module Protein
2
+ class Config
3
+ attr_accessor :error_logger
4
+ end
5
+ end
@@ -3,7 +3,7 @@ class GetConst
3
3
  class << self
4
4
  def call(input)
5
5
  if input.is_a?(String)
6
- input.constantize
6
+ Object.const_get(input)
7
7
  elsif input != nil
8
8
  input
9
9
  else
@@ -1,5 +1,6 @@
1
1
  require "net/http"
2
2
  require "json"
3
+ require "base64"
3
4
 
4
5
  module Protein
5
6
  module Payload
@@ -15,13 +15,13 @@ class Processor
15
15
  private
16
16
 
17
17
  def process_and_log_call(service_name, service_class, request_buf)
18
- Rails.logger.info "Processing RPC call: #{service_name}"
18
+ Protein.logger.info "Processing RPC call: #{service_name}"
19
19
 
20
20
  start_time = Time.now
21
21
  response_buf, errors = process_call(service_class, request_buf)
22
22
  duration_ms = ((Time.now - start_time) * 1000).round
23
23
 
24
- Rails.logger.info "#{response_buf ? 'Resolved' : 'Rejected'} in #{duration_ms}ms"
24
+ Protein.logger.info "#{response_buf ? 'Resolved' : 'Rejected'} in #{duration_ms}ms"
25
25
 
26
26
  Payload::Response.encode(response_buf, errors) if service_class.response?
27
27
  end
@@ -44,13 +44,13 @@ class Processor
44
44
  end
45
45
 
46
46
  def process_and_log_push(service_name, service_class, request_buf)
47
- Rails.logger.info "Processing RPC push: #{service_name}"
47
+ Protein.logger.info "Processing RPC push: #{service_name}"
48
48
 
49
49
  start_time = Time.now
50
50
  process_push(service_class, request_buf)
51
51
  duration_ms = ((Time.now - start_time) * 1000).round
52
52
 
53
- Rails.logger.info "Processed in #{duration_ms}ms"
53
+ Protein.logger.info "Processed in #{duration_ms}ms"
54
54
 
55
55
  nil
56
56
  end
@@ -1,3 +1,5 @@
1
+ require 'shellwords'
2
+
1
3
  module Protein
2
4
  class ProtoCompiler
3
5
  class << self
@@ -34,7 +34,7 @@ class Server
34
34
 
35
35
  if worker_count.is_a?(Integer) && worker_count > 1
36
36
  Parallel.each(1..worker_count, in_processes: worker_count) do |worker|
37
- Rails.logger.info "Starting server #{worker}/#{worker_count} with PID #{Process.pid}"
37
+ Protein.logger.info "Starting server #{worker}/#{worker_count} with PID #{Process.pid}"
38
38
  transport_class.serve(router)
39
39
  end
40
40
  else
@@ -11,7 +11,8 @@ class Service
11
11
 
12
12
  def proto(proto_module = nil)
13
13
  @proto_module = proto_module
14
- @service_name ||= proto_module.to_s.split("::").last.underscore
14
+ @service_name ||= proto_module.to_s.split("::").last.
15
+ scan(/[A-Z][^A-Z]*/).map(&:downcase).join("_")
15
16
  @request_class ||= "#{proto_module}::Request"
16
17
  @response_class ||= "#{proto_module}::Response"
17
18
  end
@@ -30,7 +31,7 @@ class Service
30
31
 
31
32
  def response?
32
33
  response_class != false &&
33
- (!response_class.is_a?(String) || !response_class.safe_constantize.nil?)
34
+ (!response_class.is_a?(String) || !(Object.const_get(response_class) rescue nil).nil?)
34
35
  rescue NameError
35
36
  false
36
37
  end
@@ -45,12 +46,19 @@ class Service
45
46
  end
46
47
 
47
48
  attr_reader :request, :response, :errors
48
- delegate :response?, :response_class, to: :class
49
49
 
50
50
  def initialize(request)
51
51
  @request = request
52
52
  end
53
53
 
54
+ def response?
55
+ self.class.response?
56
+ end
57
+
58
+ def response_class
59
+ self.class.response_class
60
+ end
61
+
54
62
  def process
55
63
  @success = nil
56
64
  @response = response_class.new if response?
@@ -1,3 +1,3 @@
1
1
  module Protein
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,19 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protein
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Słuszniak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-20 00:00:00.000000000 Z
11
+ date: 2018-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: google-protobuf
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.0.0
13
53
  - !ruby/object:Gem::Dependency
14
54
  name: parallel
15
55
  requirement: !ruby/object:Gem::Requirement
16
56
  requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.0'
17
60
  - - ">="
18
61
  - !ruby/object:Gem::Version
19
62
  version: 1.0.0
@@ -21,6 +64,9 @@ dependencies:
21
64
  prerelease: false
22
65
  version_requirements: !ruby/object:Gem::Requirement
23
66
  requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
24
70
  - - ">="
25
71
  - !ruby/object:Gem::Version
26
72
  version: 1.0.0
@@ -35,6 +81,7 @@ files:
35
81
  - lib/protein.rb
36
82
  - lib/protein/amqp_adapter.rb
37
83
  - lib/protein/client.rb
84
+ - lib/protein/config.rb
38
85
  - lib/protein/errors.rb
39
86
  - lib/protein/get_const.rb
40
87
  - lib/protein/http_adapter.rb
@@ -68,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
115
  version: '0'
69
116
  requirements: []
70
117
  rubyforge_project:
71
- rubygems_version: 2.6.8
118
+ rubygems_version: 2.6.14
72
119
  signing_key:
73
120
  specification_version: 4
74
121
  summary: Multi-platform remote procedure call (RPC) system based on Protocol Buffers