@@ -3,14 +3,23 @@ package decoder
33import (
44 "io"
55 "log"
6+ "os"
7+ "strconv"
68 "sync"
79 "time"
810
911 "github.com/yomorun/y3-codec-golang/pkg/common"
1012)
1113
12- // bufferSize is the capacity of decoder.
13- const bufferSize = 50
14+ const bufferSizeEnvKey = "YOMO_BUFFER_SIZE"
15+
16+ var (
17+ // bufferSize is the capacity of decoder.
18+ bufferSize = 200
19+
20+ // dropSizeWhenFull is the size of data to drop when the buffer is full.
21+ dropSizeWhenFull = getDropDataSize (bufferSize )
22+ )
1423
1524// Iterable iterate through and get the data of observe
1625type Iterable interface {
@@ -396,6 +405,14 @@ func (o *observableImpl) Unmarshal(unmarshaller Unmarshaller, factory func() int
396405}
397406
398407func createObservable (f func (next chan interface {})) Observable {
408+ if os .Getenv (bufferSizeEnvKey ) != "" {
409+ newSize , err := strconv .Atoi (os .Getenv (bufferSizeEnvKey ))
410+ if newSize > 0 && err == nil {
411+ bufferSize = newSize
412+ dropSizeWhenFull = getDropDataSize (bufferSize )
413+ }
414+ }
415+
399416 next := make (chan interface {}, bufferSize )
400417 subscribers := make ([]chan interface {}, 0 )
401418
@@ -414,10 +431,16 @@ func dropOldData(next chan interface{}) {
414431 continue
415432 }
416433
417- // the "next" channel is full, drop 1/2 old data to receive the new data.
418- for i := 0 ; i < bufferSize / 2 ; i ++ {
434+ // the "next" channel is full, drop old data to receive the new data.
435+ for i := 0 ; i < dropSizeWhenFull ; i ++ {
419436 <- next
420437 }
421438 }
422439 }
423440}
441+
442+ // get the drop size when the buffer is full.
443+ func getDropDataSize (bufferSize int ) int {
444+ dropSize := float64 (bufferSize ) * 0.2
445+ return int (dropSize )
446+ }
0 commit comments