Skip to content
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

[ISSUE #4] Addressing infinite loop and undersized default buffer #7

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This application only has two configurations and both are set with sensible defa
| Env Variable | Description | Default |
| :--- | :---- | ---: |
| READ_LOCATION | Where on the filesystem the API should be looking for logs | `/var/logs` |
| BUFFER_SIZE | The size of the file chunk to read into memory for processing at a time | 4096 (bytes) |
| BUFFER_SIZE | The size of the file chunk to read into memory for processing at a time | 8192 (bytes) |

### Running in a Container

Expand All @@ -46,7 +46,7 @@ This will expose the service running on port 8000. The root endpoint redirects t
This will expose the service running locally on port 8000. The root endpoint redirects to a Swagger UI which documents the API.

#### Notes about local run mode
- The default read buffer size is 4096. This can be changed by setting an environment variable locally.
- The default read buffer size is 8192. This can be changed by setting an environment variable locally.
- The logs are read from `/var/logs` on the underlying system.
- If the code changes, the changes will be redeployed automatically as long as the server is still running.

Expand Down
7 changes: 5 additions & 2 deletions app/core/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def parse_file(
file_name: str,
line_count: int,
search_term: Union[str | None] = None,
buffer_size: int = 4096,
buffer_size: int = 8192,
) -> List[str]:

"""Opens and parses a file object from the bottom up by manipulating the file read pointer.
Expand Down Expand Up @@ -48,8 +48,11 @@ def parse_file(
# Split the data at line breaks
log_chunk = binary_data.splitlines(True)

# [ISSUE #4] scrollback != buffer check ensures that if the scrollback is the size of the buffer
# The program does not get stuck in an endless loop.

# If this is not the last read before EOF, we want to pop off a piece to use to size where the cursor should move.
if current_position - buffer_size > 0:
if current_position - buffer_size > 0 and scrollback != buffer_size:
scrollback = len(log_chunk.pop(0))
else:
scrollback = 0
Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

READ_LOCATION = "READ_LOCATION"
log_dir = os.getenv(READ_LOCATION, "/var/log")
buffer = int(os.getenv("BUFFER_SIZE", 4096))
buffer = int(os.getenv("BUFFER_SIZE", 8192))


@app.get("/", include_in_schema=False)
Expand Down
Loading
Loading