mirror of
https://github.com/abraunegg/onedrive
synced 2024-06-09 09:22:18 +02:00
Refine code changes
* Refine code changes from #2608 based on memory allocation and usage
This commit is contained in:
parent
2bb82f718f
commit
ec933af290
|
@ -30,6 +30,13 @@ class ClientSideFiltering {
|
||||||
this.appConfig = appConfig;
|
this.appConfig = appConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~this() {
|
||||||
|
object.destroy(appConfig);
|
||||||
|
object.destroy(paths);
|
||||||
|
object.destroy(fileMask);
|
||||||
|
object.destroy(directoryMask);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialise the required items
|
// Initialise the required items
|
||||||
bool initialise() {
|
bool initialise() {
|
||||||
// Log what is being done
|
// Log what is being done
|
||||||
|
|
16
src/config.d
16
src/config.d
|
@ -682,11 +682,21 @@ class ApplicationConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto file = File(filename, "r");
|
auto file = File(filename, "r");
|
||||||
scope(exit) file.close();
|
string lineBuffer;
|
||||||
scope(failure) file.close();
|
scope(exit) {
|
||||||
|
file.close();
|
||||||
|
object.destroy(file);
|
||||||
|
object.destroy(lineBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
scope(failure) {
|
||||||
|
file.close();
|
||||||
|
object.destroy(file);
|
||||||
|
object.destroy(lineBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (line; file.byLine()) {
|
foreach (line; file.byLine()) {
|
||||||
string lineBuffer = stripLeft(line).to!string;
|
lineBuffer = stripLeft(line).to!string;
|
||||||
if (lineBuffer.empty || lineBuffer[0] == ';' || lineBuffer[0] == '#') continue;
|
if (lineBuffer.empty || lineBuffer[0] == ';' || lineBuffer[0] == '#') continue;
|
||||||
auto c = lineBuffer.matchFirst(configRegex);
|
auto c = lineBuffer.matchFirst(configRegex);
|
||||||
if (c.empty) {
|
if (c.empty) {
|
||||||
|
|
|
@ -176,36 +176,7 @@ class CurlResponse {
|
||||||
|
|
||||||
class CurlEngine {
|
class CurlEngine {
|
||||||
|
|
||||||
__gshared CurlEngine[] curlEnginePool;
|
__gshared static CurlEngine[] curlEnginePool; // __gshared is used for thread-shared static variables
|
||||||
|
|
||||||
static CurlEngine get() {
|
|
||||||
synchronized(CurlEngine.classinfo) {
|
|
||||||
if (curlEnginePool.empty) {
|
|
||||||
return new CurlEngine;
|
|
||||||
} else {
|
|
||||||
CurlEngine curlEngine = curlEnginePool[$-1];
|
|
||||||
curlEnginePool.popBack();
|
|
||||||
return curlEngine;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static releaseAll() {
|
|
||||||
synchronized(CurlEngine.classinfo) {
|
|
||||||
foreach(curlEngine; curlEnginePool) {
|
|
||||||
curlEngine.shutdown();
|
|
||||||
object.destroy(curlEngine);
|
|
||||||
}
|
|
||||||
curlEnginePool = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void release() {
|
|
||||||
cleanUp();
|
|
||||||
synchronized(CurlEngine.classinfo) {
|
|
||||||
curlEnginePool ~= this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HTTP http;
|
HTTP http;
|
||||||
bool keepAlive;
|
bool keepAlive;
|
||||||
|
@ -213,16 +184,52 @@ class CurlEngine {
|
||||||
CurlResponse response;
|
CurlResponse response;
|
||||||
File uploadFile;
|
File uploadFile;
|
||||||
|
|
||||||
|
static CurlEngine getCurlInstance() {
|
||||||
|
synchronized (CurlEngine.classinfo) {
|
||||||
|
if (curlEnginePool.empty) {
|
||||||
|
return new CurlEngine; // Constructs a new CurlEngine with a fresh HTTP instance
|
||||||
|
} else {
|
||||||
|
CurlEngine curlEngine = curlEnginePool[$ - 1];
|
||||||
|
curlEnginePool = curlEnginePool[0 .. $ - 1];
|
||||||
|
return curlEngine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void releaseAll() {
|
||||||
|
synchronized (CurlEngine.classinfo) {
|
||||||
|
foreach (CurlEngine curlEngine; curlEnginePool) {
|
||||||
|
curlEngine.cleanUp(); // Cleanup instance by resetting values
|
||||||
|
curlEngine.shutdown(); // Assume proper cleanup of any resources used by HTTP
|
||||||
|
object.destroy(curlEngine);
|
||||||
|
}
|
||||||
|
curlEnginePool.length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup curlEnginePool
|
||||||
|
object.destroy(curlEnginePool);
|
||||||
|
}
|
||||||
|
|
||||||
this() {
|
this() {
|
||||||
http = HTTP();
|
http = HTTP(); // Directly initializes HTTP using its default constructor
|
||||||
response = null;
|
response = null; // Initialize as null
|
||||||
}
|
}
|
||||||
|
|
||||||
~this() {
|
~this() {
|
||||||
object.destroy(http);
|
// The destructor should only clean up resources owned directly by this instance
|
||||||
object.destroy(response);
|
// Avoid modifying or destroying shared/static resources here
|
||||||
if (uploadFile.isOpen())
|
if (uploadFile.isOpen())
|
||||||
uploadFile.close();
|
uploadFile.close();
|
||||||
|
|
||||||
|
// Cleanup curlEnginePool
|
||||||
|
object.destroy(curlEnginePool);
|
||||||
|
}
|
||||||
|
|
||||||
|
void release() {
|
||||||
|
cleanUp();
|
||||||
|
synchronized (CurlEngine.classinfo) {
|
||||||
|
curlEnginePool ~= this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialise(ulong dnsTimeout, ulong connectTimeout, ulong dataTimeout, ulong operationTimeout, int maxRedirects, bool httpsDebug, string userAgent, bool httpProtocol, ulong userRateLimit, ulong protocolVersion, bool keepAlive=true) {
|
void initialise(ulong dnsTimeout, ulong connectTimeout, ulong dataTimeout, ulong operationTimeout, int maxRedirects, bool httpsDebug, string userAgent, bool httpProtocol, ulong userRateLimit, ulong protocolVersion, bool keepAlive=true) {
|
||||||
|
|
|
@ -100,7 +100,8 @@ class LogBuffer {
|
||||||
// Use dnotify's functionality for GUI notifications, if GUI notifications is enabled
|
// Use dnotify's functionality for GUI notifications, if GUI notifications is enabled
|
||||||
version(Notifications) {
|
version(Notifications) {
|
||||||
try {
|
try {
|
||||||
auto n = new Notification("Log Notification", message, "IGNORED");
|
auto n = new Notification("OneDrive Client for Linux", message, "IGNORED");
|
||||||
|
//n.timeout = 5;
|
||||||
n.show();
|
n.show();
|
||||||
} catch (NotificationError e) {
|
} catch (NotificationError e) {
|
||||||
sendGUINotification = false;
|
sendGUINotification = false;
|
||||||
|
|
12
src/main.d
12
src/main.d
|
@ -1123,12 +1123,6 @@ void performStandardExitProcess(string scopeCaller = null) {
|
||||||
object.destroy(oneDriveWebhook);
|
object.destroy(oneDriveWebhook);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown the sync engine
|
|
||||||
if (syncEngineInstance !is null) {
|
|
||||||
addLogEntry("Shutdown Sync Engine instance", ["debug"]);
|
|
||||||
object.destroy(syncEngineInstance);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shutdown the client side filtering objects
|
// Shutdown the client side filtering objects
|
||||||
if (selectiveSync !is null) {
|
if (selectiveSync !is null) {
|
||||||
addLogEntry("Shutdown Client Side Filtering instance", ["debug"]);
|
addLogEntry("Shutdown Client Side Filtering instance", ["debug"]);
|
||||||
|
@ -1151,6 +1145,12 @@ void performStandardExitProcess(string scopeCaller = null) {
|
||||||
object.destroy(filesystemMonitor);
|
object.destroy(filesystemMonitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shutdown the sync engine
|
||||||
|
if (syncEngineInstance !is null) {
|
||||||
|
addLogEntry("Shutdown Sync Engine instance", ["debug"]);
|
||||||
|
object.destroy(syncEngineInstance);
|
||||||
|
}
|
||||||
|
|
||||||
// Shutdown the database
|
// Shutdown the database
|
||||||
if (itemDB !is null) {
|
if (itemDB !is null) {
|
||||||
addLogEntry("Shutdown Database instance", ["debug"]);
|
addLogEntry("Shutdown Database instance", ["debug"]);
|
||||||
|
|
|
@ -114,12 +114,16 @@ class OneDriveApi {
|
||||||
subscriptionUrl = appConfig.globalGraphEndpoint ~ "/v1.0/subscriptions";
|
subscriptionUrl = appConfig.globalGraphEndpoint ~ "/v1.0/subscriptions";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~this() {
|
||||||
|
object.destroy(curlEngine);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialise the OneDrive API class
|
// Initialise the OneDrive API class
|
||||||
bool initialise(bool keepAlive=true) {
|
bool initialise(bool keepAlive=true) {
|
||||||
// Initialise the curl engine
|
// Initialise the curl engine
|
||||||
this.keepAlive = keepAlive;
|
this.keepAlive = keepAlive;
|
||||||
if (curlEngine is null) {
|
if (curlEngine is null) {
|
||||||
curlEngine = CurlEngine.get();
|
curlEngine = CurlEngine.getCurlInstance();
|
||||||
curlEngine.initialise(appConfig.getValueLong("dns_timeout"), appConfig.getValueLong("connect_timeout"), appConfig.getValueLong("data_timeout"), appConfig.getValueLong("operation_timeout"), appConfig.defaultMaxRedirects, appConfig.getValueBool("debug_https"), appConfig.getValueString("user_agent"), appConfig.getValueBool("force_http_11"), appConfig.getValueLong("rate_limit"), appConfig.getValueLong("ip_protocol_version"), keepAlive);
|
curlEngine.initialise(appConfig.getValueLong("dns_timeout"), appConfig.getValueLong("connect_timeout"), appConfig.getValueLong("data_timeout"), appConfig.getValueLong("operation_timeout"), appConfig.defaultMaxRedirects, appConfig.getValueBool("debug_https"), appConfig.getValueString("user_agent"), appConfig.getValueBool("force_http_11"), appConfig.getValueLong("rate_limit"), appConfig.getValueLong("ip_protocol_version"), keepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -298,6 +298,13 @@ class SyncEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~this() {
|
||||||
|
processPool = null;
|
||||||
|
addLogEntry("processPool = null");
|
||||||
|
object.destroy(oneDriveApiInstance);
|
||||||
|
addLogEntry("object.destroy(oneDriveApiInstance)");
|
||||||
|
}
|
||||||
|
|
||||||
// Initialise the Sync Engine class
|
// Initialise the Sync Engine class
|
||||||
bool initialise() {
|
bool initialise() {
|
||||||
|
|
||||||
|
@ -519,7 +526,7 @@ class SyncEngine {
|
||||||
addLogEntry("Perform a Full Scan True-Up: " ~ to!string(appConfig.fullScanTrueUpRequired), ["debug"]);
|
addLogEntry("Perform a Full Scan True-Up: " ~ to!string(appConfig.fullScanTrueUpRequired), ["debug"]);
|
||||||
|
|
||||||
// Fetch the API response of /delta to track changes that were performed online
|
// Fetch the API response of /delta to track changes that were performed online
|
||||||
fetchOneDriveDeltaAPIResponse(null, null, null);
|
fetchOneDriveDeltaAPIResponse();
|
||||||
// Process any download activities or cleanup actions
|
// Process any download activities or cleanup actions
|
||||||
processDownloadActivities();
|
processDownloadActivities();
|
||||||
|
|
||||||
|
|
74
src/util.d
74
src/util.d
|
@ -201,42 +201,54 @@ Regex!char wild2regex(const(char)[] pattern) {
|
||||||
return regex(str, "i");
|
return regex(str, "i");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test Internet access to Microsoft OneDrive
|
// Test Internet access to Microsoft OneDrive using a simple HTTP HEAD request
|
||||||
bool testInternetReachability(ApplicationConfig appConfig) {
|
bool testInternetReachability(ApplicationConfig appConfig) {
|
||||||
CurlEngine curlEngine;
|
auto http = HTTP();
|
||||||
bool result = false;
|
http.url = "https://login.microsoftonline.com";
|
||||||
|
|
||||||
|
// Configure timeouts based on application configuration
|
||||||
|
http.dnsTimeout = dur!"seconds"(appConfig.getValueLong("dns_timeout"));
|
||||||
|
http.connectTimeout = dur!"seconds"(appConfig.getValueLong("connect_timeout"));
|
||||||
|
http.dataTimeout = dur!"seconds"(appConfig.getValueLong("data_timeout"));
|
||||||
|
http.operationTimeout = dur!"seconds"(appConfig.getValueLong("operation_timeout"));
|
||||||
|
|
||||||
|
// Set IP protocol version
|
||||||
|
http.handle.set(CurlOption.ipresolve, appConfig.getValueLong("ip_protocol_version"));
|
||||||
|
|
||||||
|
// Set HTTP method to HEAD for minimal data transfer
|
||||||
|
http.method = HTTP.Method.head;
|
||||||
|
|
||||||
|
// Execute the request and handle exceptions
|
||||||
try {
|
try {
|
||||||
// Use preconfigured object with all the correct http values assigned
|
addLogEntry("Attempting to contact Microsoft OneDrive Login Service");
|
||||||
curlEngine = CurlEngine.get();
|
http.perform();
|
||||||
curlEngine.initialise(appConfig.getValueLong("dns_timeout"), appConfig.getValueLong("connect_timeout"), appConfig.getValueLong("data_timeout"), appConfig.getValueLong("operation_timeout"), appConfig.defaultMaxRedirects, appConfig.getValueBool("debug_https"), appConfig.getValueString("user_agent"), appConfig.getValueBool("force_http_11"), appConfig.getValueLong("rate_limit"), appConfig.getValueLong("ip_protocol_version"));
|
|
||||||
|
|
||||||
// Configure the remaining items required
|
// Check response for HTTP status code
|
||||||
// URL to use
|
if (http.statusLine.code >= 200 && http.statusLine.code < 400) {
|
||||||
// HTTP connection test method
|
addLogEntry("Successfully reached Microsoft OneDrive Login Service");
|
||||||
|
} else {
|
||||||
curlEngine.connect(HTTP.Method.head, "https://login.microsoftonline.com");
|
addLogEntry("Failed to reach Microsoft OneDrive Login Service. HTTP status code: " ~ to!string(http.statusLine.code));
|
||||||
addLogEntry("Attempting to contact Microsoft OneDrive Login Service", ["debug"]);
|
throw new Exception("HTTP Request Failed with Status Code: " ~ to!string(http.statusLine.code));
|
||||||
curlEngine.http.perform();
|
|
||||||
addLogEntry("Shutting down HTTP engine as successfully reached OneDrive Login Service", ["debug"]);
|
|
||||||
result = true;
|
|
||||||
} catch (SocketException e) {
|
|
||||||
addLogEntry("HTTP Socket Issue", ["debug"]);
|
|
||||||
addLogEntry("Cannot connect to Microsoft OneDrive Login Service - Socket Issue");
|
|
||||||
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
|
|
||||||
} catch (CurlException e) {
|
|
||||||
addLogEntry("No Network Connection", ["debug"]);
|
|
||||||
addLogEntry("Cannot connect to Microsoft OneDrive Login Service - Network Connection Issue");
|
|
||||||
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown engine
|
http.shutdown();
|
||||||
curlEngine.http.shutdown();
|
return true;
|
||||||
curlEngine.releaseAll();
|
} catch (SocketException e) {
|
||||||
object.destroy(curlEngine);
|
addLogEntry("Cannot connect to Microsoft OneDrive Service - Socket Issue: " ~ e.msg);
|
||||||
curlEngine = null;
|
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
|
||||||
|
http.shutdown();
|
||||||
// Return test result
|
return false;
|
||||||
return result;
|
} catch (CurlException e) {
|
||||||
|
addLogEntry("Cannot connect to Microsoft OneDrive Service - Network Connection Issue: " ~ e.msg);
|
||||||
|
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
|
||||||
|
http.shutdown();
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
addLogEntry("Unexpected error occurred: " ~ e.toString());
|
||||||
|
displayOneDriveErrorMessage(e.toString(), getFunctionName!({}));
|
||||||
|
http.shutdown();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retry Internet access test to Microsoft OneDrive
|
// Retry Internet access test to Microsoft OneDrive
|
||||||
|
|
Loading…
Reference in a new issue