hooks.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package remote_manager
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  6. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  7. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  10. "github.com/panjf2000/gnet/v2"
  11. )
  12. type DifyServer struct {
  13. gnet.BuiltinEventEngine
  14. engine gnet.Engine
  15. // listening address
  16. addr string
  17. port uint16
  18. // enabled multicore
  19. multicore bool
  20. // event loop count
  21. num_loops int
  22. // read new connections
  23. response *stream.StreamResponse[*RemotePluginRuntime]
  24. plugins map[int]*RemotePluginRuntime
  25. plugins_lock *sync.RWMutex
  26. shutdown_chan chan bool
  27. }
  28. func (s *DifyServer) OnBoot(c gnet.Engine) (action gnet.Action) {
  29. s.engine = c
  30. return gnet.None
  31. }
  32. func (s *DifyServer) OnOpen(c gnet.Conn) (out []byte, action gnet.Action) {
  33. // new plugin connected
  34. c.SetContext(&codec{})
  35. runtime := &RemotePluginRuntime{
  36. conn: c,
  37. response: stream.NewStreamResponse[[]byte](512),
  38. callbacks: make(map[string][]func([]byte)),
  39. callbacks_lock: &sync.RWMutex{},
  40. shutdown_chan: make(chan bool),
  41. alive: true,
  42. }
  43. // store plugin runtime
  44. s.plugins_lock.Lock()
  45. s.plugins[c.Fd()] = runtime
  46. s.plugins_lock.Unlock()
  47. // start a timer to check if handshake is completed in 10 seconds
  48. time.AfterFunc(time.Second*10, func() {
  49. if !runtime.handshake {
  50. // close connection
  51. c.Close()
  52. }
  53. })
  54. // verified
  55. verified := true
  56. if verified {
  57. return nil, gnet.None
  58. }
  59. return nil, gnet.Close
  60. }
  61. func (s *DifyServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
  62. // plugin disconnected
  63. s.plugins_lock.Lock()
  64. plugin := s.plugins[c.Fd()]
  65. delete(s.plugins, c.Fd())
  66. s.plugins_lock.Unlock()
  67. // close plugin
  68. plugin.onDisconnected()
  69. // uninstall plugin
  70. if plugin.handshake && plugin.registration_transferred {
  71. if err := plugin.Unregister(); err != nil {
  72. log.Error("unregister plugin failed, error: %v", err)
  73. }
  74. }
  75. return gnet.None
  76. }
  77. func (s *DifyServer) OnShutdown(c gnet.Engine) {
  78. close(s.shutdown_chan)
  79. }
  80. func (s *DifyServer) OnTraffic(c gnet.Conn) (action gnet.Action) {
  81. codec := c.Context().(*codec)
  82. messages, err := codec.Decode(c)
  83. if err != nil {
  84. return gnet.Close
  85. }
  86. // get plugin runtime
  87. s.plugins_lock.RLock()
  88. runtime, ok := s.plugins[c.Fd()]
  89. s.plugins_lock.RUnlock()
  90. if !ok {
  91. return gnet.Close
  92. }
  93. // handle messages
  94. for _, message := range messages {
  95. s.onMessage(runtime, message)
  96. }
  97. return gnet.None
  98. }
  99. func (s *DifyServer) onMessage(runtime *RemotePluginRuntime, message []byte) {
  100. // handle message
  101. if runtime.handshake_failed {
  102. // do nothing if handshake has failed
  103. return
  104. }
  105. if !runtime.handshake {
  106. key := string(message)
  107. info, err := GetConnectionInfo(key)
  108. if err == cache.ErrNotFound {
  109. // close connection if handshake failed
  110. runtime.conn.Write([]byte("handshake failed, invalid key\n"))
  111. runtime.conn.Close()
  112. runtime.handshake_failed = true
  113. return
  114. } else if err != nil {
  115. // close connection if handshake failed
  116. runtime.conn.Write([]byte("internal error\n"))
  117. runtime.conn.Close()
  118. return
  119. }
  120. runtime.tenant_id = info.TenantId
  121. // handshake completed
  122. runtime.handshake = true
  123. } else if !runtime.registration_transferred {
  124. // process handle shake if not completed
  125. declaration, err := parser.UnmarshalJsonBytes[plugin_entities.PluginDeclaration](message)
  126. if err != nil {
  127. // close connection if handshake failed
  128. runtime.conn.Write([]byte("handshake failed\n"))
  129. runtime.conn.Close()
  130. return
  131. }
  132. runtime.Config = declaration
  133. // registration transferred
  134. runtime.registration_transferred = true
  135. runtime.checksum = runtime.calculateChecksum()
  136. runtime.InitState()
  137. runtime.SetActiveAt(time.Now())
  138. // trigger registration event
  139. if err := runtime.Register(); err != nil {
  140. runtime.conn.Write([]byte("register failed\n"))
  141. log.Error("register failed, error: %v", err)
  142. runtime.conn.Close()
  143. return
  144. }
  145. // publish runtime to watcher
  146. s.response.Write(runtime)
  147. } else {
  148. // continue handle messages if handshake completed
  149. runtime.response.Write(message)
  150. }
  151. }