Skip to content

Commit

Permalink
Merge remote-tracking branch 'sequelpro/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
abhibeckert committed Mar 10, 2017
2 parents 9c5357a + 7c56b11 commit 833d872
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 39 deletions.
1 change: 0 additions & 1 deletion Frameworks/SPMySQLFramework/Source/SPMySQL Private APIs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
- (void)_disconnect;
- (void)_updateConnectionVariables;
- (void)_restoreConnectionVariables;
- (BOOL)_checkConnectionIfNecessary;
- (void)_validateThreadSetup;
+ (void)_removeThreadVariables:(NSNotification *)aNotification;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct {
MYSQL *mySQLConnection;
volatile BOOL *keepAlivePingThreadActivePointer;
volatile BOOL *keepAliveLastPingSuccessPointer;
void *parentId;
} SPMySQLConnectionPingDetails;

@interface SPMySQLConnection (Ping_and_KeepAlive)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#import "SPMySQL Private APIs.h"
#import "Locking.h"
#import <pthread.h>
#include <stdio.h>

@implementation SPMySQLConnection (Ping_and_KeepAlive)

Expand Down Expand Up @@ -85,7 +86,7 @@ - (void)_threadedKeepAlive
}

keepAliveThread = [NSThread currentThread];
[keepAliveThread setName:@"SPMySQL connection keepalive monitor thread"];
[keepAliveThread setName:[NSString stringWithFormat:@"SPMySQL connection keepalive monitor thread (id=%p)", self]];

// If the maximum number of ping failures has been reached, determine whether to reconnect.
if (keepAliveLastPingBlocked || keepAlivePingFailures >= 3) {
Expand Down Expand Up @@ -158,6 +159,7 @@ - (BOOL)_pingConnectionUsingLoopDelay:(NSUInteger)loopDelay
pingDetails->mySQLConnection = mySQLConnection;
pingDetails->keepAliveLastPingSuccessPointer = &keepAliveLastPingSuccess;
pingDetails->keepAlivePingThreadActivePointer = &keepAlivePingThreadActive;
pingDetails->parentId = self;

// Create a pthread for the ping
pthread_t keepAlivePingThread_t;
Expand Down Expand Up @@ -216,9 +218,11 @@ - (BOOL)_pingConnectionUsingLoopDelay:(NSUInteger)loopDelay
*/
void _backgroundPingTask(void *ptr)
{
pthread_setname_np("SPMySQL _backgroundPingTask() worker thread");

SPMySQLConnectionPingDetails *pingDetails = (SPMySQLConnectionPingDetails *)ptr;

char threadNameBuf[80];
snprintf(threadNameBuf, sizeof(threadNameBuf), "SPMySQL _backgroundPingTask() worker thread (id=%p)", pingDetails->parentId);
pthread_setname_np(threadNameBuf);

// Set up a cleanup routine
pthread_cleanup_push(_pingThreadCleanup, pingDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ - (NSString *)escapeString:(NSString *)theString includingQuotes:(BOOL)includeQu
// Ensure per-thread variables are set up
[self _validateThreadSetup];

if (![self _checkConnectionIfNecessary]) return nil;
if (![self checkConnectionIfNecessary]) return nil;

// Perform a lossy conversion to bytes, using NSData to do the hard work. Preserves
// nul characters correctly.
Expand Down Expand Up @@ -259,7 +259,7 @@ - (id)queryString:(NSString *)theQueryString usingEncoding:(NSStringEncoding)the
[self _validateThreadSetup];

// Check the connection if necessary, returning nil if the state couldn't be validated
if (![self _checkConnectionIfNecessary]) return nil;
if (![self checkConnectionIfNecessary]) return nil;

// Determine whether a maximum query size needs to be restored from a previous query
if (queryActionShouldRestoreMaxQuerySize != NSNotFound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ - (SPMySQLResult *)listProcesses
if (state != SPMySQLConnected) return nil;

// Check the connection if appropriate
if (![self _checkConnectionIfNecessary]) return nil;
if (![self checkConnectionIfNecessary]) return nil;

// Lock the connection before using it
[self _lockConnection];
Expand Down Expand Up @@ -153,7 +153,7 @@ - (BOOL)killQueryOnThreadID:(unsigned long)theThreadID

- (BOOL)serverShutdown
{
if([self _checkConnectionIfNecessary]) {
if([self checkConnectionIfNecessary]) {
[self _lockConnection];
// Ensure per-thread variables are set up
[self _validateThreadSetup];
Expand Down
1 change: 1 addition & 0 deletions Frameworks/SPMySQLFramework/Source/SPMySQLConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
- (BOOL)isConnected;
- (BOOL)isConnectedViaSSL;
- (BOOL)checkConnection;
- (BOOL)checkConnectionIfNecessary;
- (double)timeConnected;
- (BOOL)userTriggeredDisconnect;

Expand Down
60 changes: 31 additions & 29 deletions Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ - (BOOL)isConnectedViaSSL
*
* WARNING: This method may return NO if the current thread is cancelled!
* You MUST check the isCancelled flag before using the result!
*
* NOTE: In general -checkConnectionIfNecessary should be used instead!
*/
- (BOOL)checkConnection
{
Expand Down Expand Up @@ -374,6 +376,35 @@ - (BOOL)checkConnection
return connectionVerified;
}

/**
* If thirty seconds have passed since the last time the connection was
* used, check the connection.
* This minimises the impact of continuous additional connection checks -
* each of which requires a round trip to the server - but handles most
* network issues.
* Returns whether the connection is considered still valid.
*
* WARNING: This method may return NO if the current thread is cancelled!
* You MUST check the isCancelled flag before using the result!
*/
- (BOOL)checkConnectionIfNecessary
{

// If the connection has been dropped in the background, trigger a
// reconnect and return the success state here
if (state == SPMySQLConnectionLostInBackground) {
return [self _reconnectAllowingRetries:YES];
}

// If the connection was recently used, return success
if (_elapsedSecondsSinceAbsoluteTime(lastConnectionUsedTime) < 30) {
return YES;
}

// Otherwise check the connection
return [self checkConnection];
}

/**
* Retrieve the time elapsed since the connection was established, in seconds.
* This time is retrieved in a monotonically increasing fashion and is high
Expand Down Expand Up @@ -1042,35 +1073,6 @@ - (void)_restoreConnectionVariables
[self setEncodingUsesLatin1Transport:encodingUsesLatin1Transport];
}

/**
* If thirty seconds have passed since the last time the connection was
* used, check the connection.
* This minimises the impact of continuous additional connection checks -
* each of which requires a round trip to the server - but handles most
* network issues.
* Returns whether the connection is considered still valid.
*
* WARNING: This method may return NO if the current thread is cancelled!
* You MUST check the isCancelled flag before using the result!
*/
- (BOOL)_checkConnectionIfNecessary
{

// If the connection has been dropped in the background, trigger a
// reconnect and return the success state here
if (state == SPMySQLConnectionLostInBackground) {
return [self _reconnectAllowingRetries:YES];
}

// If the connection was recently used, return success
if (_elapsedSecondsSinceAbsoluteTime(lastConnectionUsedTime) < 30) {
return YES;
}

// Otherwise check the connection
return [self checkConnection];
}

/**
* Ensure that the thread this method is called on has been registered for
* use with MySQL. MySQL requires thread-specific variables for safe
Expand Down
2 changes: 1 addition & 1 deletion Source/SPDatabaseStructure.m
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ - (BOOL)_ensureConnectionUnsafe
if (!mySQLConnection || !delegate) return NO;

// Check the connection state
if ([mySQLConnection isConnected] && [mySQLConnection checkConnection]) return YES;
if ([mySQLConnection isConnected] && [mySQLConnection checkConnectionIfNecessary]) return YES;

// the result of checkConnection may be meaningless if the thread was cancelled during execution. (issue #2353)
if([[NSThread currentThread] isCancelled]) return NO;
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Sequel Pro <img alt="Logo" src="https://sequelpro.com/images/logo.png" align="right" height="50">
==========

Sequel Pro is a fast, easy-to-use Mac database management application for working with MySQL databases.
Sequel Pro is a fast, easy-to-use Mac database management application for working with MySQL & MariaDB databases.

You can find more details on our website: [sequelpro.com](http://sequelpro.com)

Expand Down

0 comments on commit 833d872

Please sign in to comment.