Skip to content

Commit

Permalink
✂️ Simplify config loading
Browse files Browse the repository at this point in the history
No point in using mmap here, since our config file is likely smaller than a single memory page.

We also used mmap wrong by keeping a reference to the mapped region after unmap. This clearly fixes that.
  • Loading branch information
database64128 committed Nov 18, 2023
1 parent 09bd4c0 commit afb5efc
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 206 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ require (
gopkg.in/telebot.v3 v3.1.4
)

require (
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.14.0
)
require go.uber.org/multierr v1.10.0 // indirect
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,6 @@ golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
22 changes: 0 additions & 22 deletions mmap/mmap_generic.go

This file was deleted.

35 changes: 0 additions & 35 deletions mmap/mmap_test.go

This file was deleted.

25 changes: 0 additions & 25 deletions mmap/mmap_unix_fast.go

This file was deleted.

27 changes: 0 additions & 27 deletions mmap/mmap_unix_generic.go

This file was deleted.

53 changes: 0 additions & 53 deletions mmap/mmap_unixwindows.go

This file was deleted.

25 changes: 0 additions & 25 deletions mmap/mmap_windows.go

This file was deleted.

19 changes: 6 additions & 13 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"os"

"github.com/database64128/cubic-rce-bot/mmap"
"go.uber.org/zap"
tele "gopkg.in/telebot.v3"
)
Expand All @@ -25,26 +24,20 @@ type Runner struct {
}

func (r *Runner) loadConfig() error {
content, err := mmap.ReadFile[string](r.configPath)
f, err := os.Open(r.configPath)
if err != nil {
return err
}
defer mmap.Unmap(content)

if content == r.cachedConfigContent {
return nil
return fmt.Errorf("failed to open config file: %w", err)
}
defer f.Close()

var config Config
sr := strings.NewReader(content)
d := json.NewDecoder(sr)
d := json.NewDecoder(f)
d.DisallowUnknownFields()
if err = d.Decode(&config); err != nil {
return err
return fmt.Errorf("failed to decode config file: %w", err)
}

r.Config = config
r.cachedConfigContent = content
r.Handler.ReplaceUserCommandsByID(config.UserCommandsByID())
return nil
}
Expand Down

0 comments on commit afb5efc

Please sign in to comment.