feat: save channel proxy configs, close #1078#1080
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the channel configuration experience by introducing a robust proxy preset management system. Users can now save frequently used proxy settings, which are automatically stored when applied to a channel. These saved presets can then be easily selected from a dropdown when configuring new or existing channels, significantly reducing manual input and potential errors. A new dedicated section in the system settings allows for viewing and managing these saved proxy configurations. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a feature to save and manage proxy configurations as presets. The implementation is comprehensive, covering backend logic for storage and frontend UI for management and usage. I've identified a minor inconsistency in the frontend data handling and a potential race condition on the backend that could be addressed to improve robustness.
| toast.success(t('channels.messages.updateSuccess')); | ||
| // Auto-save to proxy presets | ||
| if (values.type === ProxyType.URL && values.url) { | ||
| saveProxyPreset.mutate({ url: values.url, username: values.username, password: values.password }); |
There was a problem hiding this comment.
For consistency with channels-action-dialog.tsx and to avoid sending empty strings for optional fields, it's better to convert empty strings to undefined. This ensures that empty optional fields are not included in the GraphQL mutation payload.
| saveProxyPreset.mutate({ url: values.url, username: values.username, password: values.password }); | |
| saveProxyPreset.mutate({ url: values.url, username: values.username || undefined, password: values.password || undefined }); |
| func (s *SystemService) SaveProxyPreset(ctx context.Context, preset ProxyPreset) error { | ||
| presets, err := s.ProxyPresets(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| found := false | ||
| for i, p := range presets { | ||
| if p.URL == preset.URL { | ||
| presets[i] = preset | ||
| found = true | ||
|
|
||
| break | ||
| } | ||
| } | ||
|
|
||
| if !found { | ||
| presets = append(presets, preset) | ||
| } | ||
|
|
||
| jsonBytes, err := json.Marshal(presets) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal proxy presets: %w", err) | ||
| } | ||
|
|
||
| return s.setSystemValue(ctx, SystemKeyProxyPresets, string(jsonBytes)) | ||
| } |
There was a problem hiding this comment.
The current implementation of SaveProxyPreset uses a read-modify-write pattern that can lead to a race condition. If two requests try to modify presets concurrently (e.g., one saving, one deleting), one of the updates could be lost. The same issue exists in DeleteProxyPreset.
To ensure data consistency, these operations should be performed within a database transaction with row-level locking.
Example of how this could be implemented with ent:
func (s *SystemService) SaveProxyPreset(ctx context.Context, preset ProxyPreset) error {
return s.db.WithTx(ctx, func(tx *ent.Tx) error {
txCtx := ent.NewContext(ctx, tx)
// Lock the row for update
sys, err := tx.System.Query().
Where(system.KeyEQ(SystemKeyProxyPresets)).
ForUpdate().
Only(txCtx)
var presets []ProxyPreset
if err != nil {
if !ent.IsNotFound(err) {
return fmt.Errorf("failed to get proxy presets: %w", err)
}
// If not found, presets is an empty slice
} else {
if err := json.Unmarshal([]byte(sys.Value), &presets); err != nil {
return fmt.Errorf("failed to unmarshal proxy presets: %w", err)
}
}
// ... (rest of the modification logic for saving)
jsonBytes, err := json.Marshal(presets)
if err != nil {
return fmt.Errorf("failed to marshal proxy presets: %w", err)
}
// This would require setSystemValue to be adapted to use the transaction client
return setSystemValue(txCtx, SystemKeyProxyPresets, string(jsonBytes))
})
}
No description provided.