Skip to content

Commit 1132e43

Browse files
feat(frame): replace framing to Y3 frame (#211) (#225)
* feat(frame): replace framing to Y3 frame * fix(heartbeat): client sends ping and zipper responses pong * feat(frame): add FrameType String method * sytle: fix the issues in codacy Co-authored-by: Hong Xiaojian <xiaojian.hong@gmail.com>
1 parent 67e6f79 commit 1132e43

59 files changed

Lines changed: 996 additions & 1193 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Set up Go 1.x
1616
uses: actions/setup-go@v2
1717
with:
18-
go-version: 1.15
18+
go-version: 1.16
1919

2020
- name: Test
2121
run: go test $(go list ./... | grep -v /example)

core/quic/conn.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"errors"
55
"time"
66

7-
"github.com/yomorun/yomo/internal/decoder"
8-
"github.com/yomorun/yomo/internal/framing"
7+
"github.com/yomorun/yomo/internal/core"
8+
"github.com/yomorun/yomo/internal/frame"
99
)
1010

1111
const (
@@ -24,9 +24,9 @@ const (
2424
// Conn represents the QUIC connection.
2525
type Conn struct {
2626
// Signal is the specified stream to receive the signal.
27-
Signal decoder.ReadWriter
27+
Signal *core.FrameStream
2828
// Type is the type of connection. Possible value: source, stream-function, server-sender.
29-
Type string
29+
Type core.ConnectionType
3030
// Name is the name of connection.
3131
Name string
3232
// Heartbeat is the channel to receive heartbeat.
@@ -44,7 +44,7 @@ type Conn struct {
4444
}
4545

4646
// NewConn inits a new QUIC connection.
47-
func NewConn(name string, connType string) *Conn {
47+
func NewConn(name string, connType core.ConnectionType) *Conn {
4848
return &Conn{
4949
Name: name,
5050
Type: connType,
@@ -55,12 +55,12 @@ func NewConn(name string, connType string) *Conn {
5555
}
5656

5757
// SendSignal sends the signal to client.
58-
func (c *Conn) SendSignal(f framing.Frame) error {
58+
func (c *Conn) SendSignal(f frame.Frame) error {
5959
if c.Signal == nil {
6060
return errors.New("Signal is nil")
6161
}
6262

63-
err := c.Signal.Write(f)
63+
_, err := c.Signal.WriteFrame(f)
6464
return err
6565
}
6666

core/rx/factory.go

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"context"
55

66
"github.com/reactivex/rxgo/v2"
7+
"github.com/yomorun/yomo/core/quic"
8+
"github.com/yomorun/yomo/internal/core"
79
"github.com/yomorun/yomo/internal/decoder"
10+
"github.com/yomorun/yomo/internal/frame"
811
"github.com/yomorun/yomo/logger"
912
)
1013

@@ -13,11 +16,8 @@ type Factory interface {
1316
// FromChannel creates a new Stream from a channel.
1417
FromChannel(ctx context.Context, channel chan interface{}) Stream
1518

16-
// FromReader creates a new Stream from decoder.Reader.
17-
FromReader(ctx context.Context, reader decoder.Reader) Stream
18-
19-
// FromReaderWithDecoder creates a new Stream with decoder.
20-
FromReaderWithDecoder(readers chan decoder.Reader, opts ...decoder.Option) Stream
19+
// FromQuicStream creates a new Stream from QuicStream.
20+
FromQuicStream(ctx context.Context, stream quic.Stream) Stream
2121

2222
// FromItems creates a new Stream from items.
2323
FromItems(ctx context.Context, items []interface{}) Stream
@@ -60,56 +60,32 @@ func (fac *factoryImpl) FromChannel(ctx context.Context, channel chan interface{
6060
return CreateObservable(ctx, f)
6161
}
6262

63-
// FromReader creates a new Stream from decoder.Reader.
64-
func (fac *factoryImpl) FromReader(ctx context.Context, reader decoder.Reader) Stream {
63+
// FromQuicStream creates a new RxStream from QUIC Stream.
64+
func (fac *factoryImpl) FromQuicStream(ctx context.Context, stream quic.Stream) Stream {
6565
f := func(ctx context.Context, next chan rxgo.Item) {
6666
defer close(next)
6767

68-
frameChan := reader.Read()
6968
for {
70-
select {
71-
case <-ctx.Done():
72-
return
73-
case frame, ok := <-frameChan:
74-
if !ok {
75-
return
76-
}
69+
f, err := core.ParseFrame(stream)
70+
if err != nil {
71+
logger.Error("Parse the frame failed", "err", err)
72+
break
73+
}
7774

78-
logger.Debug("Receive frame from source.")
79-
next <- Of(frame.Data())
75+
switch f.Type() {
76+
case frame.TagOfDataFrame:
77+
dataFrame := f.(*frame.DataFrame)
78+
logger.Debug("Receive data frame from source.", "TransactionID", dataFrame.TransactionID())
79+
next <- Of(dataFrame.GetCarriage())
80+
default:
81+
logger.Debug("Only support data frame in RxStream.", "type", f.Type())
8082
}
8183
}
8284
}
8385

8486
return CreateObservable(ctx, f)
8587
}
8688

87-
// FromReaderWithDecoder creates a Stream with decoder.
88-
func (fac *factoryImpl) FromReaderWithDecoder(readers chan decoder.Reader, opts ...decoder.Option) Stream {
89-
f := func(ctx context.Context, next chan rxgo.Item) {
90-
defer close(next)
91-
92-
for {
93-
select {
94-
case <-ctx.Done():
95-
return
96-
case reader, ok := <-readers:
97-
if !ok {
98-
return
99-
}
100-
101-
go func() {
102-
frameChan := reader.Read()
103-
for frame := range frameChan {
104-
Of(decoder.FromItems([]interface{}{frame.Data()}, opts...)).SendContext(ctx, next)
105-
}
106-
}()
107-
}
108-
}
109-
}
110-
return CreateObservable(decoder.GetContext(opts...), f)
111-
}
112-
11389
// FromItems creates a new Stream from items.
11490
func (fac *factoryImpl) FromItems(ctx context.Context, items []interface{}) Stream {
11591
next := make(chan rxgo.Item)

core/rx/stream_operator_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/reactivex/rxgo/v2"
1111
"github.com/stretchr/testify/assert"
1212
y3 "github.com/yomorun/y3-codec-golang"
13-
"github.com/yomorun/yomo/internal/decoder"
1413
)
1514

1615
// HELPER FUNCTIONS
@@ -252,16 +251,16 @@ func Test_Subscribe_MultipleKeys(t *testing.T) {
252251
})
253252
}
254253

255-
func Test_RawBytes(t *testing.T) {
256-
buf := &bytes.Buffer{}
257-
buf.Write([]byte{0, 0, 6, 0, 0, 0, 1, 2, 3})
258-
obs := decoder.FromStream(decoder.NewReader(buf))
259-
rawBytes := obs.RawBytes()
260-
for b := range rawBytes {
261-
assert.Equal(t, []byte{1, 2, 3}, b)
262-
break
263-
}
264-
}
254+
// func Test_RawBytes(t *testing.T) {
255+
// buf := &bytes.Buffer{}
256+
// buf.Write([]byte{0, 0, 6, 0, 0, 0, 1, 2, 3})
257+
// obs := decoder.FromStream(decoder.NewReader(buf))
258+
// rawBytes := obs.RawBytes()
259+
// for b := range rawBytes {
260+
// assert.Equal(t, []byte{1, 2, 3}, b)
261+
// break
262+
// }
263+
// }
265264

266265
func Test_Encode(t *testing.T) {
267266
t.Run("string", func(t *testing.T) {

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/yomorun/yomo
22

3-
go 1.15
3+
go 1.16
44

55
require (
66
github.com/cenkalti/backoff/v4 v4.1.1
@@ -10,6 +10,7 @@ require (
1010
github.com/stretchr/objx v0.1.1 // indirect
1111
github.com/stretchr/testify v1.7.0
1212
github.com/tidwall/gjson v1.8.1
13+
github.com/yomorun/y3 v1.0.2
1314
github.com/yomorun/y3-codec-golang v1.7.0
1415
go.opentelemetry.io/otel v1.0.0-RC2
1516
go.opentelemetry.io/otel/exporters/jaeger v1.0.0-RC2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
172172
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
173173
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
174174
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
175+
github.com/yomorun/y3 v1.0.2 h1:eugmFj2dV5om53A/AxQt++SJVAWeOdLjD5NSgngbO8A=
176+
github.com/yomorun/y3 v1.0.2/go.mod h1:+zwvZrKHe8D3fTMXNTsUsZXuI+kYxv3LRA2fSJEoWbo=
175177
github.com/yomorun/y3-codec-golang v1.7.0 h1:UYL6zuj132dnqG4N1MHuSY4h3DmC/6DfkMjNtTlugKI=
176178
github.com/yomorun/y3-codec-golang v1.7.0/go.mod h1:R+y8hQ/AHZ1tDzWtmspVeX7omqVWFJ42gdlXIOp35rA=
177179
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

0 commit comments

Comments
 (0)