Skip to content

Commit

Permalink
json format error bug make crash.Fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
devtw committed Mar 17, 2014
1 parent a5072d8 commit 7000f13
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 65 deletions.
Binary file modified GoHAProxy
Binary file not shown.
34 changes: 33 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"SrcPort": "9001",
"Mode":"tcp",
"Type":"Source",
"KeepAlive":60,
"KeepAlive":5,
"CheckTime":1,
"DstList":
[
Expand All @@ -66,6 +66,38 @@
"Check":true
}
]
},
{
"Name":"HealthTest",
"Src": "",
"SrcPort": "9003",
"Mode":"tcp",
"Type":"Health",
"CheckTime":1,
"DstList":
[
{
"Name":"MatisVM",
"Dst":"10.7.9.59",
"DstPort": "80",
"Weight":1,
"Check":true
},
{
"Name":"Outsite",
"Dst":"www.sbiec.com",
"DstPort": "80",
"Weight":2,
"Check":true
},
{
"Name":"DemoVM",
"Dst":"10.7.9.53",
"DstPort": "80",
"Weight":2,
"Check":true
}
]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion forwardServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ func (fs *ForwardServer) Listen(srvProxy Proxy) {
if fs.srvProxy.CheckTime == 0 {
fs.srvProxy.CheckTime = 5
}
fs.srvProxy.DstLen = len(fs.srvProxy.DstList)
if fs.srvProxy.Mode == "tcp" || fs.srvProxy.Mode == "http" || fs.srvProxy.Mode == "health" {
fs.srvProxy.DstLen = len(fs.srvProxy.DstList)
switch fs.srvProxy.Mode {
case "health":
go fs.Check()
Expand Down
125 changes: 63 additions & 62 deletions goHAProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ func main() {
}

fmt.Printf("GoHAProxy FileName:%s\n", *configInfo.FileName)

haConfig := loadConfigs()

for _, proxy := range haConfig.Configs.ProxyList {
FS := new(ForwardServer)
proxyServer.ServerList = append(proxyServer.ServerList, FS)
//proxyServer.ServerList[k] = FS
go FS.Listen(proxy)

ok,haConfig := loadConfigs()
if ok {
for _, proxy := range haConfig.Configs.ProxyList {
FS := new(ForwardServer)
proxyServer.ServerList = append(proxyServer.ServerList, FS)
//proxyServer.ServerList[k] = FS
go FS.Listen(proxy)
}
}

go Monitor()
for {
/*for k,proxy := range proxyServer.ServerList {
fmt.Printf("Proxy[%v]: %v\n", k,proxy.srvProxy)
}*/
configWatcher()
time.Sleep(500 * time.Millisecond)
}
Expand All @@ -77,72 +75,75 @@ func configWatcher() {
fmt.Printf("Config changed.Reolad.\n")
configInfo.Size = info.Size()
configInfo.ModTime = info.ModTime()
haConfig := loadConfigs()
lastkey := 0
//檢查有沒有移除掉的設定有的話就移除
for k, sProxy := range proxyServer.ServerList {
oldProxy := true
for _, proxy := range haConfig.Configs.ProxyList {
if *configInfo.Debug {
fmt.Printf("Delete PName:%s srvProxyName:%s \n", proxy.Name, sProxy.srvProxy.Name)
}
if proxy.Name == sProxy.srvProxy.Name {
oldProxy = false
break
}
}
if k > lastkey {
lastkey = k
}
if oldProxy {
if *configInfo.Debug {
fmt.Printf("Delete Proxy[%v]: %v\n", k, sProxy.srvProxy.Name)
}
sProxy.Stop()
//proxyServer.ServerList = append(proxyServer.ServerList[:k], proxyServer.ServerList[k+1:])
proxyServer.ServerList = proxyServer.ServerList[:k+copy(proxyServer.ServerList[k:], proxyServer.ServerList[k+1:])]
//proxyServer.ServerList = copy(proxyServer.ServerList[k:], proxyServer.ServerList[k+1:])

//delete(proxyServer.ServerList, k)
//time.Sleep(1 * time.Second)
}
}

//檢查有沒有新的設定
for _, proxy := range haConfig.Configs.ProxyList {
newProxy := true
ok,haConfig := loadConfigs()
if ok {
//檢查有沒有移除掉的設定有的話就移除
for k, sProxy := range proxyServer.ServerList {
if *configInfo.Debug {
fmt.Printf("Check Add PName[%d]:%s srvProxyName:%s \n", k, proxy.Name, sProxy.srvProxy.Name)
oldProxy := true
for _, proxy := range haConfig.Configs.ProxyList {
if *configInfo.Debug {
fmt.Printf("Delete PName:%s srvProxyName:%s \n", proxy.Name, sProxy.srvProxy.Name)
}
if proxy.Name == sProxy.srvProxy.Name {
oldProxy = false
break
}
}
if proxy.Name == sProxy.srvProxy.Name {
proxyServer.ServerList[k].Reload(proxy)
newProxy = false
break

if oldProxy {
if *configInfo.Debug {
fmt.Printf("Delete Proxy[%v]: %v\n", k, sProxy.srvProxy.Name)
}
sProxy.Stop()
//proxyServer.ServerList = append(proxyServer.ServerList[:k], proxyServer.ServerList[k+1:])
proxyServer.ServerList = proxyServer.ServerList[:k+copy(proxyServer.ServerList[k:], proxyServer.ServerList[k+1:])]
//proxyServer.ServerList = copy(proxyServer.ServerList[k:], proxyServer.ServerList[k+1:])

//delete(proxyServer.ServerList, k)
//time.Sleep(1 * time.Second)
}
}
if newProxy {
FS := new(ForwardServer)
proxyServer.ServerList = append(proxyServer.ServerList, FS)
//proxyServer.ServerList[lastkey+1] = FS
if *configInfo.Debug {
fmt.Printf("Add New Proxy: %v\n", proxy)

//檢查有沒有新的設定
for _, proxy := range haConfig.Configs.ProxyList {
newProxy := true
for k, sProxy := range proxyServer.ServerList {
if *configInfo.Debug {
fmt.Printf("Check Add PName[%d]:%s srvProxyName:%s \n", k, proxy.Name, sProxy.srvProxy.Name)
}
if proxy.Name == sProxy.srvProxy.Name {
proxyServer.ServerList[k].Reload(proxy)
newProxy = false
break
}
}
if newProxy {
FS := new(ForwardServer)
proxyServer.ServerList = append(proxyServer.ServerList, FS)
//proxyServer.ServerList[lastkey+1] = FS
if *configInfo.Debug {
fmt.Printf("Add New Proxy: %v\n", proxy)
}
go FS.Listen(proxy)
}
go FS.Listen(proxy)
}
}

}
defer file.Close()
}

func loadConfigs() HAConfig {
func loadConfigs() (bool,HAConfig) {
file, e := ioutil.ReadFile(*configInfo.FileName)
if e != nil {
fmt.Printf("Load GoHAProxy config error: %v\n", e)
os.Exit(1)
}
var haConfig HAConfig
json.Unmarshal(file, &haConfig)
return haConfig
err := json.Unmarshal(file, &haConfig)
if err != nil {
fmt.Printf("Config load error:%v \n",err)
return false,haConfig
}
return true,haConfig
}
6 changes: 5 additions & 1 deletion tpl/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
{{$proxy.Name}}
</td>
<td>
{{$proxy.SrcPort}}
{{if eq $proxy.Mode "health"}}
N/A
{{else}}
{{$proxy.SrcPort}}
{{end}}
</td>
<td>
{{$proxy.Mode}}
Expand Down

0 comments on commit 7000f13

Please sign in to comment.