Skip to content

Commit

Permalink
Unsynchronized sessions can delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Motok1 committed Jun 12, 2024
1 parent 746325c commit af29cea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 9 additions & 6 deletions pkg/server/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (s *APIServer) createSRPolicy(_ context.Context, input *pb.CreateSRPolicyIn
s.logger.Info("Received CreateSRPolicy API request")
s.logger.Debug("Received paramater", zap.String("input", string(inputJson)))

pcepSession, err := getPcepSession(s.pce, inputSRPolicy.GetPcepSessionAddr())
pcepSession, err := getSyncedPcepSession(s.pce, inputSRPolicy.GetPcepSessionAddr())
if err != nil {
return &pb.RequestStatus{IsSuccess: false}, err
}
Expand Down Expand Up @@ -216,7 +216,7 @@ func (s *APIServer) DeleteSRPolicy(ctx context.Context, input *pb.DeleteSRPolicy
s.logger.Info("Received DeleteSRPolicy API request")
s.logger.Debug("Received paramater", zap.String("input", string(inputJson)))

pcepSession, err := getPcepSession(s.pce, inputSRPolicy.GetPcepSessionAddr())
pcepSession, err := getSyncedPcepSession(s.pce, inputSRPolicy.GetPcepSessionAddr())
if err != nil {
return &pb.RequestStatus{IsSuccess: false}, err
}
Expand Down Expand Up @@ -284,11 +284,11 @@ var validator = map[ValidationKind]func(policy *pb.SRPolicy, asn uint32) bool{
},
}

func getPcepSession(pce *Server, addr []byte) (*Session, error) {
func getSyncedPcepSession(pce *Server, addr []byte) (*Session, error) {
pcepSessionAddr, _ := netip.AddrFromSlice(addr)
pcepSession := pce.SearchSession(pcepSessionAddr)
pcepSession := pce.SearchSession(pcepSessionAddr, true)
if pcepSession == nil {
return nil, fmt.Errorf("no session with %s", pcepSessionAddr)
return nil, fmt.Errorf("no synced session with %s", pcepSessionAddr)
}
return pcepSession, nil
}
Expand Down Expand Up @@ -477,7 +477,10 @@ func (c *APIServer) DeleteSession(ctx context.Context, input *pb.Session) (*pb.R
ssAddr, _ := netip.AddrFromSlice(input.GetAddr())

s := c.pce
ss := s.SearchSession(ssAddr)
var ss *Session
if ss = s.SearchSession(ssAddr, false); ss == nil {
return nil, fmt.Errorf("no session with %s", ssAddr)
}
if err := ss.SendClose(pcep.R_NO_EXPLANATION_PROVIDED); err != nil {
return &pb.RequestStatus{IsSuccess: false}, err
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,14 @@ func (s *Server) closeSession(session *Session) {
}
}

func (s *Server) SearchSession(peerAddr netip.Addr) *Session {
// SearchSession returns a struct pointer of (Synced) session.
// if not exist, return nil
func (s *Server) SearchSession(peerAddr netip.Addr, onlySynced bool) *Session {
for _, pcepSession := range s.sessionList {
if pcepSession.peerAddr == peerAddr && pcepSession.isSynced {
return pcepSession
if pcepSession.peerAddr == peerAddr {
if !(onlySynced) || pcepSession.isSynced {
return pcepSession
}
}
}
return nil
Expand Down

0 comments on commit af29cea

Please sign in to comment.