hooks.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. return gnet.None
  70. }
  71. func (s *DifyServer) OnShutdown(c gnet.Engine) {
  72. close(s.shutdown_chan)
  73. }
  74. func (s *DifyServer) OnTraffic(c gnet.Conn) (action gnet.Action) {
  75. codec := c.Context().(*codec)
  76. messages, err := codec.Decode(c)
  77. if err != nil {
  78. return gnet.Close
  79. }
  80. // get plugin runtime
  81. s.plugins_lock.RLock()
  82. runtime, ok := s.plugins[c.Fd()]
  83. s.plugins_lock.RUnlock()
  84. if !ok {
  85. return gnet.Close
  86. }
  87. // handle messages
  88. for _, message := range messages {
  89. s.onMessage(runtime, message)
  90. }
  91. return gnet.None
  92. }
  93. func (s *DifyServer) onMessage(runtime *RemotePluginRuntime, message []byte) {
  94. // handle message
  95. if runtime.handshake_failed {
  96. // do nothing if handshake has failed
  97. return
  98. }
  99. if !runtime.handshake {
  100. key := string(message)
  101. info, err := GetConnectionInfo(key)
  102. if err == cache.ErrNotFound {
  103. // close connection if handshake failed
  104. runtime.conn.Write([]byte("handshake failed, invalid key\n"))
  105. runtime.conn.Close()
  106. runtime.handshake_failed = true
  107. return
  108. } else if err != nil {
  109. // close connection if handshake failed
  110. runtime.conn.Write([]byte("internal error\n"))
  111. runtime.conn.Close()
  112. return
  113. }
  114. runtime.tenant_id = info.TenantId
  115. // handshake completed
  116. runtime.handshake = true
  117. } else if !runtime.registration_transferred {
  118. // process handle shake if not completed
  119. declaration, err := parser.UnmarshalJsonBytes[plugin_entities.PluginDeclaration](message)
  120. if err != nil {
  121. // close connection if handshake failed
  122. runtime.conn.Write([]byte("handshake failed\n"))
  123. runtime.conn.Close()
  124. return
  125. }
  126. runtime.Config = declaration
  127. // registration transferred
  128. runtime.registration_transferred = true
  129. runtime.checksum = runtime.calculateChecksum()
  130. runtime.InitState()
  131. runtime.SetActiveAt(time.Now())
  132. // trigger registration event
  133. if err := runtime.Register(); err != nil {
  134. runtime.conn.Write([]byte("register failed\n"))
  135. log.Error("register failed", "error", err)
  136. runtime.conn.Close()
  137. return
  138. }
  139. // publish runtime to watcher
  140. s.response.Write(runtime)
  141. } else {
  142. // continue handle messages if handshake completed
  143. runtime.response.Write(message)
  144. }
  145. }