@@ -15,143 +15,114 @@ import (
15
15
)
16
16
17
17
// SetDefaultServiceLimits sets the default limits for bundled libp2p services
18
- //
19
- // More specifically this sets the following limits:
20
- // - identify:
21
- // 128 streams in, 128 streams out, 256 streams total, 4MB min, 64MB max svc memory
22
- // 16/16/32 streams per peer
23
- // - ping:
24
- // 128 streams in, 128 sreams out, 256 streasms total, 4MB min, 64MB max svc memory
25
- // 2/3/4 streams per peer
26
- // - autonat
27
- // 128 streams in, 128 streams out, 128 streams total, 4MB min, 64MB max svc memory
28
- // 2/2/2 streams per peer
29
- // - holepunch
30
- // 128 streams in, 128 streams out, 128 streams total, 4MB min, 64MB max svc memory
31
- // 2/2/2 streams per peer
32
- // - relay v1 and v2 (separate services)
33
- // 1024 streams in, 1024 streams out, 1024 streams total, 4MB min, 64MB max svc memory
34
- // 64/64/64 streams per peer
35
- func SetDefaultServiceLimits (limiter * rcmgr.BasicLimiter ) {
36
- if limiter .ServiceLimits == nil {
37
- limiter .ServiceLimits = make (map [string ]rcmgr.Limit )
38
- }
39
- if limiter .ServicePeerLimits == nil {
40
- limiter .ServicePeerLimits = make (map [string ]rcmgr.Limit )
41
- }
42
- if limiter .ProtocolLimits == nil {
43
- limiter .ProtocolLimits = make (map [protocol.ID ]rcmgr.Limit )
44
- }
45
- if limiter .ProtocolPeerLimits == nil {
46
- limiter .ProtocolPeerLimits = make (map [protocol.ID ]rcmgr.Limit )
47
- }
48
-
18
+ func SetDefaultServiceLimits (config * rcmgr.ScalingLimitConfig ) {
49
19
// identify
50
- setServiceLimits (limiter , identify .ServiceName ,
51
- limiter .DefaultServiceLimits .
52
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ). // max 64MB service memory
53
- WithStreamLimit (128 , 128 , 256 ), // max 256 streams -- symmetric
54
- peerLimit (16 , 16 , 32 ))
55
-
56
- setProtocolLimits (limiter , identify .ID ,
57
- limiter .DefaultProtocolLimits .WithMemoryLimit (1 , 4 << 20 , 32 << 20 ),
58
- peerLimit (16 , 16 , 32 ))
59
- setProtocolLimits (limiter , identify .IDPush ,
60
- limiter .DefaultProtocolLimits .WithMemoryLimit (1 , 4 << 20 , 32 << 20 ),
61
- peerLimit (16 , 16 , 32 ))
62
- setProtocolLimits (limiter , identify .IDDelta ,
63
- limiter .DefaultProtocolLimits .WithMemoryLimit (1 , 4 << 20 , 32 << 20 ),
64
- peerLimit (16 , 16 , 32 ))
20
+ config .AddServiceLimit (
21
+ identify .ServiceName ,
22
+ rcmgr.BaseLimit {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 128 , Memory : 4 << 20 },
23
+ rcmgr.BaseLimitIncrease {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 128 , Memory : 4 << 20 },
24
+ )
25
+ config .AddServicePeerLimit (
26
+ identify .ServiceName ,
27
+ rcmgr.BaseLimit {StreamsInbound : 16 , StreamsOutbound : 16 , Streams : 32 , Memory : 1 << 20 },
28
+ rcmgr.BaseLimitIncrease {},
29
+ )
30
+ for _ , id := range [... ]protocol.ID {identify .ID , identify .IDDelta , identify .IDPush } {
31
+ config .AddProtocolLimit (
32
+ id ,
33
+ rcmgr.BaseLimit {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 128 , Memory : 4 << 20 },
34
+ rcmgr.BaseLimitIncrease {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 128 , Memory : 4 << 20 },
35
+ )
36
+ config .AddProtocolPeerLimit (
37
+ id ,
38
+ rcmgr.BaseLimit {StreamsInbound : 16 , StreamsOutbound : 16 , Streams : 32 , Memory : 32 * (256 << 20 + 16 << 10 )},
39
+ rcmgr.BaseLimitIncrease {},
40
+ )
41
+ }
65
42
66
- // ping
67
- setServiceLimits (limiter , ping .ServiceName ,
68
- limiter .DefaultServiceLimits .
69
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ). // max 64MB service memory
70
- WithStreamLimit (128 , 128 , 128 ), // max 128 streams - asymmetric
71
- peerLimit (2 , 3 , 4 ))
72
- setProtocolLimits (limiter , ping .ID ,
73
- limiter .DefaultProtocolLimits .WithMemoryLimit (1 , 4 << 20 , 64 << 20 ),
74
- peerLimit (2 , 3 , 4 ))
43
+ // ping
44
+ addServiceAndProtocolLimit (config ,
45
+ ping .ServiceName , ping .ID ,
46
+ rcmgr.BaseLimit {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 64 , Memory : 4 << 20 },
47
+ rcmgr.BaseLimitIncrease {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 64 , Memory : 4 << 20 },
48
+ )
49
+ addServicePeerAndProtocolPeerLimit (
50
+ config ,
51
+ ping .ServiceName , ping .ID ,
52
+ rcmgr.BaseLimit {StreamsInbound : 2 , StreamsOutbound : 3 , Streams : 4 , Memory : 32 * (256 << 20 + 16 << 10 )},
53
+ rcmgr.BaseLimitIncrease {},
54
+ )
75
55
76
56
// autonat
77
- setServiceLimits (limiter , autonat .ServiceName ,
78
- limiter .DefaultServiceLimits .
79
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ). // max 64MB service memory
80
- WithStreamLimit (128 , 128 , 128 ), // max 128 streams - asymmetric
81
- peerLimit (2 , 2 , 2 ))
82
- setProtocolLimits (limiter , autonat .AutoNATProto ,
83
- limiter .DefaultProtocolLimits .WithMemoryLimit (1 , 4 << 20 , 64 << 20 ),
84
- peerLimit (2 , 2 , 2 ))
57
+ addServiceAndProtocolLimit (config ,
58
+ autonat .ServiceName , autonat .AutoNATProto ,
59
+ rcmgr.BaseLimit {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 64 , Memory : 4 << 20 },
60
+ rcmgr.BaseLimitIncrease {StreamsInbound : 4 , StreamsOutbound : 4 , Streams : 4 , Memory : 2 << 20 },
61
+ )
62
+ addServicePeerAndProtocolPeerLimit (
63
+ config ,
64
+ autonat .ServiceName , autonat .AutoNATProto ,
65
+ rcmgr.BaseLimit {StreamsInbound : 2 , StreamsOutbound : 2 , Streams : 2 , Memory : 1 << 20 },
66
+ rcmgr.BaseLimitIncrease {},
67
+ )
85
68
86
69
// holepunch
87
- setServiceLimits (limiter , holepunch .ServiceName ,
88
- limiter .DefaultServiceLimits .
89
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ). // max 64MB service memory
90
- WithStreamLimit (128 , 128 , 256 ), // max 256 streams - symmetric
91
- peerLimit (2 , 2 , 2 ))
92
- setProtocolLimits (limiter , holepunch .Protocol ,
93
- limiter .DefaultProtocolLimits .WithMemoryLimit (1 , 4 << 20 , 64 << 20 ),
94
- peerLimit (2 , 2 , 2 ))
70
+ addServiceAndProtocolLimit (config ,
71
+ holepunch .ServiceName , holepunch .Protocol ,
72
+ rcmgr.BaseLimit {StreamsInbound : 32 , StreamsOutbound : 32 , Streams : 64 , Memory : 4 << 20 },
73
+ rcmgr.BaseLimitIncrease {StreamsInbound : 8 , StreamsOutbound : 8 , Streams : 16 , Memory : 4 << 20 },
74
+ )
75
+ addServicePeerAndProtocolPeerLimit (config ,
76
+ holepunch .ServiceName , holepunch .Protocol ,
77
+ rcmgr.BaseLimit {StreamsInbound : 2 , StreamsOutbound : 2 , Streams : 2 , Memory : 1 << 20 },
78
+ rcmgr.BaseLimitIncrease {},
79
+ )
95
80
96
81
// relay/v1
97
- setServiceLimits (limiter , relayv1 .ServiceName ,
98
- limiter .DefaultServiceLimits .
99
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ). // max 64MB service memory
100
- WithStreamLimit (1024 , 1024 , 1024 ), // max 1024 streams - asymmetric
101
- peerLimit (64 , 64 , 64 ))
82
+ config .AddServiceLimit (
83
+ relayv1 .ServiceName ,
84
+ rcmgr.BaseLimit {StreamsInbound : 256 , StreamsOutbound : 256 , Streams : 256 , Memory : 16 << 20 },
85
+ rcmgr.BaseLimitIncrease {StreamsInbound : 256 , StreamsOutbound : 256 , Streams : 256 , Memory : 16 << 20 },
86
+ )
87
+ config .AddServicePeerLimit (
88
+ relayv1 .ServiceName ,
89
+ rcmgr.BaseLimit {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 64 , Memory : 1 << 20 },
90
+ rcmgr.BaseLimitIncrease {},
91
+ )
102
92
103
93
// relay/v2
104
- setServiceLimits (limiter , relayv2 .ServiceName ,
105
- limiter .DefaultServiceLimits .
106
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ). // max 64MB service memory
107
- WithStreamLimit (1024 , 1024 , 1024 ), // max 1024 streams - asymmetric
108
- peerLimit (64 , 64 , 64 ))
94
+ config .AddServiceLimit (
95
+ relayv2 .ServiceName ,
96
+ rcmgr.BaseLimit {StreamsInbound : 256 , StreamsOutbound : 256 , Streams : 256 , Memory : 16 << 20 },
97
+ rcmgr.BaseLimitIncrease {StreamsInbound : 256 , StreamsOutbound : 256 , Streams : 256 , Memory : 16 << 20 },
98
+ )
99
+ config .AddServicePeerLimit (
100
+ relayv2 .ServiceName ,
101
+ rcmgr.BaseLimit {StreamsInbound : 64 , StreamsOutbound : 64 , Streams : 64 , Memory : 1 << 20 },
102
+ rcmgr.BaseLimitIncrease {},
103
+ )
109
104
110
105
// circuit protocols, both client and service
111
- setProtocolLimits (limiter , circuit .ProtoIDv1 ,
112
- limiter .DefaultProtocolLimits .
113
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ).
114
- WithStreamLimit (1280 , 1280 , 1280 ),
115
- peerLimit (128 , 128 , 128 ))
116
- setProtocolLimits (limiter , circuit .ProtoIDv2Hop ,
117
- limiter .DefaultProtocolLimits .
118
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ).
119
- WithStreamLimit (1280 , 1280 , 1280 ),
120
- peerLimit (128 , 128 , 128 ))
121
- setProtocolLimits (limiter , circuit .ProtoIDv2Stop ,
122
- limiter .DefaultProtocolLimits .
123
- WithMemoryLimit (1 , 4 << 20 , 64 << 20 ).
124
- WithStreamLimit (1280 , 1280 , 1280 ),
125
- peerLimit (128 , 128 , 128 ))
126
-
127
- }
128
-
129
- func setServiceLimits (limiter * rcmgr.BasicLimiter , svc string , limit rcmgr.Limit , peerLimit rcmgr.Limit ) {
130
- if _ , ok := limiter .ServiceLimits [svc ]; ! ok {
131
- limiter .ServiceLimits [svc ] = limit
132
- }
133
- if _ , ok := limiter .ServicePeerLimits [svc ]; ! ok {
134
- limiter .ServicePeerLimits [svc ] = peerLimit
106
+ for _ , proto := range [... ]protocol.ID {circuit .ProtoIDv1 , circuit .ProtoIDv2Hop , circuit .ProtoIDv2Stop } {
107
+ config .AddProtocolLimit (
108
+ proto ,
109
+ rcmgr.BaseLimit {StreamsInbound : 640 , StreamsOutbound : 640 , Streams : 640 , Memory : 16 << 20 },
110
+ rcmgr.BaseLimitIncrease {StreamsInbound : 640 , StreamsOutbound : 640 , Streams : 640 , Memory : 16 << 20 },
111
+ )
112
+ config .AddProtocolPeerLimit (
113
+ proto ,
114
+ rcmgr.BaseLimit {StreamsInbound : 128 , StreamsOutbound : 128 , Streams : 128 , Memory : 32 << 20 },
115
+ rcmgr.BaseLimitIncrease {},
116
+ )
135
117
}
136
118
}
137
119
138
- func setProtocolLimits (limiter * rcmgr.BasicLimiter , proto protocol.ID , limit rcmgr.Limit , peerLimit rcmgr.Limit ) {
139
- if _ , ok := limiter .ProtocolLimits [proto ]; ! ok {
140
- limiter .ProtocolLimits [proto ] = limit
141
- }
142
- if _ , ok := limiter .ProtocolPeerLimits [proto ]; ! ok {
143
- limiter .ProtocolPeerLimits [proto ] = peerLimit
144
- }
120
+ func addServiceAndProtocolLimit (config * rcmgr.ScalingLimitConfig , service string , proto protocol.ID , limit rcmgr.BaseLimit , increase rcmgr.BaseLimitIncrease ) {
121
+ config .AddServiceLimit (service , limit , increase )
122
+ config .AddProtocolLimit (proto , limit , increase )
145
123
}
146
124
147
- func peerLimit (numStreamsIn , numStreamsOut , numStreamsTotal int ) rcmgr.Limit {
148
- return & rcmgr.StaticLimit {
149
- // memory: 256kb for window buffers plus some change for message buffers per stream
150
- Memory : int64 (numStreamsTotal * (256 << 10 + 16384 )),
151
- BaseLimit : rcmgr.BaseLimit {
152
- StreamsInbound : numStreamsIn ,
153
- StreamsOutbound : numStreamsOut ,
154
- Streams : numStreamsTotal ,
155
- },
156
- }
125
+ func addServicePeerAndProtocolPeerLimit (config * rcmgr.ScalingLimitConfig , service string , proto protocol.ID , limit rcmgr.BaseLimit , increase rcmgr.BaseLimitIncrease ) {
126
+ config .AddServicePeerLimit (service , limit , increase )
127
+ config .AddProtocolPeerLimit (proto , limit , increase )
157
128
}
0 commit comments