Skip to content

Commit fc4c8de

Browse files
committed
exec_profiles: store retry policy in Arc instead of Box
This way, it's easier to reuse retry policy for multiple execution profiles/statement configs - there is no need for additional allocation of Box.
1 parent 025342c commit fc4c8de

File tree

11 files changed

+38
-28
lines changed

11 files changed

+38
-28
lines changed

docs/source/execution-profiles/maximal-example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let profile = ExecutionProfile::builder()
1818
.consistency(Consistency::All)
1919
.serial_consistency(Some(SerialConsistency::Serial))
2020
.request_timeout(Some(Duration::from_secs(30)))
21-
.retry_policy(Box::new(FallthroughRetryPolicy::new()))
21+
.retry_policy(Arc::new(FallthroughRetryPolicy::new()))
2222
.load_balancing_policy(Arc::new(DefaultPolicy::default()))
2323
.speculative_execution_policy(
2424
Some(

docs/source/retry-policy/default.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ To use in `Session`:
99
# extern crate scylla;
1010
# use scylla::Session;
1111
# use std::error::Error;
12+
# use std::sync::Arc;
1213
# async fn check_only_compiles() -> Result<(), Box<dyn Error>> {
1314
use scylla::{Session, SessionBuilder};
1415
use scylla::transport::ExecutionProfile;
1516
use scylla::transport::retry_policy::DefaultRetryPolicy;
1617

1718
let handle = ExecutionProfile::builder()
18-
.retry_policy(Box::new(DefaultRetryPolicy::new()))
19+
.retry_policy(Arc::new(DefaultRetryPolicy::new()))
1920
.build()
2021
.into_handle();
2122

@@ -45,7 +46,7 @@ my_query.set_retry_policy(Some(Arc::new(DefaultRetryPolicy::new())));
4546

4647
// You can also set retry policy in an execution profile
4748
let handle = ExecutionProfile::builder()
48-
.retry_policy(Box::new(DefaultRetryPolicy::new()))
49+
.retry_policy(Arc::new(DefaultRetryPolicy::new()))
4950
.build()
5051
.into_handle();
5152
my_query.set_execution_profile_handle(Some(handle));
@@ -76,7 +77,7 @@ prepared.set_retry_policy(Some(Arc::new(DefaultRetryPolicy::new())));
7677

7778
// You can also set retry policy in an execution profile
7879
let handle = ExecutionProfile::builder()
79-
.retry_policy(Box::new(DefaultRetryPolicy::new()))
80+
.retry_policy(Arc::new(DefaultRetryPolicy::new()))
8081
.build()
8182
.into_handle();
8283
prepared.set_execution_profile_handle(Some(handle));

docs/source/retry-policy/downgrading-consistency.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ To use in `Session`:
5050
# extern crate scylla;
5151
# use scylla::Session;
5252
# use std::error::Error;
53+
# use std::sync::Arc;
5354
# async fn check_only_compiles() -> Result<(), Box<dyn Error>> {
5455
use scylla::{Session, SessionBuilder};
5556
use scylla::transport::ExecutionProfile;
5657
use scylla::transport::downgrading_consistency_retry_policy::DowngradingConsistencyRetryPolicy;
5758

5859
let handle = ExecutionProfile::builder()
59-
.retry_policy(Box::new(DowngradingConsistencyRetryPolicy::new()))
60+
.retry_policy(Arc::new(DowngradingConsistencyRetryPolicy::new()))
6061
.build()
6162
.into_handle();
6263

@@ -74,13 +75,14 @@ To use in a [simple query](../queries/simple.md):
7475
# extern crate scylla;
7576
# use scylla::Session;
7677
# use std::error::Error;
78+
# use std::sync::Arc;
7779
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
7880
use scylla::query::Query;
7981
use scylla::transport::ExecutionProfile;
8082
use scylla::transport::downgrading_consistency_retry_policy::DowngradingConsistencyRetryPolicy;
8183

8284
let handle = ExecutionProfile::builder()
83-
.retry_policy(Box::new(DowngradingConsistencyRetryPolicy::new()))
85+
.retry_policy(Arc::new(DowngradingConsistencyRetryPolicy::new()))
8486
.build()
8587
.into_handle();
8688

@@ -100,13 +102,14 @@ To use in a [prepared query](../queries/prepared.md):
100102
# extern crate scylla;
101103
# use scylla::Session;
102104
# use std::error::Error;
105+
# use std::sync::Arc;
103106
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
104107
use scylla::prepared_statement::PreparedStatement;
105108
use scylla::transport::ExecutionProfile;
106109
use scylla::transport::downgrading_consistency_retry_policy::DowngradingConsistencyRetryPolicy;
107110

108111
let handle = ExecutionProfile::builder()
109-
.retry_policy(Box::new(DowngradingConsistencyRetryPolicy::new()))
112+
.retry_policy(Arc::new(DowngradingConsistencyRetryPolicy::new()))
110113
.build()
111114
.into_handle();
112115

docs/source/retry-policy/fallthrough.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ To use in `Session`:
88
# extern crate scylla;
99
# use scylla::Session;
1010
# use std::error::Error;
11+
# use std::sync::Arc;
1112
# async fn check_only_compiles() -> Result<(), Box<dyn Error>> {
1213
use scylla::{Session, SessionBuilder};
1314
use scylla::transport::ExecutionProfile;
1415
use scylla::transport::retry_policy::FallthroughRetryPolicy;
1516

1617
let handle = ExecutionProfile::builder()
17-
.retry_policy(Box::new(FallthroughRetryPolicy::new()))
18+
.retry_policy(Arc::new(FallthroughRetryPolicy::new()))
1819
.build()
1920
.into_handle();
2021

@@ -32,13 +33,14 @@ To use in a [simple query](../queries/simple.md):
3233
# extern crate scylla;
3334
# use scylla::Session;
3435
# use std::error::Error;
36+
# use std::sync::Arc;
3537
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
3638
use scylla::query::Query;
3739
use scylla::transport::ExecutionProfile;
3840
use scylla::transport::retry_policy::FallthroughRetryPolicy;
3941

4042
let handle = ExecutionProfile::builder()
41-
.retry_policy(Box::new(FallthroughRetryPolicy::new()))
43+
.retry_policy(Arc::new(FallthroughRetryPolicy::new()))
4244
.build()
4345
.into_handle();
4446

@@ -58,13 +60,14 @@ To use in a [prepared query](../queries/prepared.md):
5860
# extern crate scylla;
5961
# use scylla::Session;
6062
# use std::error::Error;
63+
# use std::sync::Arc;
6164
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
6265
use scylla::prepared_statement::PreparedStatement;
6366
use scylla::transport::ExecutionProfile;
6467
use scylla::transport::retry_policy::FallthroughRetryPolicy;
6568

6669
let handle = ExecutionProfile::builder()
67-
.retry_policy(Box::new(FallthroughRetryPolicy::new()))
70+
.retry_policy(Arc::new(FallthroughRetryPolicy::new()))
6871
.build()
6972
.into_handle();
7073

examples/execution_profile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn main() -> Result<()> {
2222
.serial_consistency(Some(SerialConsistency::Serial))
2323
.request_timeout(Some(Duration::from_secs(42)))
2424
.load_balancing_policy(Arc::new(load_balancing::DefaultPolicy::default()))
25-
.retry_policy(Box::new(FallthroughRetryPolicy::new()))
25+
.retry_policy(Arc::new(FallthroughRetryPolicy::new()))
2626
.speculative_execution_policy(Some(Arc::new(PercentileSpeculativeExecutionPolicy {
2727
max_retry_count: 2,
2828
percentile: 42.0,
@@ -34,7 +34,7 @@ async fn main() -> Result<()> {
3434
.serial_consistency(None)
3535
.request_timeout(Some(Duration::from_secs(3)))
3636
.load_balancing_policy(Arc::new(load_balancing::DefaultPolicy::default()))
37-
.retry_policy(Box::new(DefaultRetryPolicy::new()))
37+
.retry_policy(Arc::new(DefaultRetryPolicy::new()))
3838
.speculative_execution_policy(None)
3939
.build();
4040

scylla/src/transport/execution_profile.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ pub(crate) mod defaults {
191191
pub(crate) fn load_balancing_policy() -> Arc<dyn LoadBalancingPolicy> {
192192
Arc::new(load_balancing::DefaultPolicy::default())
193193
}
194-
pub(crate) fn retry_policy() -> Box<dyn RetryPolicy> {
195-
Box::new(DefaultRetryPolicy::new())
194+
pub(crate) fn retry_policy() -> Arc<dyn RetryPolicy> {
195+
Arc::new(DefaultRetryPolicy::new())
196196
}
197197
pub(crate) fn speculative_execution_policy() -> Option<Arc<dyn SpeculativeExecutionPolicy>> {
198198
None
@@ -218,10 +218,11 @@ pub(crate) mod defaults {
218218
/// ```
219219
/// # use scylla::transport::{ExecutionProfile, retry_policy::FallthroughRetryPolicy};
220220
/// # use scylla::statement::Consistency;
221+
/// # use std::sync::Arc;
221222
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
222223
/// let profile: ExecutionProfile = ExecutionProfile::builder()
223224
/// .consistency(Consistency::Three) // as this is the number we shall count to
224-
/// .retry_policy(Box::new(FallthroughRetryPolicy::new()))
225+
/// .retry_policy(Arc::new(FallthroughRetryPolicy::new()))
225226
/// .build();
226227
/// # Ok(())
227228
/// # }
@@ -232,7 +233,7 @@ pub struct ExecutionProfileBuilder {
232233
consistency: Option<Consistency>,
233234
serial_consistency: Option<Option<SerialConsistency>>,
234235
load_balancing_policy: Option<Arc<dyn LoadBalancingPolicy>>,
235-
retry_policy: Option<Box<dyn RetryPolicy>>,
236+
retry_policy: Option<Arc<dyn RetryPolicy>>,
236237
speculative_execution_policy: Option<Option<Arc<dyn SpeculativeExecutionPolicy>>>,
237238
}
238239

@@ -302,14 +303,15 @@ impl ExecutionProfileBuilder {
302303
/// ```
303304
/// use scylla::transport::retry_policy::DefaultRetryPolicy;
304305
/// # use scylla::transport::ExecutionProfile;
306+
/// # use std::sync::Arc;
305307
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
306308
/// let profile: ExecutionProfile = ExecutionProfile::builder()
307-
/// .retry_policy(Box::new(DefaultRetryPolicy::new()))
309+
/// .retry_policy(Arc::new(DefaultRetryPolicy::new()))
308310
/// .build();
309311
/// # Ok(())
310312
/// # }
311313
/// ```
312-
pub fn retry_policy(mut self, retry_policy: Box<dyn RetryPolicy>) -> Self {
314+
pub fn retry_policy(mut self, retry_policy: Arc<dyn RetryPolicy>) -> Self {
313315
self.retry_policy = Some(retry_policy);
314316
self
315317
}
@@ -352,9 +354,10 @@ impl ExecutionProfileBuilder {
352354
/// ```
353355
/// use scylla::transport::retry_policy::DefaultRetryPolicy;
354356
/// # use scylla::transport::ExecutionProfile;
357+
/// # use std::sync::Arc;
355358
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
356359
/// let profile: ExecutionProfile = ExecutionProfile::builder()
357-
/// .retry_policy(Box::new(DefaultRetryPolicy::new()))
360+
/// .retry_policy(Arc::new(DefaultRetryPolicy::new()))
358361
/// .build();
359362
/// # Ok(())
360363
/// # }
@@ -402,7 +405,7 @@ pub(crate) struct ExecutionProfileInner {
402405
pub(crate) serial_consistency: Option<SerialConsistency>,
403406

404407
pub(crate) load_balancing_policy: Arc<dyn LoadBalancingPolicy>,
405-
pub(crate) retry_policy: Box<dyn RetryPolicy>,
408+
pub(crate) retry_policy: Arc<dyn RetryPolicy>,
406409
pub(crate) speculative_execution_policy: Option<Arc<dyn SpeculativeExecutionPolicy>>,
407410
}
408411

scylla/src/transport/session_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2724,7 +2724,7 @@ async fn test_iter_works_when_retry_policy_returns_ignore_write_error() {
27242724

27252725
let handle = ExecutionProfile::builder()
27262726
.consistency(Consistency::All)
2727-
.retry_policy(Box::new(MyRetryPolicy(retried_flag.clone())))
2727+
.retry_policy(Arc::new(MyRetryPolicy(retried_flag.clone())))
27282728
.build()
27292729
.into_handle();
27302730

scylla/tests/integration/consistency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ async fn consistency_is_correctly_set_in_cql_requests() {
288288
}
289289

290290
let fallthrough_exec_profile_builder =
291-
ExecutionProfile::builder().retry_policy(Box::new(FallthroughRetryPolicy));
291+
ExecutionProfile::builder().retry_policy(Arc::new(FallthroughRetryPolicy));
292292

293293
let translation_map = Arc::new(translation_map);
294294

@@ -421,7 +421,7 @@ async fn consistency_is_correctly_set_in_routing_info() {
421421
};
422422

423423
let exec_profile_builder = ExecutionProfile::builder()
424-
.retry_policy(Box::new(FallthroughRetryPolicy))
424+
.retry_policy(Arc::new(FallthroughRetryPolicy))
425425
.load_balancing_policy(Arc::new(reporting_load_balancer));
426426

427427
// DB preparation phase

scylla/tests/integration/execution_profiles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ async fn test_execution_profiles() {
145145

146146
let profile1 = ExecutionProfile::builder()
147147
.load_balancing_policy(policy1.clone())
148-
.retry_policy(Box::new(policy1.deref().clone()))
148+
.retry_policy(Arc::new(policy1.deref().clone()))
149149
.consistency(Consistency::One)
150150
.serial_consistency(None)
151151
.speculative_execution_policy(None)
152152
.build();
153153

154154
let profile2 = ExecutionProfile::builder()
155155
.load_balancing_policy(policy2.clone())
156-
.retry_policy(Box::new(policy2.deref().clone()))
156+
.retry_policy(Arc::new(policy2.deref().clone()))
157157
.consistency(Consistency::Two)
158158
.serial_consistency(Some(SerialConsistency::LocalSerial))
159159
.speculative_execution_policy(Some(policy2))

scylla/tests/integration/lwt_optimisation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async fn if_lwt_optimisation_mark_offered_then_negotiatied_and_lwt_routed_optima
4747
});
4848

4949
let handle = ExecutionProfile::builder()
50-
.retry_policy(Box::new(FallthroughRetryPolicy))
50+
.retry_policy(Arc::new(FallthroughRetryPolicy))
5151
.build()
5252
.into_handle();
5353

0 commit comments

Comments
 (0)