-
Notifications
You must be signed in to change notification settings - Fork 6
/
OpenPort.hx
145 lines (124 loc) · 3.66 KB
/
OpenPort.hx
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
package tink.tcp;
import tink.streams.Stream;
import tink.streams.IdealStream;
import tink.tcp.Handler;
using tink.io.Sink;
using tink.CoreApi;
typedef Session = {
var incoming(default, never):Incoming;
var sink(default, never):IdealSink;
function destroy():Void;
}
class Limitter {
var queue:List<Lazy<Future<Noise>>> = new List();
public var queued(get, never):Int;
inline function get_queued()
return queue.length;
public var running(default, null):Int = 0;
public var maxQueued:Int = 0;
public var maxRunning:Int = 0x100000;
public function new() {}
function next() {
if (running < maxRunning)
switch queue.pop() {
case null:
case v:
running++;
v.get().handle(function () {
running--;
next();
});
}
}
public function run<Result>(f:Void->Future<Result>):Option<Future<Result>>
return
if (running + queued >= maxQueued + maxRunning)
None;
else
Some(Future.async(function (cb) {
queue.add(function () {
var ret = f();
ret.handle(cb);
return ret.map(function (_) return Noise);
});
next();
}));
}
typedef Scheduler = {
function run<Result>(f:Void->Future<Result>):Option<Future<Result>>;
function clear():Void;
}
class OpenPort {
public var port(default, null):Int;
var _shutdown:Void->Promise<Noise>;
var trigger:FutureTrigger<Handler> = Future.trigger();
var handler:Future<Handler>;
var scheduler:Scheduler;
static function justRun<R>(f:Void->Future<R>) return Some(f());
public function new(accepted:Signal<Session>, port:Int, ?scheduler:Scheduler) {
this.port = port;
this.handler = trigger;
this.scheduler = switch scheduler {
case null: { run: justRun, clear: function () {} };
case v: v;
}
accepted.handle(handleSession);
}
var running:Array<Session> = [];
function handleSession(s:Session)
switch this.scheduler.run(function () return
this.handler.flatMap(function (handler) {
return handler.handle(s.incoming).flatMap(function (out) {
return out.stream.pipeTo(s.sink, { end: true }).flatMap(function (p):Promise<Noise> return switch p {
case AllWritten | SinkEnded(_, { depleted: true }):
(
if (out.allowHalfOpen) s.incoming.closed
else Future.sync(Noise)
);
case SinkEnded(_, _):
new Error('Outgoing stream to ${s.incoming.from} closed unexpectedly');
});
});
})
) {
case None: s.destroy();
case Some(f):
running.push(s);
f.handle(function () {
if (running != null)
switch running.indexOf(s) {
case -1:
case v:
var last = running.pop();
if (last != s)
running[v] = last;
}
s.destroy();
});
}
public function setHandler(handler:Handler)
return
if (running == null) true;
else {
switch [trigger, handler] {
case [null, null]:
this.handler = this.trigger = Future.trigger();
case [null, v]:
this.handler = Future.sync(v);
case [v, null]:
case [t, h]:
this.trigger = null;
t.trigger(h);
}
false;
}
public function shutdown(?hard:Bool):Promise<Bool> {
this.scheduler = { run: function (_) return None, clear: function () {} };
if (hard) {
for (s in this.running)
s.destroy();
this.running = null;
}
return true;
}
}