Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
nni
Commits
442342cb
Unverified
Commit
442342cb
authored
Jul 21, 2021
by
liuzhe-lz
Committed by
GitHub
Jul 21, 2021
Browse files
Fix bug in IP detection (#3952)
parent
d8127e02
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
36 deletions
+26
-36
ts/nni_manager/common/utils.ts
ts/nni_manager/common/utils.ts
+14
-36
ts/nni_manager/test/common/getIpv4Address.test.ts
ts/nni_manager/test/common/getIpv4Address.test.ts
+12
-0
No files found.
ts/nni_manager/common/utils.ts
View file @
442342cb
...
@@ -13,10 +13,10 @@ import * as fs from 'fs';
...
@@ -13,10 +13,10 @@ import * as fs from 'fs';
import
*
as
net
from
'
net
'
;
import
*
as
net
from
'
net
'
;
import
*
as
os
from
'
os
'
;
import
*
as
os
from
'
os
'
;
import
*
as
path
from
'
path
'
;
import
*
as
path
from
'
path
'
;
import
*
as
timersPromises
from
'
timers/promises
'
;
import
*
as
lockfile
from
'
lockfile
'
;
import
*
as
lockfile
from
'
lockfile
'
;
import
{
Deferred
}
from
'
ts-deferred
'
;
import
{
Deferred
}
from
'
ts-deferred
'
;
import
{
Container
}
from
'
typescript-ioc
'
;
import
{
Container
}
from
'
typescript-ioc
'
;
import
*
as
util
from
'
util
'
;
import
*
as
glob
from
'
glob
'
;
import
*
as
glob
from
'
glob
'
;
import
{
Database
,
DataStore
}
from
'
./datastore
'
;
import
{
Database
,
DataStore
}
from
'
./datastore
'
;
...
@@ -49,39 +49,15 @@ function getExperimentsInfoPath(): string {
...
@@ -49,39 +49,15 @@ function getExperimentsInfoPath(): string {
return
path
.
join
(
os
.
homedir
(),
'
nni-experiments
'
,
'
.experiment
'
);
return
path
.
join
(
os
.
homedir
(),
'
nni-experiments
'
,
'
.experiment
'
);
}
}
function
mkDirP
(
dirPath
:
string
):
Promise
<
void
>
{
async
function
mkDirP
(
dirPath
:
string
):
Promise
<
void
>
{
const
deferred
:
Deferred
<
void
>
=
new
Deferred
<
void
>
();
await
fs
.
promises
.
mkdir
(
dirPath
,
{
recursive
:
true
});
fs
.
exists
(
dirPath
,
(
exists
:
boolean
)
=>
{
if
(
exists
)
{
deferred
.
resolve
();
}
else
{
const
parent
:
string
=
path
.
dirname
(
dirPath
);
mkDirP
(
parent
).
then
(()
=>
{
fs
.
mkdir
(
dirPath
,
(
err
:
Error
|
null
)
=>
{
if
(
err
)
{
deferred
.
reject
(
err
);
}
else
{
deferred
.
resolve
();
}
});
}).
catch
((
err
:
Error
)
=>
{
deferred
.
reject
(
err
);
});
}
});
return
deferred
.
promise
;
}
}
function
mkDirPSync
(
dirPath
:
string
):
void
{
function
mkDirPSync
(
dirPath
:
string
):
void
{
if
(
fs
.
existsSync
(
dirPath
))
{
fs
.
mkdirSync
(
dirPath
,
{
recursive
:
true
});
return
;
}
mkDirPSync
(
path
.
dirname
(
dirPath
));
fs
.
mkdirSync
(
dirPath
);
}
}
const
delay
:
(
ms
:
number
)
=>
Promise
<
void
>
=
util
.
promisify
(
setTimeout
)
;
const
delay
=
timersPromises
.
setTimeout
;
/**
/**
* Convert index to character
* Convert index to character
...
@@ -233,19 +209,21 @@ async function getIPV4Address(): Promise<string> {
...
@@ -233,19 +209,21 @@ async function getIPV4Address(): Promise<string> {
const
socket
=
dgram
.
createSocket
(
'
udp4
'
);
const
socket
=
dgram
.
createSocket
(
'
udp4
'
);
socket
.
connect
(
1
,
'
192.0.2.0
'
);
socket
.
connect
(
1
,
'
192.0.2.0
'
);
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
// wait the system to initialize "connection"
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
// wait the system to initialize "connection"
await
yield_
();
await
timersPromises
.
setTimeout
(
1
);
try
{
cachedIpv4Address
=
socket
.
address
().
address
;
}
catch
(
error
)
{
/* retry */
}
try
{
cachedIpv4Address
=
socket
.
address
().
address
;
socket
.
close
();
return
cachedIpv4Address
;
}
catch
(
error
)
{
/* retry */
}
}
}
cachedIpv4Address
=
socket
.
address
().
address
;
// if it still fails, throw the error
cachedIpv4Address
=
socket
.
address
().
address
;
// if it still fails, throw the error
socket
.
close
();
socket
.
close
();
return
cachedIpv4Address
;
return
cachedIpv4Address
;
}
}
async
function
yield_
():
Promise
<
void
>
{
/* trigger the scheduler, do nothing */
}
/**
/**
* Get the status of canceled jobs according to the hint isEarlyStopped
* Get the status of canceled jobs according to the hint isEarlyStopped
*/
*/
...
...
ts/nni_manager/test/common/getIpv4Address.test.ts
0 → 100644
View file @
442342cb
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import
*
as
assert
from
'
assert
'
;
import
{
getIPV4Address
}
from
'
../../common/utils
'
;
it
(
'
getIpv4Address
'
,
async
()
=>
{
const
ip1
=
await
getIPV4Address
();
const
ip2
=
await
getIPV4Address
();
assert
.
match
(
ip1
,
/^
\d
+
\.\d
+
\.\d
+
\.\d
+$/
);
assert
.
equal
(
ip1
,
ip2
);
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment