-
Notifications
You must be signed in to change notification settings - Fork 42
Player Counter Queries #119
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,7 @@ public function runQuery(Allocation $allocation): ?array | |
| return null; | ||
| } | ||
|
|
||
| $ip = config('player-counter.use_alias') && is_ip($allocation->alias) ? $allocation->alias : $allocation->ip; | ||
| $ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip; | ||
| $ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip; | ||
|
Comment on lines
+42
to
43
|
||
|
|
||
| /** @var QueryTypeService $service */ | ||
|
|
@@ -54,7 +54,7 @@ public static function canRunQuery(?Allocation $allocation): bool | |
| return false; | ||
| } | ||
|
|
||
| $ip = config('player-counter.use_alias') && is_ip($allocation->alias) ? $allocation->alias : $allocation->ip; | ||
| $ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same double DNS lookup issue and duplicated logic. This line has the same redundant 🔧 Proposed fix: extract helper method and cache DNS resultAdd a private helper method and refactor both usages: + private static function resolveIp(Allocation $allocation): string
+ {
+ if (config('player-counter.use_alias') && $allocation->alias) {
+ $resolved = gethostbyname($allocation->alias);
+ if (is_ip($resolved)) {
+ return $resolved;
+ }
+ }
+
+ return $allocation->ip;
+ }
+
/** `@return` ?array{hostname: string, map: string, current_players: int, max_players: int, players: ?array<array{id: string, name: string}>} */
public function runQuery(Allocation $allocation): ?array
{
if (!static::canRunQuery($allocation)) {
return null;
}
- $ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
+ $ip = static::resolveIp($allocation);
$ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip; public static function canRunQuery(?Allocation $allocation): bool
{
if (!$allocation) {
return false;
}
- $ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
+ $ip = static::resolveIp($allocation);
return !in_array($ip, ['0.0.0.0', '::']);
}🤖 Prompt for AI Agents |
||
|
|
||
| return !in_array($ip, ['0.0.0.0', '::']); | ||
|
Comment on lines
+57
to
59
|
||
| } | ||
|
|
||
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.
Double
gethostbyname()call causes redundant DNS lookups and potential inconsistency.gethostbyname($allocation->alias)is called twice—once in the condition and once in the result. This triggers two DNS queries per invocation, which is inefficient and could yield inconsistent results if DNS changes between calls.🔧 Proposed fix: cache the resolved hostname
🤖 Prompt for AI Agents