mirror of
https://github.com/datarhei/core.git
synced 2025-10-06 00:17:07 +08:00
Fetch resources list in parallel
This commit is contained in:
@@ -45,10 +45,10 @@ type ClusterConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type cluster struct {
|
type cluster struct {
|
||||||
nodes map[string]*node
|
nodes map[string]*node // List of known nodes
|
||||||
idfiles map[string][]string
|
idfiles map[string][]string // Map from nodeid to list of files
|
||||||
idupdate map[string]time.Time
|
idupdate map[string]time.Time // Map from nodeid to time of last update
|
||||||
fileid map[string]string
|
fileid map[string]string // Map from file name to nodeid
|
||||||
|
|
||||||
limiter net.IPLimiter
|
limiter net.IPLimiter
|
||||||
|
|
||||||
|
127
cluster/node.go
127
cluster/node.go
@@ -48,7 +48,7 @@ type node struct {
|
|||||||
password string
|
password string
|
||||||
updates chan<- NodeState
|
updates chan<- NodeState
|
||||||
peer client.RestClient
|
peer client.RestClient
|
||||||
fileList []string
|
filesList []string
|
||||||
lastUpdate time.Time
|
lastUpdate time.Time
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
@@ -162,9 +162,7 @@ func newNode(address, username, password string, updates chan<- NodeState) (*nod
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
n.lock.Lock()
|
|
||||||
n.files()
|
n.files()
|
||||||
n.lock.Unlock()
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case n.updates <- n.State():
|
case n.updates <- n.State():
|
||||||
@@ -202,8 +200,8 @@ func (n *node) State() NodeState {
|
|||||||
state.State = stateDisconnected.String()
|
state.State = stateDisconnected.String()
|
||||||
} else {
|
} else {
|
||||||
state.State = n.state.String()
|
state.State = n.state.String()
|
||||||
state.Files = make([]string, len(n.fileList))
|
state.Files = make([]string, len(n.filesList))
|
||||||
copy(state.Files, n.fileList)
|
copy(state.Files, n.filesList)
|
||||||
}
|
}
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@@ -214,44 +212,101 @@ func (n *node) stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) files() {
|
func (n *node) files() {
|
||||||
memfsfiles, errMemfs := n.peer.MemFSList("name", "asc")
|
filesChan := make(chan string, 1024)
|
||||||
diskfsfiles, errDiskfs := n.peer.DiskFSList("name", "asc")
|
filesList := []string{}
|
||||||
rtmpfiles, errRTMP := n.peer.RTMPChannels()
|
|
||||||
srtfiles, errSRT := n.peer.SRTChannels()
|
|
||||||
|
|
||||||
n.lastUpdate = time.Now()
|
wgList := sync.WaitGroup{}
|
||||||
|
wgList.Add(1)
|
||||||
|
|
||||||
if errMemfs != nil || errDiskfs != nil || errRTMP != nil || errSRT != nil {
|
go func() {
|
||||||
n.fileList = nil
|
defer wgList.Done()
|
||||||
n.state = stateDisconnected
|
|
||||||
|
for file := range filesChan {
|
||||||
|
if len(file) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filesList = append(filesList, file)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
go func(f chan<- string) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
files, err := n.peer.MemFSList("name", "asc")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
filesChan <- "memfs:" + file.Name
|
||||||
|
}
|
||||||
|
}(filesChan)
|
||||||
|
|
||||||
|
go func(f chan<- string) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
files, err := n.peer.DiskFSList("name", "asc")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
filesChan <- "diskfs:" + file.Name
|
||||||
|
}
|
||||||
|
}(filesChan)
|
||||||
|
|
||||||
|
if n.hasRTMP {
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func(f chan<- string) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
files, err := n.peer.RTMPChannels()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
filesChan <- "rtmp:" + file.Name
|
||||||
|
}
|
||||||
|
}(filesChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.hasSRT {
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func(f chan<- string) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
files, err := n.peer.SRTChannels()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
filesChan <- "srt:" + file.Name
|
||||||
|
}
|
||||||
|
}(filesChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
filesChan <- ""
|
||||||
|
|
||||||
|
wgList.Wait()
|
||||||
|
|
||||||
|
n.lock.Lock()
|
||||||
|
|
||||||
|
n.filesList = make([]string, len(filesList))
|
||||||
|
copy(n.filesList, filesList)
|
||||||
|
n.lastUpdate = time.Now()
|
||||||
n.state = stateConnected
|
n.state = stateConnected
|
||||||
|
|
||||||
n.fileList = make([]string, len(memfsfiles)+len(diskfsfiles)+len(rtmpfiles)+len(srtfiles))
|
n.lock.Unlock()
|
||||||
|
|
||||||
nfiles := 0
|
|
||||||
|
|
||||||
for _, file := range memfsfiles {
|
|
||||||
n.fileList[nfiles] = "memfs:" + file.Name
|
|
||||||
nfiles++
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, file := range diskfsfiles {
|
|
||||||
n.fileList[nfiles] = "diskfs:" + file.Name
|
|
||||||
nfiles++
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, file := range rtmpfiles {
|
|
||||||
n.fileList[nfiles] = "rtmp:" + file.Name
|
|
||||||
nfiles++
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, file := range srtfiles {
|
|
||||||
n.fileList[nfiles] = "srt:" + file.Name
|
|
||||||
nfiles++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) getURL(path string) (string, error) {
|
func (n *node) getURL(path string) (string, error) {
|
||||||
|
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/datarhei/core/v16/cluster"
|
"github.com/datarhei/core/v16/cluster"
|
||||||
"github.com/datarhei/core/v16/http/api"
|
"github.com/datarhei/core/v16/http/api"
|
||||||
@@ -147,6 +148,8 @@ func (h *ClusterHandler) GetNodeProxy(c echo.Context) error {
|
|||||||
|
|
||||||
state := peer.State()
|
state := peer.State()
|
||||||
|
|
||||||
|
sort.Strings(state.Files)
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, state.Files)
|
return c.JSON(http.StatusOK, state.Files)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user