Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #553 from Microsoft/roblou/randomPort
Browse files Browse the repository at this point in the history
Select a random port to debug on
  • Loading branch information
roblourens authored Oct 25, 2016
2 parents e7cbef0 + 48a0b56 commit cbc5565
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { basename, dirname } from 'path';
import { spawn, ChildProcess } from 'child_process';
import { Client, RPCConnection } from 'json-rpc2';
import { getBinPath } from '../goPath';
import {random} from './../util';

require('console-stamp')(console);

Expand Down Expand Up @@ -214,7 +215,7 @@ class Delve {

function connectClient(port: number, host: string) {
// Add a slight delay to avoid issues on Linux with
// Delve failing calls made shortly after connection.
// Delve failing calls made shortly after connection.
setTimeout(() => {
let client = Client.$create(port, host);
client.connectSocket((err, conn) => {
Expand Down Expand Up @@ -320,7 +321,7 @@ class GoDebugSession extends DebugSession {
protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
// Launch the Delve debugger on the program
let remotePath = args.remotePath || '';
let port = args.port || 2345;
let port = args.port || random(2000, 50000);
let host = args.host || '127.0.0.1';

if (remotePath.length > 0) {
Expand Down
8 changes: 6 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function parseFilePrelude(text: string): Prelude {
}

// Takes a Go function signature like:
// (foo, bar string, baz number) (string, string)
// (foo, bar string, baz number) (string, string)
// and returns an array of parameter strings:
// ["foo", "bar string", "baz string"]
// Takes care of balancing parens so to not get confused by signatures like:
Expand Down Expand Up @@ -94,4 +94,8 @@ export function canonicalizeGOPATHPrefix(filename: string): string {
}
}
return filename;
}
}

export function random(low: number, high: number): number {
return Math.floor(Math.random() * (high - low) + low);
}

0 comments on commit cbc5565

Please sign in to comment.