hpds_node/stream_func.go

184 lines
6.0 KiB
Go
Raw Permalink Normal View History

2022-10-12 11:55:36 +08:00
package hpds_node
import (
"context"
"git.hpds.cc/Component/network"
"git.hpds.cc/Component/network/frame"
)
const (
streamFunctionLogPrefix = "\033[31m[hpds:stream_function]\033[0m "
)
// StreamFunction defines serverless streaming functions.
type StreamFunction interface {
// SetObserveDataTags set the data tag list that will be observed
// Deprecated: use hpds.WithObserveDataTags instead
SetObserveDataTags(tag ...frame.Tag)
2022-10-12 11:55:36 +08:00
// SetHandler set the handler function, which accept the raw bytes data and return the tag & response
SetHandler(fn network.AsyncHandler) error
// SetErrorHandler set the error handler function when server error occurs
SetErrorHandler(fn func(err error))
// SetPipeHandler set the pipe handler function
SetPipeHandler(fn network.PipeHandler) error
// Connect create a connection to the messageQueue
Connect() error
// Close will close the connection
Close() error
// Write Send a data to mq.
Write(tag frame.Tag, carriage []byte) error
2022-10-12 11:55:36 +08:00
}
// NewStreamFunction create a stream function.
func NewStreamFunction(name string, opts ...Option) StreamFunction {
options := NewOptions(opts...)
client := network.NewClient(name, network.ClientTypeStreamFunction, options.ClientOptions...)
sf := &streamFunction{
name: name,
emitterEndpoint: options.MqAddr,
2022-10-12 11:55:36 +08:00
client: client,
observeDataTags: make([]frame.Tag, 0),
2022-10-12 11:55:36 +08:00
}
return sf
}
var _ StreamFunction = &streamFunction{}
// streamFunction implements StreamFunction interface.
type streamFunction struct {
name string
emitterEndpoint string
2022-10-12 11:55:36 +08:00
client *network.Client
observeDataTags []frame.Tag // tag list that will be observed
2022-10-12 11:55:36 +08:00
fn network.AsyncHandler // user's function which will be invoked when data arrived
pfn network.PipeHandler
pIn chan []byte
pOut chan *frame.PayloadFrame
}
// SetObserveDataTags set the data tag list that will be observed.
// Deprecated: use hpds.WithObserveDataTags instead
func (s *streamFunction) SetObserveDataTags(tag ...frame.Tag) {
2022-10-12 11:55:36 +08:00
s.client.SetObserveDataTags(tag...)
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s SetObserveDataTag(%v)", streamFunctionLogPrefix, s.observeDataTags)
2022-10-12 11:55:36 +08:00
}
// SetHandler set the handler function, which accept the raw bytes data and return the tag & response.
func (s *streamFunction) SetHandler(fn network.AsyncHandler) error {
s.fn = fn
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s SetHandler(%v)", streamFunctionLogPrefix, s.fn)
2022-10-12 11:55:36 +08:00
return nil
}
func (s *streamFunction) SetPipeHandler(fn network.PipeHandler) error {
s.pfn = fn
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s SetHandler(%v)", streamFunctionLogPrefix, s.fn)
2022-10-12 11:55:36 +08:00
return nil
}
// Connect create a connection to the mq, when data arrived, the data will be passed to the
// handler which set by SetHandler method.
func (s *streamFunction) Connect() error {
s.client.Logger().Debugf("%s Connect()", streamFunctionLogPrefix)
// notify underlying network operations, when data with tag we observed arrived, invoke the func
s.client.SetDataFrameObserver(func(data *frame.DataFrame) {
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s receive DataFrame, tag=%# x, carriage=%# x", streamFunctionLogPrefix, data.Tag(), data.GetCarriage())
2022-10-12 11:55:36 +08:00
s.onDataFrame(data.GetCarriage(), data.GetMetaFrame())
})
if s.pfn != nil {
s.pIn = make(chan []byte)
s.pOut = make(chan *frame.PayloadFrame)
// handle user's pipe function
go func() {
s.pfn(s.pIn, s.pOut)
}()
// send user's pipe function outputs to mq
go func() {
for {
data := <-s.pOut
if data != nil {
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s pipe fn send: tag=%#x, data=%# x", streamFunctionLogPrefix, data.Tag, data.Carriage)
frm := frame.NewDataFrame()
// todo: frm.SetTransactionId
frm.SetCarriage(data.Tag, data.Carriage)
_ = s.client.WriteFrame(frm)
2022-10-12 11:55:36 +08:00
}
}
}()
}
err := s.client.Connect(context.Background(), s.emitterEndpoint)
2022-10-12 11:55:36 +08:00
if err != nil {
2023-04-05 20:59:07 +08:00
s.client.Logger().Errorf("%s Connect() error: %s", streamFunctionLogPrefix, err)
2022-10-12 11:55:36 +08:00
}
return err
}
// Close will close the connection.
func (s *streamFunction) Close() error {
if s.pIn != nil {
close(s.pIn)
}
if s.pOut != nil {
close(s.pOut)
}
if s.client != nil {
if err := s.client.Close(); err != nil {
2023-04-05 20:59:07 +08:00
s.client.Logger().Errorf("%s Close(): %v", err)
2022-10-12 11:55:36 +08:00
return err
}
}
return nil
}
// when DataFrame we observed arrived, invoke the user's function
func (s *streamFunction) onDataFrame(data []byte, metaFrame *frame.MetaFrame) {
2023-04-05 20:59:07 +08:00
s.client.Logger().Infof("%s onDataFrame ->[%s]", streamFunctionLogPrefix, s.name)
2022-10-12 11:55:36 +08:00
if s.fn != nil {
go func() {
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s execute-start fn: data[%d]=%# x", streamFunctionLogPrefix, len(data), frame.Shortly(data))
2022-10-12 11:55:36 +08:00
// invoke serverless
tag, resp := s.fn(data)
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s execute-done fn: tag=%#x, resp[%d]=%# x", streamFunctionLogPrefix, tag, len(resp), frame.Shortly(resp))
2022-10-12 11:55:36 +08:00
// if resp is not nil, means the user's function has returned something, we should send it to the mq
if len(resp) != 0 {
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s start WriteFrame(): tag=%#x, data[%d]=%# x", streamFunctionLogPrefix, tag, len(resp), frame.Shortly(resp))
2022-10-12 11:55:36 +08:00
// build a DataFrame
// TODO: seems we should implement a DeepCopy() of MetaFrame in the future
frm := frame.NewDataFrame()
2022-10-12 11:55:36 +08:00
// reuse transactionId
frm.SetTransactionId(metaFrame.TransactionId())
2022-10-12 11:55:36 +08:00
// reuse sourceId
frm.SetSourceId(metaFrame.SourceId())
frm.SetCarriage(tag, resp)
_ = s.client.WriteFrame(frm)
2022-10-12 11:55:36 +08:00
}
}()
} else if s.pfn != nil {
2023-04-05 20:59:07 +08:00
s.client.Logger().Debugf("%s pipe fn receive: data[%d]=%# x", streamFunctionLogPrefix, len(data), data)
2022-10-12 11:55:36 +08:00
s.pIn <- data
} else {
2023-04-05 20:59:07 +08:00
s.client.Logger().Warnf("%s StreamFunction is nil", streamFunctionLogPrefix)
2022-10-12 11:55:36 +08:00
}
}
// Send a DataFrame to mq.
func (s *streamFunction) Write(tag frame.Tag, carriage []byte) error {
2022-10-12 11:55:36 +08:00
fm := frame.NewDataFrame()
fm.SetCarriage(tag, carriage)
return s.client.WriteFrame(fm)
}
// SetErrorHandler set the error handler function when server error occurs
func (s *streamFunction) SetErrorHandler(fn func(err error)) {
s.client.SetErrorHandler(fn)
}