Skip to content

Commit

Permalink
Merge pull request #57 from ipfs/add-timeout-input
Browse files Browse the repository at this point in the history
feat: add request timeout slider
  • Loading branch information
2color authored Aug 30, 2024
2 parents 2f48510 + 241cc63 commit a1985f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"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 @@ func main() {
}
}

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 @@ func startServer(ctx context.Context, d *daemon, tcpListener, metricsUsername, m

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")
}

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
}
}

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

0 comments on commit a1985f1

Please sign in to comment.