batcher.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "time"
  17. "github.com/coreos/etcd/raft/raftpb"
  18. )
  19. var (
  20. emptyMsgProp = raftpb.Message{Type: raftpb.MsgProp}
  21. )
  22. type Batcher struct {
  23. batchedN int
  24. batchedT time.Time
  25. batchN int
  26. batchD time.Duration
  27. }
  28. func NewBatcher(n int, d time.Duration) *Batcher {
  29. return &Batcher{
  30. batchN: n,
  31. batchD: d,
  32. batchedT: time.Now(),
  33. }
  34. }
  35. func (b *Batcher) ShouldBatch(now time.Time) bool {
  36. b.batchedN++
  37. batchedD := now.Sub(b.batchedT)
  38. if b.batchedN >= b.batchN || batchedD >= b.batchD {
  39. b.Reset(now)
  40. return false
  41. }
  42. return true
  43. }
  44. func (b *Batcher) Reset(t time.Time) {
  45. b.batchedN = 0
  46. b.batchedT = t
  47. }
  48. func canBatch(m raftpb.Message) bool {
  49. return m.Type == raftpb.MsgAppResp && m.Reject == false
  50. }
  51. type ProposalBatcher struct {
  52. *Batcher
  53. raftpb.Message
  54. }
  55. func NewProposalBatcher(n int, d time.Duration) *ProposalBatcher {
  56. return &ProposalBatcher{
  57. Batcher: NewBatcher(n, d),
  58. Message: emptyMsgProp,
  59. }
  60. }
  61. func (b *ProposalBatcher) Batch(m raftpb.Message) {
  62. b.Message.From = m.From
  63. b.Message.To = m.To
  64. b.Message.Entries = append(b.Message.Entries, m.Entries...)
  65. }
  66. func (b *ProposalBatcher) IsEmpty() bool {
  67. return len(b.Message.Entries) == 0
  68. }
  69. func (b *ProposalBatcher) Reset(t time.Time) {
  70. b.Batcher.Reset(t)
  71. b.Message = emptyMsgProp
  72. }