1use std::time::Duration;
4
5#[derive(Debug, Clone, Default)]
7pub enum TimeoutPolicy {
8 #[default]
10 None,
11 Cancel(Duration),
13 Warn(Duration),
15}
16
17#[derive(Debug, Clone)]
19pub struct OffloadConfig {
20 pub max_concurrent_tasks: Option<usize>,
23 pub timeout_policy: TimeoutPolicy,
25 pub deduplicate: bool,
27}
28
29impl Default for OffloadConfig {
30 fn default() -> Self {
31 Self {
32 max_concurrent_tasks: None,
33 timeout_policy: TimeoutPolicy::None,
34 deduplicate: true,
35 }
36 }
37}
38
39impl OffloadConfig {
40 pub fn builder() -> OffloadConfigBuilder {
42 OffloadConfigBuilder::default()
43 }
44}
45
46#[derive(Debug, Clone, Default)]
48pub struct OffloadConfigBuilder {
49 max_concurrent_tasks: Option<usize>,
50 timeout_policy: TimeoutPolicy,
51 deduplicate: bool,
52}
53
54impl OffloadConfigBuilder {
55 pub fn new() -> Self {
57 Self {
58 max_concurrent_tasks: None,
59 timeout_policy: TimeoutPolicy::None,
60 deduplicate: true,
61 }
62 }
63
64 pub fn max_concurrent_tasks(self, max: usize) -> Self {
66 Self {
67 max_concurrent_tasks: Some(max),
68 ..self
69 }
70 }
71
72 pub fn timeout_policy(self, policy: TimeoutPolicy) -> Self {
74 Self {
75 timeout_policy: policy,
76 ..self
77 }
78 }
79
80 pub fn timeout(self, duration: Duration) -> Self {
82 self.timeout_policy(TimeoutPolicy::Cancel(duration))
83 }
84
85 pub fn deduplicate(self, enabled: bool) -> Self {
87 Self {
88 deduplicate: enabled,
89 ..self
90 }
91 }
92
93 pub fn build(self) -> OffloadConfig {
95 OffloadConfig {
96 max_concurrent_tasks: self.max_concurrent_tasks,
97 timeout_policy: self.timeout_policy,
98 deduplicate: self.deduplicate,
99 }
100 }
101}