Less memory allocations
This commit is contained in:
parent
e80115cf69
commit
e9a315574b
@ -48,7 +48,7 @@ type Daemon struct {
|
|||||||
hostname *string
|
hostname *string
|
||||||
motd *string
|
motd *string
|
||||||
passwords *string
|
passwords *string
|
||||||
clients map[*Client]bool
|
clients map[*Client]struct{}
|
||||||
clientAliveness map[*Client]*ClientAlivenessState
|
clientAliveness map[*Client]*ClientAlivenessState
|
||||||
rooms map[string]*Room
|
rooms map[string]*Room
|
||||||
roomSinks map[*Room]chan ClientEvent
|
roomSinks map[*Room]chan ClientEvent
|
||||||
@ -64,7 +64,7 @@ func NewDaemon(version string, hostname, motd, passwords *string, logSink chan<-
|
|||||||
motd: motd,
|
motd: motd,
|
||||||
passwords: passwords,
|
passwords: passwords,
|
||||||
}
|
}
|
||||||
daemon.clients = make(map[*Client]bool)
|
daemon.clients = make(map[*Client]struct{})
|
||||||
daemon.clientAliveness = make(map[*Client]*ClientAlivenessState)
|
daemon.clientAliveness = make(map[*Client]*ClientAlivenessState)
|
||||||
daemon.rooms = make(map[string]*Room)
|
daemon.rooms = make(map[string]*Room)
|
||||||
daemon.roomSinks = make(map[*Room]chan ClientEvent)
|
daemon.roomSinks = make(map[*Room]chan ClientEvent)
|
||||||
@ -303,8 +303,9 @@ func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (daemon *Daemon) Processor(events <-chan ClientEvent) {
|
func (daemon *Daemon) Processor(events <-chan ClientEvent) {
|
||||||
|
var now time.Time
|
||||||
for event := range events {
|
for event := range events {
|
||||||
now := time.Now()
|
now = time.Now()
|
||||||
client := event.client
|
client := event.client
|
||||||
|
|
||||||
// Check for clients aliveness
|
// Check for clients aliveness
|
||||||
@ -334,7 +335,7 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
|
|||||||
|
|
||||||
switch event.eventType {
|
switch event.eventType {
|
||||||
case EventNew:
|
case EventNew:
|
||||||
daemon.clients[client] = true
|
daemon.clients[client] = struct{}{}
|
||||||
daemon.clientAliveness[client] = &ClientAlivenessState{
|
daemon.clientAliveness[client] = &ClientAlivenessState{
|
||||||
pingSent: false,
|
pingSent: false,
|
||||||
timestamp: now,
|
timestamp: now,
|
||||||
|
16
events.go
16
events.go
@ -66,9 +66,12 @@ func Logger(logdir string, events <-chan LogEvent) {
|
|||||||
mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
||||||
perm := os.FileMode(0660)
|
perm := os.FileMode(0660)
|
||||||
var format string
|
var format string
|
||||||
|
var logfile string
|
||||||
|
var fd *os.File
|
||||||
|
var err error
|
||||||
for event := range events {
|
for event := range events {
|
||||||
logfile := path.Join(logdir, event.where)
|
logfile = path.Join(logdir, event.where)
|
||||||
fd, err := os.OpenFile(logfile, mode, perm)
|
fd, err = os.OpenFile(logfile, mode, perm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Can not open logfile", logfile, err)
|
log.Println("Can not open logfile", logfile, err)
|
||||||
continue
|
continue
|
||||||
@ -96,10 +99,13 @@ type StateEvent struct {
|
|||||||
// Room states shows that either topic or key has been changed
|
// Room states shows that either topic or key has been changed
|
||||||
// Each room's state is written to separate file in statedir
|
// Each room's state is written to separate file in statedir
|
||||||
func StateKeeper(statedir string, events <-chan StateEvent) {
|
func StateKeeper(statedir string, events <-chan StateEvent) {
|
||||||
|
var fn string
|
||||||
|
var data string
|
||||||
|
var err error
|
||||||
for event := range events {
|
for event := range events {
|
||||||
fn := path.Join(statedir, event.where)
|
fn = path.Join(statedir, event.where)
|
||||||
data := event.topic + "\n" + event.key + "\n"
|
data = event.topic + "\n" + event.key + "\n"
|
||||||
err := ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
|
err = ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Can not write statefile %s: %v", fn, err)
|
log.Printf("Can not write statefile %s: %v", fn, err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user