Skip to content

Commit d777899

Browse files
committed
Add time-based fast write strategy for czar
1 parent d361d4f commit d777899

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

protocol/czar/mux.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"io"
66
"sync"
7+
"time"
78

89
"github.com/mohanson/daze/lib/doa"
910
"github.com/mohanson/daze/lib/priority"
@@ -13,6 +14,7 @@ import (
1314
type Stream struct {
1415
idx uint8
1516
mux *Mux
17+
qtw time.Time
1618
rbf []byte
1719
rch chan []byte
1820
rer *Err
@@ -85,6 +87,7 @@ func (s *Stream) Write(p []byte) (int, error) {
8587
buf []byte
8688
l = 0
8789
n = 0
90+
z = 0
8891
)
8992
for {
9093
switch {
@@ -102,7 +105,13 @@ func (s *Stream) Write(p []byte) (int, error) {
102105
binary.BigEndian.PutUint16(buf[2:4], uint16(l))
103106
copy(buf[4:], p[:l])
104107
p = p[l:]
105-
err := s.mux.pri.Pri(1, func() error {
108+
z = func() int {
109+
if time.Now().After(s.qtw) {
110+
return 2
111+
}
112+
return 1
113+
}()
114+
err := s.mux.pri.Pri(z, func() error {
106115
if err := s.wer.Get(); err != nil {
107116
return err
108117
}
@@ -125,6 +134,7 @@ func NewStream(idx uint8, mux *Mux) *Stream {
125134
return &Stream{
126135
idx: idx,
127136
mux: mux,
137+
qtw: time.Now().Add(time.Second * 8),
128138
rbf: make([]byte, 0),
129139
rch: make(chan []byte, 32),
130140
rer: NewErr(),
@@ -254,7 +264,7 @@ func NewMux(conn io.ReadWriteCloser) *Mux {
254264
ach: make(chan *Stream),
255265
con: conn,
256266
idp: NewSip(),
257-
pri: priority.NewPriority(2),
267+
pri: priority.NewPriority(3),
258268
rer: NewErr(),
259269
usb: make([]*Stream, 256),
260270
}

0 commit comments

Comments
 (0)