github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/lib/pacer/tokens.go (about) 1 // Tokens for controlling concurrency 2 3 package pacer 4 5 // TokenDispenser is for controlling concurrency 6 type TokenDispenser struct { 7 tokens chan struct{} 8 } 9 10 // NewTokenDispenser makes a pool of n tokens 11 func NewTokenDispenser(n int) *TokenDispenser { 12 td := &TokenDispenser{ 13 tokens: make(chan struct{}, n), 14 } 15 // Fill up the upload tokens 16 for i := 0; i < n; i++ { 17 td.tokens <- struct{}{} 18 } 19 return td 20 } 21 22 // Get gets a token from the pool - don't forget to return it with Put 23 func (td *TokenDispenser) Get() { 24 <-td.tokens 25 return 26 } 27 28 // Put returns a token 29 func (td *TokenDispenser) Put() { 30 td.tokens <- struct{}{} 31 }