fix: (issues/3), Daemonize the router.

This commit is contained in:
James Wells 2021-02-26 08:14:28 -08:00
parent 9de01210ab
commit a871ad8441
Signed by: jwells
GPG key ID: 73196D10B8E65666
4 changed files with 68 additions and 61 deletions

View file

@ -5,8 +5,8 @@ type GagentConfig struct {
Name string `hcl:"name,optional"`
Mode string `hcl:"mode,attr"`
UUID string `hcl:"uuid,optional"`
ListenAddr string `hc:"listenaddr,optional"`
ListenPort int `hc:"listenport,optional"`
ListenAddr string `hcl:"address,optional"`
ListenPort int `hcl:"port,optional"`
Clients []*ClientDetails `hcl:"client,block"`
Routers []*RouterDetails `hcl:"router,block"`
Workers []*WorkerDetails `hcl:"worker,block"`

View file

@ -23,41 +23,8 @@ func pop(msg []string) (head, tail []string) {
return
}
// Main is the initiation function for a Router
func Main(config gs.GagentConfig) {
/*
* This is our router task.
*
* It uses the multi-threaded server model to deal requests out to a
* pool of workers and route replies back to clients. One worker can
* handle one request at a time but one client can talk to multiple
* workers at once.
*
* Frontend socket talks to clients over TCP
*/
frontend, _ := zmq.NewSocket(zmq.ROUTER)
defer frontend.Close()
frontend.Bind(fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.ListenPort))
// Backend socket talks to workers over inproc
backend, _ := zmq.NewSocket(zmq.DEALER)
defer backend.Close()
backend.Bind("inproc://backend")
// Launch pool of worker threads, precise number is not critical
for i := 0; i < 5; i++ {
go agentRouter(i)
}
// Connect backend to frontend via a proxy
err := zmq.Proxy(frontend, backend, nil)
log.Fatalln("Proxy interrupted:", err)
}
// Each worker task works on one request at a time and sends a random number
// Each router task works on one request at a time and sends a random number
// of replies back, with random delays between replies:
func agentRouter(workerNum int) {
interp := picol.InitInterp()
interp.RegisterCoreCommands()
@ -77,9 +44,44 @@ func agentRouter(workerNum int) {
// Sleep for some fraction of a second
time.Sleep(time.Duration(rand.Intn(1000)+1) * time.Millisecond)
fmt.Println(fmt.Sprintf("Worker %d: %s", workerNum, identity))
fmt.Println(fmt.Sprintf("Worker %d: %s", workerNum, content))
log.Printf("Worker %d: %s\n", workerNum, identity)
log.Printf("Worker %d: %s\n", workerNum, content)
worker.SendMessage(identity, content)
}
}
}
// Main is the initiation function for a Router
func Main(config gs.GagentConfig) {
/*
* This is our router task.
*
* It uses the multi-threaded server model to deal requests out to a
* pool of workers and route replies back to clients. One worker can
* handle one request at a time but one client can talk to multiple
* workers at once.
*
* Frontend socket talks to clients over TCP
*/
frontend, _ := zmq.NewSocket(zmq.ROUTER)
log.Printf("Starting router\n")
defer frontend.Close()
frontend.Bind(fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.ListenPort))
// Backend socket talks to workers over inproc
backend, _ := zmq.NewSocket(zmq.DEALER)
defer backend.Close()
backend.Bind("inproc://backend")
// Launch pool of worker threads, precise number is not critical
for i := 0; i < 5; i++ {
go agentRouter(i)
}
// Connect backend to frontend via a proxy
err := zmq.Proxy(frontend, backend, nil)
log.Fatalln("Proxy interrupted:", err)
}

View file

@ -27,6 +27,8 @@ func pop(msg []string) (head, tail []string) {
func Main(config gs.GagentConfig) {
// Frontend socket talks to clients over TCP
frontend, _ := zmq.NewSocket(zmq.ROUTER)
fmt.Printf("Running in worker mode\n")
defer frontend.Close()
connectString := fmt.Sprintf("tcp://%s", config.Routers[0].RouterAddr)
frontend.Bind(connectString)