Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pdpserver/server/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,10 @@ func benchmarkRawPolicySet(p *pdp.PolicyStorage, b *testing.B) {
c := s.c
s.RUnlock()

r := s.rawValidate(p, c, benchmarkRequests[n%len(benchmarkRequests)].Body)
r, err := s.rawValidate(p, c, benchmarkRequests[n%len(benchmarkRequests)].Body)
if err != nil {
b.Fatal("unexpected err: ", err)
}

effect, n, err := pdp.UnmarshalResponseToAssignmentsArray(r, a[:])
if err != nil {
Expand Down Expand Up @@ -956,7 +959,10 @@ func benchmarkRawPolicySetToBuffer(p *pdp.PolicyStorage, b *testing.B) {
c := s.c
s.RUnlock()

r := s.rawValidateToBuffer(p, c, benchmarkRequests[n%len(benchmarkRequests)].Body, buf[:])
r, err := s.rawValidateToBuffer(p, c, benchmarkRequests[n%len(benchmarkRequests)].Body, buf[:])
if err != nil {
b.Fatal("unexpected err: ", err)
}

effect, n, err := pdp.UnmarshalResponseToAssignmentsArray(r, a[:])
if err != nil {
Expand Down
15 changes: 3 additions & 12 deletions pdpserver/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ func TestValidate_hook_pre(t *testing.T) {

msg := new(pb.Msg)

_, err := s.Validate(nil, msg)
if err != nil {
t.Fatal(err)
}
s.Validate(nil, msg)

if e := true; called != e {
t.Errorf("hook was not called")
Expand All @@ -46,10 +43,7 @@ func TestValidate_hook_post(t *testing.T) {

msg := new(pb.Msg)

_, err := s.Validate(nil, msg)
if err != nil {
t.Fatal(err)
}
s.Validate(nil, msg)

if e := true; called != e {
t.Error("hook was not called")
Expand Down Expand Up @@ -86,10 +80,7 @@ func TestValidate_hook_context(t *testing.T) {

msg := new(pb.Msg)

_, err := s.Validate(nil, msg)
if err != nil {
t.Fatal(err)
}
s.Validate(nil, msg)

if e := true; hasKey != e {
t.Error("key was not passed from pre hook to post hook")
Expand Down
29 changes: 10 additions & 19 deletions pdpserver/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func makeFailureResponseWithBuffer(b []byte, err error) []byte {
return b[:n]
}

func (s *Server) rawValidate(p *pdp.PolicyStorage, c *pdp.LocalContentStorage, in []byte) []byte {
func (s *Server) rawValidate(p *pdp.PolicyStorage, c *pdp.LocalContentStorage, in []byte) ([]byte, error) {
if p == nil {
return makeFailureResponse(newMissingPolicyError())
return makeFailureResponse(newMissingPolicyError()), newMissingPolicyError()
}

ctx, err := s.newContext(c, in)
if err != nil {
return makeFailureResponse(err)
return makeFailureResponse(err), err
}

if s.opts.logger.Level >= log.DebugLevel {
Expand All @@ -74,12 +74,7 @@ func (s *Server) rawValidate(p *pdp.PolicyStorage, c *pdp.LocalContentStorage, i
}).Debug("Response")
}

out, err := r.Marshal(ctx)
if err != nil {
panic(err)
}

return out
return r.Marshal(ctx)
}

func (s *Server) rawValidateWithAllocator(p *pdp.PolicyStorage, c *pdp.LocalContentStorage, in []byte, f func(n int) ([]byte, error)) []byte {
Expand Down Expand Up @@ -117,14 +112,14 @@ func (s *Server) rawValidateWithAllocator(p *pdp.PolicyStorage, c *pdp.LocalCont
return out
}

func (s *Server) rawValidateToBuffer(p *pdp.PolicyStorage, c *pdp.LocalContentStorage, in []byte, out []byte) []byte {
func (s *Server) rawValidateToBuffer(p *pdp.PolicyStorage, c *pdp.LocalContentStorage, in []byte, out []byte) ([]byte, error) {
if p == nil {
return makeFailureResponseWithBuffer(out, newMissingPolicyError())
return makeFailureResponseWithBuffer(out, newMissingPolicyError()), newMissingPolicyError()
}

ctx, err := s.newContext(c, in)
if err != nil {
return makeFailureResponseWithBuffer(out, err)
return makeFailureResponseWithBuffer(out, err), err
}

if s.opts.logger.Level >= log.DebugLevel {
Expand All @@ -145,11 +140,7 @@ func (s *Server) rawValidateToBuffer(p *pdp.PolicyStorage, c *pdp.LocalContentSt
}

n, err := r.MarshalToBuffer(out, ctx)
if err != nil {
panic(err)
}

return out[:n]
return out[:n], err
}

// Validate is a server handler for gRPC call
Expand Down Expand Up @@ -180,12 +171,12 @@ func (s *Server) Validate(ctx context.Context, in *pb.Msg) (*pb.Msg, error) {
s.RUnlock()

if s.opts.autoResponseSize {
msg.Body = s.rawValidate(p, c, in.Body)
msg.Body, err = s.rawValidate(p, c, in.Body)
return msg, err
}

b := s.pool.Get()
msg.Body = s.rawValidateToBuffer(p, c, in.Body, b)
msg.Body, err = s.rawValidateToBuffer(p, c, in.Body, b)
s.pool.Put(b)

return msg, err
Expand Down
6 changes: 5 additions & 1 deletion pdpserver/server/stream_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ func (s *Server) NewValidationStream(stream pb.PDP_NewValidationStreamServer) er
return buffer, nil
})})
} else {
err = stream.Send(&pb.Msg{Body: s.rawValidateToBuffer(p, c, in.Body, buffer)})
body, err := s.rawValidateToBuffer(p, c, in.Body, buffer)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you define new err variable, which shadows existing one. Can you make sure that this doesn't break existing logic, e.g. now external err is not updated with the error coming from Send. Consider renaming the new error variable to avoid ambiguity.

if err != nil {
return err
}
err = stream.Send(&pb.Msg{Body: body})
}
if err != nil {
s.opts.logger.WithFields(log.Fields{
Expand Down