Commit d20a0188 by usual2970

优化

parent 5000dbff
......@@ -41,6 +41,7 @@ type Collect struct {
wg WaitGroupWrap
chanPool chan bool
chanItem chan *Item
f *os.File
}
func NewCollect(filePath string, needUnzip bool) *Collect {
......@@ -69,8 +70,8 @@ func (c *Collect) GetItems() error {
if err != nil {
return err
}
defer f.Close()
rd := bufio.NewReader(f)
c.f = f
rd := bufio.NewReader(c.f)
c.wg.Wrap(func() {
for {
......@@ -90,7 +91,7 @@ func (c *Collect) GetItems() error {
}
})
c.wg.Wrap(func() {
for {
select {
case item := <-c.chanItem:
......@@ -101,11 +102,10 @@ func (c *Collect) GetItems() error {
})
case <-time.After(30 * time.Second):
log.Printf("timed out") //等待30后退出
break
return
}
}
})
return nil
}
......@@ -152,5 +152,10 @@ func (c *Collect) handleItem(item *Item) {
}
func (c *Collect) Exit() {
c.wg.Wait()
if c.f != nil {
c.f.Close()
}
}
......@@ -22,7 +22,7 @@ func TestOnItem(t *testing.T) {
})
err := c.GetItems()
c.Exit()
fmt.Println(err)
c.Exit()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment