Skip to content

Commit

Permalink
#817 use PreCall and PostCall plugins for RegisterFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Sep 3, 2023
1 parent 883cc59 commit e43aa14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
16 changes: 8 additions & 8 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type PluginContainer interface {

DoPreHandleRequest(ctx context.Context, req *protocol.Message) error
DoPreCall(ctx context.Context, serviceName, methodName string, args interface{}) (interface{}, error)
DoPostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}) (interface{}, error)
DoPostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}, err error) (interface{}, error)

DoPreWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error
DoPostWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error
Expand Down Expand Up @@ -96,7 +96,7 @@ type (
}

PostCallPlugin interface {
PostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}) (interface{}, error)
PostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}, err error) (interface{}, error)
}

// PreWriteResponsePlugin represents .
Expand Down Expand Up @@ -314,18 +314,18 @@ func (p *pluginContainer) DoPreCall(ctx context.Context, serviceName, methodName
}

// DoPostCall invokes PostCallPlugin plugin.
func (p *pluginContainer) DoPostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}) (interface{}, error) {
var err error
func (p *pluginContainer) DoPostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}, err error) (interface{}, error) {
var e error
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PostCallPlugin); ok {
reply, err = plugin.PostCall(ctx, serviceName, methodName, args, reply)
if err != nil {
return reply, err
reply, e = plugin.PostCall(ctx, serviceName, methodName, args, reply, err)
if e != nil {
return reply, e
}
}
}

return reply, err
return reply, e
}

// DoPreWriteResponse invokes PreWriteResponse plugin.
Expand Down
12 changes: 9 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,7 @@ func (s *Server) handleRequest(ctx context.Context, req *protocol.Message) (res
err = service.call(ctx, mtype, reflect.ValueOf(argv), reflect.ValueOf(replyv))
}

if err == nil {
replyv, err = s.Plugins.DoPostCall(ctx, serviceName, methodName, argv, replyv)
}
replyv, err = s.Plugins.DoPostCall(ctx, serviceName, methodName, argv, replyv, err)

// return argc to object pool
reflectTypePools.Put(mtype.ArgType, argv)
Expand Down Expand Up @@ -795,13 +793,21 @@ func (s *Server) handleRequestForFunction(ctx context.Context, req *protocol.Mes
}

replyv := reflectTypePools.Get(mtype.ReplyType)
argv, err = s.Plugins.DoPreCall(ctx, serviceName, methodName, argv)
if err != nil {
// return reply to object pool
reflectTypePools.Put(mtype.ReplyType, replyv)
return s.handleError(res, err)
}

if mtype.ArgType.Kind() != reflect.Ptr {
err = service.callForFunction(ctx, mtype, reflect.ValueOf(argv).Elem(), reflect.ValueOf(replyv))
} else {
err = service.callForFunction(ctx, mtype, reflect.ValueOf(argv), reflect.ValueOf(replyv))
}

replyv, err = s.Plugins.DoPostCall(ctx, serviceName, methodName, argv, replyv, err)

reflectTypePools.Put(mtype.ArgType, argv)

if err != nil {
Expand Down

0 comments on commit e43aa14

Please sign in to comment.