From 920deb3df7c41a4e729aaaea3a26dad27747669a Mon Sep 17 00:00:00 2001 From: Marius Date: Mon, 15 Aug 2022 23:26:58 +0200 Subject: [PATCH] cli: Add flag to specify progress interval --- cmd/tusd/cli/flags.go | 2 ++ cmd/tusd/cli/serve.go | 1 + pkg/handler/config.go | 9 +++++++++ pkg/handler/unrouted_handler.go | 4 ++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/tusd/cli/flags.go b/cmd/tusd/cli/flags.go index bee3eb3..0d16c0a 100644 --- a/cmd/tusd/cli/flags.go +++ b/cmd/tusd/cli/flags.go @@ -46,6 +46,7 @@ var Flags struct { GrpcHooksRetry int GrpcHooksBackoff int EnabledHooks []hooks.HookType + ProgressHooksInterval int64 ShowVersion bool ExposeMetrics bool MetricsPath string @@ -89,6 +90,7 @@ func ParseFlags() { flag.StringVar(&Flags.AzObjectPrefix, "azure-object-prefix", "", "Prefix for Azure object names") flag.StringVar(&Flags.AzEndpoint, "azure-endpoint", "", "Custom Endpoint to use for Azure BlockBlob Storage (requires azure-storage to be pass)") flag.StringVar(&Flags.EnabledHooksString, "hooks-enabled-events", "pre-create,post-create,post-receive,post-terminate,post-finish", "Comma separated list of enabled hook events (e.g. post-create,post-finish). Leave empty to enable default events") + flag.Int64Var(&Flags.ProgressHooksInterval, "progress-hooks-interval", 1000, "Interval in milliseconds at which the post-receive progress hooks are emitted for each active upload") flag.StringVar(&Flags.PluginHookPath, "hooks-plugin", "", "Path to a Go plugin for loading hook functions") flag.StringVar(&Flags.FileHooksDir, "hooks-dir", "", "Directory to search for available hooks scripts") flag.StringVar(&Flags.HttpHooksEndpoint, "hooks-http", "", "An HTTP endpoint to which hook events will be sent to") diff --git a/cmd/tusd/cli/serve.go b/cmd/tusd/cli/serve.go index 77ed125..e04469e 100644 --- a/cmd/tusd/cli/serve.go +++ b/cmd/tusd/cli/serve.go @@ -34,6 +34,7 @@ func Serve() { NotifyTerminatedUploads: true, NotifyUploadProgress: true, NotifyCreatedUploads: true, + UploadProgressInterval: time.Duration(Flags.ProgressHooksInterval) * time.Millisecond, } if err := SetupPreHooks(&config); err != nil { diff --git a/pkg/handler/config.go b/pkg/handler/config.go index bb3f610..5d0b4b6 100644 --- a/pkg/handler/config.go +++ b/pkg/handler/config.go @@ -5,6 +5,7 @@ import ( "log" "net/url" "os" + "time" ) // Config provides a way to configure the Handler depending on your needs. @@ -40,6 +41,10 @@ type Config struct { // NotifyCreatedUploads indicates whether sending notifications about // the upload having been created using the CreatedUploads channel should be enabled. NotifyCreatedUploads bool + // UploadProgressInterval specifies the interval at which the upload progress + // notifications are sent to the UploadProgress channel, if enabled. + // Defaults to 1s. + UploadProgressInterval time.Duration // Logger is the logger to use internally, mostly for printing requests. Logger *log.Logger // Respect the X-Forwarded-Host, X-Forwarded-Proto and Forwarded headers @@ -92,5 +97,9 @@ func (config *Config) validate() error { return errors.New("tusd: StoreComposer in Config needs to contain a non-nil core") } + if config.UploadProgressInterval <= 0 { + config.UploadProgressInterval = 1 * time.Second + } + return nil } diff --git a/pkg/handler/unrouted_handler.go b/pkg/handler/unrouted_handler.go index 9a55b88..1d49c77 100644 --- a/pkg/handler/unrouted_handler.go +++ b/pkg/handler/unrouted_handler.go @@ -776,7 +776,7 @@ var mimeInlineBrowserWhitelist = map[string]struct{}{ "audio/webm": struct{}{}, "video/webm": struct{}{}, "audio/ogg": struct{}{}, - "video/ogg": struct{}{}, + "video/ogg": struct{}{}, "application/ogg": struct{}{}, } @@ -984,7 +984,7 @@ func (handler *UnroutedHandler) sendProgressMessages(hook HookEvent, reader *bod previousOffset = hook.Upload.Offset } return - case <-time.After(1 * time.Second): + case <-time.After(handler.config.UploadProgressInterval): hook.Upload.Offset = originalOffset + reader.bytesRead() if hook.Upload.Offset != previousOffset { handler.UploadProgress <- hook