-
Notifications
You must be signed in to change notification settings - Fork 35
feat: add network readiness #210
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
Open
gpevnev
wants to merge
1
commit into
main
Choose a base branch
from
feat/add-network-readiness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+414
−0
Open
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,93 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "time" | ||
|
|
||
| "github.com/flashbots/builder-playground/playground" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var waitReadyURL string | ||
| var waitReadyTimeout time.Duration | ||
| var waitReadyInterval time.Duration | ||
|
|
||
| var WaitReadyCmd = &cobra.Command{ | ||
| Use: "wait-ready", | ||
| Short: "Wait for the network to be ready for transactions", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return waitForReady() | ||
| }, | ||
| } | ||
|
|
||
| func InitWaitReadyCmd() { | ||
| WaitReadyCmd.Flags().StringVar(&waitReadyURL, "url", "http://localhost:8080/readyz", "readyz endpoint URL") | ||
| WaitReadyCmd.Flags().DurationVar(&waitReadyTimeout, "timeout", 60*time.Second, "maximum time to wait") | ||
| WaitReadyCmd.Flags().DurationVar(&waitReadyInterval, "interval", 1*time.Second, "poll interval") | ||
| } | ||
|
|
||
| func waitForReady() error { | ||
| fmt.Printf("Waiting for %s (timeout: %s, interval: %s)\n", waitReadyURL, waitReadyTimeout, waitReadyInterval) | ||
|
|
||
| sig := make(chan os.Signal, 1) | ||
| signal.Notify(sig, os.Interrupt) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| go func() { | ||
| <-sig | ||
| cancel() | ||
| }() | ||
|
|
||
| client := &http.Client{ | ||
| Timeout: 5 * time.Second, | ||
| } | ||
|
|
||
| deadline := time.Now().Add(waitReadyTimeout) | ||
| attempt := 0 | ||
|
|
||
| for time.Now().Before(deadline) { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return fmt.Errorf("interrupted") | ||
| default: | ||
| } | ||
|
|
||
| attempt++ | ||
| elapsed := time.Since(deadline.Add(-waitReadyTimeout)) | ||
|
|
||
| resp, err := client.Get(waitReadyURL) | ||
| if err != nil { | ||
| fmt.Printf(" [%s] Attempt %d: connection error: %v\n", elapsed.Truncate(time.Second), attempt, err) | ||
| time.Sleep(waitReadyInterval) | ||
| continue | ||
| } | ||
|
|
||
| var readyzResp playground.ReadyzResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&readyzResp); err != nil { | ||
| resp.Body.Close() | ||
| fmt.Printf(" [%s] Attempt %d: failed to parse response: %v\n", elapsed.Truncate(time.Second), attempt, err) | ||
| time.Sleep(waitReadyInterval) | ||
| continue | ||
| } | ||
| resp.Body.Close() | ||
|
|
||
| if resp.StatusCode == http.StatusOK && readyzResp.Ready { | ||
| fmt.Printf(" [%s] Ready! (200 OK)\n", elapsed.Truncate(time.Second)) | ||
| return nil | ||
| } | ||
|
|
||
| errMsg := "" | ||
| if readyzResp.Error != "" { | ||
| errMsg = fmt.Sprintf(" - %s", readyzResp.Error) | ||
| } | ||
| fmt.Printf(" [%s] Attempt %d: %d %s%s\n", elapsed.Truncate(time.Second), attempt, resp.StatusCode, http.StatusText(resp.StatusCode), errMsg) | ||
| time.Sleep(waitReadyInterval) | ||
| } | ||
|
|
||
| return fmt.Errorf("timeout waiting for readyz after %s", waitReadyTimeout) | ||
| } |
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,87 @@ | ||
| package playground | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "sync" | ||
| ) | ||
|
|
||
| type ReadyzServer struct { | ||
| checker NetworkReadyChecker | ||
| manifest *Manifest | ||
| port int | ||
| server *http.Server | ||
| mu sync.RWMutex | ||
| } | ||
|
|
||
| type ReadyzResponse struct { | ||
| Ready bool `json:"ready"` | ||
| Error string `json:"error,omitempty"` | ||
| } | ||
|
|
||
| func NewReadyzServer(checker NetworkReadyChecker, manifest *Manifest, port int) *ReadyzServer { | ||
| return &ReadyzServer{ | ||
| checker: checker, | ||
| manifest: manifest, | ||
| port: port, | ||
| } | ||
| } | ||
|
|
||
| func (s *ReadyzServer) Start() error { | ||
| mux := http.NewServeMux() | ||
| mux.HandleFunc("/readyz", s.handleReadyz) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while at it, might also be useful to add a |
||
|
|
||
| s.server = &http.Server{ | ||
| Addr: fmt.Sprintf(":%d", s.port), | ||
| Handler: mux, | ||
| } | ||
|
|
||
| go func() { | ||
| if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
| fmt.Printf("Readyz server error: %v\n", err) | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (s *ReadyzServer) Stop() error { | ||
| if s.server != nil { | ||
| return s.server.Shutdown(context.Background()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *ReadyzServer) handleReadyz(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodGet { | ||
| http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| ctx := r.Context() | ||
| ready, err := s.checker.IsNetworkReady(ctx, s.manifest) | ||
|
|
||
| response := ReadyzResponse{ | ||
| Ready: ready, | ||
| } | ||
|
|
||
| if err != nil { | ||
| response.Error = err.Error() | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
|
|
||
| if ready { | ||
| w.WriteHeader(http.StatusOK) | ||
| } else { | ||
| w.WriteHeader(http.StatusServiceUnavailable) | ||
| } | ||
|
|
||
| json.NewEncoder(w).Encode(response) | ||
gpevnev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (s *ReadyzServer) Port() int { | ||
| return s.port | ||
| } | ||
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
Oops, something went wrong.
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.
What is the utility of this server? In the previous iteration, Playground was meant to work as a template engine, not as a long live instance.
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.
Right now it also works as runner for
docker-compose, and this is just more correct way to actually ensure readiness for processing transactions. Example use case: we need to understand when we can start working with network in a CI job and when it's considered readyUh oh!
There was an error while loading. Please reload this page.
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.
I agree that it would be useful to have a way to ensure the playground is ready and healthy before doing other actions. But, I am not sure I follow what the intended workflow is.
Is the ready server running forever after the deployment is ready and healthy? Or it just stops alongside the deployment command?
Why is this workflow better than just using the
wait-readyflag? I would find the ready server useful if it were to take minutes to deploy anything but it takes seconds right now.