Skip to content
Snippets Groups Projects
Commit 92be49be authored by ale's avatar ale
Browse files

Merge branch 'master' into jessie

parents 1c01bf6c 799a263b
Branches
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"hash/crc32"
"net" "net"
"strings" "strings"
"time" "time"
...@@ -403,3 +404,9 @@ func GeneratePassword() string { ...@@ -403,3 +404,9 @@ func GeneratePassword() string {
rand.Read(b) rand.Read(b)
return base64.StdEncoding.EncodeToString(b) return base64.StdEncoding.EncodeToString(b)
} }
// GenerateUsername returns a username somehow related to the name of
// the mount, possibly unique (but not actually guaranteed to be so).
func GenerateUsername(path string) string {
return fmt.Sprintf("source%d", crc32.ChecksumIEEE([]byte(path)))
}
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"hash/crc32"
"log" "log"
"net/url" "net/url"
"os" "os"
...@@ -140,14 +139,10 @@ func getClient() *autoradio.Client { ...@@ -140,14 +139,10 @@ func getClient() *autoradio.Client {
return autoradio.NewClient(autoradio.NewEtcdClient(false)) return autoradio.NewClient(autoradio.NewEtcdClient(false))
} }
func generateUsername(path string) string {
return fmt.Sprintf("source%d", crc32.ChecksumIEEE([]byte(path)))
}
func setRelay(m *autoradio.Mount, relayUrl string) { func setRelay(m *autoradio.Mount, relayUrl string) {
if relayUrl == "" { if relayUrl == "" {
// Randomly generate source credentials. // Randomly generate source credentials.
m.Username = generateUsername(m.Name) m.Username = autoradio.GenerateUsername(m.Name)
m.Password = autoradio.GeneratePassword() m.Password = autoradio.GeneratePassword()
} else { } else {
// Validate the given relay URL. // Validate the given relay URL.
......
debug.go 0 → 100644
package autoradio
import "sort"
// MountStatus reports the configuration and status of a mount,
// including eventual transcoded mounts that source it.
type MountStatus struct {
Mount *Mount
Listeners int
TransMounts []*MountStatus
}
func newMountStatus(m *Mount, nodes []*NodeStatus) *MountStatus {
var listeners int
for _, n := range nodes {
for _, ims := range n.Mounts {
if ims.Name == m.Name {
listeners += ims.Listeners
break
}
}
}
return &MountStatus{
Mount: m,
Listeners: listeners,
}
}
type mountStatusList []*MountStatus
func (l mountStatusList) Len() int { return len(l) }
func (l mountStatusList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l mountStatusList) Less(i, j int) bool {
return l[i].Mount.Name < l[j].Mount.Name
}
// MountsToStatus converts a list of mounts (and eventually the
// current list of nodes) to a nicely sorted and tree-aggregated list
// of MountStatus objects. The list of nodes can be nil, in which case
// listener statistics will be omitted.
func MountsToStatus(mounts []*Mount, nodes []*NodeStatus) []*MountStatus {
// Aggregate stats, and create a tree of transcoding mounts.
ms := make(map[string]*MountStatus)
for _, m := range mounts {
if m.HasTranscoder() {
continue
}
ms[m.Name] = newMountStatus(m, nodes)
}
for _, m := range mounts {
if !m.HasTranscoder() {
continue
}
src := ms[m.Transcoding.SourceName]
if src == nil {
continue
}
src.TransMounts = append(src.TransMounts, newMountStatus(m, nodes))
}
msl := make([]*MountStatus, 0, len(ms))
for _, m := range ms {
msl = append(msl, m)
}
// Sort everything (including transcoded mounts).
sort.Sort(mountStatusList(msl))
for _, s := range msl {
if s.TransMounts != nil {
sort.Sort(mountStatusList(s.TransMounts))
}
}
return msl
}
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
...@@ -266,76 +265,16 @@ func (h *HTTPRedirector) serveSource(mount *autoradio.Mount, w http.ResponseWrit ...@@ -266,76 +265,16 @@ func (h *HTTPRedirector) serveSource(mount *autoradio.Mount, w http.ResponseWrit
proxy.ServeHTTP(w, r) proxy.ServeHTTP(w, r)
} }
type mountStatus struct {
Mount *autoradio.Mount
Listeners int
TransMounts []*mountStatus
}
func newMountStatus(m *autoradio.Mount, nodes []*autoradio.NodeStatus) *mountStatus {
var listeners int
for _, n := range nodes {
for _, ims := range n.Mounts {
if ims.Name == m.Name {
listeners += ims.Listeners
break
}
}
}
return &mountStatus{
Mount: m,
Listeners: listeners,
}
}
type mountStatusList []*mountStatus
func (l mountStatusList) Len() int { return len(l) }
func (l mountStatusList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l mountStatusList) Less(i, j int) bool {
return l[i].Mount.Name < l[j].Mount.Name
}
// Serve our cluster status page. // Serve our cluster status page.
func (h *HTTPRedirector) serveStatusPage(w http.ResponseWriter, r *http.Request) { func (h *HTTPRedirector) serveStatusPage(w http.ResponseWriter, r *http.Request) {
nodes, _ := h.client.GetNodes() nodes, _ := h.client.GetNodes()
mounts, _ := h.client.ListMounts() mounts, _ := h.client.ListMounts()
// Aggregate stats, and create a tree of transcoding mounts. msl := autoradio.MountsToStatus(mounts, nodes)
ms := make(map[string]*mountStatus)
for _, m := range mounts {
if m.HasTranscoder() {
continue
}
ms[m.Name] = newMountStatus(m, nodes)
}
for _, m := range mounts {
if !m.HasTranscoder() {
continue
}
src := ms[m.Transcoding.SourceName]
if src == nil {
continue
}
src.TransMounts = append(src.TransMounts, newMountStatus(m, nodes))
}
msl := make([]*mountStatus, 0, len(ms))
for _, m := range ms {
msl = append(msl, m)
}
// Sort everything.
sort.Sort(mountStatusList(msl))
for _, s := range msl {
if s.TransMounts != nil {
sort.Sort(mountStatusList(s.TransMounts))
}
}
ctx := struct { ctx := struct {
Domain string Domain string
Nodes []*autoradio.NodeStatus Nodes []*autoradio.NodeStatus
Mounts []*mountStatus Mounts []*autoradio.MountStatus
}{h.domain, nodes, msl} }{h.domain, nodes, msl}
var buf bytes.Buffer var buf bytes.Buffer
......
...@@ -68,9 +68,8 @@ func (bw *BandwidthMonitor) Run(stop chan bool) { ...@@ -68,9 +68,8 @@ func (bw *BandwidthMonitor) Run(stop chan bool) {
t := time.NewTicker(bw.period) t := time.NewTicker(bw.period)
for { for {
select { select {
case <-t.C: case now := <-t.C:
if c, err := getBytesSentForDevice(bw.device); err == nil { if c, err := getBytesSentForDevice(bw.device); err == nil {
now := time.Now()
bw.lock.Lock() bw.lock.Lock()
bw.rate = float64(c-bw.counter) / now.Sub(bw.stamp).Seconds() bw.rate = float64(c-bw.counter) / now.Sub(bw.stamp).Seconds()
bw.counter = c bw.counter = c
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment