Skip to content

Commit 976326c

Browse files
committed
some server settings for network send length
1 parent 83155b2 commit 976326c

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

src/Backends/SwarmSwarmBackend.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ await RunWithSession(async () =>
571571
// TODO: This will require separate remote sessions per-user for multiuser support
572572
await HttpClient.PostJson($"{Address}/API/InterruptAll", new() { ["session_id"] = Session, ["other_sessions"] = false }, RequestAdapter());
573573
}
574-
JObject response = await websocket.ReceiveJson(1024 * 1024 * 1024, true);
574+
JObject response = await websocket.ReceiveJson(Utilities.ExtraLargeMaxReceive, true);
575575
if (response is not null)
576576
{
577577
AutoThrowException(response);

src/BuiltinExtensions/ComfyUIBackend/ComfyUIAPIAbstractBackend.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ async Task doInterruptNow()
343343
await doInterruptNow();
344344
return;
345345
}
346-
Task<byte[]> getData = socket.ReceiveData(1024 * 1024 * 1024, Program.GlobalProgramCancel);
346+
Task<byte[]> getData = socket.ReceiveData(Utilities.ExtraLargeMaxReceive, Program.GlobalProgramCancel);
347347
Task t = await Task.WhenAny(getData, interruptTask);
348348
if (t == interruptTask)
349349
{

src/Core/Settings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ public string GetExternalUrl()
233233

234234
[ConfigComment("How many entries in an X-Forwarded-For header to trust.\nDefaults to 3.\nSet to 0 to not trust any forwarded-for.")]
235235
public int MaxXForwardedFor = 3;
236+
237+
[ConfigComment("Maximum megabytes that can be sent as a single message from a client to the Swarm server.\nSet this lower to limit above, set this higher to allow very large file uploads.")]
238+
public int MaxNetworkRequestMegabytes = 200;
236239
}
237240

238241
/// <summary>Settings related to file paths.</summary>

src/Utils/Utilities.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,11 @@ public static Task WhenAny(params Task[] tasks)
298298
return Task.WhenAny(tasks);
299299
}
300300

301+
/// <summary>Effectively-unlimited max receive length, for internal transfers. Set to 100 gigabytes.</summary>
302+
public static long ExtraLargeMaxReceive = 100L * 1024 * 1024 * 1024;
303+
301304
/// <summary>Receive raw binary data from a WebSocket.</summary>
302-
public static async Task<byte[]> ReceiveData(this WebSocket socket, int maxBytes, CancellationToken limit)
305+
public static async Task<byte[]> ReceiveData(this WebSocket socket, long maxBytes, CancellationToken limit)
303306
{
304307
byte[] buffer = new byte[8192];
305308
using MemoryStream ms = new();
@@ -318,14 +321,14 @@ public static async Task<byte[]> ReceiveData(this WebSocket socket, int maxBytes
318321
}
319322

320323
/// <summary>Receive raw binary data from a WebSocket.</summary>
321-
public static async Task<byte[]> ReceiveData(this WebSocket socket, TimeSpan maxDuration, int maxBytes)
324+
public static async Task<byte[]> ReceiveData(this WebSocket socket, TimeSpan maxDuration, long maxBytes)
322325
{
323326
using CancellationTokenSource cancel = TimedCancel(maxDuration);
324327
return await ReceiveData(socket, maxBytes, cancel.Token);
325328
}
326329

327330
/// <summary>Receive JSON data from a WebSocket.</summary>
328-
public static async Task<JObject> ReceiveJson(this WebSocket socket, int maxBytes, bool nullOnEmpty = false)
331+
public static async Task<JObject> ReceiveJson(this WebSocket socket, long maxBytes, bool nullOnEmpty = false)
329332
{
330333
string raw = Encoding.UTF8.GetString(await ReceiveData(socket, maxBytes, Program.GlobalProgramCancel));
331334
if (nullOnEmpty && string.IsNullOrWhiteSpace(raw))
@@ -336,7 +339,7 @@ public static async Task<JObject> ReceiveJson(this WebSocket socket, int maxByte
336339
}
337340

338341
/// <summary>Receive JSON data from a WebSocket.</summary>
339-
public static async Task<JObject> ReceiveJson(this WebSocket socket, TimeSpan maxDuration, int maxBytes, bool nullOnEmpty = false)
342+
public static async Task<JObject> ReceiveJson(this WebSocket socket, TimeSpan maxDuration, long maxBytes, bool nullOnEmpty = false)
340343
{
341344
string raw = Encoding.UTF8.GetString(await ReceiveData(socket, maxDuration, maxBytes));
342345
if (nullOnEmpty && string.IsNullOrWhiteSpace(raw))

src/WebAPI/API.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void Error(string message)
5151
if (context.WebSockets.IsWebSocketRequest)
5252
{
5353
socket = await context.WebSockets.AcceptWebSocketAsync();
54-
input = await socket.ReceiveJson(TimeSpan.FromMinutes(1), 100 * 1024 * 1024); // TODO: Configurable limits
54+
input = await socket.ReceiveJson(TimeSpan.FromMinutes(1), Program.ServerSettings.Network.MaxNetworkRequestMegabytes * (1024 * 1024));
5555
}
5656
else if (context.Request.Method == "POST")
5757
{
@@ -61,7 +61,7 @@ void Error(string message)
6161
context.Response.Redirect("/Error/BasicAPI");
6262
return;
6363
}
64-
if (context.Request.ContentLength <= 0 || context.Request.ContentLength >= 100 * 1024 * 1024) // TODO: Configurable limits
64+
if (context.Request.ContentLength <= 0 || context.Request.ContentLength >= Program.ServerSettings.Network.MaxNetworkRequestMegabytes * (1024 * 1024))
6565
{
6666
Error($"Request has invalid content length: {context.Request.ContentLength}");
6767
context.Response.Redirect("/Error/BasicAPI");

src/WebAPI/T2IAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static int guessBatchSize(JObject input)
100100
int batchOffset = images * guessBatchSize(rawInput);
101101
while (!cancelTok.IsCancellationRequested)
102102
{
103-
byte[] rec = await socket.ReceiveData(1024 * 1024 * 1024, linked.Token);
103+
byte[] rec = await socket.ReceiveData(Program.ServerSettings.Network.MaxNetworkRequestMegabytes * (1024 * 1024), linked.Token);
104104
Volatile.Write(ref retain, true);
105105
if (socket.State != WebSocketState.Open || cancelTok.IsCancellationRequested || Volatile.Read(ref ended))
106106
{

0 commit comments

Comments
 (0)