Skip to content

Commit 4862e8b

Browse files
committed
Networking retry on download error for models.
1 parent 078c3bd commit 4862e8b

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

gpt4all-chat/download.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void Download::downloadModel(const QString &modelFile)
108108
const QString error
109109
= QString("ERROR: Could not open temp file: %1 %2").arg(tempFile->fileName()).arg(modelFile);
110110
qWarning() << error;
111+
clearRetry(modelFile);
111112
ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadErrorRole, error);
112113
return;
113114
}
@@ -140,6 +141,7 @@ void Download::downloadModel(const QString &modelFile)
140141
QNetworkReply *modelReply = m_networkManager.get(request);
141142
connect(qApp, &QCoreApplication::aboutToQuit, modelReply, &QNetworkReply::abort);
142143
connect(modelReply, &QNetworkReply::downloadProgress, this, &Download::handleDownloadProgress);
144+
connect(modelReply, &QNetworkReply::errorOccurred, this, &Download::handleErrorOccurred);
143145
connect(modelReply, &QNetworkReply::finished, this, &Download::handleModelDownloadFinished);
144146
connect(modelReply, &QNetworkReply::readyRead, this, &Download::handleReadyRead);
145147
m_activeDownloads.insert(modelReply, tempFile);
@@ -254,13 +256,51 @@ void Download::parseReleaseJsonFile(const QByteArray &jsonData)
254256
emit releaseInfoChanged();
255257
}
256258

259+
bool Download::hasRetry(const QString &filename) const
260+
{
261+
return m_activeRetries.contains(filename);
262+
}
263+
264+
bool Download::shouldRetry(const QString &filename)
265+
{
266+
int retries = 0;
267+
if (m_activeRetries.contains(filename))
268+
retries = m_activeRetries.value(filename);
269+
270+
++retries;
271+
272+
// Allow up to ten retries for now
273+
if (retries < 10) {
274+
m_activeRetries.insert(filename, retries);
275+
return true;
276+
}
277+
278+
return false;
279+
}
280+
281+
void Download::clearRetry(const QString &filename)
282+
{
283+
m_activeRetries.remove(filename);
284+
}
285+
257286
void Download::handleErrorOccurred(QNetworkReply::NetworkError code)
258287
{
259288
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
260289
if (!modelReply)
261290
return;
262291

292+
// This occurs when the user explicitly cancels the download
293+
if (code == QNetworkReply::OperationCanceledError)
294+
return;
295+
263296
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
297+
if (shouldRetry(modelFilename)) {
298+
downloadModel(modelFilename);
299+
return;
300+
}
301+
302+
clearRetry(modelFilename);
303+
264304
const QString error
265305
= QString("ERROR: Network error occurred attempting to download %1 code: %2 errorString %3")
266306
.arg(modelFilename)
@@ -355,6 +395,7 @@ void HashAndSaveFile::hashAndSave(const QString &expectedHash, const QString &sa
355395
// but will only work if the destination is on the same filesystem
356396
if (tempFile->rename(saveFilePath)) {
357397
emit hashAndSaveFinished(true, QString(), tempFile, modelReply);
398+
ModelList::globalInstance()->updateModelsFromDirectory();
358399
return;
359400
}
360401

@@ -406,11 +447,15 @@ void Download::handleModelDownloadFinished()
406447
qWarning() << errorString;
407448
modelReply->deleteLater();
408449
tempFile->deleteLater();
409-
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadingRole, false);
410-
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, errorString);
450+
if (!hasRetry(modelFilename)) {
451+
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadingRole, false);
452+
ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, errorString);
453+
}
411454
return;
412455
}
413456

457+
clearRetry(modelFilename);
458+
414459
// The hash and save needs the tempFile closed
415460
tempFile->close();
416461

gpt4all-chat/download.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,15 @@ private Q_SLOTS:
7878
private:
7979
void parseReleaseJsonFile(const QByteArray &jsonData);
8080
QString incompleteDownloadPath(const QString &modelFile);
81+
bool hasRetry(const QString &filename) const;
82+
bool shouldRetry(const QString &filename);
83+
void clearRetry(const QString &filename);
8184

8285
HashAndSaveFile *m_hashAndSave;
8386
QMap<QString, ReleaseInfo> m_releaseMap;
8487
QNetworkAccessManager m_networkManager;
8588
QMap<QNetworkReply*, QFile*> m_activeDownloads;
89+
QHash<QString, int> m_activeRetries;
8690
QDateTime m_startTime;
8791

8892
private:

0 commit comments

Comments
 (0)