2 Commits

Author SHA1 Message Date
b612 beaae47d8c go mod support 2022-03-14 11:00:51 +08:00
b612 51c0d54943 add write nil func 2022-02-23 14:33:20 +08:00
4 changed files with 50 additions and 3 deletions
+8 -2
View File
@@ -20,6 +20,7 @@ type StarBuffer struct {
pEnd uint64
cap uint64
isClose atomic.Value
isEnd atomic.Value
rmu sync.Mutex
wmu sync.Mutex
}
@@ -29,6 +30,7 @@ func NewStarBuffer(cap uint64) *StarBuffer {
rtnBuffer.cap = cap
rtnBuffer.datas = make([]byte, cap)
rtnBuffer.isClose.Store(false)
rtnBuffer.isEnd.Store(false)
return rtnBuffer
}
@@ -48,7 +50,7 @@ func (star *StarBuffer) Len() uint64 {
}
func (star *StarBuffer) getByte() (byte, error) {
if star.isClose.Load().(bool) {
if star.isClose.Load().(bool) || (star.Len() == 0 && star.isEnd.Load().(bool)) {
return 0, io.EOF
}
if star.Len() == 0 {
@@ -97,7 +99,7 @@ func (star *StarBuffer) Close() error {
return nil
}
func (star *StarBuffer) Read(buf []byte) (int, error) {
if star.isClose.Load().(bool) {
if star.isClose.Load().(bool) || (star.Len() == 0 && star.isEnd.Load().(bool)) {
return 0, io.EOF
}
if buf == nil {
@@ -125,6 +127,10 @@ func (star *StarBuffer) Read(buf []byte) (int, error) {
}
func (star *StarBuffer) Write(bts []byte) (int, error) {
if bts == nil && !star.isEnd.Load().(bool) {
star.isEnd.Store(true)
return 0, nil
}
if bts == nil || star.isClose.Load().(bool) {
return 0, io.EOF
}
+25
View File
@@ -59,3 +59,28 @@ func Test_Circle_Speed(t *testing.T) {
time.Sleep(time.Second * 10)
fmt.Println(count)
}
func Test_Circle_Speed2(t *testing.T) {
buf := NewStarBuffer(8192)
count := uint64(0)
for i := 1; i <= 10; i++ {
go func() {
for {
buf.Write([]byte("hello world b612 hello world b612 b612 b612 b612 b612 b612"))
}
}()
}
for i := 1; i <= 10; i++ {
go func() {
for {
mybuf := make([]byte, 1024)
j, err := buf.Read(mybuf)
if err == nil {
atomic.AddUint64(&count, uint64(j))
}
}
}()
}
time.Sleep(time.Second * 10)
fmt.Println(float64(count) / 10 / 1024 / 1024)
}
+5
View File
@@ -0,0 +1,5 @@
module b612.me/stario
go 1.16
require golang.org/x/crypto v0.0.0-20220313003712-b769efc7c000
+11
View File
@@ -0,0 +1,11 @@
golang.org/x/crypto v0.0.0-20220313003712-b769efc7c000 h1:SL+8VVnkqyshUSz5iNnXtrBQzvFF2SkROm6t5RczFAE=
golang.org/x/crypto v0.0.0-20220313003712-b769efc7c000/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=