forked from riccardobl/lncd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c75cdf
commit 2b98789
Showing
3 changed files
with
151 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
type HealthStatus struct { | ||
Status string `json:"status"` | ||
Stats Stats | ||
Message string `json:"message"` | ||
|
||
} | ||
|
||
func healthCheckHandler(w http.ResponseWriter, r *http.Request) { | ||
var stats *Stats = getStats() | ||
var err error = nil | ||
if stats == nil { | ||
err = errors.New("starting") | ||
} | ||
|
||
if err == nil { | ||
w.Header().Set("Content-Type", "application/json") | ||
err = json.NewEncoder(w).Encode(HealthStatus{ | ||
Status: "OK", | ||
Stats: *stats, | ||
Message: "", | ||
}) | ||
} | ||
|
||
if err != nil { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusServiceUnavailable) | ||
json.NewEncoder(w).Encode(HealthStatus{ | ||
Status: "FAIL", | ||
Stats: Stats{}, | ||
Message: err.Error(), | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
var ( | ||
lastStats *Stats = nil | ||
) | ||
|
||
type ConnectionStats struct { | ||
NumPendingActions int | ||
Status string | ||
} | ||
|
||
type Stats struct { | ||
NumConnections int | ||
Connections []ConnectionStats | ||
} | ||
|
||
func refreshStats(pool *ConnectionPool, stats *Stats) *Stats { | ||
pool.mutex.Lock() | ||
defer pool.mutex.Unlock() | ||
|
||
if stats == nil { | ||
stats = &Stats{ | ||
NumConnections: 0, | ||
Connections: nil, | ||
} | ||
} | ||
|
||
stats.NumConnections = len(pool.connections) | ||
if stats.Connections == nil || len(stats.Connections) != len(pool.connections) { | ||
stats.Connections = make([]ConnectionStats, len(pool.connections)) | ||
} | ||
|
||
var i int | ||
for _, conn := range pool.connections { | ||
stats.Connections[i] = ConnectionStats{ | ||
NumPendingActions: len(conn.actions), | ||
Status: conn.connInfo.Status, | ||
} | ||
i++ | ||
} | ||
|
||
return stats | ||
} | ||
|
||
func getStats() *Stats { | ||
return lastStats | ||
} | ||
|
||
func startStatsLoop(pool *ConnectionPool) { | ||
ticker := time.NewTicker(LNCD_STATS_INTERVAL) | ||
go func() { | ||
for range ticker.C { | ||
lastStats = refreshStats(pool, lastStats) | ||
|
||
if lastStats != nil { | ||
var statsString string = "" | ||
statsString += fmt.Sprintf("Active connections: %d\n", lastStats.NumConnections) | ||
for i, conn := range lastStats.Connections { | ||
statsString += fmt.Sprintf(" Connection id: %d\n", i) | ||
statsString += fmt.Sprintf(" Pending actions: %d\n", conn.NumPendingActions) | ||
statsString += fmt.Sprintf(" Status: %s", conn.Status) | ||
} | ||
log.Debugf("Stats:\n %s", statsString) | ||
} | ||
} | ||
}() | ||
} | ||
|
||
|