Skip to content

Commit 9b9893a

Browse files
committed
document monoprocess backend
1 parent cbe93c0 commit 9b9893a

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

backend/monoprocess/monoprocess.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ type monoprocessBackend struct {
2323
logger *slog.Logger
2424
}
2525

26+
// NewMonoprocessBackend wraps an existing backend and improves its responsiveness
27+
// in case the backend and worker are running in the same process. This backend
28+
// uses channels to notify the worker every time there is a new task ready to be
29+
// worked on. Note that only one worker will be notified.
30+
// IMPORTANT: Only use this backend if the backend and worker are running in the
31+
// same process.
2632
func NewMonoprocessBackend(b backend.Backend, signalBufferSize int, signalTimeout time.Duration) *monoprocessBackend {
2733
if signalTimeout <= 0 {
28-
signalTimeout = time.Second
34+
signalTimeout = time.Second // default
2935
}
3036
mb := &monoprocessBackend{
3137
Backend: b,
@@ -41,12 +47,12 @@ func (b *monoprocessBackend) GetWorkflowTask(ctx context.Context) (*task.Workflo
4147
if w, err := b.Backend.GetWorkflowTask(ctx); w != nil || err != nil {
4248
return w, err
4349
}
44-
b.logger.Debug("worker waiting for workflow task signal")
50+
b.logger.DebugContext(ctx, "worker waiting for workflow task signal")
4551
select {
4652
case <-ctx.Done():
4753
return nil, ctx.Err()
4854
case <-b.workflowSignal:
49-
b.logger.Debug("worker got a workflow task signal")
55+
b.logger.DebugContext(ctx, "worker got a workflow task signal")
5056
return b.GetWorkflowTask(ctx)
5157
}
5258
}
@@ -55,12 +61,12 @@ func (b *monoprocessBackend) GetActivityTask(ctx context.Context) (*task.Activit
5561
if a, err := b.Backend.GetActivityTask(ctx); a != nil || err != nil {
5662
return a, err
5763
}
58-
b.logger.Debug("worker waiting for activity task signal")
64+
b.logger.DebugContext(ctx, "worker waiting for activity task signal")
5965
select {
6066
case <-ctx.Done():
6167
return nil, ctx.Err()
6268
case <-b.activitySignal:
63-
b.logger.Debug("worker got an activity task signal")
69+
b.logger.DebugContext(ctx, "worker got an activity task signal")
6470
return b.GetActivityTask(ctx)
6571
}
6672
}
@@ -89,7 +95,7 @@ func (b *monoprocessBackend) CompleteWorkflowTask(
8995
continue
9096
}
9197
if !b.notifyActivityWorker(ctx) {
92-
break // no reason to notify more
98+
break // no reason to notify more, queue is full
9399
}
94100
}
95101
for _, e := range timerEvents {
@@ -98,7 +104,7 @@ func (b *monoprocessBackend) CompleteWorkflowTask(
98104
b.logger.Warn("unknown attributes type in timer event", "type", reflect.TypeOf(e.Attributes).String())
99105
continue
100106
}
101-
b.logger.Debug("scheduling timer to notify workflow worker")
107+
b.logger.DebugContext(ctx, "scheduling timer to notify workflow worker")
102108
time.AfterFunc(attr.At.Sub(time.Now()), func() { b.notifyWorkflowWorker(ctx) }) // TODO: cancel timer if the event gets cancelled
103109
}
104110
for _, e := range workflowEvents {
@@ -108,7 +114,7 @@ func (b *monoprocessBackend) CompleteWorkflowTask(
108114
continue
109115
}
110116
if !b.notifyWorkflowWorker(ctx) {
111-
break // no reason to notify more
117+
break // no reason to notify more, queue is full
112118
}
113119
}
114120
return nil
@@ -139,35 +145,31 @@ func (b *monoprocessBackend) SignalWorkflow(ctx context.Context, instanceID stri
139145
}
140146

141147
func (b *monoprocessBackend) notifyActivityWorker(ctx context.Context) bool {
148+
ctx, cancel := context.WithTimeout(ctx, b.signalTimeout)
149+
defer cancel()
142150
select {
143151
case <-ctx.Done():
144152
// we didn't manage to notify the worker that there is a new task, it
145153
// will pick it up after the poll timeout
146-
b.logger.Debug("failed to signal activity task to worker, context cancelled")
147-
case <-time.After(b.signalTimeout):
148-
// we didn't manage to notify the worker that there is a new task, it
149-
// will pick it up after the poll timeout
150-
b.logger.Debug("failed to signal activity task to worker, timeout")
154+
b.logger.DebugContext(ctx, "failed to signal activity task to worker", "reason", ctx.Err())
155+
return false
151156
case b.activitySignal <- struct{}{}:
152-
b.logger.Debug("signalled a new activity task to worker")
157+
b.logger.DebugContext(ctx, "signalled a new activity task to worker")
153158
return true
154159
}
155-
return false
156160
}
157161

158162
func (b *monoprocessBackend) notifyWorkflowWorker(ctx context.Context) bool {
163+
ctx, cancel := context.WithTimeout(ctx, b.signalTimeout)
164+
defer cancel()
159165
select {
160166
case <-ctx.Done():
161167
// we didn't manage to notify the worker that there is a new task, it
162168
// will pick it up after the poll timeout
163-
b.logger.Debug("failed to signal workflow task to worker, context cancelled")
164-
case <-time.After(b.signalTimeout):
165-
// we didn't manage to notify the worker that there is a new task, it
166-
// will pick it up after the poll timeout
167-
b.logger.Debug("failed to signal workflow task to worker, timeout")
169+
b.logger.DebugContext(ctx, "failed to signal workflow task to worker", "reason", ctx.Err())
170+
return false
168171
case b.workflowSignal <- struct{}{}:
169-
b.logger.Debug("signalled a new workflow task to worker")
172+
b.logger.DebugContext(ctx, "signalled a new workflow task to worker")
170173
return true
171174
}
172-
return false
173175
}

0 commit comments

Comments
 (0)