mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 18:15:42 +01:00
Use factored out rate throttling client in checker.
This commit is contained in:
parent
07ab770a35
commit
bc90389090
1 changed files with 11 additions and 26 deletions
|
|
@ -11,7 +11,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
|
@ -40,22 +39,9 @@ import (
|
||||||
// topicMessages stores the collected topicMessages for a specific topic.
|
// topicMessages stores the collected topicMessages for a specific topic.
|
||||||
type topicMessages []string
|
type topicMessages []string
|
||||||
|
|
||||||
type client interface {
|
|
||||||
Get(url string) (*http.Response, error)
|
|
||||||
}
|
|
||||||
type limitingClient struct {
|
|
||||||
client
|
|
||||||
limiter *rate.Limiter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lc *limitingClient) Get(url string) (*http.Response, error) {
|
|
||||||
lc.limiter.Wait(context.Background())
|
|
||||||
return lc.client.Get(url)
|
|
||||||
}
|
|
||||||
|
|
||||||
type processor struct {
|
type processor struct {
|
||||||
opts *options
|
opts *options
|
||||||
client client
|
client util.Client
|
||||||
|
|
||||||
redirects map[string]string
|
redirects map[string]string
|
||||||
noneTLS map[string]struct{}
|
noneTLS map[string]struct{}
|
||||||
|
|
@ -278,18 +264,21 @@ func (p *processor) checkRedirect(r *http.Request, via []*http.Request) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) httpClient() client {
|
func (p *processor) httpClient() util.Client {
|
||||||
|
|
||||||
if p.client != nil {
|
if p.client != nil {
|
||||||
return p.client
|
return p.client
|
||||||
}
|
}
|
||||||
|
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
|
|
||||||
client.CheckRedirect = p.checkRedirect
|
client.CheckRedirect = p.checkRedirect
|
||||||
|
|
||||||
var tlsConfig tls.Config
|
var tlsConfig tls.Config
|
||||||
if p.opts.Insecure {
|
if p.opts.Insecure {
|
||||||
tlsConfig.InsecureSkipVerify = true
|
tlsConfig.InsecureSkipVerify = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.opts.ClientCert != nil && p.opts.ClientKey != nil {
|
if p.opts.ClientCert != nil && p.opts.ClientKey != nil {
|
||||||
cert, err := tls.LoadX509KeyPair(*p.opts.ClientCert, *p.opts.ClientKey)
|
cert, err := tls.LoadX509KeyPair(*p.opts.ClientCert, *p.opts.ClientKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -297,26 +286,22 @@ func (p *processor) httpClient() client {
|
||||||
}
|
}
|
||||||
tlsConfig.Certificates = []tls.Certificate{cert}
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.Transport = &http.Transport{
|
client.Transport = &http.Transport{
|
||||||
TLSClientConfig: &tlsConfig,
|
TLSClientConfig: &tlsConfig,
|
||||||
}
|
}
|
||||||
p.client = &client
|
|
||||||
|
|
||||||
if p.opts.Rate == nil {
|
if p.opts.Rate == nil {
|
||||||
|
p.client = &client
|
||||||
return &client
|
return &client
|
||||||
}
|
}
|
||||||
|
|
||||||
var r float64
|
p.client = &util.LimitingClient{
|
||||||
if p.opts.Rate != nil {
|
Client: &client,
|
||||||
r = *p.opts.Rate
|
Limiter: rate.NewLimiter(rate.Limit(*p.opts.Rate), 1),
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
p.client = &limitingClient{
|
|
||||||
client: &client,
|
|
||||||
limiter: rate.NewLimiter(rate.Limit(r), 1),
|
|
||||||
}
|
|
||||||
return p.client
|
return p.client
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var yearFromURL = regexp.MustCompile(`.*/(\d{4})/[^/]+$`)
|
var yearFromURL = regexp.MustCompile(`.*/(\d{4})/[^/]+$`)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue