Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Slow down successive (same) exercise execution via throttle #818

Merged
merged 13 commits into from
Sep 4, 2024
17 changes: 8 additions & 9 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: windows-latest, r: '3.6'}
- {os: ubuntu-20.04, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-20.04, r: 'release'}
- {os: ubuntu-20.04, r: 'oldrel-1'}
- {os: ubuntu-20.04, r: 'oldrel-2'}
- {os: ubuntu-20.04, r: 'oldrel-3'}
- {os: ubuntu-20.04, r: 'oldrel-4'}
- { os: macOS-latest, r: "release" }
- { os: windows-latest, r: "release" }
- { os: ubuntu-20.04, r: "devel", http-user-agent: "release" }
- { os: ubuntu-20.04, r: "release" }
- { os: ubuntu-20.04, r: "oldrel-1" }
- { os: ubuntu-20.04, r: "oldrel-2" }
- { os: ubuntu-20.04, r: "oldrel-3" }
- { os: ubuntu-20.04, r: "oldrel-4" }

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ Config/Needs/coverage: covr
Config/Needs/website: pkgdown, tidyverse/tidytemplate
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
SystemRequirements: pandoc (>= 1.14) - http://pandoc.org
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# learnr (development version)

- Added a new option, `tutorial.exercise.throttle`, to slow down successive exercise execution. This option should be set to the number of seconds a user will have to wait between performing code executions. The option defaults to 1 second to deter rapid code executions. To disable submission throttling, call `options(tutorial.exercise.throttle = 0)` within your setup chunk. (@internaut, #818)

- Removed dependency on ellipsis (@olivroy, #809)

- Added Norwegian translation contributed by @jonovik. (#806)

# learnr 0.11.5
Expand Down
6 changes: 5 additions & 1 deletion R/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ setup_exercise_handler <- function(exercise_rx, session) {
# setup reactive values for return
rv <- reactiveValues(triggered = 0, result = NULL)

# throttle option to slow down successive exercise execution requests
throttle_s <- getOption("tutorial.exercise.throttle", 1) # in seconds
if (is.numeric(throttle_s) && throttle_s > 0) {
exercise_rx <- throttle(exercise_rx, throttle_s * 1000) # in milliseconds
}
# observe input
observeEvent(exercise_rx(), {

# get exercise from app
exercise <- exercise_rx()
# Add tutorial information
Expand Down
14 changes: 14 additions & 0 deletions vignettes/articles/exercises.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ This option can also be set either globally or per-chunk:
insert_snippet("exerciseeval")
```

To prevent server overload caused by users rapidly clicking the "Run Code" button, you can set a delay between exercise code submissions using the `tutorial.exercise.throttle` option (in seconds). This will ensure that the same user can only start the evaluation of the same exercise per `tutorial.exercise.throttle` seconds. This option is set globally and the default is a throttle of of 1 second.

```{r snippet-exercisethrottle, eval = FALSE}
# Set exercise submission throttle time to 5 seconds
options("tutorial.exercise.throttle" = 5)
```

To disable of this behavior, you can set the option to 0:

```{r snippet-exercisethrottleoff, eval = FALSE}
# Disable exercise submission throttle
options("tutorial.exercise.throttle" = 0)
```

## Exercise Setup {#exercise-setup}

Code chunks with `exercise=TRUE` are evaluated within standalone environments.
Expand Down
Loading