Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grpc reconnection and add /info endpoint #18

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading