|
| 1 | +local server_id = start_server("maock_subscribe_update_delete.json") |
| 2 | + |
| 3 | +register_service_handler(server_id, "subscribe", "handle_subscribe", 20) |
| 4 | + |
| 5 | +register_service_handler(server_id, "subs-update", "handle_subscribe_update", 20) |
| 6 | + |
| 7 | +register_service_handler(server_id, "subs-del", "handle_ubsubscribe", 20) |
| 8 | + |
| 9 | +local location_header_with_subs_id = {"/some-hardcoded-api-root/some-service/v2/some-operation", "/", "subs_id_placeholder"} |
| 10 | + |
| 11 | +local timestamp_reporting_stat = 0 |
| 12 | + |
| 13 | +local pseudo_uuid_as_worker_thread_id_for_stats_output = generate_uuid_v4() |
| 14 | + |
| 15 | +local number_of_notify_request_sent = 0 |
| 16 | + |
| 17 | +local number_of_notify_request_sent_last_second = 0 |
| 18 | + |
| 19 | +math.randomseed(os.time()) |
| 20 | + |
| 21 | +-- utility functions begin |
| 22 | +local function tokenize_path_and_query(path) |
| 23 | + path = path .. "?" |
| 24 | + results = string.gmatch(path, '([^?]+)') |
| 25 | + tokens = {} |
| 26 | + for res in results do |
| 27 | + table.insert(tokens, res) |
| 28 | + end |
| 29 | + return tokens |
| 30 | +end |
| 31 | + |
| 32 | +local function tokenize_path(path) |
| 33 | + path = path .. "/" |
| 34 | + results = string.gmatch(path, '([^/]+)') |
| 35 | + tokens = {} |
| 36 | + for res in results do |
| 37 | + table.insert(tokens, res) |
| 38 | + end |
| 39 | + return tokens |
| 40 | +end |
| 41 | +-- utility functions end |
| 42 | + |
| 43 | +function handle_subscribe(response_addr, request_headers, request_payload) |
| 44 | + subscription_id = generate_uuid_v4() |
| 45 | + store_value(subscription_id, request_payload) |
| 46 | + location_header_with_subs_id[2] = subscription_id |
| 47 | + response_header = {[":status"] = "201", ["location"] = table.concat(location_header_with_subs_id)} |
| 48 | + send_response(response_addr, response_header, "") |
| 49 | +end |
| 50 | + |
| 51 | +function handle_subscribe_update(response_addr, request_headers, request_payload) |
| 52 | + path = headers[":path"] |
| 53 | + tokens = tokenize_path_and_query(path) |
| 54 | + path_without_query = tokens[1] |
| 55 | + path_tokens = tokenize_path(path_without_query) |
| 56 | + subscription_id = path_tokens[table.getn(path_tokens)] |
| 57 | + subscribe_request_payload = get_value(subscription_id) |
| 58 | + if (subscribe_request_payload == nil) |
| 59 | + then |
| 60 | + response_header = {[":status"] = "404"} |
| 61 | + send_response(response_addr, response_header, "") |
| 62 | + return |
| 63 | + end |
| 64 | + response_header = {[":status"] = "200"} |
| 65 | + send_response(response_addr, response_header, '{"status": "success"}') |
| 66 | + |
| 67 | + -- send notify |
| 68 | + doc = rapidjson.Document() |
| 69 | + ok, message = doc:parse(subscribe_request_payload) |
| 70 | + notify_uri = doc:get("/callback-uri") |
| 71 | + |
| 72 | + notify_headers = parse_uri(notify_uri) |
| 73 | + |
| 74 | + notify_response_headers, notify_response_payload = send_http_request_and_await_response(notify_headers, "") |
| 75 | + |
| 76 | + if (notify_response_headers ~= nil) |
| 77 | + then |
| 78 | + number_of_notify_request_sent = number_of_notify_request_sent + 1 |
| 79 | + end |
| 80 | + now = time_since_epoch() |
| 81 | + if (now - timestamp_reporting_stat > 1000) |
| 82 | + then |
| 83 | + output = string.format("thread: %s, number request sent: %d, tps = %d", pseudo_uuid_as_worker_thread_id_for_stats_output, number_of_notify_request_sent, ((number_of_request_sent - number_of_notify_request_sent_last_second)*1000)/(now - timestamp_reporting_stat)) |
| 84 | + number_of_notify_request_sent_last_second = number_of_notify_request_sent; |
| 85 | + timestamp_reporting_stat = now |
| 86 | + print (output) |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +function handle_ubsubscribe(response_addr, request_headers, request_payload) |
| 91 | + path = headers[":path"] |
| 92 | + tokens = tokenize_path_and_query(path) |
| 93 | + path_without_query = tokens[1] |
| 94 | + path_tokens = tokenize_path(path_without_query) |
| 95 | + subscription_id = path_tokens[table.getn(path_tokens)] |
| 96 | + subscribe_request_payload = get_value(subscription_id) |
| 97 | + if (subscribe_request_payload == nil) |
| 98 | + then |
| 99 | + response_header = {[":status"] = "404"} |
| 100 | + send_response(response_addr, response_header, "") |
| 101 | + return |
| 102 | + end |
| 103 | + response_header = {[":status"] = "204"} |
| 104 | + delete_value(subscription_id) |
| 105 | + send_response(response_addr, response_header, "") |
| 106 | +end |
0 commit comments