Skip to content

Commit

Permalink
perf: Skip writes if -editable is not set, and app is set to unicast …
Browse files Browse the repository at this point in the history
…mode. Closes #464
  • Loading branch information
lo5 committed Oct 25, 2021
1 parent b89a81f commit 5703c73
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
13 changes: 12 additions & 1 deletion broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Sub struct {
// Broker represents a message broker.
type Broker struct {
site *Site
editable bool
clients map[string]map[*Client]interface{} // route => client-set
publish chan Pub
subscribe chan Sub
Expand All @@ -70,9 +71,10 @@ type Broker struct {
appsMux sync.RWMutex // mutex for tracking apps
}

func newBroker(site *Site) *Broker {
func newBroker(site *Site, editable bool) *Broker {
return &Broker{
site,
editable,
make(map[string]map[*Client]interface{}),
make(chan Pub, 1024), // TODO tune
make(chan Sub, 1024), // TODO tune
Expand Down Expand Up @@ -146,6 +148,15 @@ func parseMsg(s []byte) Msg {
// patch broadcasts changes to clients and patches site data.
func (b *Broker) patch(route string, data []byte) {
b.publish <- Pub{route, data}

// Skip writes if the route belongs to an app set to unicast mode, and -editable is not set.
if !b.editable {
app := b.getApp(route)
if app != nil && app.mode == unicastMode {
return
}
}

// Write AOF entry with patch marker "*" as-is to log file.
// FIXME bufio.Scanner.Scan() is not reliable if line length > 65536 chars,
// so reading back in is unreliable.
Expand Down
3 changes: 3 additions & 0 deletions pkg/keychain/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func (kc *Keychain) Len() int {
}

func newLruCache(size int) (*lru.Cache, error) {
if size < 8 {
size = 8
}
cache, err := lru.New(size)
if err != nil {
return nil, fmt.Errorf("failed creating keychain LRU cache: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Run(conf ServerConf) {
initSite(site, conf.Init)
}

broker := newBroker(site)
broker := newBroker(site, conf.Editable)
go broker.run()

if conf.Debug {
Expand Down

0 comments on commit 5703c73

Please sign in to comment.