Skip to content

Commit 2406a44

Browse files
committed
stz plugin
1 parent a9d0e9d commit 2406a44

5 files changed

Lines changed: 139 additions & 0 deletions

File tree

server/internal/plugins/manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/m1k1o/neko/server/internal/config"
1414
"github.com/m1k1o/neko/server/internal/plugins/chat"
1515
"github.com/m1k1o/neko/server/internal/plugins/filetransfer"
16+
"github.com/m1k1o/neko/server/internal/plugins/scaletozero"
1617
"github.com/m1k1o/neko/server/pkg/types"
1718
)
1819

@@ -47,6 +48,7 @@ func New(config *config.Plugins) *ManagerCtx {
4748
// add built-in plugins
4849
manager.plugins.addPlugin(filetransfer.NewPlugin())
4950
manager.plugins.addPlugin(chat.NewPlugin())
51+
manager.plugins.addPlugin(scaletozero.NewPlugin())
5052

5153
return manager
5254
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package scaletozero
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/spf13/viper"
6+
)
7+
8+
type Config struct {
9+
Enabled bool
10+
}
11+
12+
func (Config) Init(cmd *cobra.Command) error {
13+
cmd.PersistentFlags().Bool("scaletozero.enabled", false, "enable scale-to-zero")
14+
if err := viper.BindPFlag("scaletozero.enabled", cmd.PersistentFlags().Lookup("scaletozero.enabled")); err != nil {
15+
return err
16+
}
17+
18+
return nil
19+
}
20+
21+
func (c *Config) Set() {
22+
c.Enabled = viper.GetBool("scaletozero.enabled")
23+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package scaletozero
2+
3+
import (
4+
"context"
5+
"sync"
6+
7+
"github.com/m1k1o/neko/server/pkg/types"
8+
"github.com/onkernel/kernel-images/server/lib/scaletozero"
9+
"github.com/rs/zerolog"
10+
"github.com/rs/zerolog/log"
11+
)
12+
13+
func NewManager(
14+
sessions types.SessionManager,
15+
config *Config,
16+
) *Manager {
17+
logger := log.With().Str("module", "scaletozero").Logger()
18+
19+
return &Manager{
20+
logger: logger,
21+
config: config,
22+
sessions: sessions,
23+
ctrl: scaletozero.NewUnikraftCloudController(),
24+
}
25+
}
26+
27+
type Manager struct {
28+
logger zerolog.Logger
29+
config *Config
30+
sessions types.SessionManager
31+
ctrl scaletozero.Controller
32+
mu sync.Mutex
33+
shutdown bool
34+
pending int
35+
}
36+
37+
func (m *Manager) Start() error {
38+
if !m.config.Enabled {
39+
return nil
40+
}
41+
m.logger.Info().Msg("scale-to-zero plugin enabled")
42+
43+
m.sessions.OnConnected(func(session types.Session) {
44+
m.mu.Lock()
45+
defer m.mu.Unlock()
46+
if m.shutdown {
47+
return
48+
}
49+
50+
m.pending++
51+
m.ctrl.Disable(context.Background())
52+
})
53+
54+
m.sessions.OnDisconnected(func(session types.Session) {
55+
m.mu.Lock()
56+
defer m.mu.Unlock()
57+
if m.shutdown {
58+
return
59+
}
60+
m.pending--
61+
m.ctrl.Enable(context.Background())
62+
})
63+
64+
m.logger.Info().Msg("scale-to-zero hooks enabled")
65+
return nil
66+
}
67+
68+
func (m *Manager) Shutdown() error {
69+
m.mu.Lock()
70+
defer m.mu.Unlock()
71+
m.shutdown = true
72+
73+
for i := 0; i < m.pending; i++ {
74+
m.ctrl.Enable(context.Background())
75+
}
76+
77+
return nil
78+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package scaletozero
2+
3+
import (
4+
"github.com/m1k1o/neko/server/pkg/types"
5+
)
6+
7+
type Plugin struct {
8+
config *Config
9+
manager *Manager
10+
}
11+
12+
func NewPlugin() *Plugin {
13+
return &Plugin{
14+
config: &Config{},
15+
}
16+
}
17+
18+
func (p *Plugin) Name() string {
19+
return PluginName
20+
}
21+
22+
func (p *Plugin) Config() types.PluginConfig {
23+
return p.config
24+
}
25+
26+
func (p *Plugin) Start(m types.PluginManagers) error {
27+
p.manager = NewManager(m.SessionManager, p.config)
28+
return p.manager.Start()
29+
}
30+
31+
func (p *Plugin) Shutdown() error {
32+
return p.manager.Shutdown()
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package scaletozero
2+
3+
const PluginName = "scaletozero"

0 commit comments

Comments
 (0)