gcpc 0.0.5 → 0.0.6

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
  SHA256:
3
- metadata.gz: b617d83445ce50725dbf992702e226b89f255f0760d969732e42c782b6af05e4
4
- data.tar.gz: 6fd34095e7bee0b101d04daa6d6824dcd1977ff97e804463f86837d6ef8a4b67
3
+ metadata.gz: d4dfcbba14815f166e194aa5d106bf86c1adb86d4feb7bd1b406808974a2137d
4
+ data.tar.gz: ed718afdbaeeb0b415cd9f5923b30f69dd9a17066712f4a932f2d55102da27c1
5
5
  SHA512:
6
- metadata.gz: 6e17d2db5ed5dc4818f7d4995024f5e9c16b42ceb9fbd6d4cb09008d929dedf3dce6937dab2105f0913731e34554be1cdc524b8b8cc8990b22d9829aebeb1d41
7
- data.tar.gz: 4565614b54983a4b15bb16eff6570c526a29ca053c5d15459e5031602ab07b060cdb607a538e4246f0c6c615cc1eb28a15545a1cceaa02d6447e849470832d14
6
+ metadata.gz: 21c978898799abeb533747aca3bcb52d152d9f92c6260db56ca5b0da74b08985a521974b9707a3d36a3772ebe1017edab5a2ee2d948841a81e414594647fa39a
7
+ data.tar.gz: 1d99113e0c48bad884963d12c43878efb55681fe47268ce1fbc2bb975ffb9a6d83e426f7dc654810ea6a1b4227542c3b21c6a8c2acd6c2185c6e2a030addc52d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gcpc (0.0.5)
4
+ gcpc (0.0.6)
5
5
  google-cloud-pubsub
6
6
 
7
7
  GEM
@@ -30,7 +30,7 @@ GEM
30
30
  googleauth (>= 0.6.2, < 0.10.0)
31
31
  grpc (>= 1.7.2, < 2.0)
32
32
  rly (~> 0.2.3)
33
- google-protobuf (3.9.0)
33
+ google-protobuf (3.9.0-universal-darwin)
34
34
  googleapis-common-protos (1.3.9)
35
35
  google-protobuf (~> 3.0)
36
36
  googleapis-common-protos-types (~> 1.0)
@@ -44,7 +44,7 @@ GEM
44
44
  multi_json (~> 1.11)
45
45
  os (>= 0.9, < 2.0)
46
46
  signet (~> 0.7)
47
- grpc (1.22.0)
47
+ grpc (1.22.0-universal-darwin)
48
48
  google-protobuf (~> 3.8)
49
49
  googleapis-common-protos-types (~> 1.0)
50
50
  grpc-google-iam-v1 (0.6.9)
@@ -37,6 +37,6 @@ module Gcpc
37
37
 
38
38
  extend Forwardable
39
39
 
40
- def_delegators :@engine, :publish, :publish_async, :topic
40
+ def_delegators :@engine, :publish, :publish_batch, :publish_async, :topic
41
41
  end
42
42
  end
@@ -1,11 +1,15 @@
1
+ require "gcpc/publisher/engine/batch_engine"
2
+ require "gcpc/publisher/engine/chained_interceptor"
3
+
1
4
  module Gcpc
2
5
  class Publisher
3
6
  class Engine
4
7
  # @param [Google::Cloud::Pubsub::Topic] topic
5
8
  # @param [<#publish>] interceptors
6
9
  def initialize(topic:, interceptors:)
7
- @topic = topic
8
- @interceptors = interceptors.map { |i| (i.class == Class) ? i.new : i }
10
+ @topic = topic
11
+ interceptors = interceptors.map { |i| (i.class == Class) ? i.new : i }
12
+ @interceptor = ChainedInterceptor.new(interceptors)
9
13
  end
10
14
 
11
15
  attr_reader :topic
@@ -16,39 +20,31 @@ module Gcpc
16
20
  d = data.dup
17
21
  a = attributes.dup
18
22
 
19
- intercept!(@interceptors, d, a) do |dd, aa|
23
+ @interceptor.intercept!(d, a) do |dd, aa|
20
24
  do_publish(dd, aa)
21
25
  end
22
26
  end
23
27
 
28
+ # @param [Proc] block
29
+ def publish_batch(&block)
30
+ batch_engine = BatchEngine.new(topic: @topic, interceptor: @interceptor)
31
+ yield batch_engine
32
+ batch_engine.flush
33
+ end
34
+
35
+ # @param [String] data
36
+ # @param [Hash] attributes
24
37
  def publish_async(data, attributes = {}, &block)
25
38
  d = data.dup
26
39
  a = attributes.dup
27
40
 
28
- intercept!(@interceptors, d, a) do |dd, aa|
41
+ @interceptor.intercept!(d, a) do |dd, aa|
29
42
  do_publish_async(dd, aa, &block)
30
43
  end
31
44
  end
32
45
 
33
46
  private
34
47
 
35
- # @param [<#publish>] interceptors
36
- # @param [String] data
37
- # @param [Hash] attributes
38
- # @param [Proc] block
39
- def intercept!(interceptors, data, attributes, &block)
40
- if interceptors.size == 0
41
- return yield(data, attributes)
42
- end
43
-
44
- i = interceptors.first
45
- rest = interceptors[1..-1]
46
-
47
- i.publish(data, attributes) do |d, a|
48
- intercept!(rest, d, a, &block)
49
- end
50
- end
51
-
52
48
  # @param [String] data
53
49
  # @param [Hash] attributes
54
50
  def do_publish(data, attributes)
@@ -0,0 +1,37 @@
1
+ module Gcpc
2
+ class Publisher
3
+ class Engine
4
+ class BatchEngine
5
+ # @param [Google::Cloud::Pubsub::Topic] topic
6
+ # @param [Engine::ChainedInterceptor] interceptor
7
+ def initialize(topic:, interceptor:)
8
+ @topic = topic
9
+ @interceptor = interceptor
10
+ @messages = [] # Container of data and attributes
11
+ end
12
+
13
+ # Enqueue a message
14
+ #
15
+ # @param [String] data
16
+ # @param [Hash] attributes
17
+ def publish(data, attributes = {})
18
+ d = data.dup
19
+ a = attributes.dup
20
+
21
+ @interceptor.intercept!(d, a) do |dd, aa|
22
+ @messages << [dd, aa]
23
+ end
24
+ end
25
+
26
+ # Flush all enqueued messages
27
+ def flush
28
+ @topic.publish do |t|
29
+ @messages.each do |(data, attributes)|
30
+ t.publish data, attributes
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ module Gcpc
2
+ class Publisher
3
+ class Engine
4
+ class ChainedInterceptor
5
+ # @param [<#publish>] interceptors
6
+ def initialize(interceptors)
7
+ @interceptors = interceptors
8
+ end
9
+
10
+ # @param [String] data
11
+ # @param [Hash] attributes
12
+ # @param [Proc] block
13
+ def intercept!(data, attributes, &block)
14
+ do_intercept!(@interceptors, data, attributes, &block)
15
+ end
16
+
17
+ private
18
+
19
+ # @param [<#publish>] interceptors
20
+ # @param [String] data
21
+ # @param [Hash] attributes
22
+ # @param [Proc] block
23
+ def do_intercept!(interceptors, data, attributes, &block)
24
+ if interceptors.size == 0
25
+ return yield(data, attributes)
26
+ end
27
+
28
+ i = interceptors.first
29
+ rest = interceptors[1..-1]
30
+
31
+ i.publish(data, attributes) do |d, a|
32
+ do_intercept!(rest, d, a, &block)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Gcpc
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nao Minami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-02 00:00:00.000000000 Z
11
+ date: 2019-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,6 +106,8 @@ files:
106
106
  - lib/gcpc/publisher.rb
107
107
  - lib/gcpc/publisher/base_interceptor.rb
108
108
  - lib/gcpc/publisher/engine.rb
109
+ - lib/gcpc/publisher/engine/batch_engine.rb
110
+ - lib/gcpc/publisher/engine/chained_interceptor.rb
109
111
  - lib/gcpc/publisher/topic_client.rb
110
112
  - lib/gcpc/subscriber.rb
111
113
  - lib/gcpc/subscriber/base_handler.rb