github.com/superfly/nomad@v0.10.5-fly/command/agent/helpers.go (about)

     1  package agent
     2  
     3  // rpcHandlerForAlloc is a helper that given an allocation ID returns whether to
     4  // use the local clients RPC, the local clients remote RPC or the server on the
     5  // agent.
     6  func (s *HTTPServer) rpcHandlerForAlloc(allocID string) (localClient, remoteClient, server bool) {
     7  	c := s.agent.Client()
     8  	srv := s.agent.Server()
     9  
    10  	// See if the local client can handle the request.
    11  	localAlloc := false
    12  	if c != nil {
    13  		// If there is an error it means that the client doesn't have the
    14  		// allocation so we can't use the local client
    15  		_, err := c.GetAllocState(allocID)
    16  		if err == nil {
    17  			localAlloc = true
    18  		}
    19  	}
    20  
    21  	// Only use the client RPC to server if we don't have a server and the local
    22  	// client can't handle the call.
    23  	useClientRPC := c != nil && !localAlloc && srv == nil
    24  
    25  	// Use the server as a last case.
    26  	useServerRPC := !localAlloc && !useClientRPC && srv != nil
    27  
    28  	return localAlloc, useClientRPC, useServerRPC
    29  }
    30  
    31  // rpcHandlerForNode is a helper that given a node ID returns whether to
    32  // use the local clients RPC, the local clients remote RPC or the server on the
    33  // agent. If there is a local node and no node id is given, it is assumed the
    34  // local node is being targed.
    35  func (s *HTTPServer) rpcHandlerForNode(nodeID string) (localClient, remoteClient, server bool) {
    36  	c := s.agent.Client()
    37  	srv := s.agent.Server()
    38  
    39  	// See if the local client can handle the request.
    40  	localClient = c != nil && // Must have a client
    41  		(nodeID == "" || // If no node ID is given
    42  			nodeID == c.NodeID()) // Requested node is the local node.
    43  
    44  	// Only use the client RPC to server if we don't have a server and the local
    45  	// client can't handle the call.
    46  	useClientRPC := c != nil && !localClient && srv == nil
    47  
    48  	// Use the server as a last case.
    49  	useServerRPC := !localClient && !useClientRPC && srv != nil && nodeID != ""
    50  
    51  	return localClient, useClientRPC, useServerRPC
    52  }