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:
parent
25bfe420b0
commit
c6e3b7417e
43
README.md
43
README.md
|
@ -743,11 +743,11 @@ The following example code to send single notification in Go.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/appleboy/gorush/rpc/proto"
|
"github.com/appleboy/gorush/rpc/proto"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -802,6 +802,47 @@ function main() {
|
||||||
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
|
## 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/).
|
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/).
|
||||||
|
|
|
@ -2,7 +2,6 @@ package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/appleboy/gorush/rpc/proto"
|
"github.com/appleboy/gorush/rpc/proto"
|
||||||
|
|
||||||
|
@ -13,8 +12,6 @@ import (
|
||||||
// generate protobuffs
|
// generate protobuffs
|
||||||
// protoc --go_out=plugins=grpc,import_path=proto:. *.proto
|
// protoc --go_out=plugins=grpc,import_path=proto:. *.proto
|
||||||
|
|
||||||
var backoff = time.Second
|
|
||||||
|
|
||||||
type healthClient struct {
|
type healthClient struct {
|
||||||
client proto.HealthClient
|
client proto.HealthClient
|
||||||
conn *grpc.ClientConn
|
conn *grpc.ClientConn
|
||||||
|
@ -37,25 +34,24 @@ func (c *healthClient) Check(ctx context.Context) (bool, error) {
|
||||||
var err error
|
var err error
|
||||||
req := new(proto.HealthCheckRequest)
|
req := new(proto.HealthCheckRequest)
|
||||||
|
|
||||||
for {
|
res, err = c.client.Check(ctx, req)
|
||||||
res, err = c.client.Check(ctx, req)
|
if err == nil {
|
||||||
if err == nil {
|
if res.GetStatus() == proto.HealthCheckResponse_SERVING {
|
||||||
if res.GetStatus() == proto.HealthCheckResponse_SERVING {
|
return true, nil
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
}
|
||||||
switch grpc.Code(err) {
|
return false, nil
|
||||||
case
|
|
||||||
codes.Aborted,
|
|
||||||
codes.DataLoss,
|
|
||||||
codes.DeadlineExceeded,
|
|
||||||
codes.Internal,
|
|
||||||
codes.Unavailable:
|
|
||||||
// non-fatal errors
|
|
||||||
default:
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
<-time.After(backoff)
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/appleboy/gorush/rpc/proto"
|
"github.com/appleboy/gorush/rpc"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,10 +22,16 @@ func main() {
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
c := proto.NewHealthClient(conn)
|
client := rpc.NewGrpcHealthClient(conn)
|
||||||
r, err := c.Check(context.Background(), &proto.HealthCheckRequest{})
|
|
||||||
if err != nil {
|
for {
|
||||||
log.Fatalf("could not greet: %v", err)
|
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())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/appleboy/gorush/rpc/proto"
|
"github.com/appleboy/gorush/rpc/proto"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
// Package context defines the Context type, which carries deadlines,
|
// Package context defines the Context type, which carries deadlines,
|
||||||
// cancelation signals, and other request-scoped values across API boundaries
|
// cancelation signals, and other request-scoped values across API boundaries
|
||||||
// and between processes.
|
// 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
|
// 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
|
// servers should accept a Context. The chain of function calls between must
|
||||||
|
|
|
@ -585,10 +585,10 @@
|
||||||
"revisionTime": "2017-02-08T20:51:15Z"
|
"revisionTime": "2017-02-08T20:51:15Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "dr5+PfIRzXeN+l1VG+s0lea9qz8=",
|
"checksumSHA1": "GtamqiJoL7PGHsN454AoffBFMa8=",
|
||||||
"path": "golang.org/x/net/context",
|
"path": "golang.org/x/net/context",
|
||||||
"revision": "ab5485076ff3407ad2d02db054635913f017b0ed",
|
"revision": "a337091b0525af65de94df2eb7e98bd9962dcbe2",
|
||||||
"revisionTime": "2017-07-19T21:11:51Z"
|
"revisionTime": "2016-10-09T20:39:03Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "WHc3uByvGaMcnSoI21fhzYgbOgg=",
|
"checksumSHA1": "WHc3uByvGaMcnSoI21fhzYgbOgg=",
|
||||||
|
|
Loading…
Reference in New Issue