Fix some tests.
[lightning.git] / throttle.go
index a8411001099fdc8c3d068c7bd4133a24dd968416..8b087eeae269787bf6367d46cc4eae3e9f73cefe 100644 (file)
@@ -1,4 +1,8 @@
-package main
+// Copyright (C) The Lightning Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+package lightning
 
 import (
        "sync"
@@ -15,7 +19,12 @@ type throttle struct {
 }
 
 func (t *throttle) Acquire() {
-       t.setupOnce.Do(func() { t.ch = make(chan bool, t.Max) })
+       t.setupOnce.Do(func() {
+               if t.Max < 1 {
+                       panic("throttle.Max < 1")
+               }
+               t.ch = make(chan bool, t.Max)
+       })
        t.wg.Add(1)
        t.ch <- true
 }
@@ -40,3 +49,16 @@ func (t *throttle) Wait() error {
        t.wg.Wait()
        return t.Err()
 }
+
+func (t *throttle) Go(f func() error) error {
+       t.Acquire()
+       if t.Err() != nil {
+               t.Release()
+               return t.Err()
+       }
+       go func() {
+               t.Report(f())
+               t.Release()
+       }()
+       return nil
+}