batcher.go ×9

Frontier kind: Code frontier

unlabeled · c_0ecf7e540bd7

5 tests · 137 LOC · 3 files · introduces 0 tests · 66 LOC · 2 files

Introduces — evidence that enters the hierarchy at this concept

Code
11 ranges66 lines · 2 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
32 ranges137 lines · 3 files · Browse complete extent
All tests (intent)
5 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Introduced files, introduced tests, and structurally relevant concept specializationbatcher.go ×4 · 18 introduced LOCbatcher.go ×4batcher.go ×1 · 3 introduced LOCbatcher.go ×1batcher.go ×1 · 3 introduced LOCbatcher.go ×1TestStreamBatcher_MinDelay · 0 introduced LOCTestStreamBatcher_MinDel…batcher.go ×1 · 2 introduced LOCbatcher.go ×1batcher.go ×2 · 4 introduced LOCbatcher.go ×2event_time_source.go ×3 · 9 introduced LOCevent_time_source.go ×3event_time_source.go ×1 · 2 introduced LOCevent_time_source.go ×1event_time_source.go ×1 · 11 introduced LOCevent_time_source.go ×1event_time_source.go ×2 · 10 introduced LOCevent_time_source.go ×2event_time_source.go ×3 · 3 introduced LOCevent_time_source.go ×3event_time_source.go ×1 · 3 introduced LOCevent_time_source.go ×1event_time_source.go ×1 · 5 introduced LOCevent_time_source.go ×1event_time_source.go ×3 · 9 introduced LOCevent_time_source.go ×3event_time_source.go ×2 · 4 introduced LOCevent_time_source.go ×2event_time_source.go ×1 · 6 introduced LOCevent_time_source.go ×1util.go ×1 · 2 introduced LOCutil.go ×1event_time_source.go ×1 · 5 introduced LOCevent_time_source.go ×1util.go ×1 · 2 introduced LOCutil.go ×1TestOperatorServiceMetadata, TestWorkflowServiceMetadata · 0 introduced LOCTestOperatorServiceMetad…go.temporal.io/server/common/clock/event_time_source.go · 221 LOCclock/event_time_source.…go.temporal.io/server/common/stream_batcher/batcher.go · 154 LOCstream_batcher/batcher.g…go.temporal.io/server/common/util/util.go · 197 LOCutil/util.goTestOperatorServiceMetadata · introduced test · go.temporal.io/server/common/api/TestOperatorServiceMetadataTestOperatorServiceMetad…TestWorkflowServiceMetadata · introduced test · go.temporal.io/server/common/api/TestWorkflowServiceMetadataTestWorkflowServiceMetad…TestStreamBatcher_AddTimeout · introduced test · go.temporal.io/server/common/stream_batcher/TestStreamBatcher_AddTimeoutTestStreamBatcher_AddTim…TestStreamBatcher_MaxDelay · introduced test · go.temporal.io/server/common/stream_batcher/TestStreamBatcher_MaxDelayTestStreamBatcher_MaxDel…TestStreamBatcher_MaxItems · introduced test · go.temporal.io/server/common/stream_batcher/TestStreamBatcher_MaxItemsTestStreamBatcher_MaxIte…TestStreamBatcher_MinDelay · introduced test · go.temporal.io/server/common/stream_batcher/TestStreamBatcher_MinDelayTestStreamBatcher_MinDel…TestStreamBatcher_Random · introduced test · go.temporal.io/server/common/stream_batcher/TestStreamBatcher_RandomTestStreamBatcher_RandomFocused concept · batcher.go ×9 · 66 introduced LOCbatcher.go ×9

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

2 files ranked by introduced lines: 66 introduced LOC across 11 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

go.temporal.io/server/common/stream_batcher/batcher.go 60 introduced LOC · 9 ranges

Open complete file

45 // NewBatcher creates a Batcher. `fn` is the processing function, `opts` are the timing options.
46 // `clock` is usually clock.NewRealTimeSource but can be a fake time source for testing.
47 > func NewBatcher[T, R any](fn func([]T) R, opts BatcherOptions, timeSource clock.TimeSource) *Batcher[T, R] { batcher.go
48 > return &Batcher[T, R]{
49 > fn: fn,
50 > opts: opts,
51 > timeSource: timeSource,
52 > submitC: make(chan batchPair[T, R]),
53 > }
54 > }
55
56 // Add adds an item to the stream and returns when it has been processed, or if the context is
58 // for the whole batch that the item ended up in, and a context error. Even if Add returns a
59 // context error, the item may still be processed in the future!
60 > func (b *Batcher[T, R]) Add(ctx context.Context, t T) (R, error) { batcher.go
61 > resp := make(chan R, 1)
62 > pair := batchPair[T, R]{resp: resp, item: t}
63 >
64 > for {
65 > runningC := b.running.Load()
66 > for runningC == nil {
67 > // goroutine is not running, try to start it
68 > newRunningC := make(chan struct{})
69 > if b.running.CompareAndSwap(nil, &newRunningC) {
70 > // we were the first one to notice the nil, start it now
71 > go b.loop(&newRunningC)
72 > }
73 // if CompareAndSwap failed, someone else was calling Add at the same time and
74 // started the goroutine already. reload to get the new running channel.
75 > runningC = b.running.Load() batcher.go
76 }
77
78 > select { batcher.go
79 case <-(*runningC):
80 // we loaded a non-nil running channel, but it closed while we're waiting to
81 // submit. the goroutine must have just exited. try again.
82 continue
83 > case b.submitC <- pair: batcher.go
84 > select {
85 > case r := <-resp:
86 > return r, nil
87 case <-ctx.Done():
88 var zeroR R
96 }
97
98 > func (b *Batcher[T, R]) loop(runningC *chan struct{}) { batcher.go
99 > defer func() {
100 // store nil so that Add knows it should start a goroutine
101 b.running.Store(nil)
106 }()
107
108 > var items []T batcher.go
109 > var resps []chan R
110 > for {
111 > clear(items)
112 > clear(resps)
113 > items, resps = items[:0], resps[:0]
114 >
115 > // wait for first item. if no item after a while, exit the goroutine
116 > idleC, idleT := b.timeSource.NewTimer(b.opts.IdleTime)
117 > select {
118 > case pair := <-b.submitC:
119 > items = append(items, pair.item)
120 > resps = append(resps, pair.resp)
121 case <-idleC:
122 return
123 }
124 > idleT.Stop() batcher.go
125 >
126 > // try to add more items. stop after a gap of MinDelay, total time of MaxDelay,
127 > // or MaxItems items.
128 > maxWaitC, maxWaitT := b.timeSource.NewTimer(b.opts.MaxDelay)
129 > loop:
130 > for len(items) < b.opts.MaxItems {
131 > gapC, gapT := b.timeSource.NewTimer(b.opts.MinDelay)
132 > select {
133 case pair := <-b.submitC:
134 items = append(items, pair.item)
142 gapT.Stop()
143 }
144 > maxWaitT.Stop() batcher.go
145 >
146 > // process batch
147 > r := b.fn(items)
148 >
149 > // send the single response to all items in the batch
150 > for _, resp := range resps {
151 > resp <- r
152 > }
153 }
154 }
go.temporal.io/server/common/clock/event_time_source.go 6 introduced LOC · 2 ranges

Open complete file

135 tmin := ts.timers[0].deadline
136 for _, t := range ts.timers[1:] {
137 > tmin = util.MinTime(tmin, t.deadline) event_time_source.go
138 > }
139 ts.now = tmin
140 ts.fireTimers()
150
151 // Sleep is a convenience function for waiting on a new timer.
152 > func (ts *EventTimeSource) Sleep(d time.Duration) { event_time_source.go
153 > t, _ := ts.NewTimer(d)
154 > <-t
155 > }
156
157 // fireTimers fires all timers that are ready.