@@ -76,6 +76,14 @@ pub struct CanisterSettings {
76
76
/// Must be a number between 0 and 2^48^ (i.e 256TB), inclusively.
77
77
pub wasm_memory_limit : Option < Nat > ,
78
78
79
+ /// A threshold on the remaining Wasm memory of the canister.
80
+ ///
81
+ /// When the remaining memory drops below this threshold, its
82
+ /// `on_low_wasm_memory` hook will be invoked. This enables it
83
+ /// to self-optimize or raise an alert or otherwise attempt to
84
+ /// prevent itself from reaching `wasm_memory_limit`.
85
+ pub wasm_memory_threshold : Option < Nat > ,
86
+
79
87
/// The canister log visibility of the canister.
80
88
///
81
89
/// If unspecified and a canister is being created with these settings, defaults to `Controllers`, i.e. private by default.
@@ -93,6 +101,7 @@ pub struct CreateCanisterBuilder<'agent, 'canister: 'agent> {
93
101
freezing_threshold : Option < Result < FreezingThreshold , AgentError > > ,
94
102
reserved_cycles_limit : Option < Result < ReservedCyclesLimit , AgentError > > ,
95
103
wasm_memory_limit : Option < Result < WasmMemoryLimit , AgentError > > ,
104
+ wasm_memory_threshold : Option < Result < WasmMemoryLimit , AgentError > > ,
96
105
log_visibility : Option < Result < LogVisibility , AgentError > > ,
97
106
is_provisional_create : bool ,
98
107
amount : Option < u128 > ,
@@ -111,6 +120,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
111
120
freezing_threshold : None ,
112
121
reserved_cycles_limit : None ,
113
122
wasm_memory_limit : None ,
123
+ wasm_memory_threshold : None ,
114
124
log_visibility : None ,
115
125
is_provisional_create : false ,
116
126
amount : None ,
@@ -161,7 +171,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
161
171
}
162
172
}
163
173
164
- /// Pass in an optional controller for the canister. If this is [None],
174
+ /// Pass in an optional controller for the canister. If this is [` None` ],
165
175
/// it will revert the controller to default.
166
176
pub fn with_optional_controller < C , E > ( self , controller : Option < C > ) -> Self
167
177
where
@@ -199,7 +209,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
199
209
self . with_optional_controller ( Some ( controller) )
200
210
}
201
211
202
- /// Pass in a compute allocation optional value for the canister. If this is [None],
212
+ /// Pass in a compute allocation optional value for the canister. If this is [` None` ],
203
213
/// it will revert the compute allocation to default.
204
214
pub fn with_optional_compute_allocation < C , E > ( self , compute_allocation : Option < C > ) -> Self
205
215
where
@@ -224,7 +234,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
224
234
self . with_optional_compute_allocation ( Some ( compute_allocation) )
225
235
}
226
236
227
- /// Pass in a memory allocation optional value for the canister. If this is [None],
237
+ /// Pass in a memory allocation optional value for the canister. If this is [` None` ],
228
238
/// it will revert the memory allocation to default.
229
239
pub fn with_optional_memory_allocation < E , C > ( self , memory_allocation : Option < C > ) -> Self
230
240
where
@@ -249,7 +259,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
249
259
self . with_optional_memory_allocation ( Some ( memory_allocation) )
250
260
}
251
261
252
- /// Pass in a freezing threshold optional value for the canister. If this is [None],
262
+ /// Pass in a freezing threshold optional value for the canister. If this is [` None` ],
253
263
/// it will revert the freezing threshold to default.
254
264
pub fn with_optional_freezing_threshold < E , C > ( self , freezing_threshold : Option < C > ) -> Self
255
265
where
@@ -283,7 +293,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
283
293
self . with_optional_reserved_cycles_limit ( Some ( limit) )
284
294
}
285
295
286
- /// Pass in a reserved cycles limit optional value for the canister. If this is [None],
296
+ /// Pass in a reserved cycles limit optional value for the canister. If this is [` None` ],
287
297
/// it will create the canister with the default limit.
288
298
pub fn with_optional_reserved_cycles_limit < E , C > ( self , limit : Option < C > ) -> Self
289
299
where
@@ -309,7 +319,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
309
319
self . with_optional_wasm_memory_limit ( Some ( wasm_memory_limit) )
310
320
}
311
321
312
- /// Pass in a Wasm memory limit optional value for the canister. If this is [None],
322
+ /// Pass in a Wasm memory limit optional value for the canister. If this is [` None` ],
313
323
/// it will revert the Wasm memory limit to default.
314
324
pub fn with_optional_wasm_memory_limit < E , C > ( self , wasm_memory_limit : Option < C > ) -> Self
315
325
where
@@ -326,6 +336,32 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
326
336
}
327
337
}
328
338
339
+ /// Pass in a Wasm memory threshold value for the canister.
340
+ pub fn with_wasm_memory_threshold < C , E > ( self , wasm_memory_threshold : C ) -> Self
341
+ where
342
+ E : std:: fmt:: Display ,
343
+ C : TryInto < WasmMemoryLimit , Error = E > ,
344
+ {
345
+ self . with_optional_wasm_memory_threshold ( Some ( wasm_memory_threshold) )
346
+ }
347
+
348
+ /// Pass in a Wasm memory threshold optional value for the canister. If this is [`None`],
349
+ /// it will revert the Wasm memory threshold to default.
350
+ pub fn with_optional_wasm_memory_threshold < E , C > ( self , wasm_memory_threshold : Option < C > ) -> Self
351
+ where
352
+ E : std:: fmt:: Display ,
353
+ C : TryInto < WasmMemoryLimit , Error = E > ,
354
+ {
355
+ Self {
356
+ wasm_memory_threshold : wasm_memory_threshold. map ( |limit| {
357
+ limit
358
+ . try_into ( )
359
+ . map_err ( |e| AgentError :: MessageError ( format ! ( "{e}" ) ) )
360
+ } ) ,
361
+ ..self
362
+ }
363
+ }
364
+
329
365
/// Pass in a log visibility setting for the canister.
330
366
pub fn with_log_visibility < C , E > ( self , log_visibility : C ) -> Self
331
367
where
@@ -335,7 +371,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
335
371
self . with_optional_log_visibility ( Some ( log_visibility) )
336
372
}
337
373
338
- /// Pass in a log visibility optional setting for the canister. If this is [None],
374
+ /// Pass in a log visibility optional setting for the canister. If this is [` None` ],
339
375
/// it will revert the log visibility to default.
340
376
pub fn with_optional_log_visibility < E , C > ( self , log_visibility : Option < C > ) -> Self
341
377
where
@@ -385,6 +421,11 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
385
421
Some ( Ok ( x) ) => Some ( Nat :: from ( u64:: from ( x) ) ) ,
386
422
None => None ,
387
423
} ;
424
+ let wasm_memory_threshold = match self . wasm_memory_threshold {
425
+ Some ( Err ( x) ) => return Err ( AgentError :: MessageError ( format ! ( "{x}" ) ) ) ,
426
+ Some ( Ok ( x) ) => Some ( Nat :: from ( u64:: from ( x) ) ) ,
427
+ None => None ,
428
+ } ;
388
429
let log_visibility = match self . log_visibility {
389
430
Some ( Err ( x) ) => return Err ( AgentError :: MessageError ( format ! ( "{x}" ) ) ) ,
390
431
Some ( Ok ( x) ) => Some ( x) ,
@@ -412,6 +453,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
412
453
freezing_threshold,
413
454
reserved_cycles_limit,
414
455
wasm_memory_limit,
456
+ wasm_memory_threshold,
415
457
log_visibility,
416
458
} ,
417
459
specified_id : self . specified_id ,
@@ -430,6 +472,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
430
472
freezing_threshold,
431
473
reserved_cycles_limit,
432
474
wasm_memory_limit,
475
+ wasm_memory_threshold,
433
476
log_visibility,
434
477
} )
435
478
. with_effective_canister_id ( self . effective_canister_id )
@@ -956,6 +999,7 @@ pub struct UpdateCanisterBuilder<'agent, 'canister: 'agent> {
956
999
freezing_threshold : Option < Result < FreezingThreshold , AgentError > > ,
957
1000
reserved_cycles_limit : Option < Result < ReservedCyclesLimit , AgentError > > ,
958
1001
wasm_memory_limit : Option < Result < WasmMemoryLimit , AgentError > > ,
1002
+ wasm_memory_threshold : Option < Result < WasmMemoryLimit , AgentError > > ,
959
1003
log_visibility : Option < Result < LogVisibility , AgentError > > ,
960
1004
}
961
1005
@@ -971,11 +1015,12 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
971
1015
freezing_threshold : None ,
972
1016
reserved_cycles_limit : None ,
973
1017
wasm_memory_limit : None ,
1018
+ wasm_memory_threshold : None ,
974
1019
log_visibility : None ,
975
1020
}
976
1021
}
977
1022
978
- /// Pass in an optional controller for the canister. If this is [None],
1023
+ /// Pass in an optional controller for the canister. If this is [` None` ],
979
1024
/// it will revert the controller to default.
980
1025
pub fn with_optional_controller < C , E > ( self , controller : Option < C > ) -> Self
981
1026
where
@@ -1014,7 +1059,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1014
1059
self . with_optional_controller ( Some ( controller) )
1015
1060
}
1016
1061
1017
- /// Pass in a compute allocation optional value for the canister. If this is [None],
1062
+ /// Pass in a compute allocation optional value for the canister. If this is [` None` ],
1018
1063
/// it will revert the compute allocation to default.
1019
1064
pub fn with_optional_compute_allocation < C , E > ( self , compute_allocation : Option < C > ) -> Self
1020
1065
where
@@ -1039,7 +1084,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1039
1084
self . with_optional_compute_allocation ( Some ( compute_allocation) )
1040
1085
}
1041
1086
1042
- /// Pass in a memory allocation optional value for the canister. If this is [None],
1087
+ /// Pass in a memory allocation optional value for the canister. If this is [` None` ],
1043
1088
/// it will revert the memory allocation to default.
1044
1089
pub fn with_optional_memory_allocation < E , C > ( self , memory_allocation : Option < C > ) -> Self
1045
1090
where
@@ -1064,7 +1109,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1064
1109
self . with_optional_memory_allocation ( Some ( memory_allocation) )
1065
1110
}
1066
1111
1067
- /// Pass in a freezing threshold optional value for the canister. If this is [None],
1112
+ /// Pass in a freezing threshold optional value for the canister. If this is [` None` ],
1068
1113
/// it will revert the freezing threshold to default.
1069
1114
pub fn with_optional_freezing_threshold < E , C > ( self , freezing_threshold : Option < C > ) -> Self
1070
1115
where
@@ -1099,7 +1144,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1099
1144
}
1100
1145
1101
1146
/// Pass in a reserved cycles limit optional value for the canister.
1102
- /// If this is [None], leaves the reserved cycles limit unchanged.
1147
+ /// If this is [` None` ], leaves the reserved cycles limit unchanged.
1103
1148
pub fn with_optional_reserved_cycles_limit < E , C > ( self , limit : Option < C > ) -> Self
1104
1149
where
1105
1150
E : std:: fmt:: Display ,
@@ -1123,7 +1168,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1123
1168
self . with_optional_wasm_memory_limit ( Some ( wasm_memory_limit) )
1124
1169
}
1125
1170
1126
- /// Pass in a Wasm memory limit optional value for the canister. If this is [None],
1171
+ /// Pass in a Wasm memory limit optional value for the canister. If this is [` None` ],
1127
1172
/// leaves the Wasm memory limit unchanged.
1128
1173
pub fn with_optional_wasm_memory_limit < E , C > ( self , wasm_memory_limit : Option < C > ) -> Self
1129
1174
where
@@ -1140,6 +1185,32 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1140
1185
}
1141
1186
}
1142
1187
1188
+ /// Pass in a Wasm memory limit threshold value for the canister.
1189
+ pub fn with_wasm_memory_threshold < C , E > ( self , wasm_memory_threshold : C ) -> Self
1190
+ where
1191
+ E : std:: fmt:: Display ,
1192
+ C : TryInto < WasmMemoryLimit , Error = E > ,
1193
+ {
1194
+ self . with_optional_wasm_memory_threshold ( Some ( wasm_memory_threshold) )
1195
+ }
1196
+
1197
+ /// Pass in a Wasm memory limit threshold value for the canister. If this is [`None`],
1198
+ /// leaves the memory threshold unchanged.
1199
+ pub fn with_optional_wasm_memory_threshold < E , C > ( self , wasm_memory_threshold : Option < C > ) -> Self
1200
+ where
1201
+ E : std:: fmt:: Display ,
1202
+ C : TryInto < WasmMemoryLimit , Error = E > ,
1203
+ {
1204
+ Self {
1205
+ wasm_memory_threshold : wasm_memory_threshold. map ( |limit| {
1206
+ limit
1207
+ . try_into ( )
1208
+ . map_err ( |e| AgentError :: MessageError ( format ! ( "{e}" ) ) )
1209
+ } ) ,
1210
+ ..self
1211
+ }
1212
+ }
1213
+
1143
1214
/// Pass in a log visibility setting for the canister.
1144
1215
pub fn with_log_visibility < C , E > ( self , log_visibility : C ) -> Self
1145
1216
where
@@ -1149,7 +1220,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1149
1220
self . with_optional_log_visibility ( Some ( log_visibility) )
1150
1221
}
1151
1222
1152
- /// Pass in a log visibility optional setting for the canister. If this is [None],
1223
+ /// Pass in a log visibility optional setting for the canister. If this is [` None` ],
1153
1224
/// leaves the log visibility unchanged.
1154
1225
pub fn with_optional_log_visibility < E , C > ( self , log_visibility : Option < C > ) -> Self
1155
1226
where
@@ -1205,6 +1276,11 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1205
1276
Some ( Ok ( x) ) => Some ( Nat :: from ( u64:: from ( x) ) ) ,
1206
1277
None => None ,
1207
1278
} ;
1279
+ let wasm_memory_threshold = match self . wasm_memory_threshold {
1280
+ Some ( Err ( x) ) => return Err ( AgentError :: MessageError ( format ! ( "{x}" ) ) ) ,
1281
+ Some ( Ok ( x) ) => Some ( Nat :: from ( u64:: from ( x) ) ) ,
1282
+ None => None ,
1283
+ } ;
1208
1284
let log_visibility = match self . log_visibility {
1209
1285
Some ( Err ( x) ) => return Err ( AgentError :: MessageError ( format ! ( "{x}" ) ) ) ,
1210
1286
Some ( Ok ( x) ) => Some ( x) ,
@@ -1223,6 +1299,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
1223
1299
freezing_threshold,
1224
1300
reserved_cycles_limit,
1225
1301
wasm_memory_limit,
1302
+ wasm_memory_threshold,
1226
1303
log_visibility,
1227
1304
} ,
1228
1305
} )
0 commit comments