| 1 |
package global |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"runtime" |
| 6 |
"strconv" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns" |
| 9 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/conffile" |
| 10 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/num" |
| 11 |
) |
| 12 |
|
| 13 |
type Limits struct { |
| 14 |
MaxTCPQueries int |
| 15 |
MaxInflight int |
| 16 |
Servers int |
| 17 |
} |
| 18 |
|
| 19 |
func (g *Global) SetupLimits(d *conffile.Dispenser) (Limits, error) { |
| 20 |
l := Limits{MaxTCPQueries: dns.MaxTCPQueries, Servers: -1, MaxInflight: 1024} |
| 21 |
var err error |
| 22 |
for d.NextBlock(1) { |
| 23 |
switch d.Val() { |
| 24 |
case "tcp": |
| 25 |
exprs := d.RemainingArgs() |
| 26 |
if len(exprs) != 1 { |
| 27 |
return l, d.PropErr(fmt.Errorf("need single expression")) |
| 28 |
} |
| 29 |
if l.MaxTCPQueries, err = strconv.Atoi(exprs[0]); err != nil { |
| 30 |
return l, d.PropErr(err) |
| 31 |
} |
| 32 |
case "run": |
| 33 |
exprs := d.RemainingArgs() |
| 34 |
if len(exprs) != 1 { |
| 35 |
return l, d.PropErr(fmt.Errorf("need single expression")) |
| 36 |
} |
| 37 |
if l.Servers, err = num.CPU(exprs[0]); err != nil { |
| 38 |
return l, d.PropErr(err) |
| 39 |
} |
| 40 |
if l.Servers > runtime.NumCPU()*1024 { |
| 41 |
return l, d.PropErr(fmt.Errorf("should be smaller than %d: %d", runtime.NumCPU()*1024, l.Servers)) |
| 42 |
} |
| 43 |
case "inflight": |
| 44 |
exprs := d.RemainingArgs() |
| 45 |
if len(exprs) != 1 { |
| 46 |
return l, d.PropErr(fmt.Errorf("need single expression")) |
| 47 |
} |
| 48 |
if l.MaxInflight, err = num.CPU(exprs[0]); err != nil { |
| 49 |
return l, d.PropErr(err) |
| 50 |
} |
| 51 |
default: |
| 52 |
return l, d.ArgErr() |
| 53 |
} |
| 54 |
} |
| 55 |
return l, nil |
| 56 |
} |