summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Matveev <stargrave@stargrave.org>2015-10-06 10:59:49 +0300
committerSergey Matveev <stargrave@stargrave.org>2015-10-06 11:31:11 +0300
commite9a315574b96c6a235c9d55e607c3b4526eedf2f (patch)
tree1b4e08fcd2c5cc5232372a29bd7f34237e652cf4
parente80115cf6921d4fc2d64b3b809c76a5019f82309 (diff)
downloadgoircd-e9a315574b96c6a235c9d55e607c3b4526eedf2f.tar.xz
Less memory allocations
-rw-r--r--daemon.go17
-rw-r--r--events.go16
2 files changed, 20 insertions, 13 deletions
diff --git a/daemon.go b/daemon.go
index 30d8c5d..b3fed28 100644
--- a/daemon.go
+++ b/daemon.go
@@ -48,7 +48,7 @@ type Daemon struct {
hostname *string
motd *string
passwords *string
- clients map[*Client]bool
+ clients map[*Client]struct{}
clientAliveness map[*Client]*ClientAlivenessState
rooms map[string]*Room
roomSinks map[*Room]chan ClientEvent
@@ -59,12 +59,12 @@ type Daemon struct {
func NewDaemon(version string, hostname, motd, passwords *string, logSink chan<- LogEvent, stateSink chan<- StateEvent) *Daemon {
daemon := Daemon{
- version: version,
- hostname: hostname,
- motd: motd,
+ version: version,
+ hostname: hostname,
+ motd: motd,
passwords: passwords,
}
- daemon.clients = make(map[*Client]bool)
+ daemon.clients = make(map[*Client]struct{})
daemon.clientAliveness = make(map[*Client]*ClientAlivenessState)
daemon.rooms = make(map[string]*Room)
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) {
+ var now time.Time
for event := range events {
- now := time.Now()
+ now = time.Now()
client := event.client
// Check for clients aliveness
@@ -334,9 +335,9 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
switch event.eventType {
case EventNew:
- daemon.clients[client] = true
+ daemon.clients[client] = struct{}{}
daemon.clientAliveness[client] = &ClientAlivenessState{
- pingSent: false,
+ pingSent: false,
timestamp: now,
}
case EventDel:
diff --git a/events.go b/events.go
index 843e8b5..44974b8 100644
--- a/events.go
+++ b/events.go
@@ -66,9 +66,12 @@ func Logger(logdir string, events <-chan LogEvent) {
mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
perm := os.FileMode(0660)
var format string
+ var logfile string
+ var fd *os.File
+ var err error
for event := range events {
- logfile := path.Join(logdir, event.where)
- fd, err := os.OpenFile(logfile, mode, perm)
+ logfile = path.Join(logdir, event.where)
+ fd, err = os.OpenFile(logfile, mode, perm)
if err != nil {
log.Println("Can not open logfile", logfile, err)
continue
@@ -96,10 +99,13 @@ type StateEvent struct {
// Room states shows that either topic or key has been changed
// Each room's state is written to separate file in statedir
func StateKeeper(statedir string, events <-chan StateEvent) {
+ var fn string
+ var data string
+ var err error
for event := range events {
- fn := path.Join(statedir, event.where)
- data := event.topic + "\n" + event.key + "\n"
- err := ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
+ fn = path.Join(statedir, event.where)
+ data = event.topic + "\n" + event.key + "\n"
+ err = ioutil.WriteFile(fn, []byte(data), os.FileMode(0660))
if err != nil {
log.Printf("Can not write statefile %s: %v", fn, err)
}