Commit 5796d406 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Reload group descriptions periodically.

parent 69fcdfba
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"time"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
) )
...@@ -51,6 +52,7 @@ type client struct { ...@@ -51,6 +52,7 @@ type client struct {
type group struct { type group struct {
name string name string
dead bool
description *groupDescription description *groupDescription
mu sync.Mutex mu sync.Mutex
...@@ -104,7 +106,6 @@ func addGroup(name string) (*group, error) { ...@@ -104,7 +106,6 @@ func addGroup(name string) (*group, error) {
} }
g := groups.groups[name] g := groups.groups[name]
if g == nil { if g == nil {
desc, err := getDescription(name) desc, err := getDescription(name)
if err != nil { if err != nil {
...@@ -115,6 +116,29 @@ func addGroup(name string) (*group, error) { ...@@ -115,6 +116,29 @@ func addGroup(name string) (*group, error) {
description: desc, description: desc,
} }
groups.groups[name] = g groups.groups[name] = g
} else if g.dead || time.Since(g.description.loadTime) > 5*time.Second {
changed, err := descriptionChanged(name, g.description)
if err != nil {
g.dead = true
if !g.description.Public {
delGroupUnlocked(name)
}
return nil, err
}
if changed {
desc, err := getDescription(name)
if err != nil {
g.dead = true
if !g.description.Public {
delGroupUnlocked(name)
}
return nil, err
}
g.dead = false
g.description = desc
} else {
g.description.loadTime = time.Now()
}
} }
return g, nil return g, nil
...@@ -175,7 +199,7 @@ func delClient(c *client) { ...@@ -175,7 +199,7 @@ func delClient(c *client) {
g.clients = g.clients =
append(g.clients[:i], g.clients[i+1:]...) append(g.clients[:i], g.clients[i+1:]...)
c.group = nil c.group = nil
if len(g.clients) == 0 { if len(g.clients) == 0 && !g.description.Public {
delGroupUnlocked(g.name) delGroupUnlocked(g.name)
} }
return return
...@@ -253,6 +277,9 @@ func matchUser(user, pass string, users []groupUser) (bool, bool) { ...@@ -253,6 +277,9 @@ func matchUser(user, pass string, users []groupUser) (bool, bool) {
} }
type groupDescription struct { type groupDescription struct {
loadTime time.Time `json:"-"`
modTime time.Time `json:"-"`
fileSize int64 `json:"-"`
Public bool `json:"public,omitempty"` Public bool `json:"public,omitempty"`
MaxClients int `json:"max-clients,omitempty"` MaxClients int `json:"max-clients,omitempty"`
AllowAnonymous bool `json:"allow-anonymous,omitempty"` AllowAnonymous bool `json:"allow-anonymous,omitempty"`
...@@ -261,24 +288,45 @@ type groupDescription struct { ...@@ -261,24 +288,45 @@ type groupDescription struct {
Other []groupUser `json:"other,omitempty"` Other []groupUser `json:"other,omitempty"`
} }
func descriptionChanged(name string, old *groupDescription) (bool, error) {
fi, err := os.Stat(filepath.Join(groupsDir, name+".json"))
if err != nil {
if os.IsNotExist(err) {
err = userError("group does not exist")
}
return false, err
}
if fi.Size() != old.fileSize || fi.ModTime() != old.modTime {
return true, err
}
return false, err
}
func getDescription(name string) (*groupDescription, error) { func getDescription(name string) (*groupDescription, error) {
r, err := os.Open(filepath.Join(groupsDir, name+".json")) r, err := os.Open(filepath.Join(groupsDir, name+".json"))
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = userError("group does not exist") err = userError("group does not exist")
} else if os.IsPermission(err) {
err = userError("unauthorised")
} }
return nil, err return nil, err
} }
defer r.Close() defer r.Close()
var desc groupDescription var desc groupDescription
fi, err := r.Stat()
if err != nil {
return nil, err
}
desc.fileSize = fi.Size()
desc.modTime = fi.ModTime()
d := json.NewDecoder(r) d := json.NewDecoder(r)
err = d.Decode(&desc) err = d.Decode(&desc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
desc.loadTime = time.Now()
return &desc, nil return &desc, nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment