-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
81 lines (64 loc) · 1.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"log"
"time"
"google.golang.org/grpc/grpclog"
"golang.org/x/net/context"
"github.com/ppg/grpc-queue/grpcqueue"
pb "github.com/ppg/grpc-queue/proto"
)
func init() {
// Prettier CLI output
log.SetFlags(0)
}
// START MAIN CONSUMER OMIT
func main() {
// Make an in memory queue
queue := make(chan []byte, 10)
// Create a consumer
consumer := grpcqueue.NewConsumer()
// Create a service implementation and register according to proto IDL
testService := &testServer{}
pb.RegisterTestQueueConsumer(consumer, testService)
// Start consumer and wait for channel to close (in background)
go func() {
if err := consumer.Consume(queue); err != nil {
log.Fatalf("consume failed: %s", err)
}
log.Fatal("consume stopped")
}()
// END MAIN CONSUMER OMIT
// START MAIN PRODUCER OMIT
// Create a producer
producer := pb.NewTestQueueProducer(queue)
// Enqueue a couple objects
ctx := context.Background()
log.Print("Enqueue: Hello World")
producer.EnqueueTestRPC(ctx, &pb.TestRPCRequest{Message: "Hello World"})
log.Print("Enqueue: Where am I?")
producer.EnqueueTestRPC(ctx, &pb.TestRPCRequest{Message: "Where am I?"})
log.Print("Enqueue: Unknown on foo.Bar")
grpcqueue.Enqueue(ctx, "foo.Bar", "Unknown", &pb.TestRPCRequest{}, queue)
// Wait a little for these messages
log.Print("Waiting")
time.Sleep(1 * time.Second)
// Enqueue goodbye
log.Print("Enqueue: Goodbye!")
producer.EnqueueTestRPC(ctx, &pb.TestRPCRequest{Message: "Goodbye!"})
// END MAIN PRODUCER OMIT
// START MAIN WAIT OMIT
// Close channel to exit
close(queue)
// Wait a little for these messages
log.Print("Waiting")
time.Sleep(1 * time.Second)
}
// END MAIN WAIT OMIT
// START TEST SERVER OMIT
type testServer struct {
}
func (testServer) TestRPC(ctx context.Context, req *pb.TestRPCRequest) (*pb.TestRPCResponse, error) {
grpclog.Printf("[testServer] %s", req.Message)
return &pb.TestRPCResponse{}, nil
}
// END TEST SERVER OMIT