mirror of
https://github.com/dragonheim/gagent.git
synced 2025-01-18 09:36:28 -08:00
Converting client side connection over to use net/http instead of zmq.
This commit is contained in:
parent
f543276b82
commit
16d40fd93a
5 changed files with 52 additions and 53 deletions
|
@ -1,10 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
ioutil "io/ioutil"
|
||||
log "log"
|
||||
http "net/http"
|
||||
os "os"
|
||||
sync "sync"
|
||||
time "time"
|
||||
|
@ -22,8 +20,6 @@ import (
|
|||
|
||||
uuid "github.com/jakehl/goid"
|
||||
cty "github.com/zclconf/go-cty/cty"
|
||||
|
||||
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -48,13 +44,13 @@ var exitCodes = struct {
|
|||
func main() {
|
||||
filter := &logutils.LevelFilter{
|
||||
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"},
|
||||
MinLevel: logutils.LogLevel("WARN"),
|
||||
MinLevel: logutils.LogLevel("DEBUG"),
|
||||
Writer: os.Stderr,
|
||||
}
|
||||
log.SetOutput(filter)
|
||||
|
||||
var config gs.GagentConfig
|
||||
var configFile string = "/etc/gagent/gagent.hcl"
|
||||
config.File = "/etc/gagent/gagent.hcl"
|
||||
|
||||
config.Name, _ = os.Hostname()
|
||||
config.Mode = "setup"
|
||||
|
@ -64,7 +60,6 @@ func main() {
|
|||
* This is used throughout the G'Agent system to uniquely identify this node.
|
||||
* It can be overridden in the configuration file by setting uuid
|
||||
*/
|
||||
// identity, _ := uuid.NewV5(uuid.NamespaceURL, []byte("gagent"+config.Name))
|
||||
identity := uuid.NewV4UUID()
|
||||
config.UUID = identity.String()
|
||||
|
||||
|
@ -127,6 +122,7 @@ func main() {
|
|||
usage += " --version -- Show version and exit \n"
|
||||
usage += " --config=<config> -- [default: /etc/gagent/gagent.hcl] \n"
|
||||
usage += " --agent=<file> -- filename of the agent to be uploaded to the G'Agent network \n"
|
||||
usage += "\n"
|
||||
|
||||
/*
|
||||
* Consume the usage variable and the command line arguments to create a
|
||||
|
@ -136,7 +132,7 @@ func main() {
|
|||
log.Printf("[DEBUG] Arguments are %v\n", opts)
|
||||
|
||||
if opts["--config"] != nil {
|
||||
configFile = opts["--config"].(string)
|
||||
config.File = opts["--config"].(string)
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -145,9 +141,9 @@ func main() {
|
|||
if opts["setup"] == true {
|
||||
config.Mode = "setup"
|
||||
} else {
|
||||
err := hclsimple.DecodeFile(configFile, nil, &config)
|
||||
err := hclsimple.DecodeFile(config.File, nil, &config)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Failed to load configuration file: %s.\n", configFile)
|
||||
log.Printf("[ERROR] Failed to load configuration file: %s.\n", config.File)
|
||||
log.Printf("[ERROR] %s\n", err)
|
||||
os.Exit(exitCodes.m["CONFIG_FILE_MISSING"])
|
||||
}
|
||||
|
@ -211,12 +207,8 @@ func main() {
|
|||
os.Exit(exitCodes.m["NO_WORKERS_DEFINED"])
|
||||
}
|
||||
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", config.ClientPort), nil)
|
||||
|
||||
wg.Add(1)
|
||||
go gr.Main(&wg, config)
|
||||
// select {}
|
||||
|
||||
case "worker":
|
||||
/*
|
||||
|
@ -232,17 +224,11 @@ func main() {
|
|||
os.Exit(exitCodes.m["NO_ROUTERS_DEFINED"])
|
||||
}
|
||||
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", config.ClientPort), nil)
|
||||
|
||||
for key := range config.Routers {
|
||||
wg.Add(1)
|
||||
go gw.Main(&wg, config, key)
|
||||
// time.Sleep(10 * time.Second)
|
||||
}
|
||||
|
||||
// select {}
|
||||
|
||||
case "setup":
|
||||
log.Printf("[INFO] Running in setup mode\n")
|
||||
f := hclwrite.NewEmptyFile()
|
||||
|
|
|
@ -18,7 +18,7 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig, rid int, agent string) {
|
|||
log.Printf("[INFO] Starting client\n")
|
||||
|
||||
// Generate connect string for this router.
|
||||
var rport = int64(config.ClientPort)
|
||||
var rport = config.ClientPort
|
||||
if config.Routers[rid].ClientPort != 0 {
|
||||
rport = config.Routers[rid].ClientPort
|
||||
}
|
||||
|
@ -41,7 +41,6 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig, rid int, agent string) {
|
|||
mu.Unlock()
|
||||
}()
|
||||
|
||||
// time.Sleep(10 * time.Millisecond)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
// for {
|
||||
|
|
|
@ -13,6 +13,7 @@ type GagentConfig struct {
|
|||
Routers []*RouterDetails `hcl:"router,block"`
|
||||
Workers []*WorkerDetails `hcl:"worker,block"`
|
||||
Version string
|
||||
File string
|
||||
}
|
||||
|
||||
// ClientDetails is details about known clients
|
||||
|
|
|
@ -21,6 +21,9 @@ const (
|
|||
// Main is the initiation function for a Router
|
||||
func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
|
||||
defer wg.Done()
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
http.HandleFunc("/hello", answerClient)
|
||||
|
||||
log.Printf("[INFO] Starting router\n")
|
||||
|
||||
clientSock, _ := zmq.NewSocket(zmq.ROUTER)
|
||||
|
@ -28,12 +31,14 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
|
|||
|
||||
workerSock, _ := zmq.NewSocket(zmq.DEALER)
|
||||
defer workerSock.Close()
|
||||
workerListener := fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.WorkerPort)
|
||||
clientListener := fmt.Sprintf("%s:%d", config.ListenAddr, config.ClientPort)
|
||||
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", config.ClientPort), nil)
|
||||
go func() {
|
||||
http.ListenAndServe(clientListener, nil)
|
||||
}()
|
||||
|
||||
// clientSock.Bind(fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.ClientPort))
|
||||
workerSock.Bind(fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.WorkerPort))
|
||||
workerSock.Bind(workerListener)
|
||||
|
||||
workers := make([]string, 0)
|
||||
|
||||
|
@ -42,7 +47,6 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
|
|||
|
||||
poller2 := zmq.NewPoller()
|
||||
poller2.Add(workerSock, zmq.POLLIN)
|
||||
// poller2.Add(clientSock, zmq.POLLIN)
|
||||
|
||||
LOOP:
|
||||
for {
|
||||
|
@ -59,11 +63,13 @@ LOOP:
|
|||
}
|
||||
for _, socket := range sockets {
|
||||
switch s := socket.Socket; s {
|
||||
case workerSock: // Handle worker activity on backend
|
||||
case workerSock:
|
||||
// Handle worker activity on backend
|
||||
// Use worker identity for load-balancing
|
||||
msg, err := s.RecvMessage(0)
|
||||
if err != nil {
|
||||
break LOOP // Interrupted
|
||||
// Interrupted
|
||||
break LOOP
|
||||
}
|
||||
var identity string
|
||||
identity, msg = unwrap(msg)
|
||||
|
@ -98,27 +104,29 @@ func unwrap(msg []string) (head string, tail []string) {
|
|||
return
|
||||
}
|
||||
|
||||
// func answerClient(w http.ResponseWriter, r *http.Request) {
|
||||
// if r.URL.Path != "/" {
|
||||
// http.NotFound(w, r)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // Common code for all requests can go here...
|
||||
//
|
||||
// switch r.Method {
|
||||
// case http.MethodGet:
|
||||
// // Handle the GET request...
|
||||
//
|
||||
// case http.MethodPost:
|
||||
// // Handle the POST request...
|
||||
//
|
||||
// case http.MethodOptions:
|
||||
// w.Header().Set("Allow", "GET, POST, OPTIONS")
|
||||
// w.WriteHeader(http.StatusNoContent)
|
||||
//
|
||||
// default:
|
||||
// w.Header().Set("Allow", "GET, POST, OPTIONS")
|
||||
// http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
// }
|
||||
// }
|
||||
func answerClient(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
fmt.Fprintf(w, "%v\n", r)
|
||||
// http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Common code for all requests can go here...
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
fmt.Fprintf(w, "%v\n", r)
|
||||
// Handle the GET request...
|
||||
|
||||
case http.MethodPost:
|
||||
// Handle the POST request...
|
||||
|
||||
case http.MethodOptions:
|
||||
w.Header().Set("Allow", "GET, POST, OPTIONS")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
|
||||
default:
|
||||
w.Header().Set("Allow", "GET, POST, OPTIONS")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package worker
|
|||
import (
|
||||
fmt "fmt"
|
||||
log "log"
|
||||
http "net/http"
|
||||
sync "sync"
|
||||
|
||||
gs "git.dragonheim.net/dragonheim/gagent/internal/gstructs"
|
||||
|
@ -26,6 +27,10 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig, rid int) {
|
|||
subscriber, _ := zmq.NewSocket(zmq.REP)
|
||||
defer subscriber.Close()
|
||||
|
||||
go func() {
|
||||
http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddr, config.ClientPort), nil)
|
||||
}()
|
||||
|
||||
log.Printf("[DEBUG] Attempting to connect to %s\n", connectString)
|
||||
subscriber.Connect(connectString)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue