mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 12:32:21 +00:00
commit
e4de6b2f20
29
vendor/ch4-04/03/clientpub/main.go
vendored
Normal file
29
vendor/ch4-04/03/clientpub/main.go
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
|
pb "ch4-04/03/pubsubservice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
client := pb.NewPubsubServiceClient(conn)
|
||||||
|
|
||||||
|
_, err = client.Publish(context.Background(), &pb.String{Value: "golang: hello Go"})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err = client.Publish(context.Background(), &pb.String{Value: "docker: hello Docker"})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
38
vendor/ch4-04/03/clientsub/main.go
vendored
Normal file
38
vendor/ch4-04/03/clientsub/main.go
vendored
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
|
pb "ch4-04/03/pubsubservice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
client := pb.NewPubsubServiceClient(conn)
|
||||||
|
stream, err := client.Subscribe(context.Background(), &pb.String{Value: "golang:"})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
reply, err := stream.Recv()
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(reply.GetValue())
|
||||||
|
}
|
||||||
|
}
|
214
vendor/ch4-04/03/pubsubservice/pubsubservice.pb.go
vendored
Normal file
214
vendor/ch4-04/03/pubsubservice/pubsubservice.pb.go
vendored
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// source: pubsubservice.proto
|
||||||
|
|
||||||
|
package pubsubservice
|
||||||
|
|
||||||
|
import proto "github.com/golang/protobuf/proto"
|
||||||
|
import fmt "fmt"
|
||||||
|
import math "math"
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "golang.org/x/net/context"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type String struct {
|
||||||
|
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||||
|
XXX_unrecognized []byte `json:"-"`
|
||||||
|
XXX_sizecache int32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *String) Reset() { *m = String{} }
|
||||||
|
func (m *String) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*String) ProtoMessage() {}
|
||||||
|
func (*String) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_pubsubservice_f5055945d20b225a, []int{0}
|
||||||
|
}
|
||||||
|
func (m *String) XXX_Unmarshal(b []byte) error {
|
||||||
|
return xxx_messageInfo_String.Unmarshal(m, b)
|
||||||
|
}
|
||||||
|
func (m *String) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
return xxx_messageInfo_String.Marshal(b, m, deterministic)
|
||||||
|
}
|
||||||
|
func (dst *String) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_String.Merge(dst, src)
|
||||||
|
}
|
||||||
|
func (m *String) XXX_Size() int {
|
||||||
|
return xxx_messageInfo_String.Size(m)
|
||||||
|
}
|
||||||
|
func (m *String) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_String.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_String proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *String) GetValue() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Value
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*String)(nil), "pubsubservice.String")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ context.Context
|
||||||
|
var _ grpc.ClientConn
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
const _ = grpc.SupportPackageIsVersion4
|
||||||
|
|
||||||
|
// PubsubServiceClient is the client API for PubsubService service.
|
||||||
|
//
|
||||||
|
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||||
|
type PubsubServiceClient interface {
|
||||||
|
Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error)
|
||||||
|
Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type pubsubServiceClient struct {
|
||||||
|
cc *grpc.ClientConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPubsubServiceClient(cc *grpc.ClientConn) PubsubServiceClient {
|
||||||
|
return &pubsubServiceClient{cc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pubsubServiceClient) Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) {
|
||||||
|
out := new(String)
|
||||||
|
err := c.cc.Invoke(ctx, "/pubsubservice.PubsubService/Publish", in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pubsubServiceClient) Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error) {
|
||||||
|
stream, err := c.cc.NewStream(ctx, &_PubsubService_serviceDesc.Streams[0], "/pubsubservice.PubsubService/Subscribe", opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
x := &pubsubServiceSubscribeClient{stream}
|
||||||
|
if err := x.ClientStream.SendMsg(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := x.ClientStream.CloseSend(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return x, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type PubsubService_SubscribeClient interface {
|
||||||
|
Recv() (*String, error)
|
||||||
|
grpc.ClientStream
|
||||||
|
}
|
||||||
|
|
||||||
|
type pubsubServiceSubscribeClient struct {
|
||||||
|
grpc.ClientStream
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *pubsubServiceSubscribeClient) Recv() (*String, error) {
|
||||||
|
m := new(String)
|
||||||
|
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PubsubServiceServer is the server API for PubsubService service.
|
||||||
|
type PubsubServiceServer interface {
|
||||||
|
Publish(context.Context, *String) (*String, error)
|
||||||
|
Subscribe(*String, PubsubService_SubscribeServer) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterPubsubServiceServer(s *grpc.Server, srv PubsubServiceServer) {
|
||||||
|
s.RegisterService(&_PubsubService_serviceDesc, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _PubsubService_Publish_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(String)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(PubsubServiceServer).Publish(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/pubsubservice.PubsubService/Publish",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(PubsubServiceServer).Publish(ctx, req.(*String))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _PubsubService_Subscribe_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||||
|
m := new(String)
|
||||||
|
if err := stream.RecvMsg(m); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return srv.(PubsubServiceServer).Subscribe(m, &pubsubServiceSubscribeServer{stream})
|
||||||
|
}
|
||||||
|
|
||||||
|
type PubsubService_SubscribeServer interface {
|
||||||
|
Send(*String) error
|
||||||
|
grpc.ServerStream
|
||||||
|
}
|
||||||
|
|
||||||
|
type pubsubServiceSubscribeServer struct {
|
||||||
|
grpc.ServerStream
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *pubsubServiceSubscribeServer) Send(m *String) error {
|
||||||
|
return x.ServerStream.SendMsg(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _PubsubService_serviceDesc = grpc.ServiceDesc{
|
||||||
|
ServiceName: "pubsubservice.PubsubService",
|
||||||
|
HandlerType: (*PubsubServiceServer)(nil),
|
||||||
|
Methods: []grpc.MethodDesc{
|
||||||
|
{
|
||||||
|
MethodName: "Publish",
|
||||||
|
Handler: _PubsubService_Publish_Handler,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Streams: []grpc.StreamDesc{
|
||||||
|
{
|
||||||
|
StreamName: "Subscribe",
|
||||||
|
Handler: _PubsubService_Subscribe_Handler,
|
||||||
|
ServerStreams: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Metadata: "pubsubservice.proto",
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("pubsubservice.proto", fileDescriptor_pubsubservice_f5055945d20b225a) }
|
||||||
|
|
||||||
|
var fileDescriptor_pubsubservice_f5055945d20b225a = []byte{
|
||||||
|
// 132 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x28, 0x4d, 0x2a,
|
||||||
|
0x2e, 0x4d, 0x2a, 0x4e, 0x2d, 0x2a, 0xcb, 0x4c, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17,
|
||||||
|
0xe2, 0x45, 0x11, 0x54, 0x92, 0xe3, 0x62, 0x0b, 0x2e, 0x29, 0xca, 0xcc, 0x4b, 0x17, 0x12, 0xe1,
|
||||||
|
0x62, 0x2d, 0x4b, 0xcc, 0x29, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x82, 0x70, 0x8c,
|
||||||
|
0x5a, 0x19, 0xb9, 0x78, 0x03, 0xc0, 0x3a, 0x82, 0x21, 0x3a, 0x84, 0xcc, 0xb9, 0xd8, 0x03, 0x4a,
|
||||||
|
0x93, 0x72, 0x32, 0x8b, 0x33, 0x84, 0x44, 0xf5, 0x50, 0x6d, 0x80, 0x98, 0x24, 0x85, 0x5d, 0x58,
|
||||||
|
0xc8, 0x9a, 0x8b, 0x33, 0xb8, 0x34, 0xa9, 0x38, 0xb9, 0x28, 0x33, 0x29, 0x95, 0x34, 0xad, 0x06,
|
||||||
|
0x8c, 0x49, 0x6c, 0x60, 0xd7, 0x1b, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x25, 0x3b, 0x39, 0x24,
|
||||||
|
0xd4, 0x00, 0x00, 0x00,
|
||||||
|
}
|
14
vendor/ch4-04/03/pubsubservice/pubsubservice.proto
vendored
Normal file
14
vendor/ch4-04/03/pubsubservice/pubsubservice.proto
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package pubsubservice;
|
||||||
|
|
||||||
|
message String {
|
||||||
|
string value = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
service PubsubService {
|
||||||
|
rpc Publish (String) returns (String);
|
||||||
|
rpc Subscribe (String) returns (stream String);
|
||||||
|
}
|
||||||
|
|
||||||
|
//protoc --go_out=plugins=grpc:. pubsubservice.proto
|
70
vendor/ch4-04/03/server/server.go
vendored
Normal file
70
vendor/ch4-04/03/server/server.go
vendored
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/docker/pkg/pubsub"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
|
pb "ch4-04/03/pubsubservice"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PubsubService struct {
|
||||||
|
pub *pubsub.Publisher
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPubsubService() *PubsubService {
|
||||||
|
return &PubsubService{
|
||||||
|
pub: pubsub.NewPublisher(100*time.Millisecond, 10),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PubsubService) Publish(
|
||||||
|
ctx context.Context, arg *pb.String,
|
||||||
|
) (*pb.String, error) {
|
||||||
|
p.pub.Publish(arg.GetValue())
|
||||||
|
//debug
|
||||||
|
//reply := &String{Value: "<Publish> " + arg.GetValue()}
|
||||||
|
//fmt.Println(reply.GetValue())
|
||||||
|
return &pb.String{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PubsubService) Subscribe(
|
||||||
|
arg *pb.String, stream pb.PubsubService_SubscribeServer,
|
||||||
|
) error {
|
||||||
|
ch := p.pub.SubscribeTopic(func(v interface{}) bool {
|
||||||
|
if key, ok := v.(string); ok {
|
||||||
|
//debug
|
||||||
|
//fmt.Printf("<debug> %t %s %s %t\n",
|
||||||
|
// ok,arg.GetValue(),key,strings.HasPrefix(key,arg.GetValue()))
|
||||||
|
if strings.HasPrefix(key, arg.GetValue()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
for v := range ch {
|
||||||
|
if err := stream.Send(&pb.String{Value: v.(string)}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
grpcServer := grpc.NewServer()
|
||||||
|
pb.RegisterPubsubServiceServer(grpcServer, NewPubsubService())
|
||||||
|
|
||||||
|
lis, err := net.Listen("tcp", ":1234")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
grpcServer.Serve(lis)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user