client_pool.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Author: simon (ynwdlxm@163.com)
  2. // Date: 2025/10/20 11:32
  3. // Desc: 客户端连接工厂
  4. package cluster
  5. import (
  6. "context"
  7. "errors"
  8. pool "github.com/jolestar/go-commons-pool/v2"
  9. client_ "github.com/runningwater/go-redis/resp/client"
  10. )
  11. type connectionFactory struct {
  12. Peer string // 节点地址
  13. }
  14. func (c connectionFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) {
  15. client, err := client_.NewClient(c.Peer)
  16. if err != nil {
  17. return nil, err
  18. }
  19. client.Start()
  20. return pool.NewPooledObject(client), nil
  21. }
  22. func (c connectionFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error {
  23. client, ok := object.Object.(*client_.Client)
  24. if !ok {
  25. return errors.New("type mismatch")
  26. }
  27. client.Stop()
  28. return nil
  29. }
  30. func (c connectionFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool {
  31. return true
  32. }
  33. func (c connectionFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error {
  34. return nil
  35. }
  36. func (c connectionFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error {
  37. return nil
  38. }