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

Select a random port to debug on #553

Merged
merged 2 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}