forked from aws-powertools/powertools-lambda-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIdempotency.cs
181 lines (163 loc) · 6.34 KB
/
Idempotency.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using Amazon.Lambda.Core;
using AWS.Lambda.Powertools.Common;
using AWS.Lambda.Powertools.Idempotency.Persistence;
namespace AWS.Lambda.Powertools.Idempotency;
/// <summary>
/// Holds the configuration for idempotency:
/// The persistence layer to use for persisting the request and response of the function (mandatory).
/// The general configurations for idempotency (optional, see {@link IdempotencyConfig.Builder} methods to see defaults values.
/// Use it before the function handler get called.
/// Example: Idempotency.Configure(builder => builder.WithPersistenceStore(...));
/// </summary>
public sealed class Idempotency
{
/// <summary>
/// The general configurations for the idempotency
/// </summary>
public IdempotencyOptions IdempotencyOptions { get; private set; } = null!;
/// <summary>
/// The persistence layer to use for persisting the request and response of the function
/// </summary>
public BasePersistenceStore PersistenceStore { get; private set; } = null!;
/// <summary>
/// Idempotency Constructor
/// </summary>
/// <param name="powertoolsConfigurations"></param>
internal Idempotency(IPowertoolsConfigurations powertoolsConfigurations)
{
powertoolsConfigurations.SetExecutionEnvironment(this);
}
/// <summary>
/// Set Idempotency options
/// </summary>
/// <param name="options"></param>
private void SetConfig(IdempotencyOptions options)
{
IdempotencyOptions = options;
}
/// <summary>
/// Set Persistence Store
/// </summary>
/// <param name="persistenceStore"></param>
private void SetPersistenceStore(BasePersistenceStore persistenceStore)
{
PersistenceStore = persistenceStore;
}
/// <summary>
/// Holds the idempotency Instance:
/// </summary>
public static Idempotency Instance { get; } = new(PowertoolsConfigurations.Instance);
/// <summary>
/// Use this method to configure persistence layer (mandatory) and idempotency options (optional)
/// </summary>
public static void Configure(Action<IdempotencyBuilder> configurationAction)
{
var builder = new IdempotencyBuilder();
configurationAction(builder);
if (builder.Store == null)
{
throw new NullReferenceException("Persistence Layer is null, configure one with 'WithPersistenceStore()'");
}
Instance.SetConfig(builder.Options ?? new IdempotencyOptionsBuilder().Build());
Instance.SetPersistenceStore(builder.Store);
}
/// <summary>
/// Holds ILambdaContext
/// </summary>
public ILambdaContext LambdaContext { get; private set; }
/// <summary>
/// Can be used in a method which is not the handler to capture the Lambda context,
/// to calculate the remaining time before the invocation times out.
/// </summary>
/// <param name="context"></param>
public static void RegisterLambdaContext(ILambdaContext context)
{
Instance.LambdaContext = context;
}
/// <summary>
/// Create a builder that can be used to configure and create <see cref="Idempotency"/>
/// </summary>
public class IdempotencyBuilder
{
/// <summary>
/// Exposes Idempotency options
/// </summary>
internal IdempotencyOptions Options { get; private set; }
/// <summary>
/// Exposes Persistence Store
/// </summary>
internal BasePersistenceStore Store { get; private set; }
/// <summary>
/// Set the persistence layer to use for storing the request and response
/// </summary>
/// <param name="persistenceStore"></param>
/// <returns>IdempotencyBuilder</returns>
public IdempotencyBuilder WithPersistenceStore(BasePersistenceStore persistenceStore)
{
Store = persistenceStore;
return this;
}
/// <summary>
/// Configure Idempotency to use DynamoDBPersistenceStore
/// </summary>
/// <param name="builderAction">The builder being used to configure the <see cref="BasePersistenceStore"/></param>
/// <returns>IdempotencyBuilder</returns>
public IdempotencyBuilder UseDynamoDb(Action<DynamoDBPersistenceStoreBuilder> builderAction)
{
var builder =
new DynamoDBPersistenceStoreBuilder();
builderAction(builder);
Store = builder.Build();
return this;
}
/// <summary>
/// Configure Idempotency to use DynamoDBPersistenceStore
/// </summary>
/// <param name="tableName">The DynamoDb table name</param>
/// <returns>IdempotencyBuilder</returns>
public IdempotencyBuilder UseDynamoDb(string tableName)
{
var builder =
new DynamoDBPersistenceStoreBuilder();
Store = builder.WithTableName(tableName).Build();
return this;
}
/// <summary>
/// Set the idempotency configurations
/// </summary>
/// <param name="builderAction">The builder being used to configure the <see cref="IdempotencyOptions"/>.</param>
/// <returns>IdempotencyBuilder</returns>
public IdempotencyBuilder WithOptions(Action<IdempotencyOptionsBuilder> builderAction)
{
var builder = new IdempotencyOptionsBuilder();
builderAction(builder);
Options = builder.Build();
return this;
}
/// <summary>
/// Set the default idempotency configurations
/// </summary>
/// <param name="options"></param>
/// <returns>IdempotencyBuilder</returns>
public IdempotencyBuilder WithOptions(IdempotencyOptions options)
{
Options = options;
return this;
}
}
}