some changes
This commit is contained in:
27
internal/server/registry.go
Normal file
27
internal/server/registry.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type registry struct {
|
||||
lss map[string]*LiveServer
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
var reg = registry{
|
||||
lss: make(map[string]*LiveServer),
|
||||
}
|
||||
|
||||
func pushLs(ls *LiveServer) {
|
||||
reg.lss[ls.name] = ls
|
||||
}
|
||||
|
||||
func isExists(name string) bool {
|
||||
_, ok := reg.lss[name]
|
||||
return ok
|
||||
}
|
||||
|
||||
func deleteLs(name string) {
|
||||
delete(reg.lss, name)
|
||||
}
|
||||
@@ -114,7 +114,12 @@ func Create(name string) (*LiveServer, error) {
|
||||
return nil, fmt.Errorf("server name is empty")
|
||||
}
|
||||
|
||||
if isExists(name) {
|
||||
return nil, fmt.Errorf("server with this name is already exists")
|
||||
}
|
||||
|
||||
ls := &LiveServer{name: name}
|
||||
pushLs(ls)
|
||||
ls.setStatus(Status{ID: StatusStopped})
|
||||
return ls, nil
|
||||
}
|
||||
@@ -241,6 +246,17 @@ func (ls *LiveServer) stop(inst *instance) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func StopAll() (errors []error) {
|
||||
for key, ls := range reg.lss {
|
||||
slog.Debug("stopping LiveServer", slog.String("name", key))
|
||||
err := ls.Stop()
|
||||
if err != nil {
|
||||
errors = append(errors, err)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *LiveServer) Stop() error {
|
||||
inst := ls.active.Load().(*instance)
|
||||
if inst == nil {
|
||||
@@ -280,6 +296,12 @@ func (ls *LiveServer) Reload(newAddr string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close deletes [LiveServer] object from registry, and sets ls to nil
|
||||
func (ls *LiveServer) Close() {
|
||||
deleteLs(ls.name)
|
||||
ls = nil
|
||||
}
|
||||
|
||||
// package server
|
||||
|
||||
// import (
|
||||
|
||||
Reference in New Issue
Block a user