Skip to content

Commit

Permalink
fix: Fetch web requests with big payloads dropping connection (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Dec 15, 2023
1 parent 31f624f commit 425889d
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/googleai_dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ class GoogleAIClient extends g.GoogleAIClient {
},
client: client ?? createDefaultHttpClient(),
);

@override
Future<http.BaseRequest> onRequest(final http.BaseRequest request) {
return onRequestHandler(request);
}
}
10 changes: 10 additions & 0 deletions packages/googleai_dart/lib/src/http_client/http_client_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(fetch.FetchClient(mode: fetch.RequestMode.cors));
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
// If the request if bigger than 60KiB set persistentConnection to false
// Ref: https://github.com/Zekfad/fetch_client#large-payload
if ((request.contentLength ?? 0) > 61440) {
request.persistentConnection = false;
}
return Future.value(request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(http.Client());
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
return Future.value(request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ import 'package:http/http.dart' as http;
http.Client createDefaultHttpClient() => throw UnsupportedError(
'Cannot create a client without dart:html or dart:io.',
);

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) =>
throw UnsupportedError('stub');
5 changes: 5 additions & 0 deletions packages/mistralai_dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class MistralAIClient extends g.MistralAIClient {
(final d) => ChatCompletionStreamResponse.fromJson(json.decode(d)),
);
}

@override
Future<http.BaseRequest> onRequest(final http.BaseRequest request) {
return onRequestHandler(request);
}
}

class _MistralAIStreamTransformer
Expand Down
10 changes: 10 additions & 0 deletions packages/mistralai_dart/lib/src/http_client/http_client_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(fetch.FetchClient(mode: fetch.RequestMode.cors));
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
// If the request if bigger than 60KiB set persistentConnection to false
// Ref: https://github.com/Zekfad/fetch_client#large-payload
if ((request.contentLength ?? 0) > 61440) {
request.persistentConnection = false;
}
return Future.value(request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(http.Client());
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
return Future.value(request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ import 'package:http/http.dart' as http;
http.Client createDefaultHttpClient() => throw UnsupportedError(
'Cannot create a client without dart:html or dart:io.',
);

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) =>
throw UnsupportedError('stub');
5 changes: 5 additions & 0 deletions packages/ollama_dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,9 @@ class OllamaClient extends g.OllamaClient {
),
);
}

@override
Future<http.BaseRequest> onRequest(final http.BaseRequest request) {
return onRequestHandler(request);
}
}
10 changes: 10 additions & 0 deletions packages/ollama_dart/lib/src/http_client/http_client_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(fetch.FetchClient(mode: fetch.RequestMode.cors));
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
// If the request if bigger than 60KiB set persistentConnection to false
// Ref: https://github.com/Zekfad/fetch_client#large-payload
if ((request.contentLength ?? 0) > 61440) {
request.persistentConnection = false;
}
return Future.value(request);
}
5 changes: 5 additions & 0 deletions packages/ollama_dart/lib/src/http_client/http_client_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(http.Client());
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
return Future.value(request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ import 'package:http/http.dart' as http;
http.Client createDefaultHttpClient() => throw UnsupportedError(
'Cannot create a client without dart:html or dart:io.',
);

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) =>
throw UnsupportedError('stub');

0 comments on commit 425889d

Please sign in to comment.