From b00c77af8066cf74f05ccaca2b08c8952e1ae8a6 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 1 Apr 2016 07:01:51 -0700 Subject: [PATCH] Increase RN devtools retry timeout Summary:The 200ms timeout was causing resource issues and causing a lot of overhead when you're not running the devtools, since it will basically create a new socket every 200ms. Also clean up the way we do logging so it's completely compiled out in prod, and standardize all the names we use for threading to lowercase react. Reviewed By: frantic Differential Revision: D3115975 fb-gh-sync-id: e6e51c0621d8e9fc4eadb864acd678b8b5d322a1 fbshipit-source-id: e6e51c0621d8e9fc4eadb864acd678b8b5d322a1 --- Libraries/Devtools/setupDevtools.js | 2 +- Libraries/WebSocket/RCTSRWebSocket.m | 92 +++++++++------------- Libraries/WebSocket/RCTWebSocketExecutor.m | 2 +- Libraries/WebSocket/RCTWebSocketManager.m | 2 +- React/Base/RCTBatchedBridge.m | 2 +- React/Base/RCTModuleData.m | 2 +- React/Executors/RCTJSCExecutor.m | 4 +- React/Modules/RCTAsyncLocalStorage.m | 2 +- React/Modules/RCTUIManager.m | 2 +- 9 files changed, 48 insertions(+), 62 deletions(-) diff --git a/Libraries/Devtools/setupDevtools.js b/Libraries/Devtools/setupDevtools.js index 4f75be24f10a46..e55810d8c493ad 100644 --- a/Libraries/Devtools/setupDevtools.js +++ b/Libraries/Devtools/setupDevtools.js @@ -40,7 +40,7 @@ function setupDevtools() { function handleClose() { if (!hasClosed) { hasClosed = true; - setTimeout(setupDevtools, 200); + setTimeout(setupDevtools, 2000); closeListeners.forEach(fn => fn()); } } diff --git a/Libraries/WebSocket/RCTSRWebSocket.m b/Libraries/WebSocket/RCTSRWebSocket.m index fe02d069768db7..54ebbc552c9a0e 100644 --- a/Libraries/WebSocket/RCTSRWebSocket.m +++ b/Libraries/WebSocket/RCTSRWebSocket.m @@ -48,8 +48,27 @@ typedef NS_ENUM(NSInteger, RCTSROpCode) { static NSString *const RCTSRWebSocketAppendToSecKeyString = @"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; -static inline int32_t validate_dispatch_data_partial_string(NSData *data); -static inline void RCTSRFastLog(NSString *format, ...); +//#define RCTSR_ENABLE_LOG +#ifdef RCTSR_ENABLE_LOG +#define RCTSRLog(format...) RCTLogInfo(format) +#else +#define RCTSRLog(...) do { } while (0) +#endif + +// This is a hack, and probably not optimal +static inline int32_t validate_dispatch_data_partial_string(NSData *data) +{ + static const int maxCodepointSize = 3; + + for (int i = 0; i < maxCodepointSize; i++) { + NSString *str = [[NSString alloc] initWithBytesNoCopy:(char *)data.bytes length:data.length - i encoding:NSUTF8StringEncoding freeWhenDone:NO]; + if (str) { + return (int32_t)data.length - i; + } + } + + return -1; +} @interface NSData (RCTSRWebSocket) @@ -306,7 +325,7 @@ - (void)_RCTSR_commonInit; _consumerStopped = YES; _webSocketVersion = 13; - _workQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); + _workQueue = dispatch_queue_create("com.facebook.react.SRWebSocket", DISPATCH_QUEUE_SERIAL); // Going to set a specific on the queue so we can validate we're on the work queue dispatch_queue_set_specific(_workQueue, (__bridge void *)self, (__bridge void *)_workQueue, NULL); @@ -411,7 +430,7 @@ - (void)_HTTPHeadersDidFinish; NSInteger responseCode = CFHTTPMessageGetResponseStatusCode(_receivedHTTPHeaders); if (responseCode >= 400) { - RCTSRFastLog(@"Request failed with response code %d", responseCode); + RCTSRLog(@"Request failed with response code %ld", responseCode); [self _failWithError:[NSError errorWithDomain:RCTSRWebSocketErrorDomain code:2132 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"received bad response code from server %ld", (long)responseCode], RCTSRHTTPResponseErrorKey:@(responseCode)}]]; return; } @@ -455,7 +474,7 @@ - (void)_readHTTPHeader; CFHTTPMessageAppendBytes(_receivedHTTPHeaders, (const UInt8 *)data.bytes, data.length); if (CFHTTPMessageIsHeaderComplete(_receivedHTTPHeaders)) { - RCTSRFastLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders))); + RCTSRLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders))); [socket _HTTPHeadersDidFinish]; } else { [socket _readHTTPHeader]; @@ -465,7 +484,7 @@ - (void)_readHTTPHeader; - (void)didConnect { - RCTSRFastLog(@"Connected"); + RCTSRLog(@"Connected"); CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (__bridge CFURLRef)_url, kCFHTTPVersion1_1); // Set host first so it defaults @@ -587,7 +606,7 @@ - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason; self.readyState = RCTSR_CLOSING; - RCTSRFastLog(@"Closing with code %d reason %@", code, reason); + RCTSRLog(@"Closing with code %ld reason %@", code, reason); if (wasConnecting) { [self _disconnect]; @@ -644,7 +663,7 @@ - (void)_failWithError:(NSError *)error; self.readyState = RCTSR_CLOSED; _selfRetain = nil; - RCTSRFastLog(@"Failing with error %@", error.localizedDescription); + RCTSRLog(@"Failing with error %@", error.localizedDescription); [self _disconnect]; } @@ -702,7 +721,7 @@ - (void)handlePing:(NSData *)pingData; - (void)handlePong:(NSData *)pongData; { - RCTSRFastLog(@"Received pong"); + RCTSRLog(@"Received pong"); [self _performDelegateBlock:^{ if ([self.delegate respondsToSelector:@selector(webSocket:didReceivePong:)]) { [self.delegate webSocket:self didReceivePong:pongData]; @@ -712,7 +731,7 @@ - (void)handlePong:(NSData *)pongData; - (void)_handleMessage:(id)message { - RCTSRFastLog(@"Received message"); + RCTSRLog(@"Received message"); [self _performDelegateBlock:^{ [self.delegate webSocket:self didReceiveMessage:message]; }]; @@ -758,7 +777,7 @@ - (void)handleCloseWithData:(NSData *)data; size_t dataSize = data.length; __block uint16_t closeCode = 0; - RCTSRFastLog(@"Received close frame"); + RCTSRLog(@"Received close frame"); if (dataSize == 1) { // TODO: handle error @@ -795,7 +814,7 @@ - (void)handleCloseWithData:(NSData *)data; - (void)_disconnect; { [self assertOnWorkQueue]; - RCTSRFastLog(@"Trying to disconnect"); + RCTSRLog(@"Trying to disconnect"); _closeWhenFinishedWriting = YES; [self _pumpWriting]; } @@ -1241,9 +1260,7 @@ - (void)_pumpScanner; return; } - while ([self _innerPumpScanner]) { - - } + while ([self _innerPumpScanner]) {} _isPumping = NO; } @@ -1333,7 +1350,6 @@ - (void)_sendFrameWithOpcode:(RCTSROpCode)opcode data:(id)data; - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode; { if (_secure && !_pinnedCertFound && (eventCode == NSStreamEventHasBytesAvailable || eventCode == NSStreamEventHasSpaceAvailable)) { - NSArray *sslCerts = _urlRequest.RCTSR_SSLPinnedCertificates; if (sslCerts) { SecTrustRef secTrust = (__bridge SecTrustRef)[aStream propertyForKey:(__bridge id)kCFStreamPropertySSLPeerTrust]; @@ -1364,10 +1380,11 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode; } } + assert(_workQueue != NULL); dispatch_async(_workQueue, ^{ switch (eventCode) { case NSStreamEventOpenCompleted: { - RCTSRFastLog(@"NSStreamEventOpenCompleted %@", aStream); + RCTSRLog(@"NSStreamEventOpenCompleted %@", aStream); if (self.readyState >= RCTSR_CLOSING) { return; } @@ -1382,7 +1399,7 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode; } case NSStreamEventErrorOccurred: { - RCTSRFastLog(@"NSStreamEventErrorOccurred %@ %@", aStream, [aStream.streamError copy]); + RCTSRLog(@"NSStreamEventErrorOccurred %@ %@", aStream, [aStream.streamError copy]); // TODO: specify error better! [self _failWithError:aStream.streamError]; _readBufferOffset = 0; @@ -1393,7 +1410,7 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode; case NSStreamEventEndEncountered: { [self _pumpScanner]; - RCTSRFastLog(@"NSStreamEventEndEncountered %@", aStream); + RCTSRLog(@"NSStreamEventEndEncountered %@", aStream); if (aStream.streamError) { [self _failWithError:aStream.streamError]; } else { @@ -1419,7 +1436,7 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode; } case NSStreamEventHasBytesAvailable: { - RCTSRFastLog(@"NSStreamEventHasBytesAvailable %@", aStream); + RCTSRLog(@"NSStreamEventHasBytesAvailable %@", aStream); const int bufferSize = 2048; uint8_t buffer[bufferSize]; @@ -1441,13 +1458,13 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode; } case NSStreamEventHasSpaceAvailable: { - RCTSRFastLog(@"NSStreamEventHasSpaceAvailable %@", aStream); + RCTSRLog(@"NSStreamEventHasSpaceAvailable %@", aStream); [self _pumpWriting]; break; } default: - RCTSRFastLog(@"(default) %@", aStream); + RCTSRLog(@"(default) %@", aStream); break; } }); @@ -1557,37 +1574,6 @@ - (NSString *)RCTSR_origin; @end -//#define RCTSR_ENABLE_LOG - -static inline void RCTSRFastLog(NSString *format, ...) -{ -#ifdef RCTSR_ENABLE_LOG - __block va_list arg_list; - va_start (arg_list, format); - - NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:arg_list]; - - va_end(arg_list); - - RCTLogInfo(@"[RCTSR] %@", formattedString); -#endif -} - -// This is a hack, and probably not optimal -static inline int32_t validate_dispatch_data_partial_string(NSData *data) -{ - static const int maxCodepointSize = 3; - - for (int i = 0; i < maxCodepointSize; i++) { - NSString *str = [[NSString alloc] initWithBytesNoCopy:(char *)data.bytes length:data.length - i encoding:NSUTF8StringEncoding freeWhenDone:NO]; - if (str) { - return (int32_t)data.length - i; - } - } - - return -1; -} - static _RCTSRRunLoopThread *networkThread = nil; static NSRunLoop *networkRunLoop = nil; diff --git a/Libraries/WebSocket/RCTWebSocketExecutor.m b/Libraries/WebSocket/RCTWebSocketExecutor.m index de26e8a91857f2..7b78d2942bd10c 100644 --- a/Libraries/WebSocket/RCTWebSocketExecutor.m +++ b/Libraries/WebSocket/RCTWebSocketExecutor.m @@ -55,7 +55,7 @@ - (void)setUp _url = [RCTConvert NSURL:URLString]; } - _jsQueue = dispatch_queue_create("com.facebook.React.WebSocketExecutor", DISPATCH_QUEUE_SERIAL); + _jsQueue = dispatch_queue_create("com.facebook.react.WebSocketExecutor", DISPATCH_QUEUE_SERIAL); _socket = [[RCTSRWebSocket alloc] initWithURL:_url]; _socket.delegate = self; _callbacks = [NSMutableDictionary new]; diff --git a/Libraries/WebSocket/RCTWebSocketManager.m b/Libraries/WebSocket/RCTWebSocketManager.m index 7288dda09765bc..eaf67d78e20ddc 100644 --- a/Libraries/WebSocket/RCTWebSocketManager.m +++ b/Libraries/WebSocket/RCTWebSocketManager.m @@ -133,7 +133,7 @@ - (instancetype)init { if ((self = [super init])) { _sockets = [NSMutableDictionary new]; - _queue = dispatch_queue_create("com.facebook.React.WebSocketManager", DISPATCH_QUEUE_SERIAL); + _queue = dispatch_queue_create("com.facebook.react.WebSocketManager", DISPATCH_QUEUE_SERIAL); } return self; } diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 86326ee7bbebf1..2f8610e34ad556 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -26,7 +26,7 @@ #define RCTAssertJSThread() \ RCTAssert(![NSStringFromClass([_javaScriptExecutor class]) isEqualToString:@"RCTJSCExecutor"] || \ - [[[NSThread currentThread] name] isEqualToString:@"com.facebook.React.JavaScript"], \ + [[[NSThread currentThread] name] isEqualToString:RCTJSCThreadName], \ @"This method must be called on JS thread") /** diff --git a/React/Base/RCTModuleData.m b/React/Base/RCTModuleData.m index d29441b40f112d..8daaba591f6561 100644 --- a/React/Base/RCTModuleData.m +++ b/React/Base/RCTModuleData.m @@ -157,7 +157,7 @@ - (void)setUpMethodQueue if (!_methodQueue && _bridge.valid) { // Create new queue (store queueName, as it isn't retained by dispatch_queue) - _queueName = [NSString stringWithFormat:@"com.facebook.React.%@Queue", self.name]; + _queueName = [NSString stringWithFormat:@"com.facebook.react.%@Queue", self.name]; _methodQueue = dispatch_queue_create(_queueName.UTF8String, DISPATCH_QUEUE_SERIAL); // assign it to the module diff --git a/React/Executors/RCTJSCExecutor.m b/React/Executors/RCTJSCExecutor.m index 4fa6b60a2a6093..d5c1730881247b 100644 --- a/React/Executors/RCTJSCExecutor.m +++ b/React/Executors/RCTJSCExecutor.m @@ -27,7 +27,7 @@ #import "RCTRedBox.h" #import "RCTSourceCode.h" -NSString *const RCTJSCThreadName = @"com.facebook.React.JavaScript"; +NSString *const RCTJSCThreadName = @"com.facebook.react.JavaScript"; NSString *const RCTJavaScriptContextCreatedNotification = @"RCTJavaScriptContextCreatedNotification"; @@ -193,7 +193,7 @@ - (instancetype)init _javaScriptThread = [[NSThread alloc] initWithTarget:[self class] selector:@selector(runRunLoopThread) object:nil]; - _javaScriptThread.name = @"com.facebook.React.JavaScript"; + _javaScriptThread.name = RCTJSCThreadName; if ([_javaScriptThread respondsToSelector:@selector(setQualityOfService:)]) { [_javaScriptThread setQualityOfService:NSOperationQualityOfServiceUserInteractive]; diff --git a/React/Modules/RCTAsyncLocalStorage.m b/React/Modules/RCTAsyncLocalStorage.m index 46d062224d022e..f3f6a208a4dc23 100644 --- a/React/Modules/RCTAsyncLocalStorage.m +++ b/React/Modules/RCTAsyncLocalStorage.m @@ -117,7 +117,7 @@ static dispatch_queue_t RCTGetMethodQueue() static dispatch_queue_t queue; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - queue = dispatch_queue_create("com.facebook.React.AsyncLocalStorageQueue", DISPATCH_QUEUE_SERIAL); + queue = dispatch_queue_create("com.facebook.react.AsyncLocalStorageQueue", DISPATCH_QUEUE_SERIAL); }); return queue; } diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index 1b6b988dfc272d..8c0a927a0d8471 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -323,7 +323,7 @@ - (void)setBridge:(RCTBridge *)bridge - (dispatch_queue_t)methodQueue { if (!_shadowQueue) { - const char *queueName = "com.facebook.React.ShadowQueue"; + const char *queueName = "com.facebook.react.ShadowQueue"; if ([NSOperation instancesRespondToSelector:@selector(qualityOfService)]) { dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0);