Return err from ProcessRequest instead of panic

chubin/logging
Igor Chubin 2 years ago
parent 5b240c590e
commit 2c367d0157

@ -11,11 +11,14 @@ import (
"time" "time"
) )
func processRequest(r *http.Request) responseWithHeader { func processRequest(r *http.Request) (*responseWithHeader, error) {
var response responseWithHeader var (
response *responseWithHeader
err error
)
if response, ok := redirectInsecure(r); ok { if resp, ok := redirectInsecure(r); ok {
return *response return resp, nil
} }
if dontCache(r) { if dontCache(r) {
@ -40,31 +43,36 @@ func processRequest(r *http.Request) responseWithHeader {
} }
time.Sleep(30 * time.Millisecond) time.Sleep(30 * time.Millisecond)
cacheBody, ok = lruCache.Get(cacheDigest) cacheBody, ok = lruCache.Get(cacheDigest)
cacheEntry = cacheBody.(responseWithHeader) if ok && cacheBody != nil {
cacheEntry = cacheBody.(responseWithHeader)
}
} }
if cacheEntry.InProgress { if cacheEntry.InProgress {
log.Printf("TIMEOUT: %s\n", cacheDigest) log.Printf("TIMEOUT: %s\n", cacheDigest)
} }
if ok && !cacheEntry.InProgress && cacheEntry.Expires.After(time.Now()) { if ok && !cacheEntry.InProgress && cacheEntry.Expires.After(time.Now()) {
response = cacheEntry response = &cacheEntry
foundInCache = true foundInCache = true
} }
} }
if !foundInCache { if !foundInCache {
lruCache.Add(cacheDigest, responseWithHeader{InProgress: true}) lruCache.Add(cacheDigest, responseWithHeader{InProgress: true})
response = get(r) response, err = get(r)
if err != nil {
return nil, err
}
if response.StatusCode == 200 || response.StatusCode == 304 || response.StatusCode == 404 { if response.StatusCode == 200 || response.StatusCode == 304 || response.StatusCode == 404 {
lruCache.Add(cacheDigest, response) lruCache.Add(cacheDigest, *response)
} else { } else {
log.Printf("REMOVE: %d response for %s from cache\n", response.StatusCode, cacheDigest) log.Printf("REMOVE: %d response for %s from cache\n", response.StatusCode, cacheDigest)
lruCache.Remove(cacheDigest) lruCache.Remove(cacheDigest)
} }
} }
return response return response, nil
} }
func get(req *http.Request) responseWithHeader { func get(req *http.Request) (*responseWithHeader, error) {
client := &http.Client{} client := &http.Client{}
@ -72,7 +80,7 @@ func get(req *http.Request) responseWithHeader {
proxyReq, err := http.NewRequest(req.Method, queryURL, req.Body) proxyReq, err := http.NewRequest(req.Method, queryURL, req.Body)
if err != nil { if err != nil {
log.Printf("Request: %s\n", err) return nil, err
} }
// proxyReq.Header.Set("Host", req.Host) // proxyReq.Header.Set("Host", req.Host)
@ -89,23 +97,22 @@ func get(req *http.Request) responseWithHeader {
} }
res, err := client.Do(proxyReq) res, err := client.Do(proxyReq)
if err != nil { if err != nil {
panic(err) return nil, err
} }
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
log.Println(err) return nil, err
} }
return responseWithHeader{ return &responseWithHeader{
InProgress: false, InProgress: false,
Expires: time.Now().Add(time.Duration(randInt(1000, 1500)) * time.Second), Expires: time.Now().Add(time.Duration(randInt(1000, 1500)) * time.Second),
Body: body, Body: body,
Header: res.Header, Header: res.Header,
StatusCode: res.StatusCode, StatusCode: res.StatusCode,
} }, nil
} }
// implementation of the cache.get_signature of original wttr.in // implementation of the cache.get_signature of original wttr.in

@ -143,7 +143,15 @@ func main() {
log.Println(err) log.Println(err)
} }
// printStat() // printStat()
response := processRequest(r) response, err := processRequest(r)
if err != nil {
log.Println(err)
return
}
if response.StatusCode == 0 {
log.Println("status code 0", response)
return
}
copyHeader(w.Header(), response.Header) copyHeader(w.Header(), response.Header)
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")

Loading…
Cancel
Save