-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Hosts] Duplicate check improvement #32805
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
src/modules/Hosts/HostsUILib/Helpers/DuplicateService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Threading; | ||
using HostsUILib.Models; | ||
using HostsUILib.Settings; | ||
using Microsoft.UI.Dispatching; | ||
|
||
namespace HostsUILib.Helpers | ||
{ | ||
public class DuplicateService : IDuplicateService, IDisposable | ||
{ | ||
private record struct Check(string Address, string[] Hosts); | ||
|
||
private readonly IUserSettings _userSettings; | ||
private readonly DispatcherQueue _dispatcherQueue; | ||
private readonly Queue<Check> _checkQueue; | ||
private readonly ManualResetEvent _checkEvent; | ||
private readonly Thread _queueThread; | ||
|
||
private readonly string[] _loopbackAddresses = | ||
{ | ||
"0.0.0.0", | ||
"::", | ||
"::0", | ||
"0:0:0:0:0:0:0:0", | ||
"127.0.0.1", | ||
"::1", | ||
"0:0:0:0:0:0:0:1", | ||
}; | ||
|
||
private ReadOnlyCollection<Entry> _entries; | ||
private bool _disposed; | ||
|
||
public DuplicateService(IUserSettings userSettings) | ||
{ | ||
_userSettings = userSettings; | ||
|
||
_dispatcherQueue = DispatcherQueue.GetForCurrentThread(); | ||
_checkQueue = new Queue<Check>(); | ||
_checkEvent = new ManualResetEvent(false); | ||
|
||
_queueThread = new Thread(ProcessQueue); | ||
_queueThread.IsBackground = true; | ||
_queueThread.Start(); | ||
} | ||
|
||
public void Initialize(IList<Entry> entries) | ||
{ | ||
_entries = entries.AsReadOnly(); | ||
|
||
if (_checkQueue.Count > 0) | ||
{ | ||
_checkQueue.Clear(); | ||
} | ||
|
||
foreach (var entry in _entries) | ||
{ | ||
if (!_userSettings.LoopbackDuplicates && _loopbackAddresses.Contains(entry.Address)) | ||
{ | ||
continue; | ||
} | ||
|
||
_checkQueue.Enqueue(new Check(entry.Address, entry.SplittedHosts)); | ||
} | ||
|
||
_checkEvent.Set(); | ||
} | ||
|
||
public void CheckDuplicates(string address, string[] hosts) | ||
{ | ||
_checkQueue.Enqueue(new Check(address, hosts)); | ||
_checkEvent.Set(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private void ProcessQueue() | ||
{ | ||
while (true) | ||
{ | ||
_checkEvent.WaitOne(); | ||
|
||
while (_checkQueue.Count > 0) | ||
{ | ||
var check = _checkQueue.Dequeue(); | ||
FindDuplicates(check.Address, check.Hosts); | ||
} | ||
|
||
_checkEvent.Reset(); | ||
} | ||
} | ||
|
||
private void FindDuplicates(string address, string[] hosts) | ||
{ | ||
var entries = _entries.Where(e => | ||
string.Equals(e.Address, address, StringComparison.OrdinalIgnoreCase) | ||
|| hosts.Intersect(e.SplittedHosts, StringComparer.OrdinalIgnoreCase).Any()); | ||
|
||
foreach (var entry in entries) | ||
{ | ||
SetDuplicate(entry); | ||
} | ||
} | ||
|
||
private void SetDuplicate(Entry entry) | ||
{ | ||
if (!_userSettings.LoopbackDuplicates && _loopbackAddresses.Contains(entry.Address)) | ||
{ | ||
_dispatcherQueue.TryEnqueue(() => | ||
{ | ||
entry.Duplicate = false; | ||
}); | ||
|
||
return; | ||
} | ||
|
||
var duplicate = false; | ||
|
||
/* | ||
* Duplicate are based on the following criteria: | ||
* Entries with the same type and at least one host in common | ||
* Entries with the same type and address, except when there is only one entry with less than 9 hosts for that type and address | ||
*/ | ||
if (_entries.Any(e => e != entry | ||
&& e.Type == entry.Type | ||
&& entry.SplittedHosts.Intersect(e.SplittedHosts, StringComparer.OrdinalIgnoreCase).Any())) | ||
{ | ||
duplicate = true; | ||
} | ||
else if (_entries.Any(e => e != entry | ||
&& e.Type == entry.Type | ||
&& string.Equals(e.Address, entry.Address, StringComparison.OrdinalIgnoreCase))) | ||
{ | ||
duplicate = entry.SplittedHosts.Length < Consts.MaxHostsCount | ||
&& _entries.Count(e => e.Type == entry.Type | ||
&& string.Equals(e.Address, entry.Address, StringComparison.OrdinalIgnoreCase) | ||
&& e.SplittedHosts.Length < Consts.MaxHostsCount) > 1; | ||
} | ||
|
||
_dispatcherQueue.TryEnqueue(() => entry.Duplicate = duplicate); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (disposing) | ||
{ | ||
_checkEvent?.Dispose(); | ||
_disposed = true; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using HostsUILib.Models; | ||
|
||
namespace HostsUILib.Helpers | ||
{ | ||
public interface IDuplicateService | ||
{ | ||
void Initialize(IList<Entry> entries); | ||
|
||
void CheckDuplicates(string address, string[] hosts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as outdated.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RFC says short version:
::
https://www.rfc-editor.org/rfc/rfc4291#section-2.5.2