-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathh2load_lua.cc
1446 lines (1333 loc) · 48.9 KB
/
h2load_lua.cc
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <numeric>
#include <cctype>
#include <mutex>
#include <iterator>
#include <future>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <boost/asio.hpp>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#ifndef LUA_OK
#define LUA_OK 0
#endif
#include <fstream>
#include <fcntl.h>
#include "h2load_utils.h"
#include "base_client.h"
#include "asio_worker.h"
#include "h2load_lua.h"
#include "asio_util.h"
const static std::string worker_index_str = "worker_index";
const static std::string group_index_str = "group_index";
const static std::string server_id_str = "server_id";
const static std::string inactive_state = "inactive_state";
const static std::string io_service_str = "ios";
const static std::string handler_id_str = "hid";
const static std::string stream_id_str = "sid";
const static std::string dummy_string = "";
thread_local static bool need_to_return_from_c_function = false;
thread_local static size_t number_of_result_to_return;
void set_group_id(lua_State* L, size_t group_id)
{
lua_pushlightuserdata(L, (void*)group_index_str.c_str());
lua_pushinteger(L, group_id);
lua_rawset(L, LUA_REGISTRYINDEX);
}
void set_worker_index(lua_State* L, size_t worker_index)
{
lua_pushlightuserdata(L, (void*)worker_index_str.c_str());
lua_pushinteger(L, worker_index);
lua_rawset(L, LUA_REGISTRYINDEX);
}
size_t get_group_id(lua_State* L)
{
size_t group_id = 0;
auto top_before = lua_gettop(L);
lua_pushlightuserdata(L, (void*)group_index_str.c_str());
lua_gettable(L, LUA_REGISTRYINDEX);
group_id = lua_tonumber(L, -1);
lua_settop(L, top_before);
return group_id;
}
size_t get_worker_index(lua_State* L)
{
auto top_before = lua_gettop(L);
lua_pushlightuserdata(L, (void*)worker_index_str.c_str());
lua_gettable(L, LUA_REGISTRYINDEX);
auto worker_index = lua_tonumber(L, -1);
lua_settop(L, top_before);
return worker_index;
}
void set_server_id(lua_State* L, std::string server_id)
{
lua_pushlightuserdata(L, (void*)server_id_str.c_str());
lua_pushlstring(L, server_id.c_str(), server_id.size());
lua_rawset(L, LUA_REGISTRYINDEX);
}
std::string get_server_id(lua_State* L)
{
std::string server_id;
auto top_before = lua_gettop(L);
lua_pushlightuserdata(L, (void*)server_id_str.c_str());
lua_gettable(L, LUA_REGISTRYINDEX);
size_t len = 0;
const char* id = lua_tolstring(L, -1, &len);
server_id.assign(id, len);
lua_settop(L, top_before);
return server_id;
}
void set_passive(lua_State* L)
{
lua_pushlightuserdata(L, (void*)inactive_state.c_str());
lua_pushboolean(L, 1);
lua_rawset(L, LUA_REGISTRYINDEX);
}
bool is_passive(lua_State* L)
{
auto top_before = lua_gettop(L);
lua_pushlightuserdata(L, (void*)inactive_state.c_str());
lua_gettable(L, LUA_REGISTRYINDEX);
auto inactive = lua_toboolean(L, -1);
lua_settop(L, top_before);
return inactive;
}
Lua_State_Data& get_lua_state_data(lua_State* L)
{
return get_runtime_data(L).lua_state_data[L];
}
std::mutex& get_lua_group_config_mutex(size_t group_id)
{
static std::vector<std::unique_ptr<std::mutex>> lua_group_config_mutexes;
auto min_required_size = group_id + 1;
if (min_required_size > lua_group_config_mutexes.size())
{
for (int i = 0; i < (min_required_size - lua_group_config_mutexes.size()); i++)
{
lua_group_config_mutexes.emplace_back(std::make_unique<std::mutex>());
}
}
return *lua_group_config_mutexes[group_id].get();
}
Lua_Group_Config& get_lua_group_config(size_t group_id)
{
static std::vector<Lua_Group_Config> lua_group_configs;
auto min_required_size = group_id + 1;
if (min_required_size > lua_group_configs.size())
{
lua_group_configs.resize(min_required_size);
}
return lua_group_configs[group_id];
}
void start_test_group(size_t group_id)
{
auto& lua_group_config = get_lua_group_config(group_id);
for (size_t worker_index = 0; worker_index < lua_group_config.data_per_worker_thread.size(); worker_index++)
{
auto coroutine_references = lua_group_config.data_per_worker_thread[worker_index].coroutine_references;
if (coroutine_references.empty())
{
continue;
}
auto start_lua_states = [coroutine_references]()
{
for (auto L : coroutine_references)
{
lua_resume_wrapper(L.first, 0);
}
};
lua_group_config.workers[worker_index]->get_io_context().post(start_lua_states);
}
}
void setup_test_group(size_t group_id)
{
auto& lua_group_config = get_lua_group_config(group_id);
/*
std::cerr << "number of workers: " << lua_group_config.number_of_workers
<< ", nummber of coroutines: " << lua_group_config.number_of_parallel_lua_coroutines
<< ", nummber of parallel connections to same host: "
<< lua_group_config.number_of_client_to_same_host_in_one_worker
<<std::endl;
*/
lua_group_config.data_per_worker_thread.clear();
for (int i = 0; i < lua_group_config.number_of_workers; i++)
{
auto lua_state = std::shared_ptr<lua_State>(luaL_newstate(), &lua_close);
init_new_lua_state(lua_state.get());
set_group_id(lua_state.get(), group_id);
set_worker_index(lua_state.get(), i);
Data_Per_Worker_Thread data;
lua_group_config.data_per_worker_thread.push_back(data);
get_lua_state_data(lua_state.get()).unique_id_within_group = lua_group_config.number_of_parallel_lua_coroutines;
lua_group_config.data_per_worker_thread[i].lua_main_states_per_worker = std::move(lua_state);
}
init_workers(group_id);
for (int i = 0; i < lua_group_config.number_of_parallel_lua_coroutines; i++)
{
auto worker_index = i % lua_group_config.number_of_workers;
auto parent_lua_state = lua_group_config.data_per_worker_thread[worker_index].lua_main_states_per_worker.get();
if (!lua_checkstack(parent_lua_state, 1))
{
std::cerr << "no enough space in stack" << std::endl;
exit(1);
}
lua_State* cL = lua_newthread(parent_lua_state);
get_lua_state_data(cL).unique_id_within_group = i;
lua_group_config.data_per_worker_thread[worker_index].coroutine_references[cL] = luaL_ref(parent_lua_state,
LUA_REGISTRYINDEX);
luaL_loadstring(cL, lua_group_config.lua_script.c_str());
}
}
int setup_parallel_test(lua_State* L)
{
size_t number_of_coroutines = 0;
size_t number_of_client = 0;
size_t number_of_workers = 0;
int top = lua_gettop(L);
if (top == 3)
{
number_of_coroutines = lua_tointeger(L, -1);
lua_pop(L, 1);
number_of_client = lua_tointeger(L, -1);
lua_pop(L, 1);
number_of_workers = lua_tointeger(L, -1);
lua_pop(L, 1);
}
auto group_id = get_group_id(L);
auto& lua_group_config = get_lua_group_config(group_id);
if ((number_of_coroutines > 0) &&
(lua_group_config.config_initialized == false) &&
(lua_group_config.lua_script.size() > 0))
{
lua_group_config.config_initialized = true;
lua_group_config.number_of_parallel_lua_coroutines = number_of_coroutines;
lua_group_config.number_of_client_to_same_host_in_one_worker = number_of_client;
lua_group_config.number_of_workers = number_of_workers;
setup_test_group(group_id);
return lua_yield(L, 0);
}
else
{
lua_pushinteger(L, get_lua_state_data(L).unique_id_within_group);
return 1;
}
}
void init_new_lua_state(lua_State* L)
{
luaL_openlibs(L);
lua_register(L, "make_connection", make_connection);
lua_register(L, "send_http_request", send_http_request);
lua_register(L, "await_response", await_response);
lua_register(L, "send_http_request_and_await_response", send_http_request_and_await_response);
lua_register(L, "forward_http_request_and_await_response", forward_http_request_and_await_response);
lua_register(L, "send_grpc_request_and_await_response", send_grpc_request_and_await_response);
lua_register(L, "setup_parallel_test", setup_parallel_test);
lua_register(L, "sleep_for_ms", sleep_for_ms);
lua_register(L, "start_server", start_server);
lua_register(L, "stop_server", stop_server);
lua_register(L, "register_service_handler", register_service_handler);
lua_register(L, "send_response", send_response);
lua_register(L, "forward_response", forward_response);
lua_register(L, "wait_for_message", wait_for_message);
lua_register(L, "resolve_hostname", resolve_hostname);
init_new_lua_state_with_common_apis(L);
}
void stop_workers(size_t number_of_groups)
{
for (int group_index = 0; group_index < number_of_groups; group_index++)
for (int i = 0; i < get_lua_group_config(group_index).workers.size(); i++)
{
auto worker_ptr = get_lua_group_config(group_index).workers[i].get();
auto stop_user_timer_and_clients = [worker_ptr]()
{
worker_ptr->prepare_worker_stop();
};
worker_ptr->get_io_context().post(stop_user_timer_and_clients);
auto dummy = std::move(get_lua_group_config(group_index).works[i]);
}
}
bool is_test_finished(size_t number_of_test_groups)
{
auto all_coroutines_finished = [](size_t group_id)
{
auto& lua_group_config = get_lua_group_config(group_id);
return (lua_group_config.number_of_parallel_lua_coroutines == lua_group_config.number_of_finished_coroutins);
};
auto server_stopped = [](size_t group_id)
{
if (get_lua_group_config(group_id).server_running)
{
return false;
}
return true;
};
for (int i = 0; i < number_of_test_groups; i++)
{
if (!all_coroutines_finished(i) || !server_stopped(i))
{
return false;
}
}
return true;
}
void load_and_run_lua_script(const std::vector<std::string>& lua_scripts, h2load::Config& config)
{
std::vector<lua_State*> bootstrap_lua_states;
// init all config and mutex to avoid runtime vector reallocation
for (size_t i = 0; i < lua_scripts.size(); i++)
{
get_lua_group_config(i);
get_lua_group_config_mutex(i);
}
// Use bootstrap L for each group to call setup_parallel_test
for (size_t i = 0; i < lua_scripts.size(); i++)
{
get_lua_group_config(i).lua_script = lua_scripts[i];
get_lua_group_config(i).config_template = config;
lua_State* L = luaL_newstate();
init_new_lua_state(L);
bootstrap_lua_states.push_back(L);
set_group_id(L, i);
set_worker_index(L, 0);
get_lua_state_data(L).unique_id_within_group = 0;
luaL_loadstring(L, lua_scripts[i].c_str());
// if setup_parallel_test is not called, meaning no new coroutine is created,
// then bootstrap L is to execute the script directly for each group
auto retCode = lua_resume_wrapper(L, 0);
if (LUA_OK == retCode)
{
// setup_parallel_test is not called, script done
}
else if (LUA_YIELD == retCode)
{
// setup_parallel_test might have been called
}
else
{
size_t len;
std::string error_str;
auto str_ptr = lua_tolstring(L, -2, &len);
error_str.assign(str_ptr, len);
lua_pop(L, 1);
std::cerr << "error running lua script:" << std::endl << error_str << std::endl << lua_scripts[i] << std::endl;
}
}
for (size_t i = 0; i < lua_scripts.size(); i++)
{
// if setup_parallel_test is not called
// coroutine_references is empty for that group
// start_test_group will do nothing
start_test_group(i);
}
size_t number_of_groups = lua_scripts.size();
while (!is_test_finished(number_of_groups))
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
/* std::cerr<<"test finished"<<std::endl; */
for (auto& L : bootstrap_lua_states)
{
lua_close(L);
}
bootstrap_lua_states.clear();
stop_workers(number_of_groups);
// to let all workers clean up and quit
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
void init_workers(size_t group_id)
{
auto& lua_group_config = get_lua_group_config(group_id);
if (lua_group_config.workers.size())
{
return;
}
static h2load::Config conf;
auto init_config = [&lua_group_config]()
{
conf.ciphers = lua_group_config.config_template.ciphers;
conf.max_concurrent_streams = lua_group_config.config_template.max_concurrent_streams;
conf.window_bits = lua_group_config.config_template.window_bits;
conf.connection_window_bits = lua_group_config.config_template.connection_window_bits;
conf.conn_active_timeout = lua_group_config.config_template.conn_active_timeout;
conf.conn_inactivity_timeout = lua_group_config.config_template.conn_inactivity_timeout;
conf.no_tls_proto = lua_group_config.config_template.no_tls_proto;
conf.header_table_size = lua_group_config.config_template.header_table_size;
conf.encoder_header_table_size = lua_group_config.config_template.encoder_header_table_size;
conf.verbose = lua_group_config.config_template.verbose;
conf.npn_list = lua_group_config.config_template.npn_list;
conf.stream_timeout_in_ms = lua_group_config.config_template.stream_timeout_in_ms;
conf.json_config_schema.ca_cert = lua_group_config.config_template.json_config_schema.ca_cert;
conf.json_config_schema.client_cert = lua_group_config.config_template.json_config_schema.client_cert;
conf.json_config_schema.private_key = lua_group_config.config_template.json_config_schema.private_key;
conf.json_config_schema.cert_verification_mode =
lua_group_config.config_template.json_config_schema.cert_verification_mode;
conf.json_config_schema.max_tls_version = lua_group_config.config_template.json_config_schema.max_tls_version;
// conf.json_config_schema.interval_to_send_ping = 5;
// conf.json_config_schema.connection_retry_on_disconnect = true;
Request request;
Scenario scenario;
scenario.requests.push_back(request);
conf.json_config_schema.scenarios.push_back(scenario);
return true;
};
static auto init_config_ret_code = init_config();
for (int i = 0; i < lua_group_config.number_of_workers; i++)
{
lua_group_config.workers.emplace_back(std::make_shared<h2load::asio_worker>(0, 0xFFFFFFFF, 1, 0, 1000, &conf));
}
for (int i = 0; i < lua_group_config.workers.size(); i++)
{
lua_group_config.works.emplace_back(lua_group_config.workers[i]->get_io_context());
}
auto thread_func = [](h2load::asio_worker * worker_ptr)
{
worker_ptr->run_event_loop();
};
for (int i = 0; i < lua_group_config.workers.size(); i++)
{
auto worker_ptr = lua_group_config.workers[i].get();
std::thread worker_thread(thread_func, worker_ptr);
worker_thread.detach();
auto start_user_timer_service = [worker_ptr]()
{
worker_ptr->start_tick_timer();
};
worker_ptr->get_io_context().post(start_user_timer_service);
}
}
h2load::asio_worker* get_worker(lua_State* L)
{
size_t group_id = get_group_id(L);
if (get_lua_group_config(group_id).workers.size() == 0)
{
init_workers(group_id);
}
return get_lua_group_config(group_id).workers[get_worker_index(L)].get();
}
Data_Per_Worker_Thread& get_runtime_data(lua_State* L)
{
return get_lua_group_config(get_group_id(L)).data_per_worker_thread[get_worker_index(L)];
}
int32_t _make_connection(lua_State* L, const std::string& uri,
std::function<void(bool, h2load::base_client*)> connected_callback,
const std::string& proto)
{
auto worker = get_worker(L);
/*
http_parser_url u {};
if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0 ||
!util::has_uri_field(u, UF_SCHEMA) || !util::has_uri_field(u, UF_HOST))
{
std::cerr << "invalid uri:" << uri << std::endl;
connected_callback(false, nullptr);
return -1;
}
*/
auto group_id = get_group_id(L);
auto clients_needed = get_lua_group_config(group_id).number_of_client_to_same_host_in_one_worker;
auto run_inside_worker = [uri, connected_callback, worker, proto, clients_needed]()
{
http_parser_url u {};
if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0 ||
!util::has_uri_field(u, UF_SCHEMA) || !util::has_uri_field(u, UF_HOST))
{
std::cerr << "invalid uri:" << uri << std::endl;
return connected_callback(false, nullptr);
}
std::string schema = util::get_uri_field(uri.c_str(), u, UF_SCHEMA).str();
std::string authority = util::get_uri_field(uri.c_str(), u, UF_HOST).str();
uint32_t port;
if (util::has_uri_field(u, UF_PORT))
{
port = u.port;
}
else
{
port = util::get_default_port(uri.c_str(), u);
}
authority.append(":").append(std::to_string(port));
auto client_id = worker->next_client_id;
std::string base_uri = schema;
base_uri.append("://").append(authority);
auto& clients = worker->get_client_pool();
if (clients[base_uri].size() < clients_needed)
{
auto client = worker->create_new_client(0xFFFFFFFF);
worker->check_in_client(client);
// pre-mature insert to block excessive client creation during test start
clients[base_uri].insert(client.get());
client->install_connected_callback(connected_callback);
client->set_prefered_authority(authority);
client->preferred_non_tls_proto = proto;
client->connect_to_host(schema, authority);
}
else
{
thread_local static std::random_device rand_dev;
thread_local static std::mt19937 generator(rand_dev());
thread_local static std::uniform_int_distribution<uint64_t> distr(0, clients_needed - 1);
auto client_index = distr(generator);
auto iter = clients[base_uri].begin();
std::advance(iter, client_index);
auto client = *iter;
if (h2load::CLIENT_IDLE == client->state)
{
client->install_connected_callback(connected_callback);
client->connect_to_host(schema, authority);
}
else if (h2load::CLIENT_CONNECTING == client->state)
{
client->install_connected_callback(connected_callback);
}
else
{
connected_callback(true, client);
}
}
};
worker->get_io_context().post(run_inside_worker);
//run_inside_worker();
return 0;
}
int make_connection(lua_State* L)
{
enter_c_function(L);
auto connected_callback = [L](bool success, h2load::base_client * client)
{
lua_pushinteger(L, success ? client->get_client_unique_id() : -1);
lua_resume_if_yielded(L, 1);
};
std::string base_uri;
if ((lua_gettop(L) == 1) && (lua_type(L, -1) == LUA_TSTRING))
{
size_t len;
const char* str = lua_tolstring(L, -1, &len);
base_uri.assign(str, len);
lua_pop(L, 1);
}
else
{
std::cerr << "invalid argument: " << __FUNCTION__ << std::endl;
}
lua_settop(L, 0);
_make_connection(L, base_uri, connected_callback, dummy_string);
return leave_c_function(L);
}
void update_orig_dst_and_proto(std::map<std::string, std::string, ci_less>& headers, std::string& payload,
std::string& orig_dst,
std::string& proto)
{
if (headers.count(h2load::x_envoy_original_dst_host_header))
{
orig_dst = headers[h2load::x_envoy_original_dst_host_header];
headers.erase(h2load::x_envoy_original_dst_host_header);
}
if (headers.count(h2load::x_proto_to_use))
{
proto = headers[h2load::x_proto_to_use];
headers.erase(h2load::x_proto_to_use);
}
}
// TODO: this is called every request, should be optimized
void update_proto(std::map<std::string, std::string, ci_less>& headers, std::string& payload,
std::string& orig_dst,
std::string& proto)
{
if (headers.count(h2load::x_proto_to_use))
{
proto = headers[h2load::x_proto_to_use];
headers.erase(h2load::x_proto_to_use);
}
}
int send_http_request(lua_State* L)
{
auto request_prep = [](std::map<std::string, std::string, ci_less>& headers, std::string & payload,
std::string & orig_dst, std::string & proto)
{
update_proto(headers, payload, orig_dst, proto);
};
enter_c_function(L);
auto request_sent = [L](int32_t stream_id, h2load::base_client * client)
{
if (stream_id && client)
{
client->queue_stream_for_user_callback(stream_id);
lua_pushinteger(L, client->get_client_unique_id());
lua_pushinteger(L, stream_id);
}
else
{
lua_pushinteger(L, -1);
lua_pushinteger(L, -1);
}
lua_resume_if_yielded(L, 2);
};
_send_http_request(L, request_prep, request_sent);
return leave_c_function(L);
}
Request_Sent_cb await_response_request_sent_cb_generator(lua_State* L)
{
return [L](int32_t stream_id, h2load::base_client * client)
{
if (stream_id > 0 && client)
{
client->queue_stream_for_user_callback(stream_id);
client->pass_response_to_lua(stream_id, L);
}
else
{
lua_createtable(L, 0, 0);
lua_pushlstring(L, "", 0);
lua_createtable(L, 0, 0);
lua_resume_if_yielded(L, 3);
}
};
}
int send_http_request_and_await_response(lua_State* L)
{
auto request_prep = [](std::map<std::string, std::string, ci_less>& headers, std::string & payload,
std::string & orig_dst, std::string & proto)
{
update_proto(headers, payload, orig_dst, proto);
};
enter_c_function(L);
_send_http_request(L, request_prep, await_response_request_sent_cb_generator(L));
return leave_c_function(L);
}
int forward_http_request_and_await_response(lua_State* L)
{
auto request_prep = [](std::map<std::string, std::string, ci_less>& headers, std::string & payload,
std::string & orig_dst, std::string & proto)
{
update_orig_dst_and_proto(headers, payload, orig_dst, proto);
};
enter_c_function(L);
_send_http_request(L, request_prep, await_response_request_sent_cb_generator(L));
return leave_c_function(L);
}
void format_length_prefixed_message(std::string& payload)
{
std::vector<char> length_prefixed_message;
uint32_t len = htonl(payload.size());
length_prefixed_message.resize(1 + sizeof(len) + payload.size());
length_prefixed_message[0] = '\0';
memcpy((void*)&length_prefixed_message[1], &len, sizeof(len));
memcpy((void*)&length_prefixed_message[1 + sizeof(len)], payload.c_str(), payload.size());
payload.assign(&length_prefixed_message[0], length_prefixed_message.size());
}
int send_grpc_request_and_await_response(lua_State* L)
{
auto request_prep = [](std::map<std::string, std::string, ci_less>& headers, std::string & payload,
std::string & orig_dst, std::string & proto)
{
headers[h2load::method_header] = "POST";
headers["content-type"] = "application/grpc";
headers["te"] = "trailers";
headers["grpc-accept-encoding"] = "identity"; // TODO:
format_length_prefixed_message(payload);
};
enter_c_function(L);
_send_http_request(L, request_prep, await_response_request_sent_cb_generator(L));
return leave_c_function(L);
}
int sleep_for_ms(lua_State* L)
{
enter_c_function(L);
int64_t ms_to_sleep = 0;
std::string stack;
int top = lua_gettop(L);
if (top == 1)
{
ms_to_sleep = lua_tointeger(L, -1);
lua_pop(L, 1);
}
lua_settop(L, 0);
if (ms_to_sleep <= 0)
{
return 0;
}
auto wakeup_me = [L]()
{
lua_resume_wrapper(L, 0);
};
auto worker = get_worker(L);
auto run_in_worker = [worker, wakeup_me, ms_to_sleep]()
{
worker->enqueue_user_timer(ms_to_sleep, wakeup_me);
};
worker->get_io_context().post(run_in_worker);
//run_in_worker();
return leave_c_function(L);
}
int await_response(lua_State* L)
{
enter_c_function(L);
uint64_t client_unique_id = -1;
int32_t stream_id = -1;
int top = lua_gettop(L);
if (top == 2)
{
stream_id = lua_tointeger(L, -1);
lua_pop(L, 1);
client_unique_id = lua_tointeger(L, -1);
lua_pop(L, 1);
}
lua_settop(L, 0);
auto worker = get_worker(L);
auto retrieve_response_cb = [worker, client_unique_id, stream_id, L]()
{
auto client_iter = worker->get_client_ids().find(client_unique_id);
if (client_iter != worker->get_client_ids().end())
{
client_iter->second->pass_response_to_lua(stream_id, L);
return;
}
lua_createtable(L, 0, 1);
lua_pushlstring(L, "", 0);
lua_resume_if_yielded(L, 2);
};
worker->get_io_context().post(retrieve_response_cb);
//retrieve_response_cb();
return leave_c_function(L);
}
int _send_http_request(lua_State* L, Request_Preprocessor request_preprocessor,
std::function<void(int32_t, h2load::base_client*)> request_sent_callback)
{
auto argument_error = false;
std::string payload;
std::map<std::string, std::string, ci_less> headers;
static std::map<std::string, std::string, ci_less> dummyHeaders;
uint32_t timeout_interval_in_ms = 0;
int top = lua_gettop(L);
for (int i = 0; i < top; i++)
{
switch (lua_type(L, -1))
{
case LUA_TSTRING:
{
size_t len;
const char* str = lua_tolstring(L, -1, &len);
payload.assign(str, len);
break;
}
case LUA_TTABLE:
{
lua_pushnil(L);
while (lua_next(L, -2) != 0)
{
size_t len;
/* uses 'key' (at index -2) and 'value' (at index -1) */
if ((LUA_TSTRING != lua_type(L, -2)) || (LUA_TSTRING != lua_type(L, -1)))
{
std::cerr << __FUNCTION__ << ": invalid http header" << std::endl;
argument_error = true;
break;
}
const char* k = lua_tolstring(L, -2, &len);
std::string key(k, len);
const char* v = lua_tolstring(L, -1, &len);
std::string value(v, len);
//util::inp_strlower(key);
headers[key] = value;
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
break;
}
case LUA_TNUMBER:
{
timeout_interval_in_ms = lua_tointeger(L, -1);
break;
}
default:
{
std::cerr << __FUNCTION__ << ": invalid parameter passed in" << std::endl;
argument_error = true;
break;
}
}
lua_pop(L, 1);
}
lua_settop(L, 0);
if (!argument_error)
{
std::string original_dst;
std::string proto;
if (request_preprocessor)
{
request_preprocessor(headers, payload, original_dst, proto);
}
std::string schema = headers[h2load::scheme_header];
headers.erase(h2load::scheme_header);
std::string authority = headers[h2load::authority_header];
headers.erase(h2load::authority_header);
std::string method = headers[h2load::method_header];
headers.erase(h2load::method_header);
std::string path = headers[h2load::path_header];
headers.erase(h2load::path_header);
std::string base_uri;
if (original_dst.size())
{
if (original_dst.find("http") != std::string::npos)
{
base_uri = original_dst;
}
else
{
base_uri.append(schema).append("://").append(original_dst);
}
}
else
{
base_uri.append(schema).append("://").append(authority);
}
h2load::asio_worker* worker;
worker = get_worker(L);
auto connected_callback = [payload, schema, authority, method, path, headers, request_sent_callback,
timeout_interval_in_ms, proto](bool success, h2load::base_client * client)
{
if (!success)
{
request_sent_callback(-1, nullptr);
return;
}
h2load::Request_Data request_to_send(0);
request_to_send.request_sent_callback = request_sent_callback;
request_to_send.string_collection.emplace_back(payload);
request_to_send.req_payload = &(request_to_send.string_collection.back());
request_to_send.string_collection.emplace_back(method);
request_to_send.method = &(request_to_send.string_collection.back());
request_to_send.string_collection.emplace_back(path);
request_to_send.path = &(request_to_send.string_collection.back());
request_to_send.string_collection.emplace_back(authority);
request_to_send.authority = &(request_to_send.string_collection.back());
request_to_send.string_collection.emplace_back(schema);
request_to_send.schema = &(request_to_send.string_collection.back());
request_to_send.req_headers_of_individual = std::move(headers);
request_to_send.req_headers_from_config = &dummyHeaders;
request_to_send.stream_timeout_in_ms = timeout_interval_in_ms;
client->requests_to_submit.emplace_back(std::move(request_to_send));
client->submit_request();
};
_make_connection(L, base_uri, connected_callback, proto);
}
else
{
request_sent_callback(-1, nullptr);
}
return 0;
}
int lua_resume_if_yielded(lua_State* L, int nargs)
{
if (LUA_YIELD == lua_status(L))
{
return lua_resume_wrapper(L, nargs);
}
else
{
number_of_result_to_return = nargs;
need_to_return_from_c_function = true;
return nargs;
}
}
void enter_c_function(lua_State* L)
{
number_of_result_to_return = 0;
need_to_return_from_c_function = false;
}
uint64_t leave_c_function(lua_State* L)
{
if (need_to_return_from_c_function)
{
return number_of_result_to_return;
}
else
{
return lua_yield(L, 0);
}
}
bool is_coroutine_with_unique_id(lua_State* L)
{
auto group_id = get_group_id(L);
auto& lua_group_config = get_lua_group_config(group_id);
auto worker_index = get_worker_index(L);
if (get_lua_state_data(L).unique_id_within_group >= 0)
{
return true;
}
return false;
}
bool is_coroutine_to_be_returned_to_pool(lua_State* L)
{
return (is_passive(L));
}
int lua_resume_wrapper(lua_State* L, int nargs)
{
auto retCode = lua_resume(L, nargs);
if (LUA_YIELD != retCode)
{
if (LUA_OK != retCode)
{
const char* error = lua_tostring(L, -1);
std::cerr<<std::endl<<std::endl<<std::flush;
std::cerr<<"ERROR: error executing lua script: "<<error<<std::endl<<std::flush;
exit(1);
}
auto group_id = get_group_id(L);
auto& lua_group_config = get_lua_group_config(group_id);
auto worker_index = get_worker_index(L);
if (is_coroutine_with_unique_id(L))
{
std::lock_guard<std::mutex> guard(get_lua_group_config_mutex(group_id));
lua_group_config.number_of_finished_coroutins++;
}
if (get_runtime_data(L).coroutine_references.count(L))
{
if (is_coroutine_to_be_returned_to_pool(L) && (LUA_OK == retCode))
{
get_runtime_data(L).lua_coroutine_pools.push_back(L);
}
else
{
auto parent_lua_state = get_runtime_data(L).lua_main_states_per_worker.get();
luaL_unref(parent_lua_state, LUA_REGISTRYINDEX, get_runtime_data(L).coroutine_references[L]);
get_runtime_data(L).coroutine_references.erase(L);
//lua_gc(parent_lua_state, LUA_GCCOLLECT, 0);
get_runtime_data(L).lua_state_data.erase(L);
}
}
}
return retCode;
}
H2Server_Config_Schema config_schema;
int start_server(lua_State* L)
{
std::string config_file_name;