Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bufferchain for HTTP2 stream #23

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions buffer/bufferchain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 buffer

import (
"errors"
"io"
"sync"
"time"
)

// ErrWriteCovered bufferchain queue is full.
var ErrWriteCovered = errors.New("chain write covered")

const defaultCapacity = 1 << 9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too big ...


/*
* ioBufferchain
* For HTTP2 stream, in order not to break the structure-adaptation interface.
*/
type ioBufferchain struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disable PutIoBuffer()

if p, _ := buf.(*pipe); p != nil {

bufferchain chan []byte
errChan chan error
mutex sync.Mutex
}

// NewIoBufferChain returns *bufferChain.
func NewIoBufferChain(capacity int) *ioBufferchain {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return IoBuffer

if capacity == 0 {
capacity = defaultCapacity
}

return &ioBufferchain{
bufferchain: make(chan []byte, capacity),
errChan: make(chan error),
}
}

func (bc *ioBufferchain) Bytes() (p []byte) {
p, b := <-bc.bufferchain
if !b {
return nil
}

return p
}

func (bc *ioBufferchain) Write(p []byte) (n int, err error) {
bytes := *GetBytes(len(p))
copy(bytes, p)
select {
case <-bc.errChan:
return 0, io.EOF
default:
bc.mutex.Lock()
defer bc.mutex.Unlock()
select {
case bc.bufferchain <- bytes:
return len(bytes), nil
default:
// chain is full conn goutine wait 1s to consumer
ticker := time.NewTicker(1 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be time.NewTimer() ?

select {
case <-bc.errChan:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ticker.Stop()

return 0, io.EOF
case bc.bufferchain <- bytes:
ticker.Stop()

return len(bytes), nil
case <-ticker.C:
return 0, ErrWriteCovered
}
}
}
}

func (bc *ioBufferchain) CloseWithError(_ error) {
select {
case <-bc.errChan:
return
default:
close(bc.errChan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need pass error code, such as ErrWriteCovered

bc.mutex.Lock()
defer bc.mutex.Unlock()
close(bc.bufferchain)
}
}

func (bc *ioBufferchain) Count(int32) int32 {
return 1
}

func (bc *ioBufferchain) Len() int {
return 0
}

func (bc *ioBufferchain) Read(p []byte) (n int, err error) {
return 0, EOF
}

func (bc *ioBufferchain) ReadOnce(r io.Reader) (n int64, err error) {
return 0, EOF
}

func (bc *ioBufferchain) ReadFrom(r io.Reader) (n int64, err error) {
return 0, EOF
}

func (bc *ioBufferchain) Grow(n int) error {
return EOF
}

func (bc *ioBufferchain) WriteString(s string) (n int, err error) {
return 0, EOF
}

func (bc *ioBufferchain) WriteByte(p byte) error {
return EOF
}

func (bc *ioBufferchain) WriteUint16(p uint16) error {
return EOF
}

func (bc *ioBufferchain) WriteUint32(p uint32) error {
return EOF
}

func (bc *ioBufferchain) WriteUint64(p uint64) error {
return EOF
}

func (bc *ioBufferchain) WriteTo(w io.Writer) (n int64, err error) {
return 0, EOF
}

func (bc *ioBufferchain) Peek(n int) []byte {
return nil
}

func (bc *ioBufferchain) Drain(offset int) {
}

func (bc *ioBufferchain) Cap() int {
return 0
}

func (bc *ioBufferchain) Reset() {}

func (bc *ioBufferchain) Clone() IoBuffer {
return nil
}

func (bc *ioBufferchain) String() string {
return ""
}

func (bc *ioBufferchain) Alloc(int) {
}

func (bc *ioBufferchain) Free() {
}

func (bc *ioBufferchain) EOF() bool {
return true
}

func (bc *ioBufferchain) SetEOF(eof bool) {
}

func (bc *ioBufferchain) Append(data []byte) error {
return EOF
}
77 changes: 77 additions & 0 deletions buffer/bufferchain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 buffer

import (
"sync/atomic"
"testing"
"time"
)

//Test write
func TestBufferWrite(t *testing.T) {
chain := NewIoBufferChain(10)
write := func(i *int32) error {
bytes := make([]byte, 1)
_, err := chain.Write(bytes)
if err == nil {
atomic.AddInt32(i, 1)
}
return err
}
var i int32
go func() {
var err error
for i <= 20 {
err = write(&i)
if err != nil {
break
}
}

if i != 10 {
t.Errorf("Capacity of bufferchain error %d", i)
}
err = write(&i)
if err == nil {
t.Error("Consumption timeout err")
}
}()
time.Sleep(2 * time.Second)
chain.CloseWithError(nil)
}

func TestBufferReade(t *testing.T) {
chain := NewIoBufferChain(10)
chain.Write(make([]byte, 1))
chain.Write(make([]byte, 1))
var i int32
reader := func(i *int32) {
_ = chain.Bytes()
atomic.AddInt32(i, 1)
}
go func() {
for {
reader(&i)
}
}()
time.Sleep(1 * time.Second)
chain.CloseWithError(nil)
if i != 2 {
t.Error("Message number error")
}
}