-
Notifications
You must be signed in to change notification settings - Fork 570
feat: add runtime configuration updates with atomic drop request handling #142
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
akshaydeo
merged 1 commit into
main
from
07-03-feat_drop_access_request_and_prom_labels_shifted_to_config
Jul 10, 2025
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/fasthttp/router" | ||
| bifrost "github.com/maximhq/bifrost/core" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| // ConfigHandler manages runtime configuration updates for Bifrost. | ||
| // It provides an endpoint to hot-reload settings from the configuration file. | ||
| type ConfigHandler struct { | ||
| client *bifrost.Bifrost | ||
| logger schemas.Logger | ||
| configPath string | ||
| } | ||
|
|
||
| // NewConfigHandler creates a new handler for configuration management. | ||
| // It requires the Bifrost client, a logger, and the path to the config file to be reloaded. | ||
| func NewConfigHandler(client *bifrost.Bifrost, logger schemas.Logger, configPath string) *ConfigHandler { | ||
| return &ConfigHandler{ | ||
| client: client, | ||
| logger: logger, | ||
| configPath: configPath, | ||
| } | ||
| } | ||
|
|
||
| // RegisterRoutes registers the configuration-related routes. | ||
| // It adds the `PUT /config` endpoint. | ||
| func (h *ConfigHandler) RegisterRoutes(r *router.Router) { | ||
| r.PUT("/config", h.handleReloadConfig) | ||
| } | ||
|
|
||
| // handleReloadConfig re-reads the configuration file and applies updatable settings. | ||
| // Currently, it supports hot-reloading of the `drop_excess_requests` setting. | ||
| // Note that settings like `prometheus_labels` cannot be changed at runtime. | ||
| func (h *ConfigHandler) handleReloadConfig(ctx *fasthttp.RequestCtx) { | ||
| var config struct { | ||
| BifrostSettings struct { | ||
| DropExcessRequests *bool `json:"drop_excess_requests,omitempty"` | ||
| } `json:"bifrost_settings"` | ||
| } | ||
|
|
||
| data, err := os.ReadFile(h.configPath) | ||
| if err != nil { | ||
| SendError(ctx, fasthttp.StatusInternalServerError, fmt.Sprintf("failed to read config file: %v", err), h.logger) | ||
| return | ||
| } | ||
|
|
||
| if err := json.Unmarshal(data, &config); err != nil { | ||
| SendError(ctx, fasthttp.StatusBadRequest, fmt.Sprintf("failed to parse config file: %v", err), h.logger) | ||
| return | ||
| } | ||
|
|
||
| if config.BifrostSettings.DropExcessRequests != nil { | ||
| h.client.UpdateDropExcessRequests(*config.BifrostSettings.DropExcessRequests) | ||
| } | ||
|
|
||
| ctx.SetStatusCode(fasthttp.StatusOK) | ||
| SendJSON(ctx, map[string]interface{}{"status": "config reloaded", "drop_excess_requests": config.BifrostSettings.DropExcessRequests}, h.logger) | ||
| } | ||
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
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.