Use a RWMutex for server config locking instead of routing config gets and sets through Server#handler.

This commit is contained in:
Mikkel Krautz 2011-05-13 15:32:42 +02:00
parent 41f6af2334
commit c785c45166
5 changed files with 62 additions and 61 deletions

View file

@ -58,14 +58,13 @@ type Server struct {
udpsend chan *Message
voicebroadcast chan *VoiceBroadcast
freezeRequest chan *freezeRequest
configRequest chan *configRequest
// Signals to the server that a client has been successfully
// authenticated.
clientAuthenticated chan *Client
// Server configuration
cfg serverconf.Config
cfg *serverconf.Config
// Clients
clients map[uint32]*Client
@ -124,13 +123,6 @@ type freezeRequest struct {
readCloser io.ReadCloser
}
type configRequest struct {
done chan bool
set bool
key string
value string
}
// Allocate a new Murmur instance
func NewServer(id int64, addr string, port int) (s *Server, err os.Error) {
s = new(Server)
@ -140,7 +132,7 @@ func NewServer(id int64, addr string, port int) (s *Server, err os.Error) {
s.port = port
s.running = false
s.cfg = make(map[string]string)
s.cfg = serverconf.New(nil)
s.sessions = make(map[uint32]bool)
s.clients = make(map[uint32]*Client)
@ -155,7 +147,6 @@ func NewServer(id int64, addr string, port int) (s *Server, err os.Error) {
s.udpsend = make(chan *Message)
s.voicebroadcast = make(chan *VoiceBroadcast)
s.freezeRequest = make(chan *freezeRequest)
s.configRequest = make(chan *configRequest)
s.clientAuthenticated = make(chan *Client)
s.Channels = make(map[int]*Channel)
@ -394,10 +385,6 @@ func (server *Server) handler() {
}
go server.handleFreezeRequest(req, &fs)
// Synchronzied config get/set
case req := <-server.configRequest:
server.handleConfigRequest(req)
// Server registration update
// Tick every hour + a minute offset based on the server id.
case <-time.Tick((3600 + ((server.Id * 60) % 600)) * 1e9):
@ -433,15 +420,6 @@ func (server *Server) handleFreezeRequest(freq *freezeRequest, fs *frozenServer)
}
}
func (server *Server) handleConfigRequest(cfgReq *configRequest) {
if cfgReq.set {
server.cfg.Set(cfgReq.key, cfgReq.value)
} else {
cfgReq.value = server.cfg.StringValue(cfgReq.key)
}
cfgReq.done <- true
}
// Handle an Authenticate protobuf message. This is handled in a separate
// goroutine to allow for remote authenticators that are slow to respond.
//
@ -1075,22 +1053,6 @@ func (s *Server) FreezeServer() io.ReadCloser {
return fr.readCloser
}
// Set the value of a config key
func (s *Server) SetConfig(key string, value string) {
cfgReq := &configRequest{make(chan bool), true, key, value}
s.configRequest <- cfgReq
<-cfgReq.done
return
}
// Get the value of a config key
func (s *Server) GetConfig(key string) (value string) {
cfgReq := &configRequest{make(chan bool), false, key, ""}
s.configRequest <- cfgReq
<-cfgReq.done
return cfgReq.value
}
// Register a client on the server.
func (s *Server) RegisterClient(client *Client) (uid uint32) {
// Increment nextUserId only if registration succeeded.