github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/cluster/clusterproviders/zk/zk_conn.go (about)

     1  package zk
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/go-zookeeper/zk"
     7  )
     8  
     9  type zkConn interface {
    10  	AddAuth(scheme string, auth []byte) error
    11  	Exists(path string) (bool, *zk.Stat, error)
    12  	Create(path string, data []byte, flags int32, acl []zk.ACL) (string, error)
    13  	Delete(path string, version int32) error
    14  	Get(path string) ([]byte, *zk.Stat, error)
    15  	Children(path string) ([]string, *zk.Stat, error)
    16  	ChildrenW(path string) ([]string, *zk.Stat, <-chan zk.Event, error)
    17  	CreateProtectedEphemeralSequential(path string, data []byte, acl []zk.ACL) (string, error)
    18  	Close()
    19  }
    20  
    21  func connectZk(servers []string, sessionTimeout time.Duration, opts ...zkConnOpt) (zkConn, error) {
    22  	opt := newZkOptions(opts...)
    23  	var conn *zk.Conn
    24  	var err error
    25  	if opt.ecb != nil {
    26  		conn, _, err = zk.Connect(servers, sessionTimeout, zk.WithEventCallback(opt.ecb))
    27  	} else {
    28  		conn, _, err = zk.Connect(servers, sessionTimeout)
    29  	}
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return &zkConnImpl{conn: conn}, nil
    34  }
    35  
    36  type zkConnImpl struct {
    37  	conn *zk.Conn
    38  }
    39  
    40  func (impl *zkConnImpl) AddAuth(scheme string, auth []byte) error {
    41  	return impl.conn.AddAuth(scheme, auth)
    42  }
    43  
    44  func (impl *zkConnImpl) Exists(path string) (bool, *zk.Stat, error) {
    45  	return impl.conn.Exists(path)
    46  }
    47  
    48  func (impl *zkConnImpl) Create(path string, data []byte, flags int32, acl []zk.ACL) (string, error) {
    49  	return impl.conn.Create(path, data, flags, acl)
    50  }
    51  
    52  func (impl *zkConnImpl) Delete(path string, version int32) error {
    53  	return impl.conn.Delete(path, version)
    54  }
    55  
    56  func (impl *zkConnImpl) Get(path string) ([]byte, *zk.Stat, error) {
    57  	return impl.conn.Get(path)
    58  }
    59  
    60  func (impl *zkConnImpl) Children(path string) ([]string, *zk.Stat, error) {
    61  	return impl.conn.Children(path)
    62  }
    63  
    64  func (impl *zkConnImpl) ChildrenW(path string) ([]string, *zk.Stat, <-chan zk.Event, error) {
    65  	return impl.conn.ChildrenW(path)
    66  }
    67  
    68  func (impl *zkConnImpl) CreateProtectedEphemeralSequential(path string, data []byte, acl []zk.ACL) (string, error) {
    69  	return impl.conn.CreateProtectedEphemeralSequential(path, data, acl)
    70  }
    71  
    72  func (impl *zkConnImpl) Close() {
    73  	impl.conn.Close()
    74  }
    75  
    76  type zkoption struct {
    77  	ecb zk.EventCallback
    78  }
    79  
    80  func newZkOptions(opts ...zkConnOpt) *zkoption {
    81  	opt := &zkoption{}
    82  	for _, fn := range opts {
    83  		fn(opt)
    84  	}
    85  	return opt
    86  }
    87  
    88  type zkConnOpt func(*zkoption)
    89  
    90  func WithEventCallback(cb zk.EventCallback) zkConnOpt {
    91  	return func(o *zkoption) {
    92  		o.ecb = cb
    93  	}
    94  }