本文整理汇总了Golang中configuration.Configuration.HostnameOrDetect方法的典型用法代码示例。如果您正苦于以下问题:Golang Configuration.HostnameOrDetect方法的具体用法?Golang Configuration.HostnameOrDetect怎么用?Golang Configuration.HostnameOrDetect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类configuration.Configuration
的用法示例。
在下文中一共展示了Configuration.HostnameOrDetect方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewRaftServer
// Creates a new server.
func NewRaftServer(config *configuration.Configuration, clusterConfig *ClusterConfiguration) *RaftServer {
if !registeredCommands {
registeredCommands = true
for _, command := range internalRaftCommands {
raft.RegisterCommand(command)
}
}
s := &RaftServer{
host: config.HostnameOrDetect(),
port: config.RaftServerPort,
path: config.RaftDir,
clusterConfig: clusterConfig,
router: mux.NewRouter(),
config: config,
}
rand.Seed(time.Now().Unix())
// Read existing name or generate a new one.
if b, err := ioutil.ReadFile(filepath.Join(s.path, "name")); err == nil {
s.name = string(b)
} else {
s.name = fmt.Sprintf("%07x", rand.Int())[0:7]
if err = ioutil.WriteFile(filepath.Join(s.path, "name"), []byte(s.name), 0644); err != nil {
panic(err)
}
}
return s
}
示例2: NewRaftServer
// Creates a new server.
func NewRaftServer(config *configuration.Configuration, clusterConfig *cluster.ClusterConfiguration) *RaftServer {
if !registeredCommands {
registeredCommands = true
for _, command := range internalRaftCommands {
raft.RegisterCommand(command)
}
}
s := &RaftServer{
host: config.HostnameOrDetect(),
port: config.RaftServerPort,
path: config.RaftDir,
bind_address: config.BindAddress,
clusterConfig: clusterConfig,
notLeader: make(chan bool, 1),
router: mux.NewRouter(),
config: config,
}
// Read existing name or generate a new one.
if b, err := ioutil.ReadFile(filepath.Join(s.path, "name")); err == nil {
s.name = string(b)
} else {
var i uint64
if _, err := os.Stat("/dev/random"); err == nil {
log.Info("Using /dev/random to initialize the raft server name")
f, err := os.Open("/dev/random")
if err != nil {
panic(err)
}
defer f.Close()
readBytes := 0
b := make([]byte, 8)
for readBytes < 8 {
n, err := f.Read(b[readBytes:])
if err != nil {
panic(err)
}
readBytes += n
}
err = binary.Read(bytes.NewBuffer(b), binary.BigEndian, &i)
if err != nil {
panic(err)
}
} else {
log.Info("Using rand package to generate raft server name")
rand.Seed(time.Now().UnixNano())
i = uint64(rand.Int())
}
s.name = fmt.Sprintf("%07x", i)[0:7]
log.Info("Setting raft name to %s", s.name)
if err = ioutil.WriteFile(filepath.Join(s.path, "name"), []byte(s.name), 0644); err != nil {
panic(err)
}
}
return s
}