Skip to content

Re-read /etc/hosts upon change - #11922

Merged
normanmaurer merged 11 commits into
netty:4.1from
aradchykov:4.1_refresh_hostsFile
Dec 16, 2021
Merged

normanmaurer merged 11 commits into
netty:4.1from
aradchykov:4.1_refresh_hostsFile

Conversation

@aradchykov

Copy link
Copy Markdown
Contributor

Re-read /etc/hosts periodically in DefaultHostsFileEntriesResolver

Motivation:

io.netty.resolver.DefaultHostsFileEntriesResolver parses /etc/hosts only once, upon creation.
With these changes DefaultHostsFileEntriesResolver will periodically re-read configuration.

Modification:

  1. Made HostsFileEntriesProvider.Parser dependency injectable via constructor to be able to test my changes
  2. Added entries expiry check and re-read before resolving addresses
  3. address now uses addresses function inside

Result:
DefaultHostsFileEntriesResolver now re-reads data from hosts file every 10 seconds.

Fixes #.
#11661

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't use wildcard imports

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Collections.singletonMap("localhost", Collections.<InetAddress>singletonList(NetUtil.LOCALHOST4));
private final Map<String, List<InetAddress>> localhostV6Addresses =
Collections.singletonMap("localhost", Collections.<InetAddress>singletonList(NetUtil.LOCALHOST6));
private final long entriesTTL = TimeUnit.MINUTES.toNanos(1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these can be static

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

assertEquals(NetUtil.LOCALHOST6, resolver.address(newHost, ResolvedAddressTypes.IPV6_ONLY));
}

public HostsFileEntriesProvider.Parser givenHostsParserWith(final Map<String, List<InetAddress>> inet4Entries,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove the public keyword

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@normanmaurer

Copy link
Copy Markdown
Member

@aradchykov

Copy link
Copy Markdown
Contributor Author

@normanmaurer yes, I signed it yesterday

DefaultHostsFileEntriesResolver(HostsFileEntriesProvider.Parser hostsFileParser) {
this.hostsFileParser = hostsFileParser;
this.refreshInterval = SystemPropertyUtil.getLong(
"io.netty.hostsFileRefreshInterval", DEFAULT_REFRESH_INTERVAL);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to static init block, also added debug log 👍, thanks


private void ensureHostsFileEntriesAreFresh() {
long last = lastRefresh.get();
if (System.nanoTime() - last > refreshInterval) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: store System.nanoTime() in a local variable and reuse it in the compareAndSet. No need to call the function two times.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


// for testing purpose only
DefaultHostsFileEntriesResolver(HostsFileEntriesProvider entries) {
DefaultHostsFileEntriesResolver(HostsFileEntriesProvider.Parser hostsFileParser) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still allow to pass in the refreshInterval so we can use it for testing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added refreshInterval back to the constuctor

@normanmaurer
normanmaurer merged commit ea6d483 into netty:4.1 Dec 16, 2021
@normanmaurer

Copy link
Copy Markdown
Member

@comchi thanks a lot!

@normanmaurer normanmaurer added this to the 4.1.72.Final milestone Dec 16, 2021
normanmaurer pushed a commit that referenced this pull request Dec 16, 2021
…11922)

Motivation:

`io.netty.resolver.DefaultHostsFileEntriesResolver` parses `/etc/hosts` only once, upon creation.
With these changes `DefaultHostsFileEntriesResolver` will periodically re-read configuration.

Modification:
1. Made `HostsFileEntriesProvider.Parser` dependency injectable via constructor to be able to test my changes
2. Added entries `expiry` check and re-read before resolving addresses
3. `address` now uses `addresses` function inside

Result:
`DefaultHostsFileEntriesResolver` now re-reads data from hosts file every 10 seconds.

Fixes #11661
laosijikaichele pushed a commit to laosijikaichele/netty that referenced this pull request Dec 16, 2021
…etty#11922)



Motivation:

`io.netty.resolver.DefaultHostsFileEntriesResolver` parses `/etc/hosts` only once, upon creation.
With these changes `DefaultHostsFileEntriesResolver` will periodically re-read configuration.

Modification:
1. Made `HostsFileEntriesProvider.Parser` dependency injectable via constructor to be able to test my changes
2. Added entries `expiry` check and re-read before resolving addresses
3. `address` now uses `addresses` function inside

Result:
`DefaultHostsFileEntriesResolver` now re-reads data from hosts file every 10 seconds. 

Fixes netty#11661
laosijikaichele pushed a commit to laosijikaichele/netty that referenced this pull request Dec 16, 2021
…etty#11922)



Motivation:

`io.netty.resolver.DefaultHostsFileEntriesResolver` parses `/etc/hosts` only once, upon creation.
With these changes `DefaultHostsFileEntriesResolver` will periodically re-read configuration.

Modification:
1. Made `HostsFileEntriesProvider.Parser` dependency injectable via constructor to be able to test my changes
2. Added entries `expiry` check and re-read before resolving addresses
3. `address` now uses `addresses` function inside

Result:
`DefaultHostsFileEntriesResolver` now re-reads data from hosts file every 10 seconds. 

Fixes netty#11661
mostroverkhov added a commit to mostroverkhov/netty that referenced this pull request Dec 21, 2021
…osts by default

Motivation:

After netty#11922, by default `io.netty.resolver.DefaultHostsFileEntriesResolver` synchronously reads `/etc/hosts` every 60 seconds, while before It read `/etc/hosts` once upon instantiation.

Modification:

By default `io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation. This corresponds to io.netty.hostsFileRefreshInterval = 0

Negative values for io.netty.hostsFileRefreshInterval are forbidden

Result:

`io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation, which corresponds to default behaviour prior to netty#11922
normanmaurer pushed a commit that referenced this pull request Dec 23, 2021
…osts by default (#11943)



Motivation:

After #11922, by default `io.netty.resolver.DefaultHostsFileEntriesResolver` synchronously reads `/etc/hosts` every 60 seconds, while before It read `/etc/hosts` once upon instantiation.

Modification:

By default `io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation. This corresponds to io.netty.hostsFileRefreshInterval = 0

Negative values for io.netty.hostsFileRefreshInterval are forbidden

Result:

`io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation, which corresponds to default behaviour prior to #11922
normanmaurer pushed a commit that referenced this pull request Dec 23, 2021
…osts by default (#11943)



Motivation:

After #11922, by default `io.netty.resolver.DefaultHostsFileEntriesResolver` synchronously reads `/etc/hosts` every 60 seconds, while before It read `/etc/hosts` once upon instantiation.

Modification:

By default `io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation. This corresponds to io.netty.hostsFileRefreshInterval = 0

Negative values for io.netty.hostsFileRefreshInterval are forbidden

Result:

`io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation, which corresponds to default behaviour prior to #11922
10brothers pushed a commit to 10brothers/netty that referenced this pull request Jan 20, 2022
…etty#11922)



Motivation:

`io.netty.resolver.DefaultHostsFileEntriesResolver` parses `/etc/hosts` only once, upon creation.
With these changes `DefaultHostsFileEntriesResolver` will periodically re-read configuration.

Modification:
1. Made `HostsFileEntriesProvider.Parser` dependency injectable via constructor to be able to test my changes
2. Added entries `expiry` check and re-read before resolving addresses
3. `address` now uses `addresses` function inside

Result:
`DefaultHostsFileEntriesResolver` now re-reads data from hosts file every 10 seconds. 

Fixes netty#11661
10brothers pushed a commit to 10brothers/netty that referenced this pull request Jan 20, 2022
…osts by default (netty#11943)



Motivation:

After netty#11922, by default `io.netty.resolver.DefaultHostsFileEntriesResolver` synchronously reads `/etc/hosts` every 60 seconds, while before It read `/etc/hosts` once upon instantiation.

Modification:

By default `io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation. This corresponds to io.netty.hostsFileRefreshInterval = 0

Negative values for io.netty.hostsFileRefreshInterval are forbidden

Result:

`io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation, which corresponds to default behaviour prior to netty#11922
raidyue pushed a commit to raidyue/netty that referenced this pull request Jul 8, 2022
…etty#11922)



Motivation:

`io.netty.resolver.DefaultHostsFileEntriesResolver` parses `/etc/hosts` only once, upon creation.
With these changes `DefaultHostsFileEntriesResolver` will periodically re-read configuration.

Modification:
1. Made `HostsFileEntriesProvider.Parser` dependency injectable via constructor to be able to test my changes
2. Added entries `expiry` check and re-read before resolving addresses
3. `address` now uses `addresses` function inside

Result:
`DefaultHostsFileEntriesResolver` now re-reads data from hosts file every 10 seconds. 

Fixes netty#11661
raidyue pushed a commit to raidyue/netty that referenced this pull request Jul 8, 2022
…osts by default (netty#11943)



Motivation:

After netty#11922, by default `io.netty.resolver.DefaultHostsFileEntriesResolver` synchronously reads `/etc/hosts` every 60 seconds, while before It read `/etc/hosts` once upon instantiation.

Modification:

By default `io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation. This corresponds to io.netty.hostsFileRefreshInterval = 0

Negative values for io.netty.hostsFileRefreshInterval are forbidden

Result:

`io.netty.resolver.DefaultHostsFileEntriesResolver` reads `/etc/hosts` once upon instantiation, which corresponds to default behaviour prior to netty#11922
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants