Converting client side connection over to use net/http instead of zmq.

This commit is contained in:
James Wells 2021-10-15 11:27:38 -07:00
parent f543276b82
commit 16d40fd93a
Signed by: jwells
GPG key ID: 73196D10B8E65666
5 changed files with 52 additions and 53 deletions

View file

@ -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 {

View file

@ -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

View file

@ -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)
}
}

View file

@ -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)