Permalink
Browse files

Eliminate delay in restarting CouchDB when app resumes

* Instead of restarting the entire Erlang VM, just restart the couch_httpd server, which is very fast.
* On wake, check whether the listener socket is still alive, and only restart if it's not.

Change-Id: I00dcd8cf23ca5d9895c07f7f61e698926df1315a
Reviewed-on: http://review.couchbase.org/11017
Tested-by: Farshid Ghods <farshid.ghods@gmail.com>
Reviewed-by: Chris Anderson <jchris@couchbase.com>
  • Loading branch information...
1 parent 5f9793d commit 8fbac9419d89da37585803c4ee7e4a8d062d6598 @snej snej committed Nov 18, 2011
Showing with 17 additions and 1 deletion.
  1. +17 −1 Framework/Classes/CouchbaseMobile.m
@@ -192,11 +192,27 @@ - (BOOL)start
return YES;
}
+- (BOOL)isServerRunning {
+ int port = [_serverURL.port intValue];
+ if (!port)
+ return NO;
+ struct sockaddr_in addr = {sizeof(struct sockaddr_in), AF_INET, htons(port), {0}};
+ int sockfd = socket(AF_INET,SOCK_STREAM, 0);
+ int result = connect(sockfd,(struct sockaddr*) &addr, sizeof(addr));
+ int connect_errno = errno;
+ close(sockfd);
+ if (_logLevel >= 2 && result != 0)
+ NSLog(@"Couchbase: Server not responding (errno=%i)", connect_errno);
+ return result == 0;
+}
+
- (void) maybeRestart {
- if (_autoRestart) [self restart];
+ if (_autoRestart && _serverURL && ![self isServerRunning])
+ [self restart];
}
- (void) restart {
+ _timeStarted = CFAbsoluteTimeGetCurrent();
[[NSNotificationCenter defaultCenter]
postNotificationName:kInternalRestartCouchNotification object:nil];
}

0 comments on commit 8fbac94

Please sign in to comment.