Add internal/httpclient package as a singleton RoundTripper used by all outbound engine requests (search, engines, autocomplete, upstream). Key Transport settings: - MaxIdleConnsPerHost = 20 (up from Go default of 2) - MaxIdleConns = 100 - IdleConnTimeout = 90s - DialContext timeout = 5s Previously, the default transport limited each host to 2 idle connections, forcing a new TCP+TLS handshake on every search for each engine. With 12 engines hitting the same upstream hosts in parallel, connections were constantly recycled. Now warm connections are reused across all goroutines and requests.
91 lines
3 KiB
Go
91 lines
3 KiB
Go
// samsa — a privacy-respecting metasearch engine
|
|
// Copyright (C) 2026-present metamorphosis-dev
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package engines
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/metamorphosis-dev/samsa/internal/config"
|
|
"github.com/metamorphosis-dev/samsa/internal/httpclient"
|
|
)
|
|
|
|
// NewDefaultPortedEngines returns the Go-native engine registry.
|
|
// If cfg is nil, API keys fall back to environment variables.
|
|
func NewDefaultPortedEngines(client *http.Client, cfg *config.Config) map[string]Engine {
|
|
if client == nil {
|
|
client = httpclient.NewClient(10 * time.Second)
|
|
}
|
|
|
|
var braveAPIKey, braveAccessToken, youtubeAPIKey string
|
|
if cfg != nil {
|
|
braveAPIKey = cfg.Engines.Brave.APIKey
|
|
braveAccessToken = cfg.Engines.Brave.AccessToken
|
|
youtubeAPIKey = cfg.Engines.YouTube.APIKey
|
|
}
|
|
if braveAPIKey == "" {
|
|
braveAPIKey = os.Getenv("BRAVE_API_KEY")
|
|
}
|
|
if braveAccessToken == "" {
|
|
braveAccessToken = os.Getenv("BRAVE_ACCESS_TOKEN")
|
|
}
|
|
if youtubeAPIKey == "" {
|
|
youtubeAPIKey = os.Getenv("YOUTUBE_API_KEY")
|
|
}
|
|
|
|
return map[string]Engine{
|
|
"wikipedia": &WikipediaEngine{client: client},
|
|
"arxiv": &ArxivEngine{client: client},
|
|
"crossref": &CrossrefEngine{client: client},
|
|
"braveapi": &BraveAPIEngine{
|
|
client: client,
|
|
apiKey: braveAPIKey,
|
|
accessGateToken: braveAccessToken,
|
|
resultsPerPage: 20,
|
|
},
|
|
"brave": &BraveEngine{client: client},
|
|
"qwant": &QwantEngine{
|
|
client: client,
|
|
category: "web-lite",
|
|
resultsPerPage: 10,
|
|
},
|
|
"duckduckgo": &DuckDuckGoEngine{client: client},
|
|
"github": &GitHubEngine{client: client},
|
|
"reddit": &RedditEngine{client: client},
|
|
"bing": &BingEngine{client: client},
|
|
"google": &GoogleEngine{client: client},
|
|
"youtube": &YouTubeEngine{
|
|
client: client,
|
|
apiKey: youtubeAPIKey,
|
|
baseURL: "https://www.googleapis.com",
|
|
},
|
|
"stackoverflow": &StackOverflowEngine{client: client, apiKey: stackoverflowAPIKey(cfg)},
|
|
// Image engines
|
|
"bing_images": &BingImagesEngine{client: client},
|
|
"ddg_images": &DuckDuckGoImagesEngine{client: client},
|
|
"qwant_images": &QwantImagesEngine{client: client},
|
|
}
|
|
}
|
|
|
|
// stackoverflowAPIKey returns the Stack Overflow API key from config or env var.
|
|
func stackoverflowAPIKey(cfg *config.Config) string {
|
|
if cfg != nil && cfg.Engines.StackOverflow != nil && cfg.Engines.StackOverflow.APIKey != "" {
|
|
return cfg.Engines.StackOverflow.APIKey
|
|
}
|
|
return os.Getenv("STACKOVERFLOW_KEY")
|
|
}
|