Skip to content

Commit

Permalink
get rid of redundant n_api_tries
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima committed Jan 19, 2024
1 parent aa1f880 commit 0080264
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DSN=mydbname.db
DB_POOL_SIZE=100
YT_API_KEYS=ALzaSyAWJfmC4-ZSNd0ntT3even_tryMA9B7tBQ,AIzaSyAUwQwQsherenxjjLTSVtheH0d1vOsam0o
YT_API_MAX_TRIES=100
LETSENCRYPT_EMAIL=[email protected]
6 changes: 0 additions & 6 deletions app/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

type Config struct {
YtApiKeys []string
YtApiMaxTries int
DSN string
DbPoolSize int
}
Expand All @@ -30,11 +29,6 @@ func ParseConfig() *Config {
}
config.YtApiKeys = strings.Split(ytApiKeys, ",")

config.YtApiMaxTries, _ = strconv.Atoi(os.Getenv("YT_API_MAX_TRIES"))
if config.YtApiMaxTries == 0 {
config.YtApiMaxTries = 100
}

config.DbPoolSize, _ = strconv.Atoi(os.Getenv("DB_POOL_SIZE"))
if config.DbPoolSize == 0 {
config.DbPoolSize = 100
Expand Down
6 changes: 5 additions & 1 deletion app/handlers/random_hander.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -251,7 +252,10 @@ func (s *Router) GetRandom(w http.ResponseWriter, r *http.Request) {
visitor := r.Header.Get("visitor")
params := r.URL.Query()

conn := s.db.Get(r.Context())
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*60))
defer cancel()

conn := s.db.Get(ctx)
defer s.db.Put(conn)

searchCriteria := ParseQueryParams(params)
Expand Down
3 changes: 1 addition & 2 deletions app/youtube/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (y *YouTubeRequester) Request(req *http.Request) (*http.Response, error) {
// Just wrap http.Get to add http code errors
// retries with fresh api keys if provided

for i := 1; i < y.conf.YtApiMaxTries; i++ {
for {
q := req.URL.Query()
q.Add("key", y.conf.YtApiKeys[y.currentApiKeyN])
req.URL.RawQuery = q.Encode()
Expand All @@ -38,7 +38,6 @@ func (y *YouTubeRequester) Request(req *http.Request) (*http.Response, error) {
}
return res, err
}
return nil, ErrorMaxTries
}

const base64range string = "0123456789abcdefghijklmnopqrstuvwxyz-_"
Expand Down

0 comments on commit 0080264

Please sign in to comment.