Skip to content

Commit

Permalink
Merge pull request #54 from evan-buss/development
Browse files Browse the repository at this point in the history
Add Search Rate Limiting
  • Loading branch information
evan-buss authored Jan 1, 2022
2 parents 5cb036f + 5d5114e commit 11389b4
Show file tree
Hide file tree
Showing 20 changed files with 620 additions and 1,088 deletions.
7 changes: 6 additions & 1 deletion cli/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/evan-buss/openbooks/core"
"github.com/evan-buss/openbooks/irc"
)

var lastSearch time.Time

func terminalMenu(irc *irc.Conn) {
fmt.Print("\ns)search\ng)et book\nd)one\n~> ")

Expand All @@ -24,8 +27,10 @@ func terminalMenu(irc *irc.Conn) {
case "s":
fmt.Print("@search ")
message, _ := reader.ReadString('\n')
core.SearchBook(irc, clean(message))
fmt.Println("\nSent search request.")
time.Sleep(time.Until(lastSearch.Add(time.Second * 15)))
core.SearchBook(irc, clean(message))
lastSearch = time.Now()
case "g":
fmt.Print("Download String: ")
message, _ := reader.ReadString('\n')
Expand Down
25 changes: 25 additions & 0 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package cli

import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"

"github.com/evan-buss/openbooks/core"
"github.com/evan-buss/openbooks/irc"
Expand Down Expand Up @@ -46,3 +49,25 @@ func (config *Config) setupLogger(handler core.EventHandler) io.Closer {

return file
}

func GetLastSearchTime() time.Time {
timestampFilePath := filepath.Join(os.TempDir(), ".openbooks")
fileInfo, err := os.Stat(timestampFilePath)

if errors.Is(err, os.ErrNotExist) {
return time.Time{}
}

return fileInfo.ModTime()
}

func SetLastSearchTime() {
timestampFilePath := filepath.Join(os.TempDir(), ".openbooks")
_, err := os.Stat(timestampFilePath)

if errors.Is(err, os.ErrNotExist) {
os.Create(timestampFilePath)
}

os.Chtimes(timestampFilePath, time.Now(), time.Now())
}
4 changes: 4 additions & 0 deletions cmd/openbooks/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"strings"
"time"

"github.com/evan-buss/openbooks/cli"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,6 +61,9 @@ var searchCmd = &cobra.Command{
Short: "Searches for a book and exits.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
nextSearchTime := cli.GetLastSearchTime().Add(15 * time.Second)
time.Sleep(time.Until(nextSearchTime))
cli.StartSearch(config, args[0])
cli.SetLastSearchTime()
},
}
2 changes: 1 addition & 1 deletion cmd/openbooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func main() {
func generateUserName() string {
rand.Seed(time.Now().UnixNano())
gofakeit.Seed(int64(rand.Int()))
return fmt.Sprintf("%s-%s", gofakeit.Adjective(), gofakeit.Noun())
return fmt.Sprintf("%s_%s", gofakeit.Adjective(), gofakeit.Noun())
}
28 changes: 19 additions & 9 deletions cmd/openbooks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"os"
"path"
"path/filepath"
"time"

"github.com/evan-buss/openbooks/server"

Expand All @@ -16,10 +18,11 @@ func init() {
serverCmd.Flags().BoolP("browser", "b", false, "Open the browser on server start.")
serverCmd.Flags().StringP("name", "n", generateUserName(), "Use a name that isn't randomly generated. One word only.")
serverCmd.Flags().StringP("port", "p", "5228", "Set the local network port for browser mode.")
serverCmd.Flags().StringP("dir", "d", path.Join(os.TempDir(), "openbooks"), "The directory where eBooks are saved when persist enabled.")
serverCmd.Flags().StringP("dir", "d", filepath.Join(os.TempDir(), "openbooks"), "The directory where eBooks are saved when persist enabled.")
serverCmd.Flags().Bool("persist", false, "Persist eBooks in 'dir'. Default is to delete after sending.")
serverCmd.Flags().String("basepath", "/", `Base path where the application is accessible. For example "/openbooks/".`)
serverCmd.Flags().StringP("server", "s", "irc.irchighway.net", "IRC server to connect to.")
serverCmd.Flags().IntP("rate-limit", "r", 10, "The number of seconds to wait between searches to reduce strain on IRC search servers. Minimum is 10 seconds.")
}

var serverCmd = &cobra.Command{
Expand All @@ -35,6 +38,7 @@ var serverCmd = &cobra.Command{
persist, _ := cmd.Flags().GetBool("persist")
basepath, _ := cmd.Flags().GetString("basepath")
url, _ := cmd.Flags().GetString("server")
rateLimit, _ := cmd.Flags().GetInt("rate-limit")

// If cli flag isn't set (default value) check for the presence of an
// environment variable and use it if found.
Expand All @@ -44,15 +48,21 @@ var serverCmd = &cobra.Command{
}
}

// If user enters a limit that's too low, set to default of 10 seconds.
if rateLimit < 10 {
rateLimit = 10
}

config := server.Config{
Log: log,
OpenBrowser: browser,
UserName: name,
Port: port,
DownloadDir: dir,
Persist: persist,
Basepath: sanitizePath(basepath),
Server: url,
Log: log,
OpenBrowser: browser,
UserName: name,
Port: port,
DownloadDir: dir,
Persist: persist,
Basepath: sanitizePath(basepath),
Server: url,
SearchTimeout: time.Duration(rateLimit) * time.Second,
}

server.Start(config)
Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ go 1.17

require (
github.com/brianvoe/gofakeit/v5 v5.11.2
github.com/dsnet/compress v0.0.1 // indirect
github.com/go-chi/chi/v5 v5.0.5
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/go-chi/chi/v5 v5.0.7
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.4.2
github.com/mholt/archiver/v3 v3.5.0
github.com/mholt/archiver/v3 v3.5.1
github.com/nwaples/rardecode v1.1.2 // indirect
github.com/rs/cors v1.8.0
github.com/schollz/progressbar/v3 v3.8.3
github.com/spf13/cobra v1.2.1
github.com/rs/cors v1.8.2
github.com/schollz/progressbar/v3 v3.8.5
github.com/spf13/cobra v1.3.0
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
)

require github.com/stretchr/testify v1.7.0

require (
github.com/andybalholm/brotli v1.0.3 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/pierrec/lz4/v4 v4.1.9 // indirect
github.com/pierrec/lz4/v4 v4.1.12 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20211031064116-611d5d643895 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 11389b4

Please sign in to comment.