grpclb 0.4.3 → 0.4.4

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
- SHA1:
3
- metadata.gz: 7b85ba65cf8d185b28e8b75ff30dc889201ed2ab
4
- data.tar.gz: 7d338e7ae2e0856698dfe9f07c9745fa2abb6beb
2
+ SHA256:
3
+ metadata.gz: 4e80ab9eb796b8eaad091f9ce0627fa84642f465c162b24ce5db3b18477c593c
4
+ data.tar.gz: d565b8ae18766ca1eecf13e96643fed70eb1ebf087c0717027c8535d4b43e1ae
5
5
  SHA512:
6
- metadata.gz: e77a00a74ccc9bfc06c7958559a77808dcad2c6798dfc4b6754a4d3d051dec8034681402136be4d38dea0a47a6d36ae575b3d78322b5a9b3d200a83c81258aee
7
- data.tar.gz: 185c097ed9def4b65b05a332f39607a14e45b73c1ac8351d9efdc7d2e4335e3853e18a1e1553211877d89cc9a992a699a11d7e91211e3634629934713f8a10c2
6
+ metadata.gz: b48b989997b1bebb1ed1048db41ad31658b17b7fff7bde04f1f96c2438e29c5dfa22a2381ae079d6b52a13146df21d631aac385371bb77e72fcdc99f639119ec
7
+ data.tar.gz: cb9ff1d94ae9ea598c1e710486a91b5fd66a0658f8f85a0f7e2ab9ab824b1adde6d1febbd2db48f990652df93de55947d502dbd81e8605d473d6b5f662a4cb31
@@ -0,0 +1,8 @@
1
+ root = true
2
+
3
+ [*]
4
+ end_of_line = lf
5
+ insert_final_newline = true
6
+ charset = utf-8
7
+ indent_style = space
8
+ indent_size = 2
data/ruby/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # grpclb/ruby
2
+
3
+ Ruby implementation of grpclb client and server.
4
+
5
+
6
+
7
+ ## Client
8
+
9
+ ```ruby
10
+ require 'grpclb/client' # or just 'grpclb'
11
+
12
+ # construct client:
13
+ client = Grpclb::Client.new('127.0.0.1:8383', 'service-name', HelloWorld::V1::Stub)
14
+
15
+ # call methods:
16
+ client.say_hello(HelloWorld::HelloRequest.new(...))
17
+ ```
18
+
19
+ Creates and maintains a load-balanced connection for given service stub.
20
+
21
+ Automatically reconnects on UNAVAILABLE errors.
22
+
23
+
24
+
25
+ ## Server
26
+
27
+ ```ruby
28
+ require 'grpclb/server' # or just 'grpclb'
29
+
30
+ # construct server:
31
+ server = Grpclb::Server.new(...) # just a subclass of GRPC::RpcServer, same initialize args
32
+
33
+ # handle services:
34
+ server.handle(HelloWorld::V1::ServiceImpl) # or .handle(HelloWorld::V1::ServiceImpl.new)
35
+
36
+ # set up and run:
37
+ server.add_http2_port '127.0.0.1:8080', :this_port_is_insecure
38
+ server.run_till_terminated
39
+ ```
40
+
41
+ Subclass of `GRPC::RpcServer`, that takes care of grpclb load reporting.
data/ruby/lib/grpclb.rb CHANGED
@@ -1,31 +1,2 @@
1
- require 'grpclb_backend_v1/backend_pb'
2
- require 'grpclb_backend_v1/backend_services_pb'
3
- require 'grpclb_balancer_v1/balancer_pb'
4
- require 'grpclb_balancer_v1/balancer_services_pb'
5
-
6
- class Grpclb::Client < ::SimpleDelegator
7
- attr_reader :target
8
-
9
- # @param [String] lb_addr the load balancer address
10
- # @param [String] target the target service name, as registered in the backend
11
- # @param [Class] service_stub the service stub class
12
- def initialize(lb_addr, target, service_stub, lb_creds: :this_channel_is_insecure, service_creds: :this_channel_is_insecure)
13
- @lb_stub = Grpclb::Balancer::V1::LoadBalancer::Stub.new(lb_addr, lb_creds)
14
- @target = target
15
- @service_stub = service_stub
16
- @service_creds = service_creds
17
-
18
- reconnect!
19
- end
20
-
21
- def reconnect!
22
- req = Grpclb::Balancer::V1::ServersRequest.new(target: target)
23
- servers = @lb_stub.servers(req).servers
24
- raise "No servers available for target '#{target}'" if servers.empty?
25
-
26
- primary = servers.first.address
27
- client = @service_stub.new(primary, @service_creds)
28
- __setobj__(client)
29
- end
30
-
31
- end
1
+ require 'grpclb/client'
2
+ require 'grpclb/server'
@@ -0,0 +1,54 @@
1
+ require 'grpclb_balancer_v1/balancer_pb'
2
+ require 'grpclb_balancer_v1/balancer_services_pb'
3
+
4
+ class Grpclb::Client
5
+ attr_reader :target
6
+
7
+ DEFAULT_MAX_RECONNECTS = 3
8
+
9
+ # @param [String] lb_addr the load balancer address
10
+ # @param [String] target the target service name, as registered in the backend
11
+ # @param [Class] service_stub the service stub class
12
+ def initialize(lb_addr, target, service_stub, lb_creds: :this_channel_is_insecure, service_creds: :this_channel_is_insecure, max_reconnects: DEFAULT_MAX_RECONNECTS) # rubocop:disable Metrics/ParameterLists, Metrics/LineLength
13
+ @lb_stub = Grpclb::Balancer::V1::LoadBalancer::Stub.new(lb_addr, lb_creds)
14
+ @target = target
15
+ @service_stub = service_stub
16
+ @service_creds = service_creds
17
+ @max_reconnects = max_reconnects
18
+
19
+ service_stub.parent.const_get(:Service).rpc_descs.each_key do |meth|
20
+ meth = GRPC::GenericService.underscore(meth.to_s).to_sym
21
+
22
+ define_singleton_method meth do |*a, &b|
23
+ with_reconnect { client.send(meth, *a, &b) }
24
+ end
25
+ end
26
+
27
+ reconnect!
28
+ end
29
+
30
+ def reconnect!
31
+ req = Grpclb::Balancer::V1::ServersRequest.new(target: target)
32
+ servers = @lb_stub.servers(req).servers
33
+ raise "No servers available for target '#{target}'" if servers.empty?
34
+
35
+ primary = servers.first.address
36
+ @client = @service_stub.new(primary, @service_creds)
37
+ end
38
+
39
+ private
40
+
41
+ def with_reconnect
42
+ retries = 0
43
+ begin
44
+ yield
45
+ rescue GRPC::BadStatus => e
46
+ raise unless e.code == GRPC::Core::StatusCodes::UNAVAILABLE
47
+ raise if retries >= @max_reconnects
48
+
49
+ reconnect!
50
+ retries += 1
51
+ retry
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,18 @@
1
+ require 'grpclb_backend_v1/backend_pb'
2
+ require 'grpclb_backend_v1/backend_services_pb'
3
+
4
+ module Grpclb
5
+ module Internal
6
+ class LoadReportService < Grpclb::Backend::V1::LoadReport::Service
7
+ include GRPC::GenericService
8
+
9
+ def initialize(pool)
10
+ @pool = pool
11
+ end
12
+
13
+ def load(_req, _call)
14
+ Backend::V1::LoadResponse.new(score: @pool.jobs_waiting)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ require 'grpclb/internal/load_report_service'
2
+
3
+ class Grpclb::Server < GRPC::RpcServer
4
+ DUMMY_POOL = Struct.new(:jobs_waiting).new(0)
5
+
6
+ def initialize(*args)
7
+ super
8
+ handle(Grpclb::Internal::LoadReportService.new(@pool || DUMMY_POOL))
9
+ end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grpclb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Black Square Media
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-06 00:00:00.000000000 Z
11
+ date: 2018-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grpc
@@ -52,13 +52,32 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  executables: []
58
72
  extensions: []
59
73
  extra_rdoc_files: []
60
74
  files:
75
+ - ruby/.editorconfig
76
+ - ruby/README.md
61
77
  - ruby/lib/grpclb.rb
78
+ - ruby/lib/grpclb/client.rb
79
+ - ruby/lib/grpclb/internal/load_report_service.rb
80
+ - ruby/lib/grpclb/server.rb
62
81
  - ruby/lib/grpclb_backend_v1/backend_pb.rb
63
82
  - ruby/lib/grpclb_backend_v1/backend_services_pb.rb
64
83
  - ruby/lib/grpclb_balancer_v1/balancer_pb.rb
@@ -82,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
101
  version: '0'
83
102
  requirements: []
84
103
  rubyforge_project:
85
- rubygems_version: 2.6.11
104
+ rubygems_version: 2.7.3
86
105
  signing_key:
87
106
  specification_version: 4
88
107
  summary: grpclb ruby protocol