init: 1.0.0

This commit is contained in:
Chang lue Tsen
2025-04-25 12:08:29 +09:00
commit 8addcc584b
1031 changed files with 76472 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
package service
import (
"sync"
"github.com/perfect-panel/ppanel-server/pkg/proc"
"github.com/perfect-panel/ppanel-server/pkg/threading"
)
type (
// Starter is the interface wraps the Start method.
Starter interface {
Start()
}
// Stopper is the interface wraps the Stop method.
Stopper interface {
Stop()
}
// Service is the interface that groups Start and Stop methods.
Service interface {
Starter
Stopper
}
// Group A ServiceGroup is a group of services.
// Attention: the starting order of the added services is not guaranteed.
Group struct {
services []Service
stopOnce func()
}
)
// NewServiceGroup returns a ServiceGroup.
func NewServiceGroup() *Group {
sg := new(Group)
sg.stopOnce = Once(sg.doStop)
return sg
}
// Add adds service into sg.
func (sg *Group) Add(service Service) {
// push front, stop with reverse order.
sg.services = append([]Service{service}, sg.services...)
}
// Start starts the ServiceGroup.
// There should not be any logic code after calling this method, because this method is a blocking one.
// Also, quitting this method will close the logx output.
func (sg *Group) Start() {
proc.AddShutdownListener(func() {
sg.stopOnce()
})
sg.doStart()
}
// Stop stops the ServiceGroup.
func (sg *Group) Stop() {
sg.stopOnce()
}
func (sg *Group) doStart() {
routineGroup := threading.NewRoutineGroup()
for i := range sg.services {
service := sg.services[i]
routineGroup.Run(func() {
service.Start()
})
}
routineGroup.Wait()
}
func (sg *Group) doStop() {
for _, service := range sg.services {
service.Stop()
}
}
// WithStart wraps a start func as a Service.
func WithStart(start func()) Service {
return startOnlyService{
start: start,
}
}
// WithStarter wraps a Starter as a Service.
func WithStarter(start Starter) Service {
return starterOnlyService{
Starter: start,
}
}
type (
stopper struct{}
startOnlyService struct {
start func()
stopper
}
starterOnlyService struct {
Starter
stopper
}
)
func (s stopper) Stop() {
}
func (s startOnlyService) Start() {
s.start()
}
func Once(fn func()) func() {
once := new(sync.Once)
return func() {
once.Do(fn)
}
}
+129
View File
@@ -0,0 +1,129 @@
package service
import (
"sync"
"testing"
"github.com/perfect-panel/ppanel-server/pkg/proc"
"github.com/stretchr/testify/assert"
)
var (
number = 1
mutex sync.Mutex
done = make(chan struct{})
)
func TestServiceGroup(t *testing.T) {
multipliers := []int{2, 3, 5, 7}
want := 1
group := NewServiceGroup()
for _, multiplier := range multipliers {
want *= multiplier
service := newMockedService(multiplier)
group.Add(service)
}
go group.Start()
for i := 0; i < len(multipliers); i++ {
<-done
}
group.Stop()
proc.Shutdown()
mutex.Lock()
defer mutex.Unlock()
assert.Equal(t, want, number)
}
func TestServiceGroup_WithStart(t *testing.T) {
multipliers := []int{2, 3, 5, 7}
want := 1
var wait sync.WaitGroup
var lock sync.Mutex
wait.Add(len(multipliers))
group := NewServiceGroup()
for _, multiplier := range multipliers {
mul := multiplier
group.Add(WithStart(func() {
lock.Lock()
want *= mul
lock.Unlock()
wait.Done()
}))
}
go group.Start()
wait.Wait()
group.Stop()
lock.Lock()
defer lock.Unlock()
assert.Equal(t, 210, want)
}
func TestServiceGroup_WithStarter(t *testing.T) {
multipliers := []int{2, 3, 5, 7}
want := 1
var wait sync.WaitGroup
var lock sync.Mutex
wait.Add(len(multipliers))
group := NewServiceGroup()
for _, multiplier := range multipliers {
mul := multiplier
group.Add(WithStarter(mockedStarter{
fn: func() {
lock.Lock()
want *= mul
lock.Unlock()
wait.Done()
},
}))
}
go group.Start()
wait.Wait()
group.Stop()
lock.Lock()
defer lock.Unlock()
assert.Equal(t, 210, want)
}
type mockedStarter struct {
fn func()
}
func (s mockedStarter) Start() {
s.fn()
}
type mockedService struct {
quit chan struct{}
multiplier int
}
func newMockedService(multiplier int) *mockedService {
return &mockedService{
quit: make(chan struct{}),
multiplier: multiplier,
}
}
func (s *mockedService) Start() {
mutex.Lock()
number *= s.multiplier
mutex.Unlock()
done <- struct{}{}
<-s.quit
}
func (s *mockedService) Stop() {
close(s.quit)
}