Skip to content

Commit b06732f

Browse files
authored
fix(plugin): support customized stdio buffer size, align serverless runtime with local runtime (#470)
* fix(plugin): support customized stdio buffer size, align serverless runtime with local runtime * fix(plugin): update config naming to pluginRuntimeBufferSize & pluginRuntimeMaxBufferSize, and keep the compatibility to stdoutBufferSize * fix: provide a default value for PLUGIN_RUNTIME_BUFFER_SIZE & PLUGIN_RUNTIME_MAX_BUFFER_SIZE * fix: remove redundant logic * fix: add compatibility to PLUGIN_STDIO_BUFFER_SIZE & PLUGIN_STDIO_MAX_BUFFER_SIZE
1 parent 9d7a48d commit b06732f

6 files changed

Lines changed: 38 additions & 7 deletions

File tree

.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,14 @@ ENFORCE_LANGGENIUS_PLUGIN_SIGNATURES=true
149149
HTTP_PROXY=
150150
HTTPS_PROXY=
151151

152-
# plugin stdio buffer size
152+
# plugin stdio buffer size(local runtime, will be deprecated in future version)
153153
PLUGIN_STDIO_BUFFER_SIZE=1024
154154
PLUGIN_STDIO_MAX_BUFFER_SIZE=5242880
155155

156+
# plugin runtime buffer size(serverless & local runtime)
157+
PLUGIN_RUNTIME_BUFFER_SIZE=1024
158+
PLUGIN_RUNTIME_MAX_BUFFER_SIZE=5242880
159+
156160
# dify backwards invocation write timeout in milliseconds
157161
DIFY_BACKWARDS_INVOCATION_WRITE_TIMEOUT=5000
158162
# dify backwards invocation read timeout in milliseconds

internal/core/plugin_manager/launcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ func (p *PluginManager) launchLocal(pluginUniqueIdentifier plugin_entities.Plugi
140140
PipMirrorUrl: p.config.PipMirrorUrl,
141141
PipPreferBinary: *p.config.PipPreferBinary,
142142
PipExtraArgs: p.config.PipExtraArgs,
143-
StdoutBufferSize: p.config.PluginStdioBufferSize,
144-
StdoutMaxBufferSize: p.config.PluginStdioMaxBufferSize,
143+
StdoutBufferSize: p.config.GetLocalRuntimeBufferSize(),
144+
StdoutMaxBufferSize: p.config.GetLocalRuntimeMaxBufferSize(),
145145
})
146146
localPluginRuntime.PluginRuntime = plugin.runtime
147147
localPluginRuntime.BasicChecksum = basic_runtime.BasicChecksum{

internal/core/plugin_manager/serverless.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (p *PluginManager) getServerlessPluginRuntime(
4444
runtimeEntity.InitState()
4545

4646
// convert to plugin runtime
47-
pluginRuntime := serverless_runtime.ServerlessPluginRuntime{
47+
pluginRuntime := &serverless_runtime.ServerlessPluginRuntime{
4848
BasicChecksum: basic_runtime.BasicChecksum{
4949
MediaTransport: basic_runtime.NewMediaTransport(p.mediaBucket),
5050
InnerChecksum: model.Checksum,
@@ -53,13 +53,15 @@ func (p *PluginManager) getServerlessPluginRuntime(
5353
LambdaURL: model.FunctionURL,
5454
LambdaName: model.FunctionName,
5555
PluginMaxExecutionTimeout: p.config.PluginMaxExecutionTimeout,
56+
RuntimeBufferSize: p.config.PluginRuntimeBufferSize,
57+
RuntimeMaxBufferSize: p.config.PluginRuntimeMaxBufferSize,
5658
}
5759

5860
if err := pluginRuntime.InitEnvironment(); err != nil {
5961
return nil, err
6062
}
6163

62-
return &pluginRuntime, nil
64+
return pluginRuntime, nil
6365
}
6466

6567
func (p *PluginManager) getServerlessPluginRuntimeModel(

internal/core/plugin_manager/serverless_runtime/io.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ func (r *ServerlessPluginRuntime) Write(sessionId string, action access_types.Pl
8787
scanner := bufio.NewScanner(response.Body)
8888
defer response.Body.Close()
8989

90-
// TODO: set a reasonable buffer size or use a reader, this is a temporary solution
91-
scanner.Buffer(make([]byte, 1024), 5*1024*1024)
90+
scanner.Buffer(make([]byte, r.RuntimeBufferSize), r.RuntimeMaxBufferSize)
9291

9392
sessionAlive := true
9493
for scanner.Scan() && sessionAlive {

internal/core/plugin_manager/serverless_runtime/type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ type ServerlessPluginRuntime struct {
2323
client *http.Client
2424

2525
PluginMaxExecutionTimeout int // in seconds
26+
27+
RuntimeBufferSize int
28+
RuntimeMaxBufferSize int
2629
}

internal/types/app/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ type Config struct {
173173
PipVerbose *bool `envconfig:"PIP_VERBOSE"`
174174
PipExtraArgs string `envconfig:"PIP_EXTRA_ARGS"`
175175

176+
// Runtime buffer configuration (applies to both local and serverless runtimes)
177+
// These are the new generic names that should be used going forward
178+
PluginRuntimeBufferSize int `envconfig:"PLUGIN_RUNTIME_BUFFER_SIZE" default:"1024"`
179+
PluginRuntimeMaxBufferSize int `envconfig:"PLUGIN_RUNTIME_MAX_BUFFER_SIZE" default:"5242880"`
180+
181+
// Legacy STDIO-specific buffer configuration (kept for backward compatibility)
182+
// If the new PluginRuntime* configs are not set, these will be used as fallback
176183
PluginStdioBufferSize int `envconfig:"PLUGIN_STDIO_BUFFER_SIZE" default:"1024"`
177184
PluginStdioMaxBufferSize int `envconfig:"PLUGIN_STDIO_MAX_BUFFER_SIZE" default:"5242880"`
178185

@@ -253,6 +260,22 @@ func (c *Config) Validate() error {
253260
return nil
254261
}
255262

263+
// Prefers Stdio (legacy) config if user has customized it, falls back to Runtime (new) config.
264+
func (c *Config) GetLocalRuntimeBufferSize() int {
265+
if c.PluginStdioBufferSize != 1024 {
266+
return c.PluginStdioBufferSize
267+
}
268+
return c.PluginRuntimeBufferSize
269+
}
270+
271+
// Prefers Stdio (legacy) config if user has customized it, falls back to Runtime (new) config.
272+
func (c *Config) GetLocalRuntimeMaxBufferSize() int {
273+
if c.PluginStdioMaxBufferSize != 5242880 {
274+
return c.PluginStdioMaxBufferSize
275+
}
276+
return c.PluginRuntimeMaxBufferSize
277+
}
278+
256279
type PlatformType string
257280

258281
const (

0 commit comments

Comments
 (0)