Skip to content

Commit 8195ea0

Browse files
committed
feat. add usage.
1 parent 86045f7 commit 8195ea0

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

default.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
package packaged
22

3+
// internal holds the service manager instance.
34
var internal = New()
45

5-
func Register(newFc NewService, opts ...UnitOptions) { internal.Register(newFc, opts...) }
6-
func Stop() { internal.Stop() }
7-
func Run() error { return internal.Run() }
8-
func Wait() { internal.Wait() }
6+
// Register adds a new service to the internal service manager.
7+
func Register(newFc NewService, opts ...UnitOptions) {
8+
internal.Register(newFc, opts...)
9+
}
10+
11+
// Stop gracefully stops all services managed by the internal service manager.
12+
func Stop() {
13+
internal.Stop()
14+
}
15+
16+
// Run starts the internal service manager and launches all registered services.
17+
func Run() error {
18+
return internal.Run()
19+
}
20+
21+
// Wait blocks execution until all services have completed or stopped.
22+
func Wait() {
23+
internal.Wait()
24+
}

example/0-startup/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/RealFax/packaged"
6+
"log"
7+
"net/http"
8+
)
9+
10+
type httpServer struct {
11+
*packaged.UnimplementedHandler
12+
ctx packaged.Namespace
13+
svc *http.Server
14+
}
15+
16+
func (s httpServer) Name() string { return "http-server" }
17+
func (s httpServer) Type() packaged.ServiceType { return packaged.ServiceTypeAsync }
18+
func (s httpServer) OnInstall() error {
19+
addr, _ := s.ctx.GetEnv("HTTP_ADDR")
20+
s.svc = &http.Server{Addr: addr}
21+
return nil
22+
}
23+
func (s httpServer) OnStart() error {
24+
return s.svc.ListenAndServe()
25+
}
26+
func (s httpServer) OnStop() error {
27+
return s.svc.Shutdown(context.Background())
28+
}
29+
30+
func main() {
31+
packaged.Register(func(ns packaged.Namespace) packaged.Service {
32+
return &httpServer{ctx: ns}
33+
})
34+
35+
if err := packaged.Run(); err != nil {
36+
log.Fatal(err)
37+
}
38+
39+
packaged.Wait()
40+
}

refs.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"sync"
88
)
99

10+
var (
11+
_ Service = &UnimplementedHandler{}
12+
_ Namespace = &namespace{}
13+
_ sort.Interface = &unitSorter{}
14+
)
15+
1016
type (
1117
ServiceType int32
1218
Restart int32
@@ -91,21 +97,28 @@ const (
9197
func (h UnimplementedHandler) mustEmbedUnimplemented() {}
9298
func (h UnimplementedHandler) Name() string { return "unimplemented" }
9399
func (h UnimplementedHandler) Type() ServiceType { return ServiceTypeIgnore }
94-
func (h UnimplementedHandler) OnInstall() error { panic("Unimplemented OnInstall") }
95-
func (h UnimplementedHandler) OnStart() error { panic("Unimplemented OnStart") }
96-
func (h UnimplementedHandler) OnStop() error { panic("Unimplemented OnStop") }
100+
func (h UnimplementedHandler) OnInstall() error { return errors.New("unimplemented OnInstall") }
101+
func (h UnimplementedHandler) OnStart() error { return errors.New("unimplemented OnStart") }
102+
func (h UnimplementedHandler) OnStop() error { return errors.New("unimplemented OnStop") }
97103

98104
func (n *namespace) Name() string { return n.name }
99-
func (n *namespace) Set(key string, value any) {
105+
106+
func (n *namespace) setValueWithLock(action func()) {
100107
n.rw.Lock()
101108
defer n.rw.Unlock()
102-
n.values[key] = value
109+
action()
110+
}
111+
112+
func (n *namespace) Set(key string, value any) {
113+
n.setValueWithLock(func() {
114+
n.values[key] = value
115+
})
103116
}
104117

105118
func (n *namespace) Del(key string) {
106-
n.rw.Lock()
107-
defer n.rw.Unlock()
108-
delete(n.values, key)
119+
n.setValueWithLock(func() {
120+
delete(n.values, key)
121+
})
109122
}
110123

111124
func (n *namespace) Get(key string) (any, bool) {
@@ -134,22 +147,21 @@ func newNamespace(name string) Namespace {
134147
}
135148
return &namespace{
136149
name: name,
137-
values: make(map[string]any),
150+
values: make(map[string]any, 16),
138151
services: make([]Service, 0, 8),
139152
EnvManager: env,
140153
}
141154
}
142155

143156
func (s unitSorter) Len() int { return len(s.units) }
144157
func (s unitSorter) Less(i, j int) bool {
145-
if s.desc {
146-
return s.units[i].Index > s.units[j].Index
147-
}
148-
return s.units[i].Index < s.units[j].Index
158+
return (s.units[i].Index > s.units[j].Index) == s.desc
149159
}
150160

151161
func (s unitSorter) Swap(i, j int) {
152162
s.units[i], s.units[j] = s.units[j], s.units[i]
153163
}
154164

155-
func (u Units) Sort(desc bool) { sort.Sort(unitSorter{units: u, desc: desc}) }
165+
func (u Units) Sort(desc bool) {
166+
sort.Sort(unitSorter{units: u, desc: desc})
167+
}

0 commit comments

Comments
 (0)