-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat(realtime): Add realtime support #146
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
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
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,83 @@ | ||
package flagsmith | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Flagsmith/flagsmith-go-client/v4/flagengine/environments" | ||
) | ||
|
||
func (c *Client) startRealtimeUpdates(ctx context.Context) { | ||
err := c.UpdateEnvironment(ctx) | ||
if err != nil { | ||
panic("Failed to fetch the environment while configuring real-time updates") | ||
} | ||
env, _ := c.environment.Load().(*environments.EnvironmentModel) | ||
stream_url := c.config.realtimeBaseUrl + "sse/environments/" + env.APIKey + "/stream" | ||
envUpdatedAt := env.UpdatedAt | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
resp, err := http.Get(stream_url) | ||
if err != nil { | ||
c.log.Errorf("Error connecting to realtime server: %v", err) | ||
continue | ||
} | ||
defer resp.Body.Close() | ||
|
||
scanner := bufio.NewScanner(resp.Body) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.HasPrefix(line, "data: ") { | ||
parsedTime, err := parseUpdatedAtFromSSE(line) | ||
if err != nil { | ||
c.log.Errorf("Error reading realtime stream: %v", err) | ||
return | ||
} | ||
if parsedTime.After(envUpdatedAt) { | ||
err = c.UpdateEnvironment(ctx) | ||
if err != nil { | ||
c.log.Errorf("Failed to update the environment: %v", err) | ||
continue | ||
} | ||
env, _ := c.environment.Load().(*environments.EnvironmentModel) | ||
|
||
envUpdatedAt = env.UpdatedAt | ||
} | ||
} | ||
} | ||
if err := scanner.Err(); err != nil { | ||
c.log.Errorf("Error reading realtime stream: %v", err) | ||
} | ||
} | ||
} | ||
} | ||
func parseUpdatedAtFromSSE(line string) (time.Time, error) { | ||
var eventData struct { | ||
UpdatedAt float64 `json:"updated_at"` | ||
} | ||
|
||
data := strings.TrimPrefix(line, "data: ") | ||
err := json.Unmarshal([]byte(data), &eventData) | ||
if err != nil { | ||
return time.Time{}, errors.New("failed to parse event data: " + err.Error()) | ||
} | ||
|
||
if eventData.UpdatedAt <= 0 { | ||
return time.Time{}, errors.New("invalid 'updated_at' value in event data") | ||
} | ||
|
||
// Convert the float timestamp into seconds and nanoseconds | ||
seconds := int64(eventData.UpdatedAt) | ||
nanoseconds := int64((eventData.UpdatedAt - float64(seconds)) * 1e9) | ||
|
||
// Return the parsed time | ||
return time.Unix(seconds, nanoseconds), nil | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure you want to
return
here? Shouldn't we go back to listening to the stream on the hopes of the next data line includes theupdated_at
field?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think you are right. I will update