add locks to map access

main
Ben Grewell 3 years ago
parent f27c73f37e
commit 3c08964b38

@ -3,7 +3,7 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="fc2840de-29dc-4fca-8e0e-a283562f60ca" name="Default Changelist" comment=""> <list default="true" id="fc2840de-29dc-4fca-8e0e-a283562f60ca" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/client.go" beforeDir="false" afterPath="$PROJECT_DIR$/client.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/controller.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"sync"
"time" "time"
api "github.com/BGrewell/go-iperf/api/go" api "github.com/BGrewell/go-iperf/api/go"
@ -16,6 +17,8 @@ func NewController(port int) (controller *Controller, err error) {
Port: port, Port: port,
clients: make(map[string]*Client), clients: make(map[string]*Client),
servers: make(map[string]*Server), servers: make(map[string]*Server),
clientLock: sync.Mutex{},
serverLock: sync.Mutex{},
} }
err = c.startListener() err = c.startListener()
@ -36,6 +39,8 @@ type Controller struct {
api.UnimplementedCommandServer api.UnimplementedCommandServer
Port int Port int
cmdClient api.CommandClient cmdClient api.CommandClient
clientLock sync.Mutex
serverLock sync.Mutex
clients map[string]*Client clients map[string]*Client
servers map[string]*Server servers map[string]*Server
} }
@ -52,7 +57,9 @@ func (c *Controller) GrpcRequestServer(context.Context, *api.StartServerRequest)
return nil, err return nil, err
} }
c.serverLock.Lock()
c.servers[srv.Id] = srv c.servers[srv.Id] = srv
c.serverLock.Unlock()
reply := &api.StartServerResponse{ reply := &api.StartServerResponse{
Id: srv.Id, Id: srv.Id,
@ -88,13 +95,17 @@ func (c *Controller) NewServer() (server *Server, err error) {
freePort, err := GetUnusedTcpPort() freePort, err := GetUnusedTcpPort()
s := NewServer() s := NewServer()
s.SetPort(freePort) s.SetPort(freePort)
c.serverLock.Lock()
c.servers[s.Id] = s c.servers[s.Id] = s
c.serverLock.Unlock()
return s, nil return s, nil
} }
// StopServer shuts down an iperf server and frees any actively used resources // StopServer shuts down an iperf server and frees any actively used resources
func (c *Controller) StopServer(id string) (err error) { func (c *Controller) StopServer(id string) (err error) {
c.serverLock.Lock()
delete(c.servers, id) delete(c.servers, id)
c.serverLock.Unlock()
return nil return nil
} }
@ -114,14 +125,18 @@ func (c *Controller) NewClient(serverAddr string) (client *Client, err error) {
cli := NewClient(serverAddr) cli := NewClient(serverAddr)
cli.SetPort(srvPort) cli.SetPort(srvPort)
c.clientLock.Lock()
c.clients[cli.Id] = cli c.clients[cli.Id] = cli
c.clientLock.Unlock()
return cli, nil return cli, nil
} }
// StopClient will clean up the server side connection and shut down any actively used resources // StopClient will clean up the server side connection and shut down any actively used resources
func (c *Controller) StopClient(id string) (err error) { func (c *Controller) StopClient(id string) (err error) {
c.clientLock.Lock()
delete(c.clients, id) delete(c.clients, id)
c.clientLock.Unlock()
return nil return nil
} }

Loading…
Cancel
Save