|
@@ -2,6 +2,8 @@ package server
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
@@ -35,9 +37,21 @@ func (app *App) Endpoint(config *app.Config) func(c *gin.Context) {
|
|
}
|
|
}
|
|
|
|
|
|
func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTime time.Duration, path string) {
|
|
func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTime time.Duration, path string) {
|
|
- endpoint, err := db.GetOne[models.Endpoint](
|
|
|
|
- db.Equal("hook_id", hookId),
|
|
|
|
|
|
+ endpointCacheKey := strings.Join(
|
|
|
|
+ []string{
|
|
|
|
+ "hook_id",
|
|
|
|
+ hookId,
|
|
|
|
+ },
|
|
|
|
+ ":",
|
|
)
|
|
)
|
|
|
|
+ endpoint, err := cache.AutoGetWithGetter[models.Endpoint](
|
|
|
|
+ endpointCacheKey,
|
|
|
|
+ func() (*models.Endpoint, error) {
|
|
|
|
+ v, err := db.GetOne[models.Endpoint](
|
|
|
|
+ db.Equal("hook_id", hookId),
|
|
|
|
+ )
|
|
|
|
+ return &v, err
|
|
|
|
+ })
|
|
if err == db.ErrDatabaseNotFound {
|
|
if err == db.ErrDatabaseNotFound {
|
|
ctx.JSON(404, exception.BadRequestError(errors.New("endpoint not found")).ToResponse())
|
|
ctx.JSON(404, exception.BadRequestError(errors.New("endpoint not found")).ToResponse())
|
|
return
|
|
return
|
|
@@ -50,9 +64,24 @@ func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTim
|
|
}
|
|
}
|
|
|
|
|
|
// get plugin installation
|
|
// get plugin installation
|
|
- pluginInstallation, err := db.GetOne[models.PluginInstallation](
|
|
|
|
- db.Equal("plugin_id", endpoint.PluginID),
|
|
|
|
- db.Equal("tenant_id", endpoint.TenantID),
|
|
|
|
|
|
+ pluginInstallationCacheKey := strings.Join(
|
|
|
|
+ []string{
|
|
|
|
+ "plugin_id",
|
|
|
|
+ endpoint.PluginID,
|
|
|
|
+ "tenant_id",
|
|
|
|
+ endpoint.TenantID,
|
|
|
|
+ },
|
|
|
|
+ ":",
|
|
|
|
+ )
|
|
|
|
+ pluginInstallation, err := cache.AutoGetWithGetter[models.PluginInstallation](
|
|
|
|
+ pluginInstallationCacheKey,
|
|
|
|
+ func() (*models.PluginInstallation, error) {
|
|
|
|
+ v, err := db.GetOne[models.PluginInstallation](
|
|
|
|
+ db.Equal("plugin_id", endpoint.PluginID),
|
|
|
|
+ db.Equal("tenant_id", endpoint.TenantID),
|
|
|
|
+ )
|
|
|
|
+ return &v, err
|
|
|
|
+ },
|
|
)
|
|
)
|
|
if err != nil {
|
|
if err != nil {
|
|
ctx.JSON(404, exception.BadRequestError(errors.New("plugin installation not found")).ToResponse())
|
|
ctx.JSON(404, exception.BadRequestError(errors.New("plugin installation not found")).ToResponse())
|
|
@@ -73,6 +102,6 @@ func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTim
|
|
if ok, originalError := app.cluster.IsPluginOnCurrentNode(pluginUniqueIdentifier); !ok {
|
|
if ok, originalError := app.cluster.IsPluginOnCurrentNode(pluginUniqueIdentifier); !ok {
|
|
app.redirectPluginInvokeByPluginIdentifier(ctx, pluginUniqueIdentifier, originalError)
|
|
app.redirectPluginInvokeByPluginIdentifier(ctx, pluginUniqueIdentifier, originalError)
|
|
} else {
|
|
} else {
|
|
- service.Endpoint(ctx, &endpoint, &pluginInstallation, maxExecutionTime, path)
|
|
|
|
|
|
+ service.Endpoint(ctx, endpoint, pluginInstallation, maxExecutionTime, path)
|
|
}
|
|
}
|
|
}
|
|
}
|