|
@@ -45,6 +45,9 @@ type DifyServer struct {
|
|
plugins_lock *sync.RWMutex
|
|
plugins_lock *sync.RWMutex
|
|
|
|
|
|
shutdown_chan chan bool
|
|
shutdown_chan chan bool
|
|
|
|
+
|
|
|
|
+ max_conn int32
|
|
|
|
+ current_conn int32
|
|
}
|
|
}
|
|
|
|
|
|
func (s *DifyServer) OnBoot(c gnet.Engine) (action gnet.Action) {
|
|
func (s *DifyServer) OnBoot(c gnet.Engine) (action gnet.Action) {
|
|
@@ -99,6 +102,10 @@ func (s *DifyServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
|
delete(s.plugins, c.Fd())
|
|
delete(s.plugins, c.Fd())
|
|
s.plugins_lock.Unlock()
|
|
s.plugins_lock.Unlock()
|
|
|
|
|
|
|
|
+ if plugin == nil {
|
|
|
|
+ return gnet.None
|
|
|
|
+ }
|
|
|
|
+
|
|
// close plugin
|
|
// close plugin
|
|
plugin.onDisconnected()
|
|
plugin.onDisconnected()
|
|
|
|
|
|
@@ -108,6 +115,9 @@ func (s *DifyServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
|
if err := plugin.Unregister(); err != nil {
|
|
if err := plugin.Unregister(); err != nil {
|
|
log.Error("unregister plugin failed, error: %v", err)
|
|
log.Error("unregister plugin failed, error: %v", err)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // decrease current connection
|
|
|
|
+ atomic.AddInt32(&s.current_conn, -1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -271,6 +281,12 @@ func (s *DifyServer) onMessage(runtime *RemotePluginRuntime, message []byte) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ atomic.AddInt32(&s.current_conn, 1)
|
|
|
|
+ if atomic.LoadInt32(&s.current_conn) > int32(s.max_conn) {
|
|
|
|
+ close([]byte("server is busy now, please try again later\n"))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
// fill in default values
|
|
// fill in default values
|
|
runtime.Config.FillInDefaultValues()
|
|
runtime.Config.FillInDefaultValues()
|
|
|
|
|