package main
import (
"fmt"
"net"
"net/rpc"
"sync"
)
type SumInput struct {
UpTo int
}
type SumOutput struct {
Result int
}
type RpcServer struct {
}
func (s *RpcServer) Calculate(in *SumInput, out *SumOutput) error {
for i := 0; i < in.UpTo; i++ {
out.Result += i
}
return nil
}
func main() {
server := new(RpcServer)
rpc.Register(server)
sock, err := net.Listen("tcp", ":1234")
if err != nil {
panic(err)
}
go func() {
for {
conn, err := sock.Accept()
if err != nil {
panic(err)
}
go rpc.ServeConn(conn)
}
}()
wg := &sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go func(i int) {
client, err := rpc.Dial("tcp", "127.0.0.1:1234")
if err != nil {
panic(err)
}
rpcOut := &SumOutput{}
err = client.Call("RpcServer.Calculate", &SumInput{100000000}, rpcOut)
if err != nil {
panic(err)
}
fmt.Println("Got reply: ", rpcOut, i)
wg.Done()
}(i)
}
wg.Wait()
}
It starts an RPC server and 100 clients in parallel, but it never makes use of more than 1 CPUs, despite that GOMAXPROCS is properly configured.
So what was stopping it from using more CPUs? And how to improve the situation?