Skip to content

Commit

Permalink
refactor Envelope modifiers to provide more flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Feb 17, 2022
1 parent 6fdccb5 commit 00dd6b6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 28 deletions.
22 changes: 15 additions & 7 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ type Context struct {
utils.SpinLock
ctx context.Context //nolint:containedctx

nb *northBridge
kv map[string]interface{}
hdr map[string]string
conn Conn
in *Envelope
wf WriteFunc
err error
nb *northBridge
kv map[string]interface{}
hdr map[string]string
conn Conn
in *Envelope
wf WriteFunc
modifiers []Modifier
err error

handlers HandlerFuncChain
index int
Expand Down Expand Up @@ -54,6 +55,12 @@ func (ctx *Context) StopExecution() {
ctx.index = abortIndex
}

// AddModifier adds one or more modifiers to the context which will be executed on each outgoing
// Envelope before writing it to the wire.
func (ctx *Context) AddModifier(modifiers ...Modifier) {
ctx.modifiers = append(ctx.modifiers, modifiers...)
}

// SetUserContext replaces the default context with the provided context.
func (ctx *Context) SetUserContext(userCtx context.Context) {
ctx.ctx = userCtx
Expand Down Expand Up @@ -131,5 +138,6 @@ func (ctx *Context) reset() {
ctx.in.release()
ctx.index = 0
ctx.handlers = ctx.handlers[:0]
ctx.modifiers = ctx.modifiers[:0]
ctx.ctx = nil
}
37 changes: 28 additions & 9 deletions desc/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import (
"github.com/clubpay/ronykit"
)

type Error struct {
Code int
Item string
Message ronykit.Message
}

// Contract is the description of the ronykit.Contract you are going to create.
type Contract struct {
Name string
Handlers []ronykit.HandlerFunc
Wrappers []ronykit.ContractWrapper
Input ronykit.Message
Output ronykit.Message
Selectors []ronykit.RouteSelector
Modifiers []ronykit.Modifier
Name string
Handlers []ronykit.HandlerFunc
Wrappers []ronykit.ContractWrapper
Input ronykit.Message
Output ronykit.Message
PossibleErrors []Error
Selectors []ronykit.RouteSelector
Modifiers []ronykit.Modifier
}

func NewContract() *Contract {
Expand All @@ -37,14 +44,26 @@ func (c *Contract) SetInput(m ronykit.Message) *Contract {
return c
}

// SetOutput sets the outgoing message for this Contract. This is an optional parameter, which
// mostly could be used by external tools such as Swagger generator etc.
// SetOutput sets the outgoing message for this Contract. This is an OPTIONAL parameter, which
// mostly could be used by external tools such as Swagger or any other doc generator tools.
func (c *Contract) SetOutput(m ronykit.Message) *Contract {
c.Output = m

return c
}

// AddPossibleError sets the possible errors for this Contract. This is OPTIONAL parameter, which
// mostly could be used by external tools such as Swagger or any other doc generator tools.
func (c *Contract) AddPossibleError(code int, item string, m ronykit.Message) *Contract {
c.PossibleErrors = append(c.PossibleErrors, Error{
Code: code,
Item: item,
Message: m,
})

return c
}

// AddSelector adds a ronykit.RouteSelector for this contract. Selectors are bundle specific.
func (c *Contract) AddSelector(s ronykit.RouteSelector) *Contract {
c.Selectors = append(c.Selectors, s)
Expand Down
6 changes: 6 additions & 0 deletions envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func (e *Envelope) Send() {
panic("BUG!! do not call Send on incoming envelope")
}

// run the modifiers in LIFO order
modifiersCount := len(e.ctx.modifiers) - 1
for idx := range e.ctx.modifiers {
e.ctx.modifiers[modifiersCount-idx](e)
}

// Use WriteFunc to write the Envelope into the connection
e.ctx.Error(e.ctx.wf(e.conn, e))

Expand Down
12 changes: 4 additions & 8 deletions std/bundle/fasthttp/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ func (b *bundle) dispatchWS(in []byte) (ronykit.DispatchFunc, error) {
}

writeFunc := func(conn ronykit.Conn, e *ronykit.Envelope) error {
for idx := range routeData.Modifiers {
routeData.Modifiers[idx](e)
}

outputMsgContainer := acquireOutgoingMessage()
outputMsgContainer.Payload = e.GetMsg()
e.WalkHdr(func(key string, val string) bool {
Expand Down Expand Up @@ -228,6 +224,8 @@ func (b *bundle) dispatchWS(in []byte) (ronykit.DispatchFunc, error) {
Set(ronykit.CtxServiceName, routeData.ServiceName).
Set(ronykit.CtxRoute, routeData.Predicate)

ctx.AddModifier(routeData.Modifiers...)

// run the execFunc with generated params
execFunc(writeFunc, routeData.Handlers...)

Expand Down Expand Up @@ -265,10 +263,6 @@ func (b *bundle) dispatchHTTP(conn *httpConn, in []byte) (ronykit.DispatchFunc,
panic("BUG!! incorrect connection")
}

for idx := range routeData.Modifiers {
routeData.Modifiers[idx](e)
}

data, err := e.GetMsg().Marshal()
if err != nil {
return err
Expand Down Expand Up @@ -296,6 +290,8 @@ func (b *bundle) dispatchHTTP(conn *httpConn, in []byte) (ronykit.DispatchFunc,
SetHdrWalker(conn).
SetMsg(routeData.Decoder(params, in))

ctx.AddModifier(routeData.Modifiers...)

// execute handler functions
execFunc(writeFunc, routeData.Handlers...)

Expand Down
6 changes: 2 additions & 4 deletions std/bundle/fastws/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ func (b *bundle) Dispatch(c ronykit.Conn, in []byte) (ronykit.DispatchFunc, erro
}

writeFunc := func(conn ronykit.Conn, e *ronykit.Envelope) error {
for idx := range routeData.Modifiers {
routeData.Modifiers[idx](e)
}

outputMsgContainer := acquireOutgoingMessage()
outputMsgContainer.Payload = e.GetMsg()
e.WalkHdr(func(key string, val string) bool {
Expand Down Expand Up @@ -130,6 +126,8 @@ func (b *bundle) Dispatch(c ronykit.Conn, in []byte) (ronykit.DispatchFunc, erro
Set(ronykit.CtxServiceName, routeData.ServiceName).
Set(ronykit.CtxRoute, routeData.Predicate)

ctx.AddModifier(routeData.Modifiers...)

// run the execFunc with generated params
execFunc(writeFunc, routeData.Handlers...)

Expand Down

0 comments on commit 00dd6b6

Please sign in to comment.