-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathProcessScheduler.cs
144 lines (127 loc) · 6.82 KB
/
ProcessScheduler.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
using System;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.JsonRpc.Server;
namespace OmniSharp.Extensions.JsonRpc
{
internal class ProcessScheduler : IDisposable
{
private readonly IObserver<(RequestProcessType type, string name, SchedulerDelegate request)> _enqueue;
private readonly CompositeDisposable _disposable = new CompositeDisposable();
public ProcessScheduler(
ILoggerFactory loggerFactory,
bool supportContentModified,
int? concurrency,
IScheduler scheduler
)
{
var concurrency1 = concurrency;
var logger = loggerFactory.CreateLogger<ProcessScheduler>();
var subject = new Subject<(RequestProcessType type, string name, SchedulerDelegate request)>();
_disposable.Add(subject);
_enqueue = subject;
var obs = Observable.Create<Unit>(
observer => {
var cd = new CompositeDisposable();
var observableQueue =
new BehaviorSubject<(RequestProcessType type, ReplaySubject<IObservable<Unit>> observer, Subject<Unit>? contentModifiedSource)>(
( RequestProcessType.Serial, new ReplaySubject<IObservable<Unit>>(TimeSpan.FromSeconds(6), Scheduler.Immediate), supportContentModified ? new Subject<Unit>() : null )
);
cd.Add(
subject.Subscribe(
item => {
if (observableQueue.Value.type != item.type)
{
logger.LogDebug("Swapping from {From} to {To}", observableQueue.Value.type, item.type);
if (supportContentModified && observableQueue.Value.type == RequestProcessType.Parallel)
{
logger.LogDebug("Cancelling any outstanding requests (switch from parallel to serial)");
observableQueue.Value.contentModifiedSource?.OnNext(Unit.Default);
observableQueue.Value.contentModifiedSource?.OnCompleted();
}
logger.LogDebug("Completing existing request process type {Type}", observableQueue.Value.type);
observableQueue.Value.observer.OnCompleted();
observableQueue.OnNext(( item.type, new ReplaySubject<IObservable<Unit>>(TimeSpan.FromSeconds(6), Scheduler.Immediate), supportContentModified ? new Subject<Unit>() : null ));
}
logger.LogDebug("Queueing {Type}:{Name} request for processing", item.type, item.name);
observableQueue.Value.observer.OnNext(
HandleRequest(item.name, item.request(observableQueue.Value.contentModifiedSource ?? Observable.Never<Unit>(), scheduler))
);
}
)
);
cd.Add(
observableQueue
.Select(
item => {
var (type, replay, _) = item;
if (type == RequestProcessType.Serial)
{
// logger.LogDebug("Changing to serial processing");
return replay.Concat();
}
if (concurrency1.HasValue)
{
// logger.LogDebug("Changing to parallel processing with concurrency of {Concurrency}", concurrency1.Value);
return replay.Merge(concurrency1.Value);
}
// logger.LogDebug("Changing to parallel processing with concurrency of {Concurrency}", "Unlimited");
return replay.Merge();
}
)
.Concat()
.Subscribe(observer)
);
return cd;
}
);
_disposable.Add(
obs
.ObserveOn(scheduler)
.Subscribe(_ => { })
);
IObservable<Unit> HandleRequest(string name, IObservable<Unit> request)
{
return request
.Catch<Unit, RequestCancelledException>(
ex => {
logger.LogDebug(ex, "Request {Name} was explicitly cancelled", name);
return Observable.Empty<Unit>();
}
)
.Catch<Unit, ContentModifiedException>(
ex => {
logger.LogDebug(ex, "Request {Name} was cancelled, due to content being modified", name);
return Observable.Empty<Unit>();
}
)
.Catch<Unit, OperationCanceledException>(
ex => {
logger.LogDebug(ex, "Request {Name} was cancelled, due to timeout", name);
return Observable.Empty<Unit>();
}
)
.Catch<Unit, Exception>(
ex => {
logger.LogCritical(Events.UnhandledException, ex, "Unhandled exception executing {Name}", name);
return Observable.Empty<Unit>();
}
)
// .Do(v => {
// logger.LogDebug("Request {Name} was processed", name);
// }, (ex) => {
// logger.LogCritical(Events.UnhandledException, ex, "Request {Name} encountered and unhandled exception", name);
// }, () => {
// logger.LogDebug("Request {Name} was completed", name);
// })
;
}
}
public void Add(RequestProcessType type, string name, SchedulerDelegate request) => _enqueue.OnNext(( type, name, request ));
public void Dispose() => _disposable.Dispose();
}
}