-
Notifications
You must be signed in to change notification settings - Fork 40
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
Merged
+350
−4
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1dd3dc7
feat: add network readiness, closes #204
gpevnev d0d3401
remove ipcpath param for reth
gpevnev 30e63d5
fix wait for ready to use existing Ready interface
gpevnev b04e7fc
add /livez endpoint
gpevnev 827e618
Revert "remove ipcpath param for reth"
gpevnev 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
Some comments aren't visible on the classic Files Changed page.
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
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,109 @@ | ||
| package playground | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "sync" | ||
| ) | ||
|
|
||
| type ReadyzServer struct { | ||
| instances []*instance | ||
| port int | ||
| server *http.Server | ||
| mu sync.RWMutex | ||
| } | ||
|
|
||
| type ReadyzResponse struct { | ||
| Ready bool `json:"ready"` | ||
| Error string `json:"error,omitempty"` | ||
| } | ||
|
|
||
| func NewReadyzServer(instances []*instance, port int) *ReadyzServer { | ||
| return &ReadyzServer{ | ||
| instances: instances, | ||
| port: port, | ||
| } | ||
| } | ||
|
|
||
| func (s *ReadyzServer) Start() error { | ||
| mux := http.NewServeMux() | ||
| mux.HandleFunc("/livez", s.handleLivez) | ||
| mux.HandleFunc("/readyz", s.handleReadyz) | ||
gpevnev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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) handleLivez(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte("OK")) //nolint:errcheck | ||
| } | ||
|
|
||
| func (s *ReadyzServer) handleReadyz(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodGet { | ||
| http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| ready, err := s.isReady() | ||
|
|
||
| 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) | ||
| } | ||
|
|
||
| if err := json.NewEncoder(w).Encode(response); err != nil { | ||
| fmt.Printf("Failed to encode readyz response: %v\n", err) | ||
| } | ||
| } | ||
|
|
||
| func (s *ReadyzServer) isReady() (bool, error) { | ||
| ctx := context.Background() | ||
| for _, inst := range s.instances { | ||
| if _, ok := inst.component.(ServiceReady); ok { | ||
| elURL := fmt.Sprintf("http://localhost:%d", inst.service.MustGetPort("http").HostPort) | ||
| ready, err := isChainProducingBlocks(ctx, elURL) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if !ready { | ||
| return false, nil | ||
| } | ||
| } | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func (s *ReadyzServer) Port() int { | ||
| return s.port | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.