123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package util
- import (
- "app.yhyue.com/BP/queued/proto"
- "context"
- "google.golang.org/grpc"
- "log"
- "time"
- )
- /*
- 任务发布封装
- */
- type Publisher struct {
- Id string
- QueueServer string
- Channel string
- C <-chan *proto.PubReq
- }
- //
- func NewPublisher(server string, channel string, ch <-chan *proto.PubReq) *Publisher {
- uuid, _ := makeUuid()
- return &Publisher{
- Id: uuid,
- C: ch,
- QueueServer: server,
- Channel: channel,
- }
- }
- //
- func (pb *Publisher) Run() {
- for {
- conn, err := grpc.Dial(pb.QueueServer, grpc.WithInsecure())
- if err != nil {
- time.Sleep(1 * time.Second)
- log.Fatalf("did not connect: %v", err)
- }
- c := proto.NewQueueServiceClient(conn)
- stream, _ := c.Publish(context.Background())
- //
- Lab:
- for {
- select {
- case msg := <-pb.C:
- uuid, _ := makeUuid()
- msg.Sender = pb.Id
- msg.Id = uuid
- msg.ChannelId = pb.Channel
- err := stream.Send(msg)
- if err != nil {
- break Lab
- }
- }
- }
- _ = conn.Close()
- time.Sleep(3 * time.Second)
- }
- }
|