-
-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generate): render concurrently (#308)
* feat(generate): render concurrently * perf(generate): set GOMAXPROCS
- Loading branch information
Showing
3 changed files
with
130 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Package pools : goroutine pools | ||
package pools | ||
|
||
// NewPool return a new pool | ||
func NewPool(size int) Pool { | ||
var p pool | ||
p.Init(size) | ||
return &p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package pools | ||
|
||
import "sync" | ||
|
||
// Pool goroutine pool | ||
type Pool interface { | ||
// Wait 等待令牌 | ||
Wait() | ||
// Done 归还令牌 | ||
Done() | ||
// Num 当前发放的令牌书 | ||
Num() int | ||
// Size 总令牌数 | ||
Size() int | ||
|
||
// WaitAll 同步等待令牌全部归还 | ||
WaitAll() | ||
// AsyncWaitAll 异步等待令牌全部归还 | ||
AsyncWaitAll() <-chan struct{} | ||
} | ||
|
||
type pool struct { | ||
pool chan struct{} | ||
|
||
wg sync.WaitGroup | ||
} | ||
|
||
func (p *pool) Init(size int) { | ||
if size >= 0 { | ||
p.pool = make(chan struct{}, size) | ||
} | ||
} | ||
|
||
func (p *pool) Wait() { | ||
if p.pool != nil { | ||
p.wg.Add(1) | ||
p.pool <- struct{}{} | ||
} | ||
} | ||
|
||
func (p *pool) Done() { | ||
if p.pool != nil { | ||
<-p.pool | ||
p.wg.Done() | ||
} | ||
} | ||
|
||
func (p *pool) Num() int { | ||
if p.pool != nil { | ||
return len(p.pool) | ||
} | ||
return 0 | ||
} | ||
|
||
func (p *pool) Size() int { | ||
if p.pool != nil { | ||
return cap(p.pool) | ||
} | ||
return 0 | ||
} | ||
|
||
func (p *pool) WaitAll() { | ||
p.wg.Wait() | ||
} | ||
|
||
func (p *pool) AsyncWaitAll() <-chan struct{} { | ||
sig := make(chan struct{}) | ||
go func() { | ||
p.WaitAll() | ||
sig <- struct{}{} | ||
}() | ||
return sig | ||
} |