github.com/XiaoMi/Gaea@v1.2.5/models/slice.go (about)

     1  // Copyright 2019 The Gaea Authors. 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 models
    16  
    17  import "errors"
    18  
    19  // Slice means config model of slice
    20  type Slice struct {
    21  	Name            string   `json:"name"`
    22  	UserName        string   `json:"user_name"`
    23  	Password        string   `json:"password"`
    24  	Master          string   `json:"master"`
    25  	Slaves          []string `json:"slaves"`
    26  	StatisticSlaves []string `json:"statistic_slaves"`
    27  
    28  	Capacity    int `json:"capacity"`     // connection pool capacity
    29  	MaxCapacity int `json:"max_capacity"` // max connection pool capacity
    30  	IdleTimeout int `json:"idle_timeout"` // close backend direct connection after idle_timeout,unit: seconds
    31  }
    32  
    33  func (s *Slice) verify() error {
    34  	if s.Name == "" {
    35  		return errors.New("must specify slice name")
    36  	}
    37  
    38  	if s.UserName == "" {
    39  		return errors.New("missing user")
    40  	}
    41  
    42  	if s.Master == "" && len(s.Slaves) == 0 {
    43  		return errors.New("both master and slaves empty")
    44  	}
    45  
    46  	for _, slave := range s.Slaves {
    47  		if slave == "" {
    48  			return errors.New("illegal slave addr")
    49  		}
    50  	}
    51  
    52  	if s.Capacity <= 0 {
    53  		return errors.New("connection pool capacity should be > 0")
    54  	}
    55  
    56  	if s.MaxCapacity <= 0 {
    57  		return errors.New("max connection pool capactiy should be > 0")
    58  	}
    59  
    60  	return nil
    61  }