diff --git a/core/client.go b/core/client.go index fb617af98..4a673f78a 100644 --- a/core/client.go +++ b/core/client.go @@ -138,10 +138,10 @@ func (c *Client) runBackground(ctx context.Context, addr string, conn quic.Conne for { select { case <-c.ctx.Done(): - fs.underlying.Close() + fs.Close() return case <-ctx.Done(): - fs.underlying.Close() + fs.Close() return case f := <-c.writeFrameChan: if err := fs.WriteFrame(f); err != nil { diff --git a/core/connector.go b/core/connector.go index e7c741fc3..ac2a70578 100644 --- a/core/connector.go +++ b/core/connector.go @@ -30,7 +30,7 @@ func NewConnector(ctx context.Context) *Connector { } // Store stores Connection to Connector, -// If the connID is the same twice, the new conntion will replace the old conntion. +// If the connID is the same twice, the new connection will replace the old connection. // If Connector be closed, The function will return ErrConnectorClosed. func (c *Connector) Store(connID string, conn Connection) error { select { @@ -44,8 +44,8 @@ func (c *Connector) Store(connID string, conn Connection) error { return nil } -// Remove removes the conntion with the specified connID. -// If the Connector does not have a conntion with the given connID, no action is taken. +// Remove removes the connection with the specified connID. +// If the Connector does not have a connection with the given connID, no action is taken. // If Connector be closed, The function will return ErrConnectorClosed. func (c *Connector) Remove(connID string) error { select { @@ -60,7 +60,7 @@ func (c *Connector) Remove(connID string) error { } // Get retrieves the Connection with the specified connID. -// If the Connector does not have a conntion with the given connID, return nil and false. +// If the Connector does not have a connection with the given connID, return nil and false. // If Connector be closed, The function will return ErrConnectorClosed. func (c *Connector) Get(connID string) (Connection, bool, error) { select { diff --git a/core/context.go b/core/context.go index 791e9ab55..8f8ed1055 100644 --- a/core/context.go +++ b/core/context.go @@ -2,7 +2,6 @@ package core import ( "context" - "errors" "sync" "time" @@ -14,9 +13,9 @@ import ( var ctxPool sync.Pool -// Context is context for stream handling. -// Context is generated subsequent to the arrival of a dataStream and retains pertinent information derived from the dataStream. -// The lifespan of the Context should align with the lifespan of the Stream. +// Context is context for connection handling. +// Context is generated subsequent to the arrival of a connection and retains pertinent information derived from the connection. +// The lifespan of the Context should align with the lifespan of the connection. type Context struct { // Connection is the connection used for reading and writing frames. Connection Connection @@ -32,7 +31,7 @@ type Context struct { Keys map[string]any // BaseLogger is the base logger. BaseLogger *slog.Logger - // Using Logger to log in stream handler scope, Logger is frame-level logger. + // Using Logger to log in connection handler scope, Logger is frame-level logger. Logger *slog.Logger } @@ -87,9 +86,9 @@ func (c *Context) Value(key any) any { } // newContext returns a new YoMo context that implements the standard library `context.Context` interface. -// The YoMo context is used to manage the lifecycle of a connection and provides a way to pass data and metadata between stream processing functions. -// The lifecycle of the context is equal to the lifecycle of the connection that it is associated with. -// The context can be used to manage timeouts, cancellations, and other aspects of stream processing. +// The YoMo context is used to manage the lifecycle of a connection and provides a way to pass data and metadata +// between connection processing functions. The lifecycle of the context is equal to the lifecycle of the connection +// that it is associated with. The context can be used to manage timeouts, cancellations, and other aspects of connection processing. func newContext(conn Connection, route router.Route, logger *slog.Logger) (c *Context) { v := ctxPool.Get() if v == nil { @@ -116,7 +115,7 @@ func newContext(conn Connection, route router.Route, logger *slog.Logger) (c *Co func (c *Context) WithFrame(f frame.Frame) error { df, ok := f.(*frame.DataFrame) if !ok { - return errors.New("connection only transmit data frame") + return nil } fmd, err := metadata.Decode(df.Metadata) @@ -138,7 +137,7 @@ func (c *Context) WithFrame(f frame.Frame) error { // CloseWithError close dataStream with an error string. func (c *Context) CloseWithError(errString string) { - c.Logger.Debug("connection closed", "error", errString) + c.Logger.Debug("connection closed", "err", errString) err := c.Connection.CloseWithError(errString) if err == nil { diff --git a/core/frame/frame.go b/core/frame/frame.go index 7caf0d9cd..55799213a 100644 --- a/core/frame/frame.go +++ b/core/frame/frame.go @@ -15,6 +15,7 @@ import ( // 3. DataFrame // 4. BackflowFrame // 5. RejectedFrame +// 6. GoawayFrame // // Read frame comments to understand the role of the frame. type Frame interface { @@ -25,7 +26,7 @@ type Frame interface { // Type defined The type of frame. type Type byte -// DataFrame carries tagged data to transmit across DataStream. +// DataFrame carries tagged data to transmit across connection. type DataFrame struct { // Metadata stores additional data beyond the Payload, // it is an map[string]string{} that be encoded in msgpack. @@ -40,8 +41,8 @@ type DataFrame struct { func (f *DataFrame) Type() Type { return TypeDataFrame } // The HandshakeFrame is the frame through which the client obtains a new connection from the server. -// It include essential details required for the creation of a fresh DataStream. -// The server then generates the DataStream utilizing this provided information. +// It includes essential details required for the creation of a fresh connection. +// The server then generates the connection utilizing this provided information. type HandshakeFrame struct { // Name is the name of the dataStream that will be created. Name string @@ -51,9 +52,9 @@ type HandshakeFrame struct { ClientType byte // ObserveDataTags is the ObserveDataTags of the dataStream that will be created. ObserveDataTags []Tag - // AuthName. + // AuthName is the authentication name. AuthName string - // AuthPayload. + // AuthPayload is the authentication payload. AuthPayload string } @@ -67,8 +68,8 @@ type HandshakeAckFrame struct{} // Type returns the type of HandshakeAckFrame. func (f *HandshakeAckFrame) Type() Type { return TypeHandshakeAckFrame } -// The BackflowFrame is used to receive the processed result of a DataStream with StreamFunction type -// and forward it to a DataStream with StreamSource type. +// The BackflowFrame is used to receive the processed result of a connection with StreamFunction type +// and forward it to a connection with StreamSource type. type BackflowFrame struct { // Tag is used for data router. Tag Tag @@ -79,7 +80,7 @@ type BackflowFrame struct { // Type returns the type of BackflowFrame. func (f *BackflowFrame) Type() Type { return TypeBackflowFrame } -// RejectedFrame is used to reject a ControlStream request. +// RejectedFrame is used to reject a client request. type RejectedFrame struct { // Message encapsulates the rationale behind the rejection of the request. Message string @@ -99,7 +100,7 @@ func (f *GoawayFrame) Type() Type { return TypeGoawayFrame } const ( TypeDataFrame Type = 0x3F // TypeDataFrame is the type of DataFrame. - TypeHandshakeFrame Type = 0x31 // TypeHandshakeFrame is the type of PayloadFrame. + TypeHandshakeFrame Type = 0x31 // TypeHandshakeFrame is the type of HandshakeFrame. TypeHandshakeAckFrame Type = 0x29 // TypeHandshakeAckFrame is the type of HandshakeAckFrame. TypeRejectedFrame Type = 0x39 // TypeRejectedFrame is the type of RejectedFrame. TypeBackflowFrame Type = 0x2D // TypeBackflowFrame is the type of BackflowFrame. diff --git a/core/metadata/metadata.go b/core/metadata/metadata.go index 4f2121330..e08d4f113 100644 --- a/core/metadata/metadata.go +++ b/core/metadata/metadata.go @@ -7,12 +7,11 @@ import ( // M stores additional information about the application. // -// There are three types of metadata in yomo: +// There are two types of metadata in yomo: // 1. Metadata from `Authentication.Authenticate()`, This is connection-level metadata. -// 2. Metadata from the handshake, This is stream-level metadata. -// 3. Metadata from the DataFrame, This is frame-level metadata. +// 2. Metadata from the DataFrame, This is frame-level metadata. // -// the main responsibility of Metadata is to route messages to stream functions. +// the main responsibility of Metadata is to route messages to connection handler. type M map[string]string // New creates an M from a given key-values map. diff --git a/core/router/default_test.go b/core/router/default_test.go index e621e8b25..a81d94e47 100644 --- a/core/router/default_test.go +++ b/core/router/default_test.go @@ -31,7 +31,7 @@ func TestRouter(t *testing.T) { assert.NoError(t, err) ids = route.GetForwardRoutes(frame.Tag(1)) - assert.Equal(t, []string{"conn-2", "conn-3"}, ids) + assert.ElementsMatch(t, []string{"conn-2", "conn-3"}, ids) router.Clean()