Skip to content

Commit

Permalink
Support port-zero binding
Browse files Browse the repository at this point in the history
Summary: We should also support binding on a rando port

Reviewed By: zeyadsalloum

Differential Revision: D15159369

fbshipit-source-id: 2a12896863ea8c816558760ef69410d73fd8a3f6
  • Loading branch information
lawrencelomax authored and facebook-github-bot committed May 1, 2019
1 parent a2485b5 commit 15a45e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 3 additions & 2 deletions idb_companion/Configuration/FBIDBPortsConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#import "FBIDBPortsConfiguration.h"

static NSString *const GrpcPortKey = @"-grpc-port";

@implementation FBIDBPortsConfiguration

#pragma mark Initializers
Expand Down Expand Up @@ -37,8 +39,7 @@ - (in_port_t)debugserverPort

- (in_port_t)grpcPort
{
return [self.userDefaults integerForKey:@"-grpc-port"] ?: 10882;
return [self.userDefaults stringForKey:GrpcPortKey] ? [self.userDefaults integerForKey:GrpcPortKey] : 10882;
}


@end
13 changes: 9 additions & 4 deletions idb_companion/Server/FBGRPCServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ @interface FBGRPCServer ()
@property (nonatomic, strong, readonly) id<FBEventReporter> eventReporter;
@property (nonatomic, strong, readonly) FBMutableFuture<NSNull *> *serverTerminated;

@property (nonatomic, assign, readwrite) in_port_t selectedPort;

@end

using grpc::Server;
Expand Down Expand Up @@ -68,20 +70,23 @@ - (instancetype)initWithPorts:(FBIDBPortsConfiguration *)ports target:(id<FBiOST
dispatch_queue_t queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
FBMutableFuture<NSNull *> *serverStarted = FBMutableFuture.future;
dispatch_async(queue, ^(void){
[self.logger logFormat:@"Starting GRPC server on port %u", self.ports.grpcPort];
string server_address("0.0.0.0:" + std::to_string(self.ports.grpcPort));
FBIDBServiceHandler service = FBIDBServiceHandler(self.commandExecutor, self.target, self.eventReporter, self.ports);
int selectedPort = self.ports.grpcPort;
unique_ptr<Server> server(ServerBuilder()
.AddListeningPort(server_address, grpc::InsecureServerCredentials())
.AddListeningPort(server_address, grpc::InsecureServerCredentials(), &selectedPort)
.RegisterService(&service)
.SetResourceQuota(ResourceQuota("idb_resource.quota").SetMaxThreads(10))
.SetMaxReceiveMessageSize(16777216) // 16MB (16 * 1024 * 1024). Default is 4MB (4 * 1024 * 1024)
.BuildAndStart()
);
self.selectedPort = selectedPort;

[serverStarted resolveWithResult:NSNull.null];
[self.logger.info logFormat:@"Started GRPC server on port %u", self.ports.grpcPort];
[self.logger.info logFormat:@"Started GRPC server on port %u", selectedPort];
server->Wait();
[self.logger.info logFormat:@"GRPC server is no longer running on port %u", self.ports.grpcPort];
[self.logger.info logFormat:@"GRPC server is no longer running on port %u", selectedPort];
[self.serverTerminated resolveWithResult:NSNull.null];
});
return serverStarted;
Expand All @@ -99,7 +104,7 @@ - (NSString *)futureType

- (id)jsonSerializableRepresentation
{
return @{@"grpc_port": @(self.ports.grpcPort)};
return @{@"grpc_port": @(self.selectedPort)};
}

@end

0 comments on commit 15a45e9

Please sign in to comment.