@netifyjs/netify 1.0.0-alpha.3 → 1.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/package.json +1 -1
- package/src/bootstrap/client/index.ts +1 -1
- package/src/bootstrap/dispatcher/index.ts +7 -0
- package/src/bootstrap/reconnect/index.ts +14 -4
- package/src/bootstrap/server/index.ts +10 -4
- package/src/buffer/index.ts +229 -88
- package/src/buffer/quantizer/index.ts +16 -3
- package/src/channel/context/index.ts +18 -11
- package/src/channel/handler/index.ts +1 -1
- package/src/channel/index.ts +30 -7
- package/src/channel/outbound/index.ts +28 -12
- package/src/channel/pipeline/index.ts +45 -5
- package/src/channel/queue/index.ts +3 -2
- package/src/channel/resolved/index.ts +13 -3
- package/src/channel/server/index.ts +19 -10
- package/src/cluster/index.ts +73 -32
- package/src/codec/binary/index.ts +19 -0
- package/src/codec/decoder/index.ts +223 -83
- package/src/codec/delimiter/index.ts +95 -37
- package/src/codec/encoder/index.ts +30 -5
- package/src/codec/fixed/index.ts +5 -1
- package/src/codec/http/aggregator/index.ts +45 -16
- package/src/codec/http/decoder/object/index.ts +156 -40
- package/src/codec/http/decoder/request/index.ts +6 -2
- package/src/codec/http/encoder/object/index.ts +2 -7
- package/src/codec/http/encoder/request/index.ts +5 -0
- package/src/codec/http/headers/index.ts +45 -2
- package/src/codec/http/message/full/request/index.ts +1 -1
- package/src/codec/http/message/full/response/index.ts +1 -1
- package/src/codec/http/message/request/index.ts +14 -1
- package/src/codec/http/message/response/index.ts +1 -1
- package/src/codec/http/version/index.ts +3 -0
- package/src/codec/length/index.ts +66 -44
- package/src/codec/message/decoder/index.ts +18 -1
- package/src/codec/message/encoder/index.ts +85 -8
- package/src/codec/prepender/index.ts +4 -1
- package/src/codec/string/decoder/index.ts +6 -1
- package/src/codec/websocket/decoder/index.ts +78 -3
- package/src/codec/websocket/encoder/index.ts +90 -6
- package/src/codec/websocket/frame/base/index.ts +19 -10
- package/src/codec/websocket/frame/text/index.ts +2 -2
- package/src/codec/websocket/protocol/index.ts +12 -0
- package/src/datagram/bootstrap/index.ts +1 -1
- package/src/datagram/transport/index.ts +30 -1
- package/src/handler/flush/index.ts +25 -5
- package/src/handler/idle/index.ts +120 -26
- package/src/handler/logging/index.ts +13 -1
- package/src/handler/metrics/index.ts +49 -16
- package/src/handler/offload/index.ts +7 -0
- package/src/handler/timeout/read/index.ts +1 -1
- package/src/handler/timeout/write/index.ts +104 -23
- package/src/handler/traffic/index.ts +251 -38
- package/src/index.ts +1 -0
- package/src/reliable/cipher/index.ts +49 -5
- package/src/reliable/cookie/index.ts +64 -2
- package/src/reliable/handler/index.ts +11 -15
- package/src/reliable/index.ts +7 -1
- package/src/reliable/lane/index.ts +277 -58
- package/src/reliable/peer/index.ts +125 -38
- package/src/serve/bootstrap/index.ts +82 -135
- package/src/serve/channel/index.ts +84 -2
- package/src/serve/gate/index.ts +215 -0
- package/src/serve/index.ts +6 -2
- package/src/serve/outbox/index.ts +63 -2
- package/src/serve/transport/index.ts +33 -3
- package/src/serve/writer/index.ts +4 -3
- package/src/shard/gateway/index.ts +102 -29
- package/src/shard/link/index.ts +161 -175
- package/src/worker/group/index.ts +231 -13
- package/src/worker/pool/index.ts +100 -19
- package/src/worker/service/index.ts +9 -1
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ByteBuf } from '../../buffer/index.ts'
|
|
2
|
-
import { ChannelGroup } from '../../channel/group/index.ts'
|
|
3
2
|
import type { Channel } from '../../channel/index.ts'
|
|
4
3
|
import type { ShardLink } from '../link/index.ts'
|
|
5
4
|
|
|
@@ -26,7 +25,12 @@ export class Gateway {
|
|
|
26
25
|
/**
|
|
27
26
|
* This shard's subscribers, by topic
|
|
28
27
|
*/
|
|
29
|
-
private readonly
|
|
28
|
+
private readonly topics: Map<string, Set<Channel>>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Topics each open channel is on
|
|
32
|
+
*/
|
|
33
|
+
private readonly channels: Map<Channel, Set<string>>
|
|
30
34
|
|
|
31
35
|
/**
|
|
32
36
|
* Channels written to since the last flush
|
|
@@ -39,18 +43,29 @@ export class Gateway {
|
|
|
39
43
|
private readonly flusher: () => void
|
|
40
44
|
|
|
41
45
|
/**
|
|
42
|
-
*
|
|
46
|
+
* Closed yet
|
|
47
|
+
*/
|
|
48
|
+
private closed: boolean
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Listens for publishes from other shards
|
|
43
52
|
* @param link the shard's link
|
|
44
53
|
*/
|
|
45
54
|
public constructor(link: ShardLink) {
|
|
55
|
+
if (link.listens(Gateway.topic)) {
|
|
56
|
+
throw new Error('Link already has a gateway')
|
|
57
|
+
}
|
|
58
|
+
|
|
46
59
|
this.link = link
|
|
47
|
-
this.
|
|
60
|
+
this.topics = new Map<string, Set<Channel>>()
|
|
61
|
+
this.channels = new Map<Channel, Set<string>>()
|
|
48
62
|
this.touched = new Set<Channel>()
|
|
49
63
|
this.flusher = (): void => {
|
|
50
|
-
for (const channel of this.touched)
|
|
64
|
+
for (const channel of this.touched) channel.flushEncoded()
|
|
51
65
|
this.touched.clear()
|
|
52
66
|
}
|
|
53
67
|
|
|
68
|
+
this.closed = false
|
|
54
69
|
link.on(Gateway.topic, (data: unknown): void => {
|
|
55
70
|
const [topic, bytes] = data as Publication
|
|
56
71
|
this.deliver(topic, bytes)
|
|
@@ -63,13 +78,18 @@ export class Gateway {
|
|
|
63
78
|
* @param topic any name, like a zone id
|
|
64
79
|
*/
|
|
65
80
|
public subscribe(channel: Channel, topic: string): void {
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
81
|
+
const joined = this.channels.get(channel) ?? this.watch(channel)
|
|
82
|
+
if (joined.has(topic)) return
|
|
83
|
+
|
|
84
|
+
joined.add(topic)
|
|
85
|
+
|
|
86
|
+
const members = this.topics.get(topic)
|
|
87
|
+
if (members === undefined) {
|
|
88
|
+
this.topics.set(topic, new Set<Channel>([channel]))
|
|
89
|
+
return
|
|
70
90
|
}
|
|
71
91
|
|
|
72
|
-
|
|
92
|
+
members.add(channel)
|
|
73
93
|
}
|
|
74
94
|
|
|
75
95
|
/**
|
|
@@ -78,11 +98,9 @@ export class Gateway {
|
|
|
78
98
|
* @param topic the topic
|
|
79
99
|
*/
|
|
80
100
|
public unsubscribe(channel: Channel, topic: string): boolean {
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
101
|
+
const removed = this.channels.get(channel)?.delete(topic) ?? false
|
|
102
|
+
if (removed) this.leave(channel, topic)
|
|
83
103
|
|
|
84
|
-
const removed = group.remove(channel)
|
|
85
|
-
if (group.size === 0) this.groups.delete(topic)
|
|
86
104
|
return removed
|
|
87
105
|
}
|
|
88
106
|
|
|
@@ -92,7 +110,7 @@ export class Gateway {
|
|
|
92
110
|
* @param topic the topic
|
|
93
111
|
*/
|
|
94
112
|
public isSubscribed(channel: Channel, topic: string): boolean {
|
|
95
|
-
return this.
|
|
113
|
+
return this.channels.get(channel)?.has(topic) ?? false
|
|
96
114
|
}
|
|
97
115
|
|
|
98
116
|
/**
|
|
@@ -100,42 +118,97 @@ export class Gateway {
|
|
|
100
118
|
* @param topic the topic
|
|
101
119
|
*/
|
|
102
120
|
public subscriberCount(topic: string): number {
|
|
103
|
-
return this.
|
|
121
|
+
return this.topics.get(topic)?.size ?? 0
|
|
104
122
|
}
|
|
105
123
|
|
|
106
124
|
/**
|
|
107
|
-
*
|
|
125
|
+
* Topics with a local channel on them
|
|
126
|
+
*/
|
|
127
|
+
public get topicCount(): number {
|
|
128
|
+
return this.topics.size
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Copies the bytes to every local subscriber
|
|
108
133
|
* @param topic the topic
|
|
109
134
|
* @param bytes a whole frame, skipping the pipelines
|
|
110
135
|
*/
|
|
111
136
|
public publish(topic: string, bytes: Uint8Array | ByteBuf): number {
|
|
112
|
-
const shared =
|
|
137
|
+
const shared = new Uint8Array(
|
|
138
|
+
bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes,
|
|
139
|
+
)
|
|
113
140
|
|
|
114
|
-
this.link.broadcast(Gateway.topic, [topic, shared])
|
|
141
|
+
if (this.link.isOpen) this.link.broadcast(Gateway.topic, [topic, shared])
|
|
115
142
|
return this.deliver(topic, shared)
|
|
116
143
|
}
|
|
117
144
|
|
|
118
145
|
/**
|
|
119
|
-
*
|
|
146
|
+
* Stops listening and frees the link
|
|
147
|
+
*/
|
|
148
|
+
public close(): void {
|
|
149
|
+
if (this.closed) return
|
|
150
|
+
this.closed = true
|
|
151
|
+
|
|
152
|
+
this.link.off(Gateway.topic)
|
|
153
|
+
this.topics.clear()
|
|
154
|
+
this.channels.clear()
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Starts tracking a channel
|
|
159
|
+
* @param channel a connection on this shard
|
|
160
|
+
*/
|
|
161
|
+
private watch(channel: Channel): Set<string> {
|
|
162
|
+
const joined = new Set<string>()
|
|
163
|
+
this.channels.set(channel, joined)
|
|
164
|
+
|
|
165
|
+
void channel.closed.then((): void => {
|
|
166
|
+
this.forget(channel)
|
|
167
|
+
})
|
|
168
|
+
return joined
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Takes a closed channel off everything
|
|
173
|
+
* @param channel just closed
|
|
174
|
+
*/
|
|
175
|
+
private forget(channel: Channel): void {
|
|
176
|
+
const joined = this.channels.get(channel)
|
|
177
|
+
if (joined === undefined) return
|
|
178
|
+
|
|
179
|
+
this.channels.delete(channel)
|
|
180
|
+
for (const topic of joined) this.leave(channel, topic)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Takes a member off a topic
|
|
185
|
+
* @param channel who's leaving
|
|
186
|
+
* @param topic what it leaves
|
|
187
|
+
*/
|
|
188
|
+
private leave(channel: Channel, topic: string): void {
|
|
189
|
+
const members = this.topics.get(topic)
|
|
190
|
+
if (members === undefined) return
|
|
191
|
+
|
|
192
|
+
members.delete(channel)
|
|
193
|
+
if (members.size === 0) this.topics.delete(topic)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Writes to this shard's subscribers
|
|
120
198
|
* @param topic the topic
|
|
121
199
|
* @param bytes the frame
|
|
122
200
|
*/
|
|
123
201
|
private deliver(topic: string, bytes: Uint8Array): number {
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
if (group.size === 0) {
|
|
128
|
-
this.groups.delete(topic)
|
|
129
|
-
return 0
|
|
130
|
-
}
|
|
202
|
+
const members = this.topics.get(topic)
|
|
203
|
+
if (members === undefined) return 0
|
|
131
204
|
|
|
132
205
|
if (this.touched.size === 0) queueMicrotask(this.flusher)
|
|
133
206
|
|
|
134
|
-
for (const channel of
|
|
207
|
+
for (const channel of members) {
|
|
135
208
|
channel.writeEncodedSync(bytes)
|
|
136
209
|
this.touched.add(channel)
|
|
137
210
|
}
|
|
138
211
|
|
|
139
|
-
return
|
|
212
|
+
return members.size
|
|
140
213
|
}
|
|
141
214
|
}
|