MCPcopy Create free account
hub / github.com/Dstack-TEE/dstack / add_rule

Method add_rule

port-forward/src/lib.rs:63–89  ·  view source on GitHub ↗

Add a forwarding rule. Returns error if the rule already exists.

(&mut self, rule: ForwardRule)

Source from the content-addressed store, hash-verified

61
62 /// Add a forwarding rule. Returns error if the rule already exists.
63 pub fn add_rule(&mut self, rule: ForwardRule) -> Result<()> {
64 if self.rules.contains_key(&rule) {
65 bail!("rule already exists: {:?}", rule);
66 }
67
68 let token = self.cancel.child_token();
69 let listen = rule.listen_sock();
70 let target = rule.target_sock();
71
72 let task = match rule.protocol {
73 Protocol::Tcp => tokio::spawn(tcp::run_tcp_forwarder(listen, target, token.clone())),
74 Protocol::Udp => tokio::spawn(udp::run_udp_forwarder(listen, target, token.clone())),
75 };
76
77 tracing::info!(
78 "added forwarding rule: {listen} -> {target} ({:?})",
79 rule.protocol
80 );
81 self.rules.insert(
82 rule,
83 RunningRule {
84 cancel: token,
85 task,
86 },
87 );
88 Ok(())
89 }
90
91 /// Remove a forwarding rule and stop its task.
92 pub async fn remove_rule(&mut self, rule: &ForwardRule) -> Result<()> {

Callers 1

Calls 5

run_tcp_forwarderFunction · 0.85
run_udp_forwarderFunction · 0.85
listen_sockMethod · 0.80
target_sockMethod · 0.80
cloneMethod · 0.80

Tested by

no test coverage detected