選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

112 行
3.5 KiB

  1. package redis
  2. import (
  3. "context"
  4. "errors"
  5. "net"
  6. "time"
  7. "github.com/redis/go-redis/v9/internal/interfaces"
  8. "github.com/redis/go-redis/v9/push"
  9. )
  10. // ErrInvalidCommand is returned when an invalid command is passed to ExecuteCommand.
  11. var ErrInvalidCommand = errors.New("invalid command type")
  12. // ErrInvalidPool is returned when the pool type is not supported.
  13. var ErrInvalidPool = errors.New("invalid pool type")
  14. // newClientAdapter creates a new client adapter for regular Redis clients.
  15. func newClientAdapter(client *baseClient) interfaces.ClientInterface {
  16. return &clientAdapter{client: client}
  17. }
  18. // clientAdapter adapts a Redis client to implement interfaces.ClientInterface.
  19. type clientAdapter struct {
  20. client *baseClient
  21. }
  22. // GetOptions returns the client options.
  23. func (ca *clientAdapter) GetOptions() interfaces.OptionsInterface {
  24. return &optionsAdapter{options: ca.client.opt}
  25. }
  26. // GetPushProcessor returns the client's push notification processor.
  27. func (ca *clientAdapter) GetPushProcessor() interfaces.NotificationProcessor {
  28. return &pushProcessorAdapter{processor: ca.client.pushProcessor}
  29. }
  30. // optionsAdapter adapts Redis options to implement interfaces.OptionsInterface.
  31. type optionsAdapter struct {
  32. options *Options
  33. }
  34. // GetReadTimeout returns the read timeout.
  35. func (oa *optionsAdapter) GetReadTimeout() time.Duration {
  36. return oa.options.ReadTimeout
  37. }
  38. // GetWriteTimeout returns the write timeout.
  39. func (oa *optionsAdapter) GetWriteTimeout() time.Duration {
  40. return oa.options.WriteTimeout
  41. }
  42. // GetNetwork returns the network type.
  43. func (oa *optionsAdapter) GetNetwork() string {
  44. return oa.options.Network
  45. }
  46. // GetAddr returns the connection address.
  47. func (oa *optionsAdapter) GetAddr() string {
  48. return oa.options.Addr
  49. }
  50. // IsTLSEnabled returns true if TLS is enabled.
  51. func (oa *optionsAdapter) IsTLSEnabled() bool {
  52. return oa.options.TLSConfig != nil
  53. }
  54. // GetProtocol returns the protocol version.
  55. func (oa *optionsAdapter) GetProtocol() int {
  56. return oa.options.Protocol
  57. }
  58. // GetPoolSize returns the connection pool size.
  59. func (oa *optionsAdapter) GetPoolSize() int {
  60. return oa.options.PoolSize
  61. }
  62. // NewDialer returns a new dialer function for the connection.
  63. func (oa *optionsAdapter) NewDialer() func(context.Context) (net.Conn, error) {
  64. baseDialer := oa.options.NewDialer()
  65. return func(ctx context.Context) (net.Conn, error) {
  66. // Extract network and address from the options
  67. network := oa.options.Network
  68. addr := oa.options.Addr
  69. return baseDialer(ctx, network, addr)
  70. }
  71. }
  72. // pushProcessorAdapter adapts a push.NotificationProcessor to implement interfaces.NotificationProcessor.
  73. type pushProcessorAdapter struct {
  74. processor push.NotificationProcessor
  75. }
  76. // RegisterHandler registers a handler for a specific push notification name.
  77. func (ppa *pushProcessorAdapter) RegisterHandler(pushNotificationName string, handler interface{}, protected bool) error {
  78. if pushHandler, ok := handler.(push.NotificationHandler); ok {
  79. return ppa.processor.RegisterHandler(pushNotificationName, pushHandler, protected)
  80. }
  81. return errors.New("handler must implement push.NotificationHandler")
  82. }
  83. // UnregisterHandler removes a handler for a specific push notification name.
  84. func (ppa *pushProcessorAdapter) UnregisterHandler(pushNotificationName string) error {
  85. return ppa.processor.UnregisterHandler(pushNotificationName)
  86. }
  87. // GetHandler returns the handler for a specific push notification name.
  88. func (ppa *pushProcessorAdapter) GetHandler(pushNotificationName string) interface{} {
  89. return ppa.processor.GetHandler(pushNotificationName)
  90. }