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 +5 -5
- data/ruby/.editorconfig +8 -0
- data/ruby/README.md +41 -0
- data/ruby/lib/grpclb.rb +2 -31
- data/ruby/lib/grpclb/client.rb +54 -0
- data/ruby/lib/grpclb/internal/load_report_service.rb +18 -0
- data/ruby/lib/grpclb/server.rb +10 -0
- metadata +22 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4e80ab9eb796b8eaad091f9ce0627fa84642f465c162b24ce5db3b18477c593c
|
4
|
+
data.tar.gz: d565b8ae18766ca1eecf13e96643fed70eb1ebf087c0717027c8535d4b43e1ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b48b989997b1bebb1ed1048db41ad31658b17b7fff7bde04f1f96c2438e29c5dfa22a2381ae079d6b52a13146df21d631aac385371bb77e72fcdc99f639119ec
|
7
|
+
data.tar.gz: cb9ff1d94ae9ea598c1e710486a91b5fd66a0658f8f85a0f7e2ab9ab824b1adde6d1febbd2db48f990652df93de55947d502dbd81e8605d473d6b5f662a4cb31
|
data/ruby/.editorconfig
ADDED
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 '
|
2
|
-
require '
|
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
|
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.
|
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:
|
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.
|
104
|
+
rubygems_version: 2.7.3
|
86
105
|
signing_key:
|
87
106
|
specification_version: 4
|
88
107
|
summary: grpclb ruby protocol
|