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: add request timeout slider #57

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"net"
"net/http"
"os"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
Expand Down Expand Up @@ -67,6 +69,8 @@
}
}

const DEFAULT_CHECK_TIMEOUT = 60

func startServer(ctx context.Context, d *daemon, tcpListener, metricsUsername, metricPassword string) error {
log.Printf("Starting %s %s\n", name, version)
l, err := net.Listen("tcp", tcpListener)
Expand All @@ -91,18 +95,31 @@

maStr := r.URL.Query().Get("multiaddr")
cidStr := r.URL.Query().Get("cid")
timeoutStr := r.URL.Query().Get("timeoutSeconds")

if cidStr == "" {
err = errors.New("missing 'cid' argument")
err = errors.New("missing 'cid' query parameter")

Check warning on line 101 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L101

Added line #L101 was not covered by tests
}

timeout := DEFAULT_CHECK_TIMEOUT
if timeoutStr != "" {
timeout, err = strconv.Atoi(timeoutStr)
if err != nil {
http.Error(w, "Invalid timeout value (in seconds)", http.StatusBadRequest)
return

Check warning on line 109 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L106-L109

Added lines #L106 - L109 were not covered by tests
}
}

log.Printf("Checking %s with timeout %d seconds", cidStr, timeout)
withTimeout, cancel := context.WithTimeout(r.Context(), time.Duration(timeout)*time.Second)
defer cancel()
var err error
var data interface{}

if maStr == "" {
data, err = d.runCidCheck(r.Context(), cidStr)
data, err = d.runCidCheck(withTimeout, cidStr)
} else {
data, err = d.runPeerCheck(r.Context(), maStr, cidStr)
data, err = d.runPeerCheck(withTimeout, maStr, cidStr)
}

if err == nil {
Expand Down
17 changes: 17 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ <h1 class="dib pa3 ma0 lh-tight">
<datalist id="defaultBackendURLs">
<option value="https://ipfs-check-backend.ipfs.io">
</datalist>
<div class="mt3">
<label class="db f6 fw6" for="timeoutSeconds">Check Timeout (seconds)</label>
<input class="db w-100 mt2" type="range" id="timeoutSeconds" name="timeoutSeconds" min="5" max="300" value="60" step="1">
<output class="db fw6 f6" for="timeoutSeconds" id="timeoutValue">60</output>
</div>
</details>
<div class="db mv4">
<button id="submit" type="submit" class="flex items-center db ph3 pv2 link pointer glow o-90 bg-blue white fw6 f5 bn br2">
Expand Down Expand Up @@ -122,6 +127,8 @@ <h2 class="f4">What does it mean if I get an error?</h2>
<script>
window.addEventListener('load', function () {
initFormValues(new URL(window.location))


document.getElementById('queryForm').addEventListener('submit', async function (e) {
e.preventDefault() // dont do a browser form post

Expand Down Expand Up @@ -164,6 +171,15 @@ <h2 class="f4">What does it mean if I get an error?</h2>
for (const [key, val] of url.searchParams) {
document.getElementById(key)?.setAttribute('value', val)
}

const timeoutSlider = document.getElementById('timeoutSeconds')
const timeoutValue = document.getElementById('timeoutValue')

timeoutSlider.addEventListener('input', function() {
timeoutValue.textContent = this.value
})
// set initial value
timeoutValue.textContent = timeoutSlider.value
}

function showInQuery (formData) {
Expand Down Expand Up @@ -199,6 +215,7 @@ <h2 class="f4">What does it mean if I get an error?</h2>
const button = document.getElementById('submit')
button.toggleAttribute('disabled')
const spinner = document.getElementById('loading-spinner')
// Toggle spinner visibility
spinner.classList.toggle('dn')
}

Expand Down
Loading