Running multiple instances of servers on different hosts

Hi, the problem is this, I start the gRPC server by passing the values in the host port structure to the NewServerStart (conf.ScanConnect.ScanServerSec.GRPC) function, everything works as it should, but I cannot figure out how to start multiple server instances depending on the passed host value , port, let’s say I pass the values of several hosts in the array

scanServerConn := [3]config.GRPCConfig{conf.ScanConnect.ScanServerFir.GRPC,
conf.ScanConnect.ScanServerSec.GRPC, conf.ScanConnect.ScanServerThi.GRPC}
for _, servConn := range scanServerConn {
fmt.Println(servConn)
if err := NewServerStart(conf.ScanConnect.ScanServerSec.GRPC); err != nil {
log.Fatalf(“Can’t run server: %s”, err)
}
}

it is clear that this will not work, the loop will perform one iteration, how can I make sure that everything is executed?, of course there is an option that I would not like to do, rewrite several copies of the servers and hard-code each of them a host and port, please tell me how you can avoid to make it more convenient? and start multiple servers with one function
here is all the code
https://github.com/8n0kkw1n/testcmd/pull/1/files

Start individual servers in go routines, if you really need the returned errors use channels to send them back into the go routine that started them.

Thanks for the explanation, so I will

I don’t understand something anyway, I run it in a goroutine, but nothing happens, something I’m doing wrong
scanServerConn := [3]config.GRPCConfig{conf.ScanConnect.ScanServerFir.GRPC, conf.ScanConnect.ScanServerSec.GRPC, conf.ScanConnect.ScanServerThi.GRPC}
for _, servConn := range scanServerConn {
go func() {
err := NewServerStart(servConn)
if err != nil {
log.Fatalf(“Can’t run server: %s”, err)
}
}()
}

Can you please use code blocks to make everything more readable?

Though it looks as if now you are just exiting after all go routine have started. You need to block after they have been started. It is common to use waitgroups, or messages with the returned error or other means. As your semantics require it.

I’d probably just sleep in the main routine and restart on error in individual go routines.

Sorry, I could, but markdown somehow does not work properly and divides the code into two parts, into plain text and into code

Thanks for the information I will try to use
sync.WaitGroup

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.