@@ -26,7 +26,7 @@ type RedisConfig struct {
2626 Prefix string
2727}
2828
29- type redisPubSub struct {
29+ type RedisPubSub struct {
3030 prefix string
3131 bufferSize uint
3232
@@ -39,13 +39,13 @@ type redisPubSub struct {
3939 closeCh chan struct {}
4040}
4141
42- func NewRedis (config RedisConfig , opts ... Option ) (* redisPubSub , error ) {
42+ func NewRedis (config RedisConfig , opts ... Option ) (* RedisPubSub , error ) {
4343 if config .Prefix != "" && ! strings .HasSuffix (config .Prefix , ":" ) {
4444 config .Prefix += ":"
4545 }
4646
4747 if config .Client == nil && config .URL == "" {
48- return nil , fmt .Errorf ("no redis client or url provided" )
48+ return nil , fmt .Errorf ("%w: no redis client or url provided" , ErrInvalidConfig )
4949 }
5050
5151 client := config .Client
@@ -63,19 +63,21 @@ func NewRedis(config RedisConfig, opts ...Option) (*redisPubSub, error) {
6363 }
6464 o .apply (opts ... )
6565
66- return & redisPubSub {
66+ return & RedisPubSub {
6767 prefix : config .Prefix ,
6868 bufferSize : o .bufferSize ,
6969
7070 client : client ,
7171 ownedClient : config .Client == nil ,
7272
73+ wg : sync.WaitGroup {},
74+ mu : sync.Mutex {},
7375 subscribers : make (map [string ]context.CancelFunc ),
7476 closeCh : make (chan struct {}),
7577 }, nil
7678}
7779
78- func (r * redisPubSub ) Publish (ctx context.Context , topic string , data []byte ) error {
80+ func (r * RedisPubSub ) Publish (ctx context.Context , topic string , data []byte ) error {
7981 select {
8082 case <- r .closeCh :
8183 return ErrPubSubClosed
@@ -86,10 +88,14 @@ func (r *redisPubSub) Publish(ctx context.Context, topic string, data []byte) er
8688 return ErrInvalidTopic
8789 }
8890
89- return r .client .Publish (ctx , r .prefix + topic , data ).Err ()
91+ if err := r .client .Publish (ctx , r .prefix + topic , data ).Err (); err != nil {
92+ return fmt .Errorf ("failed to publish message: %w" , err )
93+ }
94+
95+ return nil
9096}
9197
92- func (r * redisPubSub ) Subscribe (ctx context.Context , topic string ) (* Subscription , error ) {
98+ func (r * RedisPubSub ) Subscribe (ctx context.Context , topic string ) (* Subscription , error ) {
9399 select {
94100 case <- r .closeCh :
95101 return nil , ErrPubSubClosed
@@ -104,7 +110,7 @@ func (r *redisPubSub) Subscribe(ctx context.Context, topic string) (*Subscriptio
104110 _ , err := ps .Receive (ctx )
105111 if err != nil {
106112 closeErr := ps .Close ()
107- return nil , errors .Join (fmt .Errorf ("can't subscribe: %w" , err ), closeErr )
113+ return nil , errors .Join (fmt .Errorf ("failed to subscribe: %w" , err ), closeErr )
108114 }
109115
110116 id := uuid .NewString ()
@@ -160,7 +166,7 @@ func (r *redisPubSub) Subscribe(ctx context.Context, topic string) (*Subscriptio
160166 return & Subscription {id : id , ctx : subCtx , cancel : cancel , ch : ch }, nil
161167}
162168
163- func (r * redisPubSub ) Close () error {
169+ func (r * RedisPubSub ) Close () error {
164170 select {
165171 case <- r .closeCh :
166172 return nil
@@ -171,10 +177,12 @@ func (r *redisPubSub) Close() error {
171177 r .wg .Wait ()
172178
173179 if r .ownedClient {
174- return r .client .Close ()
180+ if err := r .client .Close (); err != nil {
181+ return fmt .Errorf ("failed to close redis client: %w" , err )
182+ }
175183 }
176184
177185 return nil
178186}
179187
180- var _ PubSub = (* redisPubSub )(nil )
188+ var _ PubSub = (* RedisPubSub )(nil )
0 commit comments