Conditions | 12 |
Total Lines | 66 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like main.Execute often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package main |
||
87 | Execute(downloadURL, nil, *conn, *skiptls, proxy, bwLimit) |
||
88 | } |
||
89 | |||
90 | func downloadTask(url string, state *State, conn int, skiptls bool, proxy string, bwLimit string) task.Task { |
||
91 | run := func(t task.Task, ctx task.Context) { |
||
92 | Execute(url, state, conn, skiptls, proxy, bwLimit) |
||
93 | } |
||
94 | return task.NewTaskWithFunc(run) |
||
95 | } |
||
96 | |||
97 | // Execute configures the HTTPDownloader and uses it to download the target. |
||
98 | func Execute(url string, state *State, conn int, skiptls bool, proxy string, bwLimit string) { |
||
99 | // Capture OS interrupt signals |
||
100 | signalChan := make(chan os.Signal, 1) |
||
101 | signal.Notify(signalChan, |
||
102 | syscall.SIGHUP, |
||
103 | syscall.SIGINT, |
||
104 | syscall.SIGTERM, |
||
105 | syscall.SIGQUIT) |
||
106 | |||
107 | var files = make([]string, 0) |
||
108 | var parts = make([]Part, 0) |
||
109 | var isInterrupted = false |
||
110 | |||
111 | doneChan := make(chan bool, conn) |
||
112 | fileChan := make(chan string, conn) |
||
113 | errorChan := make(chan error, 1) |
||
114 | stateChan := make(chan Part, 1) |
||
115 | interruptChan := make(chan bool, conn) |
||
116 | |||
117 | var downloader *HTTPDownloader |
||
118 | if state == nil { |
||
119 | downloader = NewHTTPDownloader(url, conn, skiptls, proxy, bwLimit) |
||
120 | } else { |
||
121 | downloader = &HTTPDownloader{ |
||
122 | url: state.URL, |
||
123 | file: TaskFromURL(state.URL), |
||
124 | par: int64(len(state.Parts)), |
||
125 | parts: state.Parts, |
||
126 | resumable: true, |
||
127 | } |
||
128 | } |
||
129 | go downloader.Do(doneChan, fileChan, errorChan, interruptChan, stateChan) |
||
130 | |||
131 | for { |
||
132 | select { |
||
133 | case <-signalChan: |
||
134 | // Signal all active download routines to interrupt. |
||
135 | isInterrupted = true |
||
136 | for i := 0; i < conn; i++ { |
||
137 | interruptChan <- true |
||
138 | } |
||
139 | case file := <-fileChan: |
||
140 | files = append(files, file) |
||
141 | case err := <-errorChan: |
||
142 | Errorf("%v", err) |
||
143 | panic(err) |
||
144 | case part := <-stateChan: |
||
145 | parts = append(parts, part) |
||
146 | case <-doneChan: |
||
147 | // Ensure we drain remaining part notifications before finalizing. |
||
148 | numParts := len(downloader.parts) |
||
149 | for len(files) < numParts { |
||
150 | file := <-fileChan |
||
151 | files = append(files, file) |
||
152 | } |
||
153 | for len(parts) < numParts { |
||
193 |