forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.go
140 lines (122 loc) · 2.86 KB
/
dispatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io"
"math/rand"
"sync"
"time"
)
var (
// dispatchPoolDelay is the time to wait before flushing all buffered packets
dispatchPoolDelay = 100 * time.Millisecond
// dispatchPacketBytes is how many bytes to send until choosing a new connection
dispatchPacketBytes = 32
)
type dispatcher interface {
// Copy works like io.Copy using buffers provided by fetchFunc
Copy(io.Writer, fetchFunc) error
}
type fetchFunc func() ([]byte, error)
type dispatcherPool struct {
// mu protects the dispatch packet queue 'q'
mu sync.Mutex
q []dispatchPacket
}
type dispatchPacket struct {
buf []byte
out io.Writer
}
func newDispatcherPool() dispatcher {
d := &dispatcherPool{}
go d.writeLoop()
return d
}
func (d *dispatcherPool) writeLoop() {
for {
time.Sleep(dispatchPoolDelay)
d.flush()
}
}
func (d *dispatcherPool) flush() {
d.mu.Lock()
pkts := d.q
d.q = nil
d.mu.Unlock()
if len(pkts) == 0 {
return
}
// sort by sockets; preserve the packet ordering within a socket
pktmap := make(map[io.Writer][]dispatchPacket)
outs := []io.Writer{}
for _, pkt := range pkts {
opkts, ok := pktmap[pkt.out]
if !ok {
outs = append(outs, pkt.out)
}
pktmap[pkt.out] = append(opkts, pkt)
}
// send all packets in pkts
for len(outs) != 0 {
// randomize writer on every write
r := rand.Intn(len(outs))
rpkts := pktmap[outs[r]]
rpkts[0].out.Write(rpkts[0].buf)
// dequeue packet
rpkts = rpkts[1:]
if len(rpkts) == 0 {
delete(pktmap, outs[r])
outs = append(outs[:r], outs[r+1:]...)
} else {
pktmap[outs[r]] = rpkts
}
}
}
func (d *dispatcherPool) Copy(w io.Writer, f fetchFunc) error {
for {
b, err := f()
if err != nil {
return err
}
pkts := []dispatchPacket{}
for len(b) > 0 {
pkt := b
if len(b) > dispatchPacketBytes {
pkt = pkt[:dispatchPacketBytes]
b = b[dispatchPacketBytes:]
} else {
b = nil
}
pkts = append(pkts, dispatchPacket{pkt, w})
}
d.mu.Lock()
d.q = append(d.q, pkts...)
d.mu.Unlock()
}
}
type dispatcherImmediate struct{}
func newDispatcherImmediate() dispatcher {
return &dispatcherImmediate{}
}
func (d *dispatcherImmediate) Copy(w io.Writer, f fetchFunc) error {
for {
b, err := f()
if err != nil {
return err
}
if _, err := w.Write(b); err != nil {
return err
}
}
}