mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-27 10:32:42 +00:00
feat(lib): limit concurrency of wasm-based verifiers
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
+5
-1
@@ -152,7 +152,11 @@ func New(opts Options) (*Server, error) {
|
|||||||
return nil, fmt.Errorf("can't load static/wasm/%s: %w", finfo.Name(), err)
|
return nil, fmt.Errorf("can't load static/wasm/%s: %w", finfo.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
result.validators[name] = runner
|
var concurrentLimit int64 = 4
|
||||||
|
|
||||||
|
cv := NewConcurrentVerifier(runner, concurrentLimit)
|
||||||
|
|
||||||
|
result.validators[name] = cv
|
||||||
}
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sync/semaphore"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -68,3 +70,23 @@ func hasLeadingZeroNibbles(data []byte, n uint32) bool {
|
|||||||
}
|
}
|
||||||
return count >= n
|
return count >= n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConcurrentVerifier struct {
|
||||||
|
Verifier
|
||||||
|
sem *semaphore.Weighted
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConcurrentVerifier(v Verifier, maxConcurrent int64) *ConcurrentVerifier {
|
||||||
|
return &ConcurrentVerifier{
|
||||||
|
Verifier: v,
|
||||||
|
sem: semaphore.NewWeighted(maxConcurrent),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cv *ConcurrentVerifier) Verify(ctx context.Context, challenge, verify []byte, nonce, difficulty uint32) (bool, error) {
|
||||||
|
if err := cv.sem.Acquire(ctx, 1); err != nil {
|
||||||
|
return false, fmt.Errorf("can't verify solution: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cv.Verifier.Verify(ctx, challenge, verify, nonce, difficulty)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user