refactor: pass custom log to pubsub

This commit is contained in:
edwin 2024-06-04 22:46:56 +08:00
parent f7b3619c8b
commit 58f023a8ef
1 changed files with 15 additions and 6 deletions

View File

@ -470,6 +470,12 @@ func (c *PubSub) ChannelWithSubscriptions(_ context.Context, size int) <-chan in
type ChannelOption func(c *channel) type ChannelOption func(c *channel)
func WithLogger(logger internal.Logging) ChannelOption {
return func(c *channel) {
c.logger = logger
}
}
// WithChannelSize specifies the Go chan size that is used to buffer incoming messages. // WithChannelSize specifies the Go chan size that is used to buffer incoming messages.
// //
// The default is 100 messages. // The default is 100 messages.
@ -510,6 +516,8 @@ type channel struct {
chanSize int chanSize int
chanSendTimeout time.Duration chanSendTimeout time.Duration
checkInterval time.Duration checkInterval time.Duration
logger internal.Logging
} }
func newChannel(pubSub *PubSub, opts ...ChannelOption) *channel { func newChannel(pubSub *PubSub, opts ...ChannelOption) *channel {
@ -519,6 +527,7 @@ func newChannel(pubSub *PubSub, opts ...ChannelOption) *channel {
chanSize: 100, chanSize: 100,
chanSendTimeout: time.Minute, chanSendTimeout: time.Minute,
checkInterval: 3 * time.Second, checkInterval: 3 * time.Second,
logger: internal.Logger,
} }
for _, opt := range opts { for _, opt := range opts {
opt(c) opt(c)
@ -602,12 +611,12 @@ func (c *channel) initMsgChan() {
<-timer.C <-timer.C
} }
case <-timer.C: case <-timer.C:
internal.Logger.Printf( c.logger.Printf(
ctx, "redis: %s channel is full for %s (message is dropped)", ctx, "redis: %+v channel is full for %s (message is dropped)",
c, c.chanSendTimeout) c, c.chanSendTimeout)
} }
default: default:
internal.Logger.Printf(ctx, "redis: unknown message type: %T", msg) c.logger.Printf(ctx, "redis: unknown message type: %T", msg)
} }
} }
}() }()
@ -656,12 +665,12 @@ func (c *channel) initAllChan() {
<-timer.C <-timer.C
} }
case <-timer.C: case <-timer.C:
internal.Logger.Printf( c.logger.Printf(
ctx, "redis: %s channel is full for %s (message is dropped)", ctx, "redis: %+v channel is full for %s (message is dropped)",
c, c.chanSendTimeout) c, c.chanSendTimeout)
} }
default: default:
internal.Logger.Printf(ctx, "redis: unknown message type: %T", msg) c.logger.Printf(ctx, "redis: unknown message type: %T", msg)
} }
} }
}() }()