-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[!] add Patroni REST API support (#263)
* [!] add Patroni REST API support * [-] fix nil pointer dereference
- Loading branch information
1 parent
459ef03
commit 8aef662
Showing
5 changed files
with
58 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package checker | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"strconv" | ||
"time" | ||
|
||
"net/http" | ||
|
||
"github.com/cybertec-postgresql/vip-manager/vipconfig" | ||
) | ||
|
||
// PatroniLeaderChecker will use Patroni REST API to check the leader. | ||
// --trigger-key is used to specify the endpoint to check, e.g. /leader. | ||
// --trigger-value is used to specify the HTTP code to expect, e.g. 200. | ||
type PatroniLeaderChecker struct { | ||
*vipconfig.Config | ||
} | ||
|
||
// NewPatroniLeaderChecker returns a new instance | ||
func NewPatroniLeaderChecker(conf *vipconfig.Config) (*PatroniLeaderChecker, error) { | ||
return &PatroniLeaderChecker{conf}, nil | ||
} | ||
|
||
// GetChangeNotificationStream checks the status in the loop | ||
func (c *PatroniLeaderChecker) GetChangeNotificationStream(ctx context.Context, out chan<- bool) error { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-time.After(time.Duration(c.Interval) * time.Millisecond): | ||
r, err := http.Get(c.Endpoints[0] + c.Key) | ||
if err != nil { | ||
log.Printf("patroni REST API error: %s", err) | ||
continue | ||
} | ||
out <- strconv.Itoa(r.StatusCode) == c.Nodename | ||
} | ||
} | ||
} |
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