Skip to content

Commit

Permalink
Increase RN devtools retry timeout
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javache authored and Facebook Github Bot 6 committed Apr 1, 2016
1 parent dc3836a commit b00c77a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Libraries/Devtools/setupDevtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function setupDevtools() {
function handleClose() {
if (!hasClosed) {
hasClosed = true;
setTimeout(setupDevtools, 200);
setTimeout(setupDevtools, 2000);
closeListeners.forEach(fn => fn());
}
}
Expand Down
92 changes: 39 additions & 53 deletions Libraries/WebSocket/RCTSRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];
Expand All @@ -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];
}];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -795,7 +814,7 @@ - (void)handleCloseWithData:(NSData *)data;
- (void)_disconnect;
{
[self assertOnWorkQueue];
RCTSRFastLog(@"Trying to disconnect");
RCTSRLog(@"Trying to disconnect");
_closeWhenFinishedWriting = YES;
[self _pumpWriting];
}
Expand Down Expand Up @@ -1241,9 +1260,7 @@ - (void)_pumpScanner;
return;
}

while ([self _innerPumpScanner]) {

}
while ([self _innerPumpScanner]) {}

_isPumping = NO;
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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];

Expand All @@ -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;
}
});
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Libraries/WebSocket/RCTWebSocketExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion Libraries/WebSocket/RCTWebSocketManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -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")

/**
Expand Down
2 changes: 1 addition & 1 deletion React/Base/RCTModuleData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions React/Executors/RCTJSCExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion React/Modules/RCTAsyncLocalStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b00c77a

Please sign in to comment.