1616#include < cppless/provider/aws/auth.hpp>
1717#include < cppless/provider/aws/lambda.hpp>
1818#include < cppless/utils/crypto/wrappers.hpp>
19+ #include < cppless/utils/fixed_string.hpp>
20+ #include < cppless/utils/fixed_string_serialization.hpp>
1921#include < cppless/utils/uninitialized.hpp>
2022
23+ #ifndef TARGET_NAME
24+ # define TARGET_NAME " cppless" // NOLINT
25+ #endif
26+
2127namespace cppless ::dispatcher
2228{
2329
30+ namespace aws
31+ {
32+
33+ struct default_config
34+ {
35+ constexpr static std::string_view description;
36+ constexpr static unsigned int memory = 1024 ; // MB
37+ constexpr static unsigned int ephemeral_storage = 512 ; // MB
38+ constexpr static unsigned int timeout = 60 * 5 ; // seconds
39+ };
40+
41+ template <class A , A Description>
42+ struct with_description
43+ {
44+ template <class Base >
45+ struct apply : public Base
46+ {
47+ constexpr static A description = Description;
48+ };
49+ };
50+
51+ template <unsigned int Memory>
52+ struct with_memory
53+ {
54+ template <class Base >
55+ struct apply : public Base
56+ {
57+ constexpr static unsigned int memory = Memory;
58+ };
59+ };
60+
61+ template <unsigned int EphemeralStorage>
62+ struct with_ephemeral_storage
63+ {
64+ template <class Base >
65+ struct apply : public Base
66+ {
67+ constexpr static unsigned int ephemeral_storage = EphemeralStorage;
68+ };
69+ };
70+
71+ template <unsigned int Timeout>
72+ class with_timeout
73+ {
74+ template <class Base >
75+ class apply : public Base
76+ {
77+ constexpr static unsigned int timeout = Timeout;
78+ };
79+ };
80+
81+ template <class ... Modifiers>
82+ class config ;
83+
84+ template <class Modifier , class ... Modifiers>
85+ class config <Modifier, Modifiers...>
86+ : public Modifier::template apply<config<Modifiers...>>
87+ {
88+ };
89+
90+ template <>
91+ class config <> : public default_config
92+ {
93+ };
94+
95+ } // namespace aws
96+
2497template <class Archive = json_binary_archive>
2598class aws_lambda_dispatcher
2699{
27100public:
101+ using default_config = aws::config<>;
28102 using input_archive = typename Archive::input_archive;
29103 using output_archive = typename Archive::output_archive;
30104
31- using task = cppless::task<aws_lambda_dispatcher>;
32- template <class T >
33- using sendable_task = typename task::template sendable<T>;
105+ template <class ... Modifiers>
106+ using task = cppless::task<aws_lambda_dispatcher, aws::config<Modifiers...>>;
34107
35108 explicit aws_lambda_dispatcher (std::string prefix,
36109 cppless::aws::lambda::client client,
@@ -41,6 +114,29 @@ class aws_lambda_dispatcher
41114 {
42115 }
43116
117+ template <class Config >
118+ struct meta_serializer
119+ {
120+ template <unsigned int N>
121+ constexpr static auto serialize (basic_fixed_string<char , N> identifier)
122+ {
123+ using namespace cppless ; // NOLINT
124+ return cppless::encode_base64 (cppless::serialize (
125+ map (kv (" ephemeral_storage" , Config::ephemeral_storage),
126+ kv (" memory" , Config::memory),
127+ kv (" timeout" , Config::timeout),
128+ kv (" identifier" , identifier))));
129+ }
130+
131+ static auto identifier (const std::string& identifier) -> std::string
132+ {
133+ std::stringstream ss;
134+ ss << identifier << " #" << Config::ephemeral_storage << " #"
135+ << Config::memory << " #" << Config::timeout;
136+ return ss.str ();
137+ }
138+ };
139+
44140 template <class Lambda , class Res , class ... Args>
45141 static auto main (int /* argc*/ , char * /* argv*/ []) -> int
46142 {
@@ -139,25 +235,30 @@ class aws_lambda_dispatcher
139235 return *this ;
140236 }
141237
142- template <class Res , class ... Args>
143- auto dispatch (sendable_task<Res(Args...)> & t,
238+ template <class TaskType , class Res , class ... Args>
239+ auto dispatch (TaskType & t,
144240 cppless::shared_future<Res> result_future,
145241 std::tuple<Args...> args) -> int
146242 {
147- using specialized_task_data =
148- task_data<sendable_task<Res (Args...)>, Args...>;
243+ using specialized_task_data = task_data<TaskType, Args...>;
149244
150245 int id = m_next_id++;
151246
152247 auto raw_function_name = t.identifier ();
153- std::string function_name;
248+
249+ std::string function_name = TARGET_NAME;
250+ function_name += " -" ;
154251
155252 evp_md_ctx ctx;
156253 ctx.update (raw_function_name);
157254 auto binary_digest = ctx.final ();
158255
256+ std::string function_hex;
159257 boost::algorithm::hex_lower (binary_digest,
160- std::back_inserter (function_name));
258+ std::back_inserter (function_hex));
259+
260+ // First 8 chars
261+ function_name += function_hex.substr (0 , 8 );
161262
162263 specialized_task_data data {t, args};
163264 auto string_payload = Archive::serialize (data);
0 commit comments