github.com/wfusion/gofusion@v1.1.14/mq/redis.go (about)

     1  package mq
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/wfusion/gofusion/common/infra/watermill"
     9  	"github.com/wfusion/gofusion/config"
    10  	"github.com/wfusion/gofusion/redis"
    11  
    12  	rdsDrv "github.com/redis/go-redis/v9"
    13  
    14  	millRds "github.com/wfusion/gofusion/common/infra/watermill/pubsub/redis"
    15  )
    16  
    17  func newRedis(ctx context.Context, appName, name string, conf *Conf, logger watermill.LoggerAdapter) (
    18  	pub Publisher, sub Subscriber) {
    19  
    20  	cli := redis.Use(ctx, conf.Endpoint.Instance, redis.AppName(appName))
    21  
    22  	if conf.Producer {
    23  		pub = newRedisPublisher(ctx, appName, name, conf, logger, cli)
    24  	}
    25  
    26  	if conf.Consumer {
    27  		sub = newRedisSubscriber(ctx, appName, name, conf, logger, cli)
    28  	}
    29  
    30  	return
    31  }
    32  
    33  type redisPublisher struct {
    34  	*abstractMQ
    35  	publisher *millRds.Publisher
    36  }
    37  
    38  func newRedisPublisher(ctx context.Context, appName, name string, conf *Conf, logger watermill.LoggerAdapter,
    39  	cli rdsDrv.UniversalClient) Publisher {
    40  	cfg := millRds.PublisherConfig{
    41  		Client:                cli,
    42  		Marshaller:            millRds.DefaultMarshallerUnmarshaller{AppID: config.Use(appName).AppName()},
    43  		Maxlens:               nil,
    44  		DisableRedisConnClose: true,
    45  	}
    46  
    47  	pub, err := millRds.NewPublisher(cfg, logger)
    48  	if err != nil {
    49  		panic(errors.Wrapf(err, "initialize mq component redis publisher failed: %s", err))
    50  	}
    51  
    52  	return &redisPublisher{
    53  		abstractMQ: newPub(ctx, pub, appName, name, conf, logger),
    54  		publisher:  pub,
    55  	}
    56  }
    57  
    58  func (r *redisPublisher) close() (err error) {
    59  	return r.publisher.Close()
    60  }
    61  
    62  type redisSubscriber struct {
    63  	*abstractMQ
    64  	subscriber *millRds.Subscriber
    65  }
    66  
    67  func newRedisSubscriber(ctx context.Context, appName, name string, conf *Conf, logger watermill.LoggerAdapter,
    68  	cli rdsDrv.UniversalClient) Subscriber {
    69  	cfg := millRds.SubscriberConfig{
    70  		Client:                    cli,
    71  		Unmarshaller:              millRds.DefaultMarshallerUnmarshaller{AppID: config.Use(appName).AppName()},
    72  		Consumer:                  "",
    73  		ConsumerGroup:             conf.ConsumerGroup,
    74  		NackResendSleep:           0,
    75  		BlockTime:                 0,
    76  		ClaimInterval:             0,
    77  		ClaimBatchSize:            0,
    78  		MaxIdleTime:               0,
    79  		CheckConsumersInterval:    0,
    80  		ConsumerTimeout:           0,
    81  		OldestId:                  "",
    82  		ShouldClaimPendingMessage: nil,
    83  		DisableRedisConnClose:     true,
    84  	}
    85  
    86  	sub, err := millRds.NewSubscriber(cfg, logger)
    87  	if err != nil {
    88  		panic(errors.Wrapf(err, "initialize mq component redis subscriber failed: %s", err))
    89  	}
    90  
    91  	return &redisSubscriber{
    92  		abstractMQ: newSub(ctx, sub, appName, name, conf, logger),
    93  		subscriber: sub,
    94  	}
    95  }
    96  
    97  func (r *redisSubscriber) close() (err error) {
    98  	return r.subscriber.Close()
    99  }