mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-16 05:05:00 +00:00
File IO Rate Limiting
feature: adds rate limiting for FileIO
This commit is contained in:
@@ -15,13 +15,18 @@ namespace PepperDash.Essentials.Core
|
|||||||
static CCriticalSection fileLock = new CCriticalSection();
|
static CCriticalSection fileLock = new CCriticalSection();
|
||||||
public delegate void GotFileEventHandler(object sender, FileEventArgs e);
|
public delegate void GotFileEventHandler(object sender, FileEventArgs e);
|
||||||
public static event GotFileEventHandler GotFileEvent;
|
public static event GotFileEventHandler GotFileEvent;
|
||||||
|
private static Dictionary<string, CTimer> WriteTimers;
|
||||||
|
public const long WriteTimeout = 5000;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the full file info from a path/filename, can include wildcards.
|
/// Get the full file info from a path/filename, can include wildcards.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
///
|
///
|
||||||
|
static FileIO()
|
||||||
|
{
|
||||||
|
WriteTimers = new Dictionary<string, CTimer>();
|
||||||
|
}
|
||||||
|
|
||||||
public static FileInfo[] GetFiles(string fileName)
|
public static FileInfo[] GetFiles(string fileName)
|
||||||
{
|
{
|
||||||
@@ -170,7 +175,8 @@ namespace PepperDash.Essentials.Core
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock retrying");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -190,6 +196,41 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resets (or starts) the write timer
|
||||||
|
/// </summary>
|
||||||
|
static void ResetTimer(string data, string filePath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (WriteTimers.ContainsKey(filePath))
|
||||||
|
{
|
||||||
|
WriteTimers[filePath].Stop();
|
||||||
|
WriteTimers[filePath] = null;
|
||||||
|
WriteTimers.Remove(filePath);
|
||||||
|
}
|
||||||
|
WriteTimers.Add(filePath, new CTimer((o) =>
|
||||||
|
{
|
||||||
|
Thread _WriteFileThread;
|
||||||
|
_WriteFileThread = new Thread((O) => _WriteFileMethod(data, filePath), null, Thread.eThreadStartOptions.CreateSuspended);
|
||||||
|
_WriteFileThread.Priority = Thread.eThreadPriority.LowestPriority;
|
||||||
|
_WriteFileThread.Start();
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WriteFile Thread");
|
||||||
|
WriteTimers.Remove(filePath);
|
||||||
|
}, WriteTimeout));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Debug.Console(0, "FileIO write timer has been reset.");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO ResetTimer failed: \r{0}", e);
|
||||||
|
data = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -202,11 +243,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// <param name="filePath"></param>
|
/// <param name="filePath"></param>
|
||||||
public static void WriteDataToFile(string data, string filePath)
|
public static void WriteDataToFile(string data, string filePath)
|
||||||
{
|
{
|
||||||
Thread _WriteFileThread;
|
ResetTimer(data, filePath);
|
||||||
_WriteFileThread = new Thread((O) => _WriteFileMethod(data, filePath), null, Thread.eThreadStartOptions.CreateSuspended);
|
|
||||||
_WriteFileThread.Priority = Thread.eThreadPriority.LowestPriority;
|
|
||||||
_WriteFileThread.Start();
|
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WriteFile Thread");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,6 +278,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
fileLock.Leave();
|
fileLock.Leave();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Notice, "File Written to : '{0}'", filePath);
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user