Add grpc check example. (#306)

* Add grpc check example.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* add missing package.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-11-14 00:18:48 -06:00 committed by GitHub
parent 25bfe420b0
commit c6e3b7417e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 34 deletions

View File

@ -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/).

View File

@ -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,7 +34,6 @@ 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 {
@ -56,6 +52,6 @@ func (c *healthClient) Check(ctx context.Context) (bool, error) {
default:
return false, err
}
<-time.After(backoff)
}
return false, err
}

View File

@ -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())
}

View File

@ -1,11 +1,11 @@
package main
import (
"context"
"log"
"github.com/appleboy/gorush/rpc/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

View File

@ -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

6
vendor/vendor.json vendored
View File

@ -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=",