/
ActorRefSourceActor.cs
210 lines (192 loc) · 8 KB
/
ActorRefSourceActor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//-----------------------------------------------------------------------
// <copyright file="ActorRefSourceActor.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using Akka.Actor;
using Akka.Event;
using Akka.Streams.Actors;
namespace Akka.Streams.Implementation
{
/// <summary>
/// TBD
/// </summary>
/// <typeparam name="T">TBD</typeparam>
internal class ActorRefSourceActor<T> : Actors.ActorPublisher<T>
{
/// <summary>
/// TBD
/// </summary>
/// <param name="bufferSize">TBD</param>
/// <param name="overflowStrategy">TBD</param>
/// <param name="settings">TBD</param>
/// <exception cref="NotSupportedException">
/// This exception is thrown when the specified <paramref name="overflowStrategy"/> is <see cref="Akka.Streams.OverflowStrategy.Backpressure"/>.
/// </exception>
/// <returns>TBD</returns>
public static Props Props(int bufferSize, OverflowStrategy overflowStrategy, ActorMaterializerSettings settings)
{
if (overflowStrategy == OverflowStrategy.Backpressure)
throw new NotSupportedException("Backpressure overflow strategy not supported");
var maxFixedBufferSize = settings.MaxFixedBufferSize;
return Actor.Props.Create(() => new ActorRefSourceActor<T>(bufferSize, overflowStrategy, maxFixedBufferSize));
}
/// <summary>
/// TBD
/// </summary>
protected readonly IBuffer<T> Buffer;
/// <summary>
/// TBD
/// </summary>
public readonly int BufferSize;
/// <summary>
/// TBD
/// </summary>
public readonly OverflowStrategy OverflowStrategy;
private ILoggingAdapter _log;
/// <summary>
/// TBD
/// </summary>
/// <param name="bufferSize">TBD</param>
/// <param name="overflowStrategy">TBD</param>
/// <param name="maxFixedBufferSize">TBD</param>
public ActorRefSourceActor(int bufferSize, OverflowStrategy overflowStrategy, int maxFixedBufferSize)
{
BufferSize = bufferSize;
OverflowStrategy = overflowStrategy;
Buffer = bufferSize != 0 ? Implementation.Buffer.Create<T>(bufferSize, maxFixedBufferSize) : null;
}
/// <summary>
/// TBD
/// </summary>
protected ILoggingAdapter Log => _log ?? (_log = Context.GetLogger());
/// <summary>
/// TBD
/// </summary>
/// <param name="message">TBD</param>
/// <returns>TBD</returns>
protected override bool Receive(object message)
=> DefaultReceive(message) || RequestElement(message) || (message is T && ReceiveElement((T)message));
/// <summary>
/// TBD
/// </summary>
/// <param name="message">TBD</param>
/// <returns>TBD</returns>
protected bool DefaultReceive(object message)
{
if (message is Actors.Cancel)
Context.Stop(Self);
else if (message is Status.Success)
{
if (BufferSize == 0 || Buffer.IsEmpty)
OnCompleteThenStop(); // will complete the stream successfully
else
Context.Become(DrainBufferThenComplete);
}
else if (message is Status.Failure failure && IsActive)
OnErrorThenStop(failure.Cause);
else
return false;
return true;
}
/// <summary>
/// TBD
/// </summary>
/// <param name="message">TBD</param>
/// <returns>TBD</returns>
protected virtual bool RequestElement(object message)
{
if (message is Request)
{
// totalDemand is tracked by base
if (BufferSize != 0)
while (TotalDemand > 0L && !Buffer.IsEmpty)
OnNext(Buffer.Dequeue());
return true;
}
return false;
}
/// <summary>
/// TBD
/// </summary>
/// <param name="message">TBD</param>
/// <returns>TBD</returns>
protected virtual bool ReceiveElement(T message)
{
if (IsActive)
{
if (TotalDemand > 0L)
OnNext(message);
else if (BufferSize == 0)
Log.Debug("Dropping element because there is no downstream demand: [{0}]", message);
else if (!Buffer.IsFull)
Buffer.Enqueue(message);
else
{
switch (OverflowStrategy)
{
case OverflowStrategy.DropHead:
Log.Debug("Dropping the head element because buffer is full and overflowStrategy is: [DropHead]");
Buffer.DropHead();
Buffer.Enqueue(message);
break;
case OverflowStrategy.DropTail:
Log.Debug("Dropping the tail element because buffer is full and overflowStrategy is: [DropTail]");
Buffer.DropTail();
Buffer.Enqueue(message);
break;
case OverflowStrategy.DropBuffer:
Log.Debug("Dropping all the buffered elements because buffer is full and overflowStrategy is: [DropBuffer]");
Buffer.Clear();
Buffer.Enqueue(message);
break;
case OverflowStrategy.DropNew:
// do not enqueue new element if the buffer is full
Log.Debug("Dropping the new element because buffer is full and overflowStrategy is: [DropNew]");
break;
case OverflowStrategy.Fail:
Log.Error("Failing because buffer is full and overflowStrategy is: [Fail]");
OnErrorThenStop(new BufferOverflowException($"Buffer overflow, max capacity was ({BufferSize})"));
break;
case OverflowStrategy.Backpressure:
// there is a precondition check in Source.actorRefSource factory method
Log.Debug("Backpressuring because buffer is full and overflowStrategy is: [Backpressure]");
break;
}
}
return true;
}
return false;
}
private bool DrainBufferThenComplete(object message)
{
if (message is Cancel)
{
Context.Stop(Self);
}
else if (message is Status.Failure failure && IsActive)
{
// errors must be signaled as soon as possible,
// even if previously valid completion was requested via Status.Success
OnErrorThenStop(failure.Cause);
}
else if (message is Request)
{
// totalDemand is tracked by base
while (TotalDemand > 0L && !Buffer.IsEmpty)
OnNext(Buffer.Dequeue());
if (Buffer.IsEmpty)
OnCompleteThenStop(); // will complete the stream successfully
}
else if (IsActive)
Log.Debug(
"Dropping element because Status.Success received already, only draining already buffered elements: [{0}] (pending: [{1}])",
message, Buffer.Used);
else
return false;
return true;
}
}
}