mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-12 08:23:48 +02:00
0bd10fa507
* Fix "Empty slice declaration using a literal" * Fix "collides with imported package name" * Remove unused code in pipeline * Remove unused oauth2.providerAuthHeaderWorks() * Add TODOs * Format Code * Cleanup doublestar import * Migrate deprecated functions Co-authored-by: Anbraten <anton@ju60.de>
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
// "encoding/json"
|
|
"time"
|
|
|
|
// "github.com/woodpecker-ci/woodpecker/pipeline/backend"
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// generate protobuffs
|
|
// protoc --go_out=plugins=grpc,import_path=proto:. *.proto
|
|
|
|
type healthClient struct {
|
|
client proto.HealthClient
|
|
conn *grpc.ClientConn
|
|
}
|
|
|
|
// NewGrpcHealthClient returns a new grpc Client.
|
|
func NewGrpcHealthClient(conn *grpc.ClientConn) Health {
|
|
client := new(healthClient)
|
|
client.client = proto.NewHealthClient(conn)
|
|
client.conn = conn
|
|
return client
|
|
}
|
|
|
|
func (c *healthClient) Close() error {
|
|
return c.conn.Close()
|
|
}
|
|
|
|
func (c *healthClient) Check(ctx context.Context) (bool, error) {
|
|
var res *proto.HealthCheckResponse
|
|
var err error
|
|
req := new(proto.HealthCheckRequest)
|
|
|
|
for {
|
|
res, err = c.client.Check(ctx, req)
|
|
if err == nil {
|
|
if res.GetStatus() == proto.HealthCheckResponse_SERVING {
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
switch status.Code(err) {
|
|
case
|
|
codes.Aborted,
|
|
codes.DataLoss,
|
|
codes.DeadlineExceeded,
|
|
codes.Internal,
|
|
codes.Unavailable:
|
|
// non-fatal errors
|
|
default:
|
|
return false, err
|
|
}
|
|
<-time.After(backoff)
|
|
}
|
|
}
|