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
This commit is contained in:
Pieter De Baets 2016-04-01 07:01:51 -07:00 committed by Facebook Github Bot 6
parent dc3836a9d3
commit b00c77af80
9 changed files with 48 additions and 62 deletions

View File

@ -40,7 +40,7 @@ function setupDevtools() {
function handleClose() {
if (!hasClosed) {
hasClosed = true;
setTimeout(setupDevtools, 200);
setTimeout(setupDevtools, 2000);
closeListeners.forEach(fn => fn());
}
}

View File

@ -48,8 +48,27 @@ typedef struct {
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 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_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 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
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 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
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 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (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 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
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 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
self.readyState = RCTSR_CLOSED;
_selfRetain = nil;
RCTSRFastLog(@"Failing with error %@", error.localizedDescription);
RCTSRLog(@"Failing with error %@", error.localizedDescription);
[self _disconnect];
}
@ -702,7 +721,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (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 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)_handleMessage:(id)message
{
RCTSRFastLog(@"Received message");
RCTSRLog(@"Received message");
[self _performDelegateBlock:^{
[self.delegate webSocket:self didReceiveMessage:message];
}];
@ -758,7 +777,7 @@ static inline BOOL closeCodeIsValid(int closeCode)
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 @@ static inline BOOL closeCodeIsValid(int closeCode)
- (void)_disconnect;
{
[self assertOnWorkQueue];
RCTSRFastLog(@"Trying to disconnect");
RCTSRLog(@"Trying to disconnect");
_closeWhenFinishedWriting = YES;
[self _pumpWriting];
}
@ -1241,9 +1260,7 @@ static const char CRLFCRLFBytes[] = {'\r', '\n', '\r', '\n'};
return;
}
while ([self _innerPumpScanner]) {
}
while ([self _innerPumpScanner]) {}
_isPumping = NO;
}
@ -1333,7 +1350,6 @@ static const size_t RCTSRFrameHeaderOverhead = 32;
- (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 @@ static const size_t RCTSRFrameHeaderOverhead = 32;
}
}
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 @@ static const size_t RCTSRFrameHeaderOverhead = 32;
}
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 @@ static const size_t RCTSRFrameHeaderOverhead = 32;
case NSStreamEventEndEncountered: {
[self _pumpScanner];
RCTSRFastLog(@"NSStreamEventEndEncountered %@", aStream);
RCTSRLog(@"NSStreamEventEndEncountered %@", aStream);
if (aStream.streamError) {
[self _failWithError:aStream.streamError];
} else {
@ -1419,7 +1436,7 @@ static const size_t RCTSRFrameHeaderOverhead = 32;
}
case NSStreamEventHasBytesAvailable: {
RCTSRFastLog(@"NSStreamEventHasBytesAvailable %@", aStream);
RCTSRLog(@"NSStreamEventHasBytesAvailable %@", aStream);
const int bufferSize = 2048;
uint8_t buffer[bufferSize];
@ -1441,13 +1458,13 @@ static const size_t RCTSRFrameHeaderOverhead = 32;
}
case NSStreamEventHasSpaceAvailable: {
RCTSRFastLog(@"NSStreamEventHasSpaceAvailable %@", aStream);
RCTSRLog(@"NSStreamEventHasSpaceAvailable %@", aStream);
[self _pumpWriting];
break;
}
default:
RCTSRFastLog(@"(default) %@", aStream);
RCTSRLog(@"(default) %@", aStream);
break;
}
});
@ -1557,37 +1574,6 @@ static const size_t RCTSRFrameHeaderOverhead = 32;
@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;

View File

@ -55,7 +55,7 @@ RCT_EXPORT_MODULE()
_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];

View File

@ -133,7 +133,7 @@
{
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;
}

View File

@ -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")
/**

View File

@ -157,7 +157,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
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

View File

@ -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 @@ static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context)
_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];

View File

@ -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;
}

View File

@ -323,7 +323,7 @@ RCT_EXPORT_MODULE()
- (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);