Skip to content

Commit

Permalink
support for query param max_allowable_byte_lag (#10)
Browse files Browse the repository at this point in the history
support for query param max_allowable_byte_lag
  • Loading branch information
schinns authored Sep 15, 2020
1 parent 9289b33 commit 6e6cd07
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"os"
"strconv"

"github.com/film42/pgreba/config"
"github.com/gorilla/handlers"
Expand All @@ -18,41 +19,6 @@ type HealthCheckWebService struct {
healthChecker *HealthChecker
}

// func (hc *HealthCheckWebService) getSlotHealthCheck(w http.ResponseWriter, r *http.Request) {
// // Get request info
// w.Header().Set("Content-Type", "application/json")
// params := mux.Vars(r)
// slotName := params["slot_name"]

// // Perform the health check.
// err := hc.healthChecker.CheckReplicationSlot(slotName)

// // If the slot is OK, return status: ok.
// if err == nil {
// json.NewEncoder(w).Encode(map[string]string{
// "status": "ok",
// "slot": slotName,
// })
// return
// }

// // If there was an error, set the appropriate status code.
// switch err {
// case ErrReplicationSlotNotFound:
// w.WriteHeader(http.StatusNotFound)
// case ErrReplicationSlotLagTooHigh:
// w.WriteHeader(http.StatusServiceUnavailable)
// default:
// w.WriteHeader(http.StatusInternalServerError)
// }

// // Return error to the client.
// json.NewEncoder(w).Encode(map[string]string{
// "error": err.Error(),
// "slot": slotName,
// })
// }

func (hc *HealthCheckWebService) apiGetIsPrimary(w http.ResponseWriter, r *http.Request) {
nodeInfo, err := hc.healthChecker.dataSource.GetNodeInfo()
if err != nil {
Expand All @@ -78,9 +44,28 @@ func (hc *HealthCheckWebService) apiGetIsReplica(w http.ResponseWriter, r *http.
w.WriteHeader(http.StatusServiceUnavailable)
}

// if byte lag exceeds max_allowable_byte_lag then return 500
if maxAllowableByteLagExceeded(r, nodeInfo) {
w.WriteHeader(http.StatusServiceUnavailable)
}

json.NewEncoder(w).Encode(nodeInfo)
}

func maxAllowableByteLagExceeded(r *http.Request, nodeInfo *NodeInfo) bool {
maxAllowableByteLagString := r.URL.Query().Get("max_allowable_byte_lag")
if len(maxAllowableByteLagString) == 0 {
return true
}

maxAllowableByteLag, err := strconv.ParseInt(maxAllowableByteLagString, 10, 64)
if err != nil {
panic(err)
}

return nodeInfo.ByteLag > maxAllowableByteLag
}

func main() {
versionPtr := flag.Bool("version", false, "Print the teecp version and exit.")
flag.Parse()
Expand Down Expand Up @@ -118,7 +103,6 @@ func main() {
return handlers.LoggingHandler(log.Writer(), next)
})

// For primary nodes
router.HandleFunc("/", hcs.apiGetIsPrimary).Methods("GET")
router.HandleFunc("/primary", hcs.apiGetIsPrimary).Methods("GET")

Expand Down

0 comments on commit 6e6cd07

Please sign in to comment.