-
Notifications
You must be signed in to change notification settings - Fork 57
#724 Enable URL liveness check for release builds #729
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
AzizMukhtorjonov
merged 20 commits into
zigbee-alliance:master
from
Abdulbois:#724-URL-Reachability-Check
May 21, 2026
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7ada368
Enable URL liveness check for release builds
Abdulbois b98d700
Refactor
Abdulbois 99c96d8
Fix dockerfiles
Abdulbois 45e70e3
Fix CI file
Abdulbois 3678b10
Update Ubuntu version to resolve image incompatibility issue in githu…
Abdulbois 014001f
Disable URL liveness check for CI integration tests
Abdulbois 13c3757
Refactor
Abdulbois ab462e7
Merge branch 'master' into #724-URL-Reachability-Check
Abdulbois 187548c
Disable URL liveness checker in test
Abdulbois 97932b9
Fix
Abdulbois 86daf85
Refactor URL liveness checks
Abdulbois 3ecb037
Update CI and Make files to run URL liveness checks
Abdulbois ae9c4dc
Fix `if` conditions
Abdulbois 4056ef4
Revert golang image version change
Abdulbois 9e47934
Enable checker in missed places
Abdulbois 2d8fc46
Improve namings and impl
Abdulbois 68b35e1
Fix typo
Abdulbois 0902ef4
Optimize running tests where dev tag is disabled
Abdulbois 4e61263
Merge branch 'refs/heads/master' into #724-URL-Reachability-Check
Abdulbois 505a32d
Merge branch 'refs/heads/master' into #724-URL-Reachability-Check
Abdulbois 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ on: | |
| env: | ||
| BIN_NAME: dcld | ||
| COSMOVISOR_VERSION: 1.5.0 | ||
| URL_LIVENESS_CHECK_ENABLED: true | ||
|
|
||
| jobs: | ||
|
|
||
|
|
||
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
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,5 @@ | ||
| //go:build !dev | ||
|
|
||
| package config | ||
|
|
||
| const URLLivenessCheckEnabled = true |
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,5 @@ | ||
| //go:build dev | ||
|
|
||
| package config | ||
|
|
||
| const URLLivenessCheckEnabled = false |
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,88 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/zigbee-alliance/distributed-compliance-ledger/internal/config" | ||
| ) | ||
|
|
||
| const ( | ||
| livenessCheckTimeout = 10 * time.Second | ||
| ) | ||
|
|
||
| var allowed4XXStatusCodes = []int{ | ||
| http.StatusUnauthorized, | ||
| http.StatusForbidden, | ||
| http.StatusUnavailableForLegalReasons, | ||
| http.StatusMethodNotAllowed, | ||
| } | ||
| var httpClient = &http.Client{Timeout: livenessCheckTimeout} | ||
|
|
||
| func IsLiveURL(u string) bool { | ||
| if !config.URLLivenessCheckEnabled { | ||
| return true | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), livenessCheckTimeout) | ||
| defer cancel() | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodHead, u, nil) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| resp, err := httpClient.Do(req) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusBadRequest { | ||
| return true | ||
| } | ||
|
|
||
| for _, code := range allowed4XXStatusCodes { | ||
| if code == resp.StatusCode { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // CheckURLsForLiveness checks the liveness of the given URLs concurrently and | ||
| // returns unreachable URLs as a list. | ||
| // Empty strings are skipped. | ||
| // | ||
| // Returns an empty list if all non-empty URLs are reachable. | ||
| func CheckURLsForLiveness(urls ...string) []string { | ||
| results := make([]string, len(urls)) | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i, u := range urls { | ||
| if u == "" { | ||
| continue | ||
| } | ||
| // Call each URL concurrently | ||
| wg.Add(1) | ||
| go func(i int, u string) { | ||
| defer wg.Done() | ||
| if !IsLiveURL(u) { | ||
| results[i] = u | ||
| } | ||
| }(i, u) | ||
| } | ||
| wg.Wait() | ||
|
|
||
| var unreachable []string | ||
| for _, u := range results { | ||
| if u != "" { | ||
| unreachable = append(unreachable, u) | ||
| } | ||
| } | ||
|
|
||
| return unreachable | ||
| } | ||
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,134 @@ | ||
| // Copyright 2020 DSR Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //go:build !dev | ||
|
|
||
| package cli | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const unreachableURL = "http://192.0.2.1:1" | ||
|
|
||
| func TestIsLiveURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| statusCode int | ||
| want bool | ||
| }{ | ||
| {"200 OK", http.StatusOK, true}, | ||
| {"301 redirect", http.StatusMovedPermanently, true}, | ||
| {"401 unauthorized", http.StatusUnauthorized, true}, | ||
| {"403 forbidden", http.StatusForbidden, true}, | ||
| {"405 method not allowed", http.StatusMethodNotAllowed, true}, | ||
| {"451 unavailable for legal reasons", http.StatusUnavailableForLegalReasons, true}, | ||
| {"404 not found", http.StatusNotFound, false}, | ||
| {"500 internal server error", http.StatusInternalServerError, false}, | ||
| {"502 bad gateway", http.StatusBadGateway, false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| require.Equal(t, http.MethodHead, r.Method) | ||
| w.WriteHeader(tt.statusCode) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| u, err := url.ParseRequestURI(srv.URL) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, tt.want, IsLiveURL(u.String())) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIsLiveURLUnreachable(t *testing.T) { | ||
| u, err := url.ParseRequestURI(unreachableURL) | ||
| require.NoError(t, err) | ||
|
|
||
| require.False(t, IsLiveURL(u.String())) | ||
| } | ||
|
|
||
| func TestCheckURLsForLiveness(t *testing.T) { | ||
| okSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer okSrv.Close() | ||
|
|
||
| notFoundSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer notFoundSrv.Close() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| urls []string | ||
| want []string | ||
| }{ | ||
| {"no URLs", nil, nil}, | ||
| {"all empty strings", []string{"", "", ""}, nil}, | ||
| {"all reachable", []string{okSrv.URL, okSrv.URL}, nil}, | ||
| {"single unreachable", []string{okSrv.URL, notFoundSrv.URL}, []string{notFoundSrv.URL}}, | ||
| {"empties skipped", []string{"", okSrv.URL, ""}, nil}, | ||
| { | ||
| "multiple unreachable preserve input order", | ||
| []string{okSrv.URL, notFoundSrv.URL, unreachableURL}, | ||
| []string{notFoundSrv.URL, unreachableURL}, | ||
| }, | ||
| { | ||
| "unreachable later in list", | ||
| []string{okSrv.URL, "", notFoundSrv.URL}, | ||
| []string{notFoundSrv.URL}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| require.Equal(t, tt.want, CheckURLsForLiveness(tt.urls...)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCheckURLsForLivenessRunsConcurrently(t *testing.T) { | ||
| const handlerDelay = 200 * time.Millisecond | ||
| const concurrentURLs = 5 | ||
|
|
||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| time.Sleep(handlerDelay) | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| urls := make([]string, concurrentURLs) | ||
| for i := range urls { | ||
| urls[i] = srv.URL | ||
| } | ||
|
|
||
| start := time.Now() | ||
| require.Empty(t, CheckURLsForLiveness(urls...)) | ||
| elapsed := time.Since(start) | ||
|
|
||
| // Sequential calls would take approximately concurrentURLs*handlerDelay time | ||
| // Concurrent execution should finish in roughly handlerDelay. | ||
| require.Less(t, elapsed, time.Duration(concurrentURLs)*handlerDelay/2, | ||
| "URL checks did not run concurrently (elapsed %s)", elapsed) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.