github.com/decred/dcrlnd@v0.7.6/watchtower/wtserver/delete_session.go (about)

     1  package wtserver
     2  
     3  import (
     4  	"github.com/decred/dcrlnd/watchtower/wtdb"
     5  	"github.com/decred/dcrlnd/watchtower/wtwire"
     6  )
     7  
     8  // handleDeleteSession processes a DeleteSession request for a client with given
     9  // SessionID. The id is assumed to have been previously authenticated by the
    10  // brontide connection.
    11  func (s *Server) handleDeleteSession(peer Peer, id *wtdb.SessionID) error {
    12  	var failCode wtwire.DeleteSessionCode
    13  
    14  	// Delete all session data associated with id.
    15  	err := s.cfg.DB.DeleteSession(*id)
    16  	switch {
    17  	case err == nil:
    18  		failCode = wtwire.CodeOK
    19  
    20  		log.Debugf("Session %s deleted", id)
    21  
    22  	case err == wtdb.ErrSessionNotFound:
    23  		failCode = wtwire.DeleteSessionCodeNotFound
    24  
    25  	default:
    26  		failCode = wtwire.CodeTemporaryFailure
    27  	}
    28  
    29  	return s.replyDeleteSession(peer, id, failCode)
    30  }
    31  
    32  // replyDeleteSession sends a DeleteSessionReply back to the peer containing the
    33  // error code resulting from processes a DeleteSession request.
    34  func (s *Server) replyDeleteSession(peer Peer, id *wtdb.SessionID,
    35  	code wtwire.DeleteSessionCode) error {
    36  
    37  	msg := &wtwire.DeleteSessionReply{
    38  		Code: code,
    39  	}
    40  
    41  	err := s.sendMessage(peer, msg)
    42  	if err != nil {
    43  		log.Errorf("Unable to send DeleteSessionReply to %s", id)
    44  	}
    45  
    46  	// Return the write error if the request succeeded.
    47  	if code == wtwire.CodeOK {
    48  		return err
    49  	}
    50  
    51  	// Otherwise the request failed, return a connection failure to
    52  	// disconnect the client.
    53  	return &connFailure{
    54  		ID:   *id,
    55  		Code: code,
    56  	}
    57  }