- 
                Notifications
    You must be signed in to change notification settings 
- Fork 203
[4.4] Backport new timeout config options [ADR 006] #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            robsdedude
  merged 5 commits into
  neo4j:4.4
from
robsdedude:connection-acquisition-timeout-routing-4.4
  
      
      
   
  Jun 28, 2022 
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      5fdd1db
              
                Backport new connection timeout config options
              
              
                robsdedude 8628ec4
              
                session_connection_timeout should be inf by default
              
              
                robsdedude d8f9d9e
              
                Move Deadline module
              
              
                robsdedude 7fbd766
              
                Refactoring
              
              
                robsdedude 6241153
              
                Improve error messages
              
              
                robsdedude File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # Copyright (c) "Neo4j" | ||
| # Neo4j Sweden AB [https://neo4j.com] | ||
| # | ||
| # This file is part of Neo4j. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|  | ||
|  | ||
| from contextlib import contextmanager | ||
| from time import perf_counter | ||
|  | ||
|  | ||
| class Deadline: | ||
| def __init__(self, timeout): | ||
| if timeout is None or timeout == float("inf"): | ||
| self._deadline = float("inf") | ||
| else: | ||
| self._deadline = perf_counter() + timeout | ||
| self._original_timeout = timeout | ||
|  | ||
| @property | ||
| def original_timeout(self): | ||
| return self._original_timeout | ||
|  | ||
| def expired(self): | ||
| return self.to_timeout() == 0 | ||
|  | ||
| def to_timeout(self): | ||
| if self._deadline == float("inf"): | ||
| return None | ||
| timeout = self._deadline - perf_counter() | ||
| return timeout if timeout > 0 else 0 | ||
|  | ||
| def __eq__(self, other): | ||
| if isinstance(other, Deadline): | ||
| return self._deadline == other._deadline | ||
| return NotImplemented | ||
|  | ||
| def __gt__(self, other): | ||
| if isinstance(other, Deadline): | ||
| return self._deadline > other._deadline | ||
| return NotImplemented | ||
|  | ||
| def __ge__(self, other): | ||
| if isinstance(other, Deadline): | ||
| return self._deadline >= other._deadline | ||
| return NotImplemented | ||
|  | ||
| def __lt__(self, other): | ||
| if isinstance(other, Deadline): | ||
| return self._deadline < other._deadline | ||
| return NotImplemented | ||
|  | ||
| def __le__(self, other): | ||
| if isinstance(other, Deadline): | ||
| return self._deadline <= other._deadline | ||
| return NotImplemented | ||
|  | ||
| @classmethod | ||
| def from_timeout_or_deadline(cls, timeout): | ||
| if isinstance(timeout, cls): | ||
| return timeout | ||
| return cls(timeout) | ||
|  | ||
|  | ||
| merge_deadlines = min | ||
|  | ||
|  | ||
| def merge_deadlines_and_timeouts(*deadline): | ||
| deadlines = map(Deadline.from_timeout_or_deadline, deadline) | ||
| return merge_deadlines(deadlines) | ||
|  | ||
|  | ||
| @contextmanager | ||
| def connection_deadline(connection, deadline): | ||
| original_deadline = connection.socket.get_deadline() | ||
| if deadline is None and original_deadline is not None: | ||
| # nothing to do here | ||
| yield | ||
| return | ||
| deadline = merge_deadlines( | ||
| (d for d in (deadline, original_deadline) if d is not None) | ||
| ) | ||
| connection.socket.set_deadline(deadline) | ||
| try: | ||
| yield | ||
| finally: | ||
| connection.socket.set_deadline(original_deadline) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.