Skip to content

Commit

Permalink
Add write and read timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
oleghnidets committed Jul 7, 2023
1 parent 49dbc47 commit c6afc83
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions OHMySQL.podspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Be sure to run `pod lib lint OHMySQL.podspec' to ensure this is a
# Be sure to run `pod lib lint OHMySQL.podspec --allow-warnings' to ensure this is a
# valid spec before submitting.

Pod::Spec.new do |spec|
spec.name = 'OHMySQL'
spec.version = '3.2.0'
spec.version = '3.2.1'

spec.summary = 'The Objective-C wrapper for mysqlclient (MySQL C API)'
spec.description = <<-DESC
Expand Down
6 changes: 6 additions & 0 deletions OHMySQL/Sources/Models/OHMySQLConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ NS_SWIFT_NAME(MySQLConfiguration)
/// Used for establishing secure connections using SSL.
@property (nonatomic, copy, nullable, readonly) OHSSLConfig *sslConfig;

/// The timeout in seconds for each attempt to write to the server. There is a retry if necessary, so the total effective timeout value is two times the option value.
@property (nonatomic, copy, nullable) NSNumber *writeTimeout;

/// The timeout in seconds for each attempt to read from the server. There are retries if necessary, so the total effective timeout value is three times the option value. You can set the value so that a lost connection can be detected earlier than the TCP/IP Close Wait Timeout value of 10 minutes.
@property (nonatomic, copy, nullable) NSNumber *readTimeout;


/// Initializes and returns a newly allocated object.
/// - Parameters:
Expand Down
12 changes: 11 additions & 1 deletion OHMySQL/Sources/OHMySQLStoreCoordinator.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,24 @@ - (BOOL)connect {
@synchronized (self) {
mysql_library_init;

void *_mysql = mysql_init(NULL);
MYSQL *_mysql = mysql_init(NULL);
self.store = [[OHMySQLStore alloc] initWithMySQL:_mysql];

mysql_options(_mysql, MYSQL_OPT_COMPRESS, 0);
bool reconnect = 1;
mysql_options(_mysql, MYSQL_OPT_RECONNECT, &reconnect);
mysql_options(_mysql, MYSQL_OPT_PROTOCOL, &_protocol);

if (_configuration.writeTimeout && _configuration.writeTimeout.unsignedIntValue > 0) {
unsigned int writeTimeout = _configuration.writeTimeout.unsignedIntValue;
mysql_options(_mysql, MYSQL_OPT_WRITE_TIMEOUT, (void *)&writeTimeout);
}

if (_configuration.readTimeout && _configuration.readTimeout.unsignedIntValue > 0) {
unsigned int readTimeout = _configuration.readTimeout.unsignedIntValue;
mysql_options(_mysql, MYSQL_OPT_READ_TIMEOUT, (void *)&readTimeout);
}

OHSSLConfig *SSLconfig = self.configuration.sslConfig;
if (SSLconfig) {
// https://dev.mysql.com/doc/refman/5.7/en/mysql-options.html
Expand Down
2 changes: 2 additions & 0 deletions SampleProject/OHMySQLDemo/Models/PersistentCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ final class PersistentCoordinator {
dbName: databaseName,
port: 3306,
socket: "/tmp/mysql.sock")
configuration.writeTimeout = 15
configuration.readTimeout = 5

return MySQLStoreCoordinator(configuration: configuration)
}
Expand Down

0 comments on commit c6afc83

Please sign in to comment.