tiny_lb 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/tiny_lb.rb +72 -0
  3. metadata +40 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ba4513ade0c9715de901d8a44807571054f739b5b68b785aa14d8172f84dd904
4
+ data.tar.gz: 44e6e084eb21c352ad149a46f178e352ac81e4863e68cb58050cab5d864691f0
5
+ SHA512:
6
+ metadata.gz: 00414a12c119fe0832345c95699b4029e48796310c890d8ac69a165e6cf78d13a74ccf04923454747a6d570055cce9e57a15dc2012ca83c7f7c075f0d1575c0d
7
+ data.tar.gz: e765052ef4fa81bc6c91f9468e2151f0639548f7c1146c276700a62179d6a0c9197687ff6c178df2837fa00b18a78fe693ff51369b2802c4463fccf62fc9cbf3
data/lib/tiny_lb.rb ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # tiny_lb is an in-process load balancer. it works by acting as a proxy,
4
+ # forwarding method calls to one of several downstream services based
5
+ # on a user-provided strategy.
6
+ #
7
+ # use it in the following way:
8
+ # 1. define your services; they should share a common public interface
9
+ # 2. define a strategy; a strategy is any object that responds to a call to #tiny_lb, and returns
10
+ # 3. assemble the load balancer.
11
+ # 4. use the load balancer as if it were one of your services.
12
+ #
13
+ # usage:
14
+ # class ProdSvc
15
+ # def do_work(payload)
16
+ # "prod svc handled: #{payload}"
17
+ # end
18
+ # end
19
+ #
20
+ # class ExpSvc
21
+ # def do_work(payload)
22
+ # "exp svc handled: #{payload}"
23
+ # end
24
+ # end
25
+ #
26
+ # class Rollout
27
+ # def initialize(percentage:)
28
+ # @percentage = percentage
29
+ # end
30
+ #
31
+ # def tiny_lb(services)
32
+ # # services are ordered. services[0] is the primary, services[1]
33
+ # # is the new service being rolled out to.
34
+ # rand(1..100) <= @percentage ? services[1] : services[0]
35
+ # end
36
+ # end
37
+ #
38
+ # services = [ProdSvc.new, ExpSvc.new]
39
+ # strategy = Rollout.new(percentage: 10) # 10% of traffic to ExpSvc
40
+ #
41
+ # lb = TinyLb.new(services: services, strategy: strategy)
42
+ #
43
+ # 10.times do
44
+ # puts lb.do_work("my task")
45
+ # end
46
+ #
47
+ # # output might look like this:
48
+ # #
49
+ # # prod svc handled: my task
50
+ # # prod svc handled: my task
51
+ # # exp svc handled: my task
52
+ # # prod svc handled: my task
53
+ # # prod svc handled: my task
54
+ # # prod svc handled: my task
55
+ # # prod svc handled: my task
56
+ # # prod svc handled: my task
57
+ # # prod svc handled: my task
58
+ # # prod svc handled: my task
59
+
60
+ class TinyLb
61
+ def initialize(services:, strategy:)
62
+ @services = services
63
+ @strategy = strategy
64
+ end
65
+
66
+ def method_missing(method_name, *args, &block)
67
+ dest_svc = @strategy.tiny_lb(@services)
68
+ raise NoMethodError, "undefined method `#{method_name}' for an instance of #{dest_svc.class}" unless dest_svc.respond_to?(method_name)
69
+
70
+ dest_svc.public_send(method_name, *args, &block)
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,40 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_lb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: an in-process load balancer
13
+ email: jefflunt@gmail.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - lib/tiny_lb.rb
19
+ homepage: https://github.com/jefflunt/tiny_lb
20
+ licenses:
21
+ - MIT
22
+ metadata: {}
23
+ rdoc_options: []
24
+ require_paths:
25
+ - lib
26
+ required_ruby_version: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ required_rubygems_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ requirements: []
37
+ rubygems_version: 3.6.9
38
+ specification_version: 4
39
+ summary: an in-process load balancer
40
+ test_files: []