dispatch.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2016 The etcd Authors
  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 main
  15. import (
  16. "io"
  17. "math/rand"
  18. "sync"
  19. "time"
  20. )
  21. var (
  22. // dispatchPoolDelay is the time to wait before flushing all buffered packets
  23. dispatchPoolDelay = 100 * time.Millisecond
  24. // dispatchPacketBytes is how many bytes to send until choosing a new connection
  25. dispatchPacketBytes = 32
  26. )
  27. type dispatcher interface {
  28. // Copy works like io.Copy using buffers provided by fetchFunc
  29. Copy(io.Writer, fetchFunc) error
  30. }
  31. type fetchFunc func() ([]byte, error)
  32. type dispatcherPool struct {
  33. // mu protects the dispatch packet queue 'q'
  34. mu sync.Mutex
  35. q []dispatchPacket
  36. }
  37. type dispatchPacket struct {
  38. buf []byte
  39. out io.Writer
  40. }
  41. func newDispatcherPool() dispatcher {
  42. d := &dispatcherPool{}
  43. go d.writeLoop()
  44. return d
  45. }
  46. func (d *dispatcherPool) writeLoop() {
  47. for {
  48. time.Sleep(dispatchPoolDelay)
  49. d.flush()
  50. }
  51. }
  52. func (d *dispatcherPool) flush() {
  53. d.mu.Lock()
  54. pkts := d.q
  55. d.q = nil
  56. d.mu.Unlock()
  57. if len(pkts) == 0 {
  58. return
  59. }
  60. // sort by sockets; preserve the packet ordering within a socket
  61. pktmap := make(map[io.Writer][]dispatchPacket)
  62. outs := []io.Writer{}
  63. for _, pkt := range pkts {
  64. opkts, ok := pktmap[pkt.out]
  65. if !ok {
  66. outs = append(outs, pkt.out)
  67. }
  68. pktmap[pkt.out] = append(opkts, pkt)
  69. }
  70. // send all packets in pkts
  71. for len(outs) != 0 {
  72. // randomize writer on every write
  73. r := rand.Intn(len(outs))
  74. rpkts := pktmap[outs[r]]
  75. rpkts[0].out.Write(rpkts[0].buf)
  76. // dequeue packet
  77. rpkts = rpkts[1:]
  78. if len(rpkts) == 0 {
  79. delete(pktmap, outs[r])
  80. outs = append(outs[:r], outs[r+1:]...)
  81. } else {
  82. pktmap[outs[r]] = rpkts
  83. }
  84. }
  85. }
  86. func (d *dispatcherPool) Copy(w io.Writer, f fetchFunc) error {
  87. for {
  88. b, err := f()
  89. if err != nil {
  90. return err
  91. }
  92. pkts := []dispatchPacket{}
  93. for len(b) > 0 {
  94. pkt := b
  95. if len(b) > dispatchPacketBytes {
  96. pkt = pkt[:dispatchPacketBytes]
  97. b = b[dispatchPacketBytes:]
  98. } else {
  99. b = nil
  100. }
  101. pkts = append(pkts, dispatchPacket{pkt, w})
  102. }
  103. d.mu.Lock()
  104. d.q = append(d.q, pkts...)
  105. d.mu.Unlock()
  106. }
  107. }
  108. type dispatcherImmediate struct{}
  109. func newDispatcherImmediate() dispatcher {
  110. return &dispatcherImmediate{}
  111. }
  112. func (d *dispatcherImmediate) Copy(w io.Writer, f fetchFunc) error {
  113. for {
  114. b, err := f()
  115. if err != nil {
  116. return err
  117. }
  118. if _, err := w.Write(b); err != nil {
  119. return err
  120. }
  121. }
  122. }