diff --git a/README.md b/README.md index 7880857..073ff57 100644 --- a/README.md +++ b/README.md @@ -743,11 +743,11 @@ The following example code to send single notification in Go. package main import ( + "context" "log" "github.com/appleboy/gorush/rpc/proto" - "golang.org/x/net/context" "google.golang.org/grpc" ) @@ -802,6 +802,47 @@ function main() { main(); ``` +GRPC Health Checking example: See [document](https://github.com/grpc/grpc/blob/master/doc/health-checking.md). + +[embedmd]:# (rpc/example/go/send/main.go go) +```go +package main + +import ( + "context" + "log" + + "github.com/appleboy/gorush/rpc/proto" + + "google.golang.org/grpc" +) + +const ( + address = "localhost:9000" +) + +func main() { + // Set up a connection to the server. + conn, err := grpc.Dial(address, grpc.WithInsecure()) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + c := proto.NewGorushClient(conn) + + r, err := c.Send(context.Background(), &proto.NotificationRequest{ + Platform: 2, + Tokens: []string{"1234567890"}, + Message: "test message", + }) + if err != nil { + log.Fatalf("could not greet: %v", err) + } + log.Printf("Success: %t\n", r.Success) + log.Printf("Count: %d\n", r.Counts) +} +``` + ## Run gorush in Docker Set up `gorush` in the cloud in under 5 minutes with zero knowledge of Golang or Linux shell using our [gorush Docker image](https://hub.docker.com/r/appleboy/gorush/). diff --git a/rpc/client_grpc_health.go b/rpc/client_grpc_health.go index 3699989..98c4980 100644 --- a/rpc/client_grpc_health.go +++ b/rpc/client_grpc_health.go @@ -2,7 +2,6 @@ package rpc import ( "context" - "time" "github.com/appleboy/gorush/rpc/proto" @@ -13,8 +12,6 @@ import ( // generate protobuffs // protoc --go_out=plugins=grpc,import_path=proto:. *.proto -var backoff = time.Second - type healthClient struct { client proto.HealthClient conn *grpc.ClientConn @@ -37,25 +34,24 @@ func (c *healthClient) Check(ctx context.Context) (bool, error) { 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 + res, err = c.client.Check(ctx, req) + if err == nil { + if res.GetStatus() == proto.HealthCheckResponse_SERVING { + return true, nil } - switch grpc.Code(err) { - case - codes.Aborted, - codes.DataLoss, - codes.DeadlineExceeded, - codes.Internal, - codes.Unavailable: - // non-fatal errors - default: - return false, err - } - <-time.After(backoff) + return false, nil } + switch grpc.Code(err) { + case + codes.Aborted, + codes.DataLoss, + codes.DeadlineExceeded, + codes.Internal, + codes.Unavailable: + // non-fatal errors + default: + return false, err + } + + return false, err } diff --git a/rpc/example/go/health/main.go b/rpc/example/go/health/main.go index ae2d4de..323b8a4 100644 --- a/rpc/example/go/health/main.go +++ b/rpc/example/go/health/main.go @@ -1,11 +1,12 @@ package main import ( + "context" "log" + "time" - "github.com/appleboy/gorush/rpc/proto" + "github.com/appleboy/gorush/rpc" - "golang.org/x/net/context" "google.golang.org/grpc" ) @@ -21,10 +22,16 @@ func main() { } defer conn.Close() - c := proto.NewHealthClient(conn) - r, err := c.Check(context.Background(), &proto.HealthCheckRequest{}) - if err != nil { - log.Fatalf("could not greet: %v", err) + client := rpc.NewGrpcHealthClient(conn) + + for { + ok, err := client.Check(context.Background()) + if !ok || err != nil { + log.Printf("can't connect grpc server: %v, code: %v\n", err, grpc.Code(err)) + } else { + log.Println("connect the grpc server successfully") + } + + <-time.After(time.Second) } - log.Printf("Health: %d\n", r.GetStatus()) } diff --git a/rpc/example/go/send/main.go b/rpc/example/go/send/main.go index 2d9e618..ed138ec 100644 --- a/rpc/example/go/send/main.go +++ b/rpc/example/go/send/main.go @@ -1,11 +1,11 @@ package main import ( + "context" "log" "github.com/appleboy/gorush/rpc/proto" - "golang.org/x/net/context" "google.golang.org/grpc" ) diff --git a/vendor/golang.org/x/net/context/context.go b/vendor/golang.org/x/net/context/context.go index d3681ab..a3c021d 100644 --- a/vendor/golang.org/x/net/context/context.go +++ b/vendor/golang.org/x/net/context/context.go @@ -5,6 +5,8 @@ // Package context defines the Context type, which carries deadlines, // cancelation signals, and other request-scoped values across API boundaries // and between processes. +// As of Go 1.7 this package is available in the standard library under the +// name context. https://golang.org/pkg/context. // // Incoming requests to a server should create a Context, and outgoing calls to // servers should accept a Context. The chain of function calls between must diff --git a/vendor/vendor.json b/vendor/vendor.json index 2edc56f..98d809a 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -585,10 +585,10 @@ "revisionTime": "2017-02-08T20:51:15Z" }, { - "checksumSHA1": "dr5+PfIRzXeN+l1VG+s0lea9qz8=", + "checksumSHA1": "GtamqiJoL7PGHsN454AoffBFMa8=", "path": "golang.org/x/net/context", - "revision": "ab5485076ff3407ad2d02db054635913f017b0ed", - "revisionTime": "2017-07-19T21:11:51Z" + "revision": "a337091b0525af65de94df2eb7e98bd9962dcbe2", + "revisionTime": "2016-10-09T20:39:03Z" }, { "checksumSHA1": "WHc3uByvGaMcnSoI21fhzYgbOgg=",