@@ -216,7 +216,7 @@ type VisionWriter struct {
216216 trafficState * TrafficState
217217 ctx context.Context
218218 writeOnceUserUUID * []byte
219- scheduler * Scheduler
219+ Scheduler * Scheduler
220220}
221221
222222func NewVisionWriter (writer buf.Writer , addon * Addons , state * TrafficState , context context.Context ) * VisionWriter {
@@ -228,7 +228,7 @@ func NewVisionWriter(writer buf.Writer, addon *Addons, state *TrafficState, cont
228228 trafficState : state ,
229229 ctx : context ,
230230 writeOnceUserUUID : & w ,
231- scheduler : NewScheduler (writer , addon , state , & w , context ),
231+ Scheduler : NewScheduler (writer , addon , state , & w , context ),
232232 }
233233}
234234
@@ -281,12 +281,24 @@ func (w *VisionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
281281 if w .trafficState .StartTime .IsZero () {
282282 w .trafficState .StartTime = time .Now ()
283283 }
284- w .scheduler .Buffer <- mb
285- if w .addons .Scheduler == nil {
286- w .scheduler .Trigger <- - 1 // send all buffers
284+ w .Scheduler .Buffer <- mb
285+ w .Scheduler .Trigger <- - 1 // send all buffers if no independent scheduler
286+ if w .addons .Scheduler != nil {
287+ w .Scheduler .TimeoutLock .Lock ()
288+ w .Scheduler .TimeoutCounter ++
289+ w .Scheduler .TimeoutLock .Unlock ()
290+ go func () {
291+ time .Sleep (time .Duration (w .addons .Scheduler .TimeoutMillis ) * time .Millisecond )
292+ w .Scheduler .TimeoutLock .Lock ()
293+ w .Scheduler .TimeoutCounter --
294+ if w .Scheduler .TimeoutCounter == 0 {
295+ w .Scheduler .Trigger <- 0 // send when the latest buffer timeout
296+ }
297+ w .Scheduler .TimeoutLock .Unlock ()
298+ }()
287299 }
288- if len (w .scheduler .Error ) > 0 {
289- return <- w .scheduler .Error
300+ if len (w .Scheduler .Error ) > 0 {
301+ return <- w .Scheduler .Error
290302 }
291303 return nil
292304}
@@ -518,7 +530,7 @@ func UnwrapRawConn(conn net.Conn) (net.Conn, stats.Counter, stats.Counter) {
518530// CopyRawConnIfExist use the most efficient copy method.
519531// - If caller don't want to turn on splice, do not pass in both reader conn and writer conn
520532// - writer are from *transport.Link
521- func CopyRawConnIfExist (ctx context.Context , readerConn net.Conn , writerConn net.Conn , writer buf.Writer , timer * signal.ActivityTimer , inTimer * signal.ActivityTimer ) error {
533+ func CopyRawConnIfExist (ctx context.Context , readerConn net.Conn , writerConn net.Conn , writer buf.Writer , timer * signal.ActivityTimer , inTimer * signal.ActivityTimer , scheduler * Scheduler ) error {
522534 readerConn , readCounter , _ := UnwrapRawConn (readerConn )
523535 writerConn , _ , writeCounter := UnwrapRawConn (writerConn )
524536 reader := buf .NewReader (readerConn )
@@ -581,10 +593,13 @@ func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net
581593 if readCounter != nil {
582594 readCounter .Add (int64 (buffer .Len ()))
583595 }
584- timer .Update ()
585596 if werr := writer .WriteMultiBuffer (buffer ); werr != nil {
586597 return werr
587598 }
599+ timer .Update ()
600+ }
601+ if scheduler != nil {
602+ scheduler .Trigger <- 2
588603 }
589604 if err != nil {
590605 return err
0 commit comments