NewLocalDownloadServiceWithInput creates a service using a provided input channel. If inputCh is nil, a new buffered channel is created.
(pool *download.WorkerPool, inputCh chan interface{})
| 99 | // NewLocalDownloadServiceWithInput creates a service using a provided input channel. |
| 100 | // If inputCh is nil, a new buffered channel is created. |
| 101 | func NewLocalDownloadServiceWithInput(pool *download.WorkerPool, inputCh chan interface{}) *LocalDownloadService { |
| 102 | if inputCh == nil { |
| 103 | inputCh = make(chan interface{}, 100) |
| 104 | } |
| 105 | s := &LocalDownloadService{ |
| 106 | Pool: pool, |
| 107 | InputCh: inputCh, |
| 108 | listeners: make([]chan interface{}, 0), |
| 109 | } |
| 110 | |
| 111 | // Load initial settings |
| 112 | if s.settings, _ = config.LoadSettings(); s.settings == nil { |
| 113 | s.settings = config.DefaultSettings() |
| 114 | } |
| 115 | if pool != nil && s.settings != nil { |
| 116 | runtime := s.settings.ToRuntimeConfig() |
| 117 | pool.SetGlobalRateLimit(runtime.GlobalRateLimitBps) |
| 118 | pool.SetDefaultDownloadRateLimit(runtime.DefaultDownloadRateLimitBps) |
| 119 | } |
| 120 | |
| 121 | // Lifecycle |
| 122 | ctx, cancel := context.WithCancel(context.Background()) |
| 123 | s.ctx = ctx |
| 124 | s.cancel = cancel |
| 125 | |
| 126 | // Start broadcaster |
| 127 | s.broadcastWG.Add(1) |
| 128 | go func() { |
| 129 | defer s.broadcastWG.Done() |
| 130 | s.broadcastLoop() |
| 131 | }() |
| 132 | |
| 133 | // Start progress reporter |
| 134 | if pool != nil { |
| 135 | s.reportTicker = time.NewTicker(ReportInterval) |
| 136 | s.reportWG.Add(1) |
| 137 | go func() { |
| 138 | defer s.reportWG.Done() |
| 139 | s.reportProgressLoop() |
| 140 | }() |
| 141 | } |
| 142 | |
| 143 | return s |
| 144 | } |
| 145 | |
| 146 | func (s *LocalDownloadService) broadcastLoop() { |
| 147 | for msg := range s.InputCh { |