github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/pipeline/context.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package pipeline
    15  
    16  import "github.com/pingcap/ticdc/pkg/context"
    17  
    18  // NodeContext adds two functions from `coutext.Context` and created by pipeline
    19  type NodeContext interface {
    20  	context.Context
    21  
    22  	// Message returns the message sent by the previous node
    23  	Message() Message
    24  	// SendToNextNode sends the message to the next node
    25  	SendToNextNode(msg Message)
    26  }
    27  
    28  type nodeContext struct {
    29  	context.Context
    30  	msg      Message
    31  	outputCh chan Message
    32  }
    33  
    34  func newNodeContext(ctx context.Context, msg Message, outputCh chan Message) NodeContext {
    35  	return &nodeContext{
    36  		Context:  ctx,
    37  		msg:      msg,
    38  		outputCh: outputCh,
    39  	}
    40  }
    41  
    42  func (ctx *nodeContext) Message() Message {
    43  	return ctx.msg
    44  }
    45  
    46  func (ctx *nodeContext) SendToNextNode(msg Message) {
    47  	// The header channel should never be blocked
    48  	ctx.outputCh <- msg
    49  }
    50  
    51  type messageContext struct {
    52  	NodeContext
    53  	message Message
    54  }
    55  
    56  func withMessage(ctx NodeContext, msg Message) NodeContext {
    57  	return messageContext{
    58  		NodeContext: ctx,
    59  		message:     msg,
    60  	}
    61  }
    62  
    63  func (ctx messageContext) Message() Message {
    64  	return ctx.message
    65  }