@@ -28,18 +28,21 @@ package limits
2828
2929import (
3030 "context"
31+ "fmt"
3132 "net"
3233 "strconv"
3334 "time"
3435
3536 "github.com/foxcpp/maddy/framework/config"
3637 "github.com/foxcpp/maddy/framework/container"
38+ "github.com/foxcpp/maddy/framework/log"
3739 "github.com/foxcpp/maddy/framework/module"
3840 "github.com/foxcpp/maddy/framework/module/modules"
3941 "github.com/foxcpp/maddy/internal/limits/limiters"
4042)
4143
4244type Group struct {
45+ log * log.Logger
4346 instName string
4447
4548 global limiters.MultiLimit
@@ -48,8 +51,15 @@ type Group struct {
4851 dest * limiters.BucketSet // BucketSet of MultiLimit
4952}
5053
54+ func Empty (log * log.Logger ) * Group {
55+ return & Group {
56+ log : log ,
57+ }
58+ }
59+
5160func New (c * container.C , _ , instName string ) (module.Module , error ) {
5261 return & Group {
62+ log : c .DefaultLogger .Sublogger ("limits" ),
5363 instName : instName ,
5464 }, nil
5565}
@@ -177,22 +187,28 @@ func (g *Group) TakeMsg(ctx context.Context, addr net.IP, sourceDomain string) e
177187 ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
178188 defer cancel ()
179189
190+ g .log .DebugMsg ("global TakeContext" )
180191 if err := g .global .TakeContext (ctx ); err != nil {
181- return err
192+ return fmt . Errorf ( "TakeMsg: global: %w" , err )
182193 }
194+ g .log .DebugMsg ("global TakeContext done" )
183195
184196 if g .ip != nil {
197+ g .log .DebugMsg ("ip TakeContext" , "ip" , addr .String ())
185198 if err := g .ip .TakeContext (ctx , addr .String ()); err != nil {
186199 g .global .Release ()
187- return err
200+ return fmt . Errorf ( "TakeMsg: ip: %w" , err )
188201 }
202+ g .log .DebugMsg ("ip TakeContext done" , "ip" , addr .String ())
189203 }
190204 if g .source != nil {
205+ g .log .DebugMsg ("source TakeContext" , "domain" , sourceDomain )
191206 if err := g .source .TakeContext (ctx , sourceDomain ); err != nil {
192207 g .global .Release ()
193208 g .ip .Release (addr .String ())
194- return err
209+ return fmt . Errorf ( "TakeMSg: source: %w" , err )
195210 }
211+ g .log .DebugMsg ("source TakeContext done" , "domain" , sourceDomain )
196212 }
197213 return nil
198214}
@@ -201,17 +217,27 @@ func (g *Group) TakeDest(ctx context.Context, domain string) error {
201217 if g .dest == nil {
202218 return nil
203219 }
220+
204221 ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
205222 defer cancel ()
206- return g .dest .TakeContext (ctx , domain )
223+
224+ g .log .DebugMsg ("TakeDest" , "domain" , domain )
225+ if err := g .dest .TakeContext (ctx , domain ); err != nil {
226+ return fmt .Errorf ("TakeDest: dest: %w" , err )
227+ }
228+ g .log .DebugMsg ("TakeDest done" , "domain" , domain )
229+ return nil
207230}
208231
209232func (g * Group ) ReleaseMsg (addr net.IP , sourceDomain string ) {
233+ g .log .DebugMsg ("global ReleaseMsg" )
210234 g .global .Release ()
211235 if g .ip != nil {
236+ g .log .DebugMsg ("ip ReleaseMsg" , "ip" , addr .String ())
212237 g .ip .Release (addr .String ())
213238 }
214239 if g .source != nil {
240+ g .log .DebugMsg ("source ReleaseMsg" , "domain" , sourceDomain )
215241 g .source .Release (sourceDomain )
216242 }
217243}
@@ -220,7 +246,10 @@ func (g *Group) ReleaseDest(domain string) {
220246 if g .dest == nil {
221247 return
222248 }
249+
250+ g .log .DebugMsg ("ReleaseDest" , "domain" , domain )
223251 g .dest .Release (domain )
252+ g .log .DebugMsg ("ReleaseDest done" , "domain" , domain )
224253}
225254
226255func (g * Group ) Name () string {
0 commit comments