https://github.com/bstrdlord/failover
go library that allows you to add multiple urls and send a request to the active one (pool)
https://github.com/bstrdlord/failover
failover failover-url go pool url-pool
Last synced: 8 months ago
JSON representation
go library that allows you to add multiple urls and send a request to the active one (pool)
- Host: GitHub
- URL: https://github.com/bstrdlord/failover
- Owner: bstrdlord
- Created: 2024-08-10T17:54:00.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-10T18:20:03.000Z (about 1 year ago)
- Last Synced: 2025-01-11T06:13:22.338Z (9 months ago)
- Topics: failover, failover-url, go, pool, url-pool
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# failover
go library that allows you to add multiple urls and send a request to the active one## 💡 Info
**How it works?** failover saves your url and checks for availability every n seconds. if the url is unavailable, it saves and checks to wait for availability.**Request function:** Request uses round robin algorithm to select the url
## 🚀 Usage
```gofunc main() {
f := failover.New(checkConn,
failover.OptCheckUrlDelay(30),
failover.OptMaxAttempts(10),
failover.OptCheckUrlBeforeAdding(true),
failover.OptReqOnErr(failover.ReqOnErrRemoveAndReconnect),
)// first way to add url
f.AddUrl(f.MustParseURL("https://google.com"))
f.AddUrl(f.MustParseURL("https://fjxiofujosidfujs.com"))
f.AddUrl(f.MustParseURL("https://fbi.gov"))// second way to add url
f.AddUrls(f.MustParseURL("https://youtube.com"), f.MustParseURL("https://spotify.com"), f.MustParseURL("https://github.com"))fmt.Println(f.Request(requestFunc))
fmt.Println(f.Request(requestFunc, failover.OptReqOnErr(failover.ReqOnErrIgnore))) // local option (only in this function) to ignore error
}func checkConn(url *url.URL) error {
resp, err := http.Get(url.String())
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}func requestFunc(url *url.URL) error {
fmt.Println("Request:", url.String())
_, err := http.Get(url.String())
if err != nil {
return err
}
return nil
}```