TraverseDecorators from withing p.serve loop
Remedy possible data race.
Vladimir Bauer
1 year, 10 months ago
| 147 | 147 | if filler == nil { |
| 148 | 148 | filler = NopStyle().Build() |
| 149 | 149 | } |
| 150 | type result struct { | |
| 151 | bar *Bar | |
| 152 | bs *bState | |
| 153 | } | |
| 154 | ch := make(chan result) | |
| 150 | ch := make(chan *Bar) | |
| 155 | 151 | select { |
| 156 | 152 | case p.operateState <- func(ps *pState) { |
| 157 | 153 | bs := ps.makeBarState(total, filler, options...) |
| 158 | 154 | bar := newBar(ps.ctx, p, bs) |
| 155 | bar.TraverseDecorators(func(d decor.Decorator) { | |
| 156 | if d, ok := d.(decor.AverageDecorator); ok { | |
| 157 | bs.averageDecorators = append(bs.averageDecorators, d) | |
| 158 | } | |
| 159 | if d, ok := d.(decor.EwmaDecorator); ok { | |
| 160 | bs.ewmaDecorators = append(bs.ewmaDecorators, d) | |
| 161 | } | |
| 162 | if d, ok := d.(decor.ShutdownListener); ok { | |
| 163 | bs.shutdownListeners = append(bs.shutdownListeners, d) | |
| 164 | } | |
| 165 | }) | |
| 159 | 166 | if bs.waitBar != nil { |
| 160 | 167 | ps.queueBars[bs.waitBar] = bar |
| 161 | 168 | } else { |
| 162 | 169 | ps.hm.push(bar, true) |
| 163 | 170 | } |
| 164 | 171 | ps.idCount++ |
| 165 | ch <- result{bar, bs} | |
| 172 | ch <- bar | |
| 166 | 173 | }: |
| 167 | res := <-ch | |
| 168 | bar, bs := res.bar, res.bs | |
| 169 | bar.TraverseDecorators(func(d decor.Decorator) { | |
| 170 | if d, ok := d.(decor.AverageDecorator); ok { | |
| 171 | bs.averageDecorators = append(bs.averageDecorators, d) | |
| 172 | } | |
| 173 | if d, ok := d.(decor.EwmaDecorator); ok { | |
| 174 | bs.ewmaDecorators = append(bs.ewmaDecorators, d) | |
| 175 | } | |
| 176 | if d, ok := d.(decor.ShutdownListener); ok { | |
| 177 | bs.shutdownListeners = append(bs.shutdownListeners, d) | |
| 178 | } | |
| 179 | }) | |
| 180 | return bar, nil | |
| 174 | return <-ch, nil | |
| 181 | 175 | case <-p.done: |
| 182 | 176 | return nil, DoneError |
| 183 | 177 | } |