From 10428941231a3baa9c07f50fe01d417d2dcc2746 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Fri, 3 Oct 2014 01:31:38 +0400 Subject: [PATCH] Abort downloader on shutdown, don't wait for downloads to finish. #45 #114 --- aptly/interfaces.go | 2 ++ cmd/context.go | 2 +- http/download.go | 11 +++++++++-- http/fake.go | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/aptly/interfaces.go b/aptly/interfaces.go index c79b6e15..b6375784 100644 --- a/aptly/interfaces.go +++ b/aptly/interfaces.go @@ -90,6 +90,8 @@ type Downloader interface { // Shutdown stops downloader after current tasks are finished, // but doesn't process rest of queue Shutdown() + // Abort stops downloader without waiting for shutdown + Abort() // GetProgress returns Progress object GetProgress() Progress } diff --git a/cmd/context.go b/cmd/context.go index 7befc96d..f8472f4f 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -289,7 +289,7 @@ func ShutdownContext() { context.database = nil } if context.downloader != nil { - context.downloader.Shutdown() + context.downloader.Abort() context.downloader = nil } if context.progress != nil { diff --git a/http/download.go b/http/download.go index 4332d56f..adf3c914 100644 --- a/http/download.go +++ b/http/download.go @@ -52,8 +52,8 @@ func NewDownloader(threads int, downLimit int64, progress aptly.Progress) aptly. downloader := &downloaderImpl{ queue: make(chan *downloadTask, 1000), - stop: make(chan struct{}), - stopped: make(chan struct{}), + stop: make(chan struct{}, threads), + stopped: make(chan struct{}, threads), pause: make(chan struct{}), unpause: make(chan struct{}), threads: threads, @@ -88,6 +88,13 @@ func (downloader *downloaderImpl) Shutdown() { } } +// Abort stops downloader but doesn't wait for downloader to stop +func (downloader *downloaderImpl) Abort() { + for i := 0; i < downloader.threads; i++ { + downloader.stop <- struct{}{} + } +} + // Pause pauses task processing func (downloader *downloaderImpl) Pause() { for i := 0; i < downloader.threads; i++ { diff --git a/http/fake.go b/http/fake.go index 4d256a3f..c2eda915 100644 --- a/http/fake.go +++ b/http/fake.go @@ -123,6 +123,10 @@ func (f *FakeDownloader) Download(url string, filename string, result chan<- err func (f *FakeDownloader) Shutdown() { } +// Abort does nothing +func (f *FakeDownloader) Abort() { +} + // Pause does nothing func (f *FakeDownloader) Pause() { }