Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Jul 30, 2024
1 parent 367cad5 commit 1ea1134
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 203 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
log.Panic("Invalid configuration: %s", err.Error())
}

server.Run(&config)
(&server.App{}).Run(&config)
}

func setDefault(config *app.Config) {
Expand Down
28 changes: 14 additions & 14 deletions internal/cluster/init.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package cluster

import "github.com/langgenius/dify-plugin-daemon/internal/types/app"
import (
"sync"

"github.com/langgenius/dify-plugin-daemon/internal/types/app"
)

type Cluster struct {
port uint16
}

var (
cluster *Cluster
)
plugins map[string]*PluginLifeTime
plugin_lock sync.Mutex
}

func Launch(config *app.Config) {
cluster = &Cluster{
port: uint16(config.ServerPort),
func NewCluster(config *app.Config) *Cluster {
return &Cluster{
port: uint16(config.ServerPort),
plugins: make(map[string]*PluginLifeTime),
}

go func() {
cluster.clusterLifetime()
}()
}

func GetCluster() *Cluster {
return cluster
func (c *Cluster) Launch(config *app.Config) {
go c.clusterLifetime()
}
39 changes: 39 additions & 0 deletions internal/cluster/state.go
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
package cluster

import (
"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
)

type PluginLifeTime struct {
lifetime entities.PluginRuntimeTimeLifeInterface
}

// RegisterPlugin registers a plugin to the cluster, and start to be scheduled
func (c *Cluster) RegisterPlugin(lifetime entities.PluginRuntimeTimeLifeInterface) error {
identity, err := lifetime.Identity()
if err != nil {
return err
}

lifetime.OnStop(func() {
c.plugin_lock.Lock()
delete(c.plugins, identity)
c.plugin_lock.Unlock()
})

c.plugin_lock.Lock()
if !lifetime.Stopped() {
c.plugins[identity] = &PluginLifeTime{
lifetime: lifetime,
}
}
c.plugin_lock.Unlock()

log.Info("start to schedule plugin %s", identity)

return nil
}

func (c *Cluster) SchedulePlugin(lifetime entities.PluginRuntimeTimeLifeInterface) error {
return nil
}
6 changes: 2 additions & 4 deletions internal/core/plugin_daemon/model_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ func genericInvokePlugin[Req any, Rsp any](
response_buffer_size int,
typ backwards_invocation.PluginAccessType,
action backwards_invocation.PluginAccessAction,
) (
*stream.StreamResponse[Rsp], error,
) {
runtime := plugin_manager.Get(session.PluginIdentity())
) (*stream.StreamResponse[Rsp], error) {
runtime := plugin_manager.GetGlobalPluginManager().Get(session.PluginIdentity())
if runtime == nil {
return nil, errors.New("plugin not found")
}
Expand Down
4 changes: 3 additions & 1 deletion internal/core/plugin_manager/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
)

var m sync.Map
var (
m sync.Map
)

func checkPluginExist(identity string) (entities.PluginRuntimeInterface, bool) {
if v, ok := m.Load(identity); ok {
Expand Down
16 changes: 8 additions & 8 deletions internal/core/plugin_manager/lifetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
)

func lifetime(config *app.Config, r entities.PluginRuntimeInterface) {
func (p *PluginManager) lifetime(config *app.Config, r entities.PluginRuntimeInterface) {
start_failed_times := 0
configuration := r.Configuration()

log.Info("new plugin logged in: %s", configuration.Identity())
defer log.Info("plugin %s has exited", configuration.Identity())

// store plugin runtime
m.Store(configuration.Identity(), r)
defer m.Delete(configuration.Identity())

// update lifetime state for this pod
addLifetimeState(r)
// add plugin to cluster
err := p.cluster.RegisterPlugin(r)
if err != nil {
log.Error("add plugin to cluster failed: %s", err.Error())
return
}

// remove lifetime state after plugin if it has been stopped
defer deleteLifetimeState(r)
defer r.TriggerStop()

for !r.Stopped() {
if err := r.InitEnvironment(); err != nil {
Expand Down
146 changes: 0 additions & 146 deletions internal/core/plugin_manager/lifetime_manager.go

This file was deleted.

34 changes: 25 additions & 9 deletions internal/core/plugin_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,33 @@ package plugin_manager
import (
"fmt"

"github.com/langgenius/dify-plugin-daemon/internal/cluster"
"github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
)

func List() []entities.PluginRuntimeInterface {
type PluginManager struct {
cluster *cluster.Cluster
}

var (
manager *PluginManager
)

func InitGlobalPluginManager(cluster *cluster.Cluster) {
manager = &PluginManager{
cluster: cluster,
}
}

func GetGlobalPluginManager() *PluginManager {
return manager
}

func (p *PluginManager) List() []entities.PluginRuntimeInterface {
var runtimes []entities.PluginRuntimeInterface
m.Range(func(key, value interface{}) bool {
if v, ok := value.(entities.PluginRuntimeInterface); ok {
Expand All @@ -21,7 +40,7 @@ func List() []entities.PluginRuntimeInterface {
return runtimes
}

func Get(identity string) entities.PluginRuntimeInterface {
func (p *PluginManager) Get(identity string) entities.PluginRuntimeInterface {
if v, ok := m.Load(identity); ok {
if r, ok := v.(entities.PluginRuntimeInterface); ok {
return r
Expand All @@ -30,15 +49,15 @@ func Get(identity string) entities.PluginRuntimeInterface {
return nil
}

func Put(path string, binary []byte) {
func (p *PluginManager) Put(path string, binary []byte) {
//TODO: put binary into
}

func Delete(identity string) {
func (p *PluginManager) Delete(identity string) {
//TODO: delete binary from
}

func Init(configuration *app.Config) {
func (p *PluginManager) Init(configuration *app.Config) {
// TODO: init plugin manager
log.Info("start plugin manager daemon...")

Expand All @@ -57,8 +76,5 @@ func Init(configuration *app.Config) {
}

// start plugin watcher
startWatcher(configuration)

// start plugin lifetime manager
startLifeTimeManager(configuration)
p.startWatcher(configuration)
}
Loading

0 comments on commit 1ea1134

Please sign in to comment.