Skip to content

Commit

Permalink
Merge pull request #18 from matheusd/fix-grpc-conn
Browse files Browse the repository at this point in the history
Fix grpc reconnection and add /info endpoint
  • Loading branch information
dajohi authored Mar 22, 2024
2 parents 106e9bd + 9cf4061 commit 1a0d1a9
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 120 deletions.
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
Expand All @@ -20,9 +21,33 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "%s\n%s\n", appName, version.String())
}

func newInfoHandler(server *server.Server) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
if !isReqFromLocalhost(req) {
w.WriteHeader(http.StatusForbidden)
log.Warnf("Forbidden request for info from %s", requestAddr(req))
return
}

info, err := server.FetchManagedChannels(req.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error fetching managed channels: %v\n", err)
log.Errorf("Unable to fetch managed channels: %v", err)
return
}

err = json.NewEncoder(w).Encode(info)
if err != nil {
log.Errorf("Unable to encode info: %v", err)
}
}
}

func handler(s *server.Server) http.Handler {
router := mux.NewRouter().StrictSlash(true)
router.Methods("GET").Path("/").Name("index").HandlerFunc(indexHandler)
router.Methods("GET").Path("/info").Name("info").HandlerFunc(newInfoHandler(s))
server.NewV1Handler(s, router)
logRouterConfig(router)
return router
Expand Down
10 changes: 5 additions & 5 deletions server/chanscore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package server

import "time"

// chanActivityScore holds "how much activity" a channel received through some
// ChanActivityScore holds "how much activity" a channel received through some
// period of time.
//
// The basic equation for determining the "activity" score for a channel is
Expand All @@ -12,17 +12,17 @@ import "time"
// The interpretation for this equation is that the activity score is the
// percentage of the channel capacity sent through the channel during its
// entire lifetime.
type chanActivityScore float64
type ChanActivityScore float64

// toPercent returns the activity score as a percentage.
func (s chanActivityScore) toPercent() float64 {
func (s ChanActivityScore) ToPercent() float64 {
return float64(s) * 100
}

// channelActivity returns the "activity" score for a channel, which measures
// the total amount of atoms sent through the channel during its lifetime.
func channelActivity(totalSentAtoms, channelSizeAtoms int64,
lifetime time.Duration) chanActivityScore {
lifetime time.Duration) ChanActivityScore {

if lifetime <= 0 {
panic("lifetime cannot be <= 0")
Expand All @@ -37,5 +37,5 @@ func channelActivity(totalSentAtoms, channelSizeAtoms int64,
hours = 1
}

return chanActivityScore(float64(totalSentAtoms) / float64(channelSizeAtoms) / hours)
return ChanActivityScore(float64(totalSentAtoms) / float64(channelSizeAtoms) / hours)
}
2 changes: 1 addition & 1 deletion server/chanscore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestChanActivityScore(t *testing.T) {
sent int64
size int64
life time.Duration
want chanActivityScore
want ChanActivityScore
}{{
name: "1 atom sent through 1 DCR chan within 1 hour",
sent: 1,
Expand Down
Loading

0 comments on commit 1a0d1a9

Please sign in to comment.