Set Cache-Control to 60s on failed fetch

This way we can permit a fast retry while keeping some control on DoS attacks
This commit is contained in:
Daniele Tonon
2023-08-04 15:40:30 +02:00
parent ecd5d0c173
commit 2fcf17aa1d
3 changed files with 9 additions and 0 deletions

View File

@@ -57,6 +57,7 @@ func render(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(code, "note1") {
_, redirectHex, err := nip19.Decode(code)
if err != nil {
w.Header().Set("Cache-Control", "max-age=60")
http.Error(w, "error fetching event: "+err.Error(), 404)
return
}
@@ -75,6 +76,7 @@ func render(w http.ResponseWriter, r *http.Request) {
// code can be a nevent, nprofile, npub or nip05 identifier, in which case we try to fetch the associated event
event, err := getEvent(r.Context(), code)
if err != nil {
w.Header().Set("Cache-Control", "max-age=60")
http.Error(w, "error fetching event: "+err.Error(), 404)
return
}
@@ -132,6 +134,7 @@ func render(w http.ResponseWriter, r *http.Request) {
}
}
if err != nil {
w.Header().Set("Cache-Control", "max-age=60")
http.Error(w, "error fetching event: "+err.Error(), 404)
return
}
@@ -319,6 +322,8 @@ func render(w http.ResponseWriter, r *http.Request) {
if (strings.Contains(typ, "profile") && len(renderableLastNotes) != 0) || (!strings.Contains(typ, "profile") && len(content) != 0) {
w.Header().Set("Cache-Control", "max-age=604800")
} else {
w.Header().Set("Cache-Control", "max-age=60")
}
if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {

View File

@@ -66,6 +66,8 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
if len(data) != 0 {
w.Header().Set("Cache-Control", "max-age=86400")
} else {
w.Header().Set("Cache-Control", "max-age=60")
}
if err := tmpl.ExecuteTemplate(w, "archive.html", params); err != nil {

View File

@@ -74,6 +74,8 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
if len(renderableLastNotes) != 0 {
w.Header().Set("Cache-Control", "max-age=604800")
} else {
w.Header().Set("Cache-Control", "max-age=60")
}
if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {