balance/implement/rand.go
2025-05-23 22:22:16 +08:00

48 lines
1010 B
Go

// Package dispatch...
//
// Description : dispatch...
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-04-01 5:58 下午
package implement
import (
"fmt"
"git.zhangdeman.cn/gateway/balance/abstract"
"git.zhangdeman.cn/zhangdeman/exception"
"math/rand"
"git.zhangdeman.cn/gateway/balance/define"
)
// NewRand ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 6:51 下午 2021/4/1
func NewRand() abstract.IBalance {
return &Rand{}
}
// Rand 随机选择
//
// Author : go_developer@163.com<张德满>
//
// Date : 6:01 下午 2021/4/1
type Rand struct {
}
// Get 获取 host + 端口
//
// Author : go_developer@163.com<张德满>
//
// Date : 6:01 下午 2021/4/1
func (r Rand) Get(nodeList []*define.SeverNode) (string, exception.IException) {
if len(nodeList) == 0 {
return "", exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空")
}
node := nodeList[rand.Intn(len(nodeList))]
return fmt.Sprintf("%s:%d", node.Host, node.Port), nil
}