Skip to content

Commit 74b31bd

Browse files
committed
fix(hosts): priority after update hosts
1 parent 302f250 commit 74b31bd

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

app/core/hosts.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from app.core.manager import core_manager
99
from app.db import GetDB
1010
from app.db.crud.host import get_host_by_id, get_hosts, upsert_inbounds
11-
from app.db.models import ProxyHost, ProxyHostSecurity
12-
from app.models.host import MuxSettings, TransportSettings, BaseHost
11+
from app.db.models import ProxyHostSecurity
12+
from app.models.host import BaseHost
1313

1414

1515
def _prepare_host_data(host: BaseHost) -> dict:
@@ -36,6 +36,7 @@ def _prepare_host_data(host: BaseHost) -> dict:
3636
else {},
3737
"status": host.status,
3838
"ech_config_list": host.ech_config_list,
39+
"priority": host.priority,
3940
}
4041

4142

@@ -108,7 +109,9 @@ async def get_host(self, id: int) -> dict | None:
108109
@cached()
109110
async def get_hosts(self) -> dict[int, dict]:
110111
async with self._lock:
111-
return deepcopy(self._hosts)
112+
# Return hosts sorted by priority
113+
sorted_hosts = dict(sorted(self._hosts.items(), key=lambda x: x[1]["priority"]))
114+
return deepcopy(sorted_hosts)
112115

113116

114117
host_manager: HostManager = HostManager()

app/db/crud/host.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,21 @@ async def get_hosts(
123123
db: AsyncSession,
124124
offset: Optional[int] = 0,
125125
limit: Optional[int] = 0,
126-
sort: ProxyHostSortingOptions = "priority",
126+
sort: ProxyHostSortingOptions = ProxyHostSortingOptions.priority,
127127
) -> list[ProxyHost]:
128128
"""
129-
Retrieves hosts.
129+
Retrieves hosts sorted by priority (ascending) by default.
130130
131131
Args:
132132
db (AsyncSession): Database session.
133133
offset (Optional[int]): Number of records to skip.
134134
limit (Optional[int]): Number of records to retrieve.
135+
sort (ProxyHostSortingOptions): Sorting option. Defaults to priority ascending.
135136
136137
Returns:
137-
List[ProxyHost]: List of hosts for the inbound.
138+
List[ProxyHost]: List of hosts sorted by the specified option.
138139
"""
139-
stmt = select(ProxyHost).order_by(sort)
140+
stmt = select(ProxyHost).order_by(sort.value)
140141

141142
if offset:
142143
stmt = stmt.offset(offset)

0 commit comments

Comments
 (0)