Ack acquire next chance to send data
(ctx context.Context, newBytes int)
| 24 | |
| 25 | // Ack acquire next chance to send data |
| 26 | func (this *Bandwidth) Ack(ctx context.Context, newBytes int) { |
| 27 | if newBytes <= 0 { |
| 28 | return |
| 29 | } |
| 30 | if this.totalBytes <= 0 { |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | var timestamp = fasttime.Now().Unix() |
| 35 | if this.currentTimestamp != 0 && this.currentTimestamp != timestamp { |
| 36 | this.currentTimestamp = timestamp |
| 37 | this.currentBytes = int64(newBytes) |
| 38 | |
| 39 | // 第一次发送直接放行,不需要判断 |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | if this.currentTimestamp == 0 { |
| 44 | this.currentTimestamp = timestamp |
| 45 | } |
| 46 | if atomic.AddInt64(&this.currentBytes, int64(newBytes)) <= this.totalBytes { |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | var timeout = time.NewTimer(1 * time.Second) |
| 51 | if ctx != nil { |
| 52 | select { |
| 53 | case <-timeout.C: |
| 54 | case <-ctx.Done(): |
| 55 | } |
| 56 | } else { |
| 57 | select { |
| 58 | case <-timeout.C: |
| 59 | } |
| 60 | } |
| 61 | } |