1
1
using System ;
2
+ using System . IO ;
2
3
using System . Threading ;
3
4
using System . Threading . Tasks ;
4
5
using k8s ;
@@ -30,7 +31,7 @@ public ResourceWatcher(ILogger<ResourceWatcher<TEntity>> logger, IKubernetesClie
30
31
31
32
public Task Start ( )
32
33
{
33
- _logger . LogTrace ( @"Resource Watcher startup for type ""{type}""." , typeof ( TEntity ) ) ;
34
+ _logger . LogDebug ( @"Resource Watcher startup for type ""{type}""." , typeof ( TEntity ) ) ;
34
35
return WatchResource ( ) ;
35
36
}
36
37
@@ -69,15 +70,15 @@ private async Task WatchResource()
69
70
}
70
71
else
71
72
{
72
- _logger . LogDebug ( @"Watcher for type ""{type}"" already running." , typeof ( TEntity ) ) ;
73
+ _logger . LogTrace ( @"Watcher for type ""{type}"" already running." , typeof ( TEntity ) ) ;
73
74
return ;
74
75
}
75
76
}
76
77
77
78
_cancellation = new CancellationTokenSource ( ) ;
78
79
// TODO: namespaced resources
79
80
_watcher = await _client . Watch < TEntity > (
80
- TimeSpan . FromHours ( 1 ) ,
81
+ TimeSpan . FromMinutes ( 1 ) ,
81
82
OnWatcherEvent ,
82
83
OnException ,
83
84
OnClose ,
@@ -119,11 +120,20 @@ private void OnWatcherEvent(WatchEventType type, TEntity resource)
119
120
120
121
private void OnException ( Exception e )
121
122
{
122
- _logger . LogError ( e , @"There was an error while watching the resource ""{resource}""." , typeof ( TEntity ) ) ;
123
123
_cancellation ? . Cancel ( ) ;
124
124
_watcher ? . Dispose ( ) ;
125
125
_watcher = null ;
126
126
127
+ if ( e is TaskCanceledException && e . InnerException is IOException )
128
+ {
129
+ _logger . LogTrace (
130
+ @"Either the server or the client did close the connection on watcher for resource ""{resource}"". Restart." ,
131
+ typeof ( TEntity ) ) ;
132
+ WatchResource ( ) . ConfigureAwait ( false ) ;
133
+ return ;
134
+ }
135
+
136
+ _logger . LogError ( e , @"There was an error while watching the resource ""{resource}""." , typeof ( TEntity ) ) ;
127
137
var backoff = _reconnectHandler . Retry ( TimeSpan . FromSeconds ( 5 ) ) ;
128
138
_logger . LogInformation ( "Trying to reconnect with exponential backoff {backoff}." , backoff ) ;
129
139
}
@@ -132,7 +142,7 @@ private void OnClose()
132
142
{
133
143
if ( _cancellation != null && ! _cancellation . IsCancellationRequested )
134
144
{
135
- _logger . LogInformation ( "The server closed the connection. Trying to reconnect." ) ;
145
+ _logger . LogDebug ( "The server closed the connection. Trying to reconnect." ) ;
136
146
RestartWatcher ( ) ;
137
147
}
138
148
}
0 commit comments