-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathWatcher.cs
245 lines (210 loc) · 8.37 KB
/
Watcher.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace k8s
{
/// <summary>Describes the type of a watch event.</summary>
#if NET8_0_OR_GREATER
[JsonConverter(typeof(JsonStringEnumConverter<WatchEventType>))]
#endif
public enum WatchEventType
{
/// <summary>Emitted when an object is created, modified to match a watch's filter, or when a watch is first opened.</summary>
[EnumMember(Value = "ADDED")]
Added,
/// <summary>Emitted when an object is modified.</summary>
[EnumMember(Value = "MODIFIED")]
Modified,
/// <summary>Emitted when an object is deleted or modified to no longer match a watch's filter.</summary>
[EnumMember(Value = "DELETED")]
Deleted,
/// <summary>Emitted when an error occurs while watching resources. Most commonly, the error is 410 Gone which indicates that
/// the watch resource version was outdated and events were probably lost. In that case, the watch should be restarted.
/// </summary>
[EnumMember(Value = "ERROR")]
Error,
/// <summary>Bookmarks may be emitted periodically to update the resource version. The object will
/// contain only the resource version.
/// </summary>
[EnumMember(Value = "BOOKMARK")]
Bookmark,
}
public class Watcher<T> : IDisposable
{
/// <summary>
/// indicate if the watch object is alive
/// </summary>
public bool Watching { get; private set; }
private readonly CancellationTokenSource _cts;
private readonly Func<Task<TextReader>> _streamReaderCreator;
private bool disposedValue;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "IDE0052:Remove unread private members", Justification = "Used in an async task loop")]
private readonly Task _watcherLoop;
/// <summary>
/// Initializes a new instance of the <see cref="Watcher{T}"/> class.
/// </summary>
/// <param name="streamReaderCreator">
/// A <see cref="StreamReader"/> from which to read the events.
/// </param>
/// <param name="onEvent">
/// The action to invoke when the server sends a new event.
/// </param>
/// <param name="onError">
/// The action to invoke when an error occurs.
/// </param>
/// <param name="onClosed">
/// The action to invoke when the server closes the connection.
/// </param>
public Watcher(Func<Task<StreamReader>> streamReaderCreator, Action<WatchEventType, T> onEvent,
Action<Exception> onError, Action onClosed = null)
: this(
async () => (TextReader)await streamReaderCreator().ConfigureAwait(false),
onEvent, onError, onClosed)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Watcher{T}"/> class.
/// </summary>
/// <param name="streamReaderCreator">
/// A <see cref="TextReader"/> from which to read the events.
/// </param>
/// <param name="onEvent">
/// The action to invoke when the server sends a new event.
/// </param>
/// <param name="onError">
/// The action to invoke when an error occurs.
/// </param>
/// <param name="onClosed">
/// The action to invoke when the server closes the connection.
/// </param>
public Watcher(Func<Task<TextReader>> streamReaderCreator, Action<WatchEventType, T> onEvent,
Action<Exception> onError, Action onClosed = null)
{
_streamReaderCreator = streamReaderCreator;
OnEvent += onEvent;
OnError += onError;
OnClosed += onClosed;
_cts = new CancellationTokenSource();
_watcherLoop = Task.Run(async () => await WatcherLoop(_cts.Token).ConfigureAwait(false));
}
/// <summary>
/// add/remove callbacks when any event raised from api server
/// </summary>
public event Action<WatchEventType, T> OnEvent;
/// <summary>
/// add/remove callbacks when any exception was caught during watching
/// </summary>
public event Action<Exception> OnError;
/// <summary>
/// The event which is raised when the server closes the connection.
/// </summary>
public event Action OnClosed;
public class WatchEvent
{
[JsonPropertyName("type")]
public WatchEventType Type { get; set; }
[JsonPropertyName("object")]
public T Object { get; set; }
}
private async Task WatcherLoop(CancellationToken cancellationToken)
{
try
{
Watching = true;
await foreach (var (t, evt) in CreateWatchEventEnumerator(_streamReaderCreator, OnError,
cancellationToken)
.ConfigureAwait(false)
)
{
OnEvent?.Invoke(t, evt);
}
}
catch (OperationCanceledException)
{
// ignore
}
catch (Exception e)
{
// error when transport error, IOException ect
OnError?.Invoke(e);
}
finally
{
Watching = false;
OnClosed?.Invoke();
}
}
internal static async IAsyncEnumerable<(WatchEventType, T)> CreateWatchEventEnumerator(
Func<Task<TextReader>> streamReaderCreator,
Action<Exception> onError = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Task<TR> AttachCancellationToken<TR>(Task<TR> task)
{
if (!task.IsCompleted)
{
// here to pass cancellationToken into task
return task.ContinueWith(t => t.GetAwaiter().GetResult(), cancellationToken);
}
return task;
}
using var streamReader = await AttachCancellationToken(streamReaderCreator()).ConfigureAwait(false);
for (; ; )
{
// ReadLineAsync will return null when we've reached the end of the stream.
var line = await AttachCancellationToken(streamReader.ReadLineAsync()).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
if (line == null)
{
yield break;
}
WatchEvent @event = null;
try
{
var genericEvent = KubernetesJson.Deserialize<Watcher<KubernetesObject>.WatchEvent>(line);
if (genericEvent.Object.Kind == "Status")
{
var statusEvent = KubernetesJson.Deserialize<Watcher<V1Status>.WatchEvent>(line);
var exception = new KubernetesException(statusEvent.Object);
onError?.Invoke(exception);
}
else
{
@event = KubernetesJson.Deserialize<WatchEvent>(line);
}
}
catch (Exception e)
{
onError?.Invoke(e);
}
if (@event != null)
{
yield return (@event.Type, @event.Object);
}
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_cts?.Cancel();
_cts?.Dispose();
}
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~Watcher()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(true);
GC.SuppressFinalize(this);
}
}
}