github.heygears.com/openimsdk/tools@v0.0.49/discovery/zookeeper/config.go (about)

     1  // Copyright © 2023 OpenIM. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package zookeeper
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/go-zookeeper/zk"
    21  	"github.com/openimsdk/tools/errs"
    22  )
    23  
    24  type Config struct {
    25  	ZkServers []string
    26  	Scheme    string
    27  	Username  string
    28  	Password  string
    29  	Timeout   time.Duration
    30  }
    31  
    32  func (s *ZkClient) RegisterConf2Registry(key string, conf []byte) error {
    33  	path := s.getPath(key)
    34  
    35  	exists, _, err := s.conn.Exists(path)
    36  	if err != nil {
    37  		return errs.WrapMsg(err, "Exists failed", "path", path)
    38  	}
    39  
    40  	if exists {
    41  		if err := s.conn.Delete(path, 0); err != nil {
    42  			return errs.WrapMsg(err, "Delete failed", "path", path)
    43  		}
    44  	}
    45  	_, err = s.conn.Create(path, conf, 0, zk.WorldACL(zk.PermAll))
    46  	if err != nil && err != zk.ErrNodeExists {
    47  		return errs.WrapMsg(err, "Create failed", "path", path)
    48  	}
    49  	return nil
    50  }
    51  
    52  func (s *ZkClient) GetConfFromRegistry(key string) ([]byte, error) {
    53  	path := s.getPath(key)
    54  	bytes, _, err := s.conn.Get(path)
    55  	if err != nil {
    56  		return nil, errs.WrapMsg(err, "Get failed", "path", path)
    57  	}
    58  	return bytes, nil
    59  }