Skip to content

Commit

Permalink
Remove globals from heffalump/*, add NewDefault{Heffalump,MarkovMap}()
Browse files Browse the repository at this point in the history
This removes globals from `heffalumpt/`, which are hard to reason about,
easy to get wrong, and should not be created if they are never used.

A possible drawbacks is if you would create multiple new defaults,
but this should never be the case.
  • Loading branch information
ginger51011 committed Jan 19, 2024
1 parent 5f25eb5 commit 4b8f080
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
12 changes: 7 additions & 5 deletions heffalump/heffalump.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (

var log = config.GetLogger()

// DefaultHeffalump represents a Heffalump type
var (
DefaultHeffalump *Heffalump
DefaultBuffSize int = 100 * 1 << 10
)
const DefaultBuffSize = 100 * 1 << 10

// Heffalump represents our buffer pool and markov map from Heffalump
type Heffalump struct {
Expand All @@ -39,6 +35,12 @@ func NewHeffalump(mm MarkovMap, buffsize int) *Heffalump {
}
}

// NewDefaultHeffalump instantiates a new default Heffalump from a MarkovMap created using
// using the default source text.
func NewDefaultHeffalump() *Heffalump {
return NewHeffalump(NewDefaultMarkovMap(), DefaultBuffSize)
}

// WriteHell writes markov chain heffalump hell to the provided io.Writer
func (h *Heffalump) WriteHell(bw *bufio.Writer) (int64, error) {
var n int64
Expand Down
9 changes: 3 additions & 6 deletions heffalump/markov.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"git.tcp.direct/kayos/common/squish"
)

var DefaultMarkovMap MarkovMap

func init() {
// DefaultMarkovMap is a Markov chain based on src.
// NewDefaultMarkovMap creates a new MarkovMap from the default source text.
func NewDefaultMarkovMap() MarkovMap {
src, err := squish.UnpackStr(srcGz)
if err != nil {
panic(err)
Expand All @@ -23,8 +21,7 @@ func init() {
panic("failed to unpack source")
}

DefaultMarkovMap = MakeMarkovMap(strings.NewReader(src))
DefaultHeffalump = NewHeffalump(DefaultMarkovMap, DefaultBuffSize)
return MakeMarkovMap(strings.NewReader(src))
}

// ScanHTML is a basic split function for a Scanner that returns each
Expand Down
5 changes: 4 additions & 1 deletion internal/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var (
log *zerolog.Logger
hellpotHeffalump = heffalump.DefaultHeffalump
hellpotHeffalump *heffalump.Heffalump
)

func getRealRemote(ctx *fasthttp.RequestCtx) string {
Expand Down Expand Up @@ -139,6 +139,9 @@ func Serve() error {

markovMap := heffalump.MakeMarkovMap(strings.NewReader(src))
hellpotHeffalump = heffalump.NewHeffalump(markovMap, heffalump.DefaultBuffSize)
} else {
log.Info().Msg("Using default source text")
hellpotHeffalump = heffalump.NewDefaultHeffalump()
}

l := config.HTTPBind + ":" + config.HTTPPort
Expand Down

0 comments on commit 4b8f080

Please sign in to comment.