Add mechanism to avoid overloading database server when it's restarting.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/sqlclient/trunk@27024 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2008-11-12 05:52:38 +00:00
parent 603579d3c0
commit ded3a0c6a8
3 changed files with 49 additions and 2 deletions

View file

@ -1,3 +1,10 @@
2008-11-12 Richard Frith-Macdonald <rfm@gnu.org>
* SQLClient.h:
* SQLClient.m:
Add support for tracking the number of consecutive connection failures
and imposing a delay between connection attempts.
2008-07-19 Nicola Pero <nicola.pero@meta-innovation.com>
* configure.ac: Documented the --with-additional-include=,

View file

@ -374,12 +374,15 @@ extern unsigned SQLClientTimeTick();
* Timestamp of last operation.<br />
* Maintained by -simpleExecute: -simpleQuery:recordType:listType:
* and -cache:simpleQuery:recordType:listType:
* Also set for a failed connection attempt, but not reported by the
* -lastOperation method in that case.
*/
NSTimeInterval _lastOperation;
NSTimeInterval _duration;
unsigned int _debugging; /** The current debugging level */
GSCache *_cache; /** The cache for query results */
NSThread *_cacheThread; /** Thread for cache queries */
unsigned _connectFails; /** The count of connection failures */
}
/**
@ -529,7 +532,14 @@ extern unsigned SQLClientTimeTick();
* If the <em>connected</em> instance variable is NO, this method
* calls -backendConnect to ensure that there is a connection to the
* database server established. Returns the result.<br />
* Performs any necessary locking for thread safety.
* Performs any necessary locking for thread safety.<br />
* This method also counts the number of consecutive failed connection
* attempts. A delay is enforced between each connection attempt, with
* the length of the delay growing with each failure. This ensures
* that applications which fail to deal with connection failures, and
* just keep trying to reconnect, will not overload the system/server.<br />
* The maximum delay is 30 seconds, so when the database server is restarted,
* the application can reconnect reasonably quickly.
*/
- (BOOL) connect;
@ -712,6 +722,11 @@ extern unsigned SQLClientTimeTick();
*/
- (NSString*) quotef: (NSString*)fmt, ...;
/**
* Convert a big (64 bit) integer to a string suitable for use in an SQL query.
*/
- (NSString*) quoteBigInteger: (int64_t)i;
/**
* Convert a 'C' string to a string suitable for use in an SQL query
* by using -quoteString: to convert it to a literal string format.<br />

View file

@ -1141,13 +1141,33 @@ static unsigned int maxConnections = 8;
[lock lock];
if (connected == NO)
{
if (_connectFails > 1)
{
NSTimeInterval delay;
NSTimeInterval elapsed;
/* If we have repeated connection failures, we enforce a
* delay fo up to 30 seconds between connection attempts
* to avoid overloading the system with too frequent
* connection attempts.
*/
delay = (_connectFails < 30) ? _connectFails : 30;
elapsed = GSTickerTimeNow() - _lastOperation;
if (elapsed < delay)
{
[NSThread sleepForTimeInterval: delay - elapsed];
}
}
NS_DURING
{
[self backendConnect];
_connectFails = 0;
}
NS_HANDLER
{
[lock unlock];
_lastOperation = GSTickerTimeNow();
_connectFails++;
[localException raise];
}
NS_ENDHANDLER
@ -1347,7 +1367,7 @@ static unsigned int maxConnections = 8;
- (NSDate*) lastOperation
{
if (_lastOperation > 0.0)
if (_lastOperation > 0.0 && _connectFails == 0)
{
return [NSDate dateWithTimeIntervalSinceReferenceDate: _lastOperation];
}
@ -1494,6 +1514,11 @@ static unsigned int maxConnections = 8;
return quoted;
}
- (NSString*) quoteBigInteger: (int64_t)i
{
return [NSString stringWithFormat: @"%lld", i];
}
- (NSString*) quoteCString: (const char *)s
{
NSString *str;