Skip to content

Commit

Permalink
Add proof of concepts in hack/
Browse files Browse the repository at this point in the history
  • Loading branch information
nstogner committed Jan 18, 2024
1 parent d4896cf commit 2ab82e0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
32 changes: 32 additions & 0 deletions hack/failonceserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"net/http"
"sync"
)

func main() {
// HTTP server that fails once and then succeeds for a given request path
var mtx sync.RWMutex
paths := map[string]bool{}

http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mtx.RLock()
shouldSucceed := paths[r.URL.Path]
mtx.RUnlock()

defer func() {
mtx.Lock()
paths[r.URL.Path] = true
mtx.Unlock()
}()

if !shouldSucceed {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("failure\n"))
return
}

w.Write([]byte("success\n"))
}))
}
10 changes: 10 additions & 0 deletions hack/failserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "net/http"

func main() {
http.ListenAndServe(":8081", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("unavailable\n"))
}))
}
48 changes: 48 additions & 0 deletions hack/retryproxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"errors"
"log"
"net/http"
"net/http/httputil"
"net/url"
)

func main() {
// go run ./hack/failserver
u1, err := url.Parse("http://localhost:8081")
if err != nil {
panic(err)
}
p1 := httputil.NewSingleHostReverseProxy(u1)

// go run ./hack/successserver
u2, err := url.Parse("http://localhost:8082")
if err != nil {
panic(err)
}
p2 := httputil.NewSingleHostReverseProxy(u2)

p1.ModifyResponse = func(r *http.Response) error {
if r.StatusCode == http.StatusServiceUnavailable {
// Returning an error will trigger the ErrorHandler.
return errRetry
}
return nil
}

p1.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
if err == errRetry {
log.Println("retrying")
// Simulate calling the next backend.
p2.ServeHTTP(w, r)
return
}
}

http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p1.ServeHTTP(w, r)
}))
}

var errRetry = errors.New("retry")
9 changes: 9 additions & 0 deletions hack/successserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "net/http"

func main() {
http.ListenAndServe(":8082", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("success\n"))
}))
}

0 comments on commit 2ab82e0

Please sign in to comment.