Skip to content

Commit c63be84

Browse files
authored
feat: add option to handle the overall request limit (go-acme#2209)
1 parent 69cacab commit c63be84

File tree

7 files changed

+41
-21
lines changed

7 files changed

+41
-21
lines changed

certificate/authorization.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,10 @@ import (
77
"github.com/go-acme/lego/v4/log"
88
)
99

10-
const (
11-
// overallRequestLimit is the overall number of request per second
12-
// limited on the "new-reg", "new-authz" and "new-cert" endpoints.
13-
// From the documentation the limitation is 20 requests per second,
14-
// but using 20 as value doesn't work but 18 do.
15-
// https://letsencrypt.org/docs/rate-limits/
16-
overallRequestLimit = 18
17-
)
18-
1910
func (c *Certifier) getAuthorizations(order acme.ExtendedOrder) ([]acme.Authorization, error) {
2011
resc, errc := make(chan acme.Authorization), make(chan domainError)
2112

22-
delay := time.Second / overallRequestLimit
13+
delay := time.Second / time.Duration(c.overallRequestLimit)
2314

2415
for _, authzURL := range order.Authorizations {
2516
time.Sleep(delay)

certificate/certificates.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ import (
2222
"golang.org/x/net/idna"
2323
)
2424

25+
const (
26+
// DefaultOverallRequestLimit is the overall number of request per second
27+
// limited on the "new-reg", "new-authz" and "new-cert" endpoints.
28+
// From the documentation the limitation is 20 requests per second,
29+
// but using 20 as value doesn't work but 18 do.
30+
// https://letsencrypt.org/docs/rate-limits/
31+
// ZeroSSL has a limit of 7.
32+
// https://help.zerossl.com/hc/en-us/articles/17864245480093-Advantages-over-Using-Let-s-Encrypt#h_01HT4Z1JCJFJQFJ1M3P7S085Q9
33+
DefaultOverallRequestLimit = 18
34+
)
35+
2536
// maxBodySize is the maximum size of body that we will read.
2637
const maxBodySize = 1024 * 1024
2738

@@ -94,24 +105,33 @@ type resolver interface {
94105
}
95106

96107
type CertifierOptions struct {
97-
KeyType certcrypto.KeyType
98-
Timeout time.Duration
108+
KeyType certcrypto.KeyType
109+
Timeout time.Duration
110+
OverallRequestLimit int
99111
}
100112

101113
// Certifier A service to obtain/renew/revoke certificates.
102114
type Certifier struct {
103-
core *api.Core
104-
resolver resolver
105-
options CertifierOptions
115+
core *api.Core
116+
resolver resolver
117+
options CertifierOptions
118+
overallRequestLimit int
106119
}
107120

108121
// NewCertifier creates a Certifier.
109122
func NewCertifier(core *api.Core, resolver resolver, options CertifierOptions) *Certifier {
110-
return &Certifier{
123+
c := &Certifier{
111124
core: core,
112125
resolver: resolver,
113126
options: options,
114127
}
128+
129+
c.overallRequestLimit = options.OverallRequestLimit
130+
if c.overallRequestLimit <= 0 {
131+
c.overallRequestLimit = DefaultOverallRequestLimit
132+
}
133+
134+
return c
115135
}
116136

117137
// Obtain tries to obtain a single certificate using all domains passed into it.

cmd/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"time"
55

6+
"github.com/go-acme/lego/v4/certificate"
67
"github.com/go-acme/lego/v4/lego"
78
"github.com/urfave/cli/v2"
89
"software.sslmate.com/src/go-pkcs12"
@@ -154,6 +155,11 @@ func CreateFlags(defaultPath string) []cli.Flag {
154155
Usage: "Set the certificate timeout value to a specific value in seconds. Only used when obtaining certificates.",
155156
Value: 30,
156157
},
158+
&cli.IntFlag{
159+
Name: "overall-request-limit",
160+
Usage: "ACME overall requests limit.",
161+
Value: certificate.DefaultOverallRequestLimit,
162+
},
157163
&cli.StringFlag{
158164
Name: "user-agent",
159165
Usage: "Add to the user-agent sent to the CA to identify an application embedding lego-cli",

cmd/setup.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ func newClient(ctx *cli.Context, acc registration.User, keyType certcrypto.KeyTy
3838
config.CADirURL = ctx.String("server")
3939

4040
config.Certificate = lego.CertificateConfig{
41-
KeyType: keyType,
42-
Timeout: time.Duration(ctx.Int("cert.timeout")) * time.Second,
41+
KeyType: keyType,
42+
Timeout: time.Duration(ctx.Int("cert.timeout")) * time.Second,
43+
OverallRequestLimit: ctx.Int("overall-request-limit"),
4344
}
4445
config.UserAgent = getUserAgent(ctx)
4546

docs/data/zz_cli_help.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ GLOBAL OPTIONS:
4848
--pfx.pass value The password used to encrypt the .pfx (PCKS#12) file. (default: "changeit") [$LEGO_PFX_PASSWORD]
4949
--pfx.format value The encoding format to use when encrypting the .pfx (PCKS#12) file. Supported: RC2, DES, SHA256. (default: "RC2") [$LEGO_PFX_FORMAT]
5050
--cert.timeout value Set the certificate timeout value to a specific value in seconds. Only used when obtaining certificates. (default: 30)
51+
--overall-request-limit value ACME overall requests limit. (default: 18)
5152
--user-agent value Add to the user-agent sent to the CA to identify an application embedding lego-cli
5253
--help, -h show help
5354
"""

lego/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewClient(config *Config) (*Client, error) {
5353
solversManager := resolver.NewSolversManager(core)
5454

5555
prober := resolver.NewProber(solversManager)
56-
certifier := certificate.NewCertifier(core, prober, certificate.CertifierOptions{KeyType: config.Certificate.KeyType, Timeout: config.Certificate.Timeout})
56+
certifier := certificate.NewCertifier(core, prober, certificate.CertifierOptions{KeyType: config.Certificate.KeyType, Timeout: config.Certificate.Timeout, OverallRequestLimit: config.Certificate.OverallRequestLimit})
5757

5858
return &Client{
5959
Certificate: certifier,

lego/client_config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ func NewConfig(user registration.User) *Config {
6161
}
6262

6363
type CertificateConfig struct {
64-
KeyType certcrypto.KeyType
65-
Timeout time.Duration
64+
KeyType certcrypto.KeyType
65+
Timeout time.Duration
66+
OverallRequestLimit int
6667
}
6768

6869
// createDefaultHTTPClient Creates an HTTP client with a reasonable timeout value

0 commit comments

Comments
 (0)