// Package balance... // // Description : balance... // // Author : go_developer@163.com<白茶清欢> // // Date : 2021-11-22 6:27 下午 package balance import ( "errors" "net/http" "sync" "git.zhangdeman.cn/zhangdeman/gopkg/util" "git.zhangdeman.cn/zhangdeman/gopkg/balance/define" ) // NewRandom ... // // Author : go_developer@163.com<白茶清欢> // // Date : 6:29 下午 2021/11/22 func NewRandom(severList []*define.ServerNode) (IBalance, error) { if nil == severList || len(severList) == 0 { return nil, errors.New("sever list is empty") } return &Random{ base{ lock: &sync.RWMutex{}, severList: make([]*define.ServerNode, 0), }, }, nil } // Random 随机负载均衡 // // Author : go_developer@163.com<白茶清欢> // // Date : 6:27 下午 2021/11/22 type Random struct { base } // GetServerNode ... // // Author : go_developer@163.com<白茶清欢> // // Date : 6:31 下午 2021/11/22 func (r *Random) GetServerNode(req *http.Request) (*define.ServerNode, error) { r.RLock() defer r.RUnlock() if len(r.severList) == 0 { return nil, errors.New("sever list is empty") } randomID := util.GetHashID(util.GenRandomString("", 128)) % uint64(len(r.severList)) return r.severList[int(randomID)], nil }