-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filter instagram-server requests from hitting fbcode codenav server
Summary: Here we introduce a method to fail fast and immediately respond with a DaemonQueryFailure instance in cases where a specified regex directory pattern is matched against. Reviewed By: kinto0 Differential Revision: D47655034 fbshipit-source-id: c4f992bf28c8221c368b85f0514b180420743e3a
- Loading branch information
1 parent
42a5f00
commit 1ffcbe1
Showing
5 changed files
with
451 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,62 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import abc | ||
import re | ||
|
||
from re import Pattern | ||
from typing import Optional | ||
|
||
from ..language_server import daemon_connection | ||
|
||
from .daemon_query import DaemonQueryFailure | ||
|
||
|
||
class AbstractDaemonQueryFailer(abc.ABC): | ||
@abc.abstractmethod | ||
def query_failure( | ||
self, | ||
path: str, | ||
) -> Optional[DaemonQueryFailure]: | ||
"""A result of None indicates that failure should not take place""" | ||
raise NotImplementedError() | ||
|
||
def query_connection_failure( | ||
self, path: str | ||
) -> Optional[daemon_connection.DaemonConnectionFailure]: | ||
"""A result of None indicates that failure should not take place""" | ||
raise NotImplementedError() | ||
|
||
|
||
class DaemonQueryNoOpFailer(AbstractDaemonQueryFailer): | ||
def query_failure(self, path: str) -> Optional[DaemonQueryFailure]: | ||
return None | ||
|
||
def query_connection_failure( | ||
self, path: str | ||
) -> Optional[daemon_connection.DaemonConnectionFailure]: | ||
return None | ||
|
||
|
||
class RegexDaemonQueryFailer(AbstractDaemonQueryFailer): | ||
"""Fails daemon queries matching a specified regex pattern""" | ||
|
||
def __init__(self, reject_regex: str) -> None: | ||
self.reject_regex = reject_regex | ||
self.compiled_reject_regex: Pattern[str] = re.compile(reject_regex) | ||
|
||
def _matches_regex(self, path: str) -> Optional[str]: | ||
if self.compiled_reject_regex.match(path): | ||
return f"Not querying daemon for path: {path} as matches regex: {self.reject_regex}" | ||
|
||
def query_failure(self, path: str) -> Optional[DaemonQueryFailure]: | ||
if (fail_message := self._matches_regex(path)) is not None: | ||
return DaemonQueryFailure(fail_message) | ||
|
||
def query_connection_failure( | ||
self, path: str | ||
) -> Optional[daemon_connection.DaemonConnectionFailure]: | ||
if (fail_message := self._matches_regex(path)) is not None: | ||
return daemon_connection.DaemonConnectionFailure(fail_message) |
Oops, something went wrong.