Class: Strongman::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/strongman.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader_block, name: nil, parent: nil, max_batch_size: Float::INFINITY) ⇒ Batch

Returns a new instance of Batch.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/strongman.rb', line 17

def initialize(loader_block, name: nil, parent: nil, max_batch_size: Float::INFINITY)
  @name = name
  @queue = Concurrent::Array.new
  @promise = Concurrent::Promises.resolvable_future
  @loader_block = loader_block
  @lock = Concurrent::ReadWriteLock.new
  @parent = parent
  @children = Concurrent::Array.new
  @fulfilling = Concurrent::AtomicBoolean.new(false)
  @fulfilled = Concurrent::AtomicBoolean.new(false)
  @max_batch_size = max_batch_size

  @parent.children << self if @parent

  @root = nil
  @batch_chain = nil
end

Instance Attribute Details

#fulfilledObject

Returns the value of attribute fulfilled.



14
15
16
# File 'lib/strongman.rb', line 14

def fulfilled
  @fulfilled
end

#fulfillingObject

Returns the value of attribute fulfilling.



15
16
17
# File 'lib/strongman.rb', line 15

def fulfilling
  @fulfilling
end

#lockObject

Returns the value of attribute lock.



13
14
15
# File 'lib/strongman.rb', line 13

def lock
  @lock
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/strongman.rb', line 12

def name
  @name
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/strongman.rb', line 11

def parent
  @parent
end

Instance Method Details

#batch_chainObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/strongman.rb', line 123

def batch_chain
  if @batch_chain
    @batch_chain
  else
    @batch_chain = Concurrent::Array.new

    add_children = -> (batch) {
      @batch_chain << batch
      if batch.children.size > 0
        batch.children.flat_map(&add_children)
      end
    }
    add_children.(root)

    @batch_chain
  end
end

#childrenObject



171
172
173
# File 'lib/strongman.rb', line 171

def children
  @children ||= Concurrent::Array.new
end

#fulfill!Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/strongman.rb', line 155

def fulfill!
  results = @loader_block.call(@queue)

  if results.is_a?(Concurrent::Promises::Future)
    # if the strongman loader block returns a promise (e.g. if the block uses another loader),
    # make sure to touch it to kick off any delayed effects before chaining
    results.touch.then do |inner_results|
      @promise.fulfill(normalize_results(inner_results))
    end.flat
  else
    @promise.fulfill(normalize_results(results))
  end

  self
end

#fulfill_hierarchyObject

Raises:

  • (Error)


141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/strongman.rb', line 141

def fulfill_hierarchy
  raise Error.new("Only run #fulfill_hierarchy on root batches") if @parent

  with_lock do
    return if fulfilled?

    mark_fulfilling!
    batch_chain.reverse.each(&:fulfill!)
  ensure
    mark_fulfilled!
    mark_not_fulfilling!
  end
end

#fulfilled?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/strongman.rb', line 35

def fulfilled?
  root.fulfilled.true?
end

#fulfilling?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/strongman.rb', line 39

def fulfilling?
  root.fulfilling.true?
end

#mark_fulfilled!Object



86
87
88
89
# File 'lib/strongman.rb', line 86

def mark_fulfilled!
  root.fulfilled.make_true
  self
end

#mark_fulfilling!Object



91
92
93
94
# File 'lib/strongman.rb', line 91

def mark_fulfilling!
  root.fulfilling.make_true
  self
end

#mark_not_fulfilling!Object



96
97
98
99
# File 'lib/strongman.rb', line 96

def mark_not_fulfilling!
  root.fulfilling.make_false
  self
end

#needs_fulfilling?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/strongman.rb', line 43

def needs_fulfilling?
  !fulfilled? && !fulfilling?
end

#queue(key) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/strongman.rb', line 47

def queue(key)
  @queue << key

  future = @promise.then do |results|
    unless results.key?(key)
      raise StandardError, "Batch loader didn't resolve a key: #{key}. Resolved keys: #{results.keys}"
    end

    result = results[key]

    if result.is_a?(Concurrent::Promises::Future)
      result
    else
      Concurrent::Promises.resolvable_future.fulfill(result)
    end
  end.flat

  #
  # If our queue is full, fulfill immediately and return the bare future
  #
  if @queue.size >= @max_batch_size
    root.fulfill_hierarchy

    future
  else
    #
    # If the queue is not full, create a delayed future that fulfills when the value is requested and chains
    # to the inner future
    #
    Concurrent::Promises.delay do
      # with_lock do
      root.fulfill_hierarchy if root.needs_fulfilling?
      # end

      future
    end.flat
  end
end

#rootObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/strongman.rb', line 107

def root
  if @root
    @root
  else
    find_top = -> (batch) {
      if batch.parent
        find_top.(batch.parent)
      else
        batch
      end
    }

    @root = find_top.(self)
  end
end

#with_lockObject



101
102
103
104
105
# File 'lib/strongman.rb', line 101

def with_lock
  root.lock.with_write_lock do
    yield
  end
end