# PRC-10 in-process rate limiter, built in steps. Each step is the whole
# program at that point in the walkthrough, and is built and tested on its own.
#   step-1  a token bucket per client key: integer refill that carries part
#           tokens, lazy refill on access, retry-after hints, an injected
#           clock, a bounded table that drops only full buckets, one mutex
#   step-2  change: per-client tiers (free, paid, ...) that can change while
#           requests are in flight, never giving a client more than its
#           capacity in one burst
# The tests start real threads, so they carry the concurrency label and the
# tsan preset runs them. This example is also built with -Wconversion and
# -Wshadow (PUBLIC, so its tests and demos are too).
find_package(Threads REQUIRED)

set(prc_rate_sources bucket.cpp limiter.cpp)
foreach(step IN ITEMS 1 2)
  list(TRANSFORM prc_rate_sources PREPEND step-${step}/ OUTPUT_VARIABLE sources)
  add_library(prc_rate_step${step} STATIC ${sources})
  target_include_directories(prc_rate_step${step} PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/step-${step})
  target_compile_options(prc_rate_step${step} PUBLIC -Wconversion -Wshadow)
  target_link_libraries(prc_rate_step${step} PUBLIC Threads::Threads)
  lld_add_test(prc_rate_step${step}_tests
    SOURCES step-${step}/tests/test_step${step}.cpp LABELS unit concurrency)
  target_link_libraries(prc_rate_step${step}_tests PRIVATE prc_rate_step${step})
  lld_add_example(prc_rate_step${step}_demo SOURCES step-${step}/main.cpp
    EXPECTED_OUTPUT step-${step}/expected_output.txt)
  target_link_libraries(prc_rate_step${step}_demo PRIVATE prc_rate_step${step})
endforeach()
