Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
R
railgun-routing-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
railgun-routing-server
Commits
d6abf586
Commit
d6abf586
authored
Jan 07, 2026
by
nanamicat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
metric
parent
377c3acf
Pipeline
#42459
passed with stages
in 1 minute and 8 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
12 deletions
+22
-12
src/main.rs
src/main.rs
+3
-2
src/quality.rs
src/quality.rs
+1
-1
src/router.rs
src/router.rs
+15
-7
src/settings.rs
src/settings.rs
+3
-2
No files found.
src/main.rs
View file @
d6abf586
...
...
@@ -75,7 +75,7 @@ async fn main() -> Result<()> {
let
now
=
Instant
::
now
();
// 将超时的路由器下线
for
router
in
routers
.values_mut
()
{
if
router
.is_online
()
&&
router
.
time
.duration_since
(
now
)
>=
TIMEOUT
{
if
router
.is_online
()
&&
router
.
last_seen
.duration_since
(
now
)
>=
TIMEOUT
{
router
.offline
();
}
}
...
...
@@ -92,7 +92,8 @@ async fn main() -> Result<()> {
}
else
if
updating
.router_id
==
0
&&
now
.duration_since
(
start
)
>=
TIMEOUT
// 刚启动时静默,先学习
&&
let
Some
(
router
)
=
routers
.get
(
&
uplink
.id
)
&&
let
Some
(
downlink
)
=
router
.update
(
&
routers
,
&
connections
)
&&
router
.is_online
()
&&
let
Some
(
downlink
)
=
router
.update
(
now
,
&
routers
,
&
connections
)
{
updating
.router_id
=
router
.id
;
updating
.message
=
downlink
;
...
...
src/quality.rs
View file @
d6abf586
...
...
@@ -23,7 +23,7 @@ impl Quality {
if
self
.reliability
==
0.0
{
i32
::
MAX
}
else
{
self
.delay
+
((
1.0
-
self
.reliability
)
*
2
000.0
)
.round
()
as
i32
+
self
.cost
as
i32
self
.delay
+
((
1.0
-
self
.reliability
)
*
3
000.0
)
.round
()
as
i32
+
self
.cost
as
i32
}
}
}
...
...
src/router.rs
View file @
d6abf586
use
crate
::
UpdatingState
;
use
crate
::
data
::{
ConnectionData
,
RouterData
};
use
crate
::
protocol
::{
Downlink
,
MessageType
,
PeerQuality
,
Uplink
};
use
crate
::
quality
::
Quality
;
use
crate
::
settings
::
THROTTLE
;
use
crate
::
settings
::{
HALF_LIFE
,
PENALTY
};
use
crate
::
UpdatingState
;
use
serde
::
Serialize
;
use
std
::
collections
::
BTreeMap
;
use
std
::
net
::
SocketAddr
;
...
...
@@ -18,7 +18,9 @@ pub struct Router {
pub
via
:
BTreeMap
<
u8
,
u8
>
,
// dst router_id -> next hop router_id
pub
plan
:
BTreeMap
<
u8
,
BTreeMap
<
u8
,
u8
>>
,
// group id -> region id -> gateway_id
#[serde(skip)]
pub
time
:
Instant
,
// ms
pub
last_seen
:
Instant
,
#[serde(skip)]
pub
last_update
:
Instant
,
#[serde(skip)]
pub
addr
:
Option
<
SocketAddr
>
,
// Static config
...
...
@@ -45,7 +47,8 @@ impl Router {
.collect
(),
via
:
routers
.iter
()
.filter
(|
r
|
r
.id
!=
data
.id
)
.map
(|
r
|
(
r
.id
,
r
.id
))
.collect
(),
plan
:
BTreeMap
::
new
(),
time
:
Instant
::
now
(),
last_seen
:
Instant
::
now
(),
last_update
:
Instant
::
now
(),
addr
:
None
,
}
// router.reset(all_router_ids);
...
...
@@ -63,7 +66,7 @@ impl Router {
tracing
::
info!
(
"router {} online"
,
self
.id
);
}
self
.addr
=
Some
(
addr
);
self
.
time
=
now
;
self
.
last_seen
=
now
;
}
pub
fn
offline
(
&
mut
self
)
{
...
...
@@ -136,7 +139,12 @@ impl Router {
}
}
pub
fn
update
(
&
self
,
routers
:
&
BTreeMap
<
u8
,
Router
>
,
connections
:
&
BTreeMap
<
u8
,
BTreeMap
<
u8
,
ConnectionData
>>
)
->
Option
<
Downlink
>
{
pub
fn
penalty
(
&
self
,
now
:
Instant
)
->
i32
{
(
PENALTY
as
f32
*
f32
::
exp2
(
-
now
.duration_since
(
self
.last_update
)
.div_duration_f32
(
HALF_LIFE
)))
as
i32
}
pub
fn
update
(
&
self
,
now
:
Instant
,
routers
:
&
BTreeMap
<
u8
,
Router
>
,
connections
:
&
BTreeMap
<
u8
,
BTreeMap
<
u8
,
ConnectionData
>>
)
->
Option
<
Downlink
>
{
let
penalty
=
self
.penalty
(
now
);
let
mut
changed_via
:
BTreeMap
<
u8
,
u8
>
=
BTreeMap
::
new
();
// let mut metric: BTreeMap<u8, i32> = BTreeMap::new();
...
...
@@ -155,7 +163,7 @@ impl Router {
// 无论如何都不可达就标记为直连
changed_via
.insert
(
to
.id
,
to
.id
);
}
Some
((
best_router
,
best_metric
))
if
current_router
!=
*
best_router
&&
(
*
best_metric
+
THROTTLE
<
current_metric
)
=>
{
Some
((
best_router
,
best_metric
))
if
current_router
!=
*
best_router
&&
(
*
best_metric
+
penalty
<
current_metric
)
=>
{
changed_via
.insert
(
to
.id
,
best_router
.id
);
}
_
=>
{}
...
...
src/settings.rs
View file @
d6abf586
...
...
@@ -8,5 +8,6 @@ pub struct Settings {
pub
http_bind
:
SocketAddr
,
}
pub
const
TIMEOUT
:
Duration
=
Duration
::
from_secs
(
10
);
pub
const
THROTTLE
:
i32
=
20
;
pub
const
TIMEOUT
:
Duration
=
Duration
::
from_secs
(
20
);
pub
const
HALF_LIFE
:
Duration
=
Duration
::
from_secs
(
60
);
pub
const
PENALTY
:
i32
=
100
;
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