From 1a02c374ce329e47a6c83bd6cf6f9d796682460a Mon Sep 17 00:00:00 2001 From: Dima Krasner Date: Wed, 25 Dec 2024 17:13:01 +0200 Subject: [PATCH] drop the golang.org/x/sync/semaphore dependency --- fed/resolve.go | 12 ++++++------ fed/resolve_test.go | 2 +- go.mod | 1 - go.sum | 2 -- lock/lock.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 lock/lock.go diff --git a/fed/resolve.go b/fed/resolve.go index ff2cc8d6..5e7b8090 100644 --- a/fed/resolve.go +++ b/fed/resolve.go @@ -26,7 +26,7 @@ import ( "github.com/dimkr/tootik/cfg" "github.com/dimkr/tootik/data" "github.com/dimkr/tootik/httpsig" - "golang.org/x/sync/semaphore" + "github.com/dimkr/tootik/lock" "hash/crc32" "io" "log/slog" @@ -53,7 +53,7 @@ type Resolver struct { sender BlockedDomains *BlockList db *sql.DB - locks []*semaphore.Weighted + locks []lock.Lock } var ( @@ -78,10 +78,10 @@ func NewResolver(blockedDomains *BlockList, domain string, cfg *cfg.Config, clie }, BlockedDomains: blockedDomains, db: db, - locks: make([]*semaphore.Weighted, cfg.MaxResolverRequests), + locks: make([]lock.Lock, cfg.MaxResolverRequests), } for i := 0; i < len(r.locks); i++ { - r.locks[i] = semaphore.NewWeighted(1) + r.locks[i] = lock.New() } return &r @@ -190,10 +190,10 @@ func (r *Resolver) tryResolve(ctx context.Context, key httpsig.Key, host, name s if !isLocal && flags&ap.Offline == 0 { lock := r.locks[crc32.ChecksumIEEE([]byte(host+name))%uint32(len(r.locks))] - if err := lock.Acquire(ctx, 1); err != nil { + if err := lock.Lock(ctx); err != nil { return nil, nil, err } - defer lock.Release(1) + defer lock.Unlock() } var tmp ap.Actor diff --git a/fed/resolve_test.go b/fed/resolve_test.go index 22ea4b05..6683d904 100644 --- a/fed/resolve_test.go +++ b/fed/resolve_test.go @@ -536,7 +536,7 @@ func TestResolve_FederatedActorFirstTimeCancelled(t *testing.T) { resolver := NewResolver(&blockList, "localhost.localdomain", &cfg, &client, db) for i := range resolver.locks { - assert.NoError(resolver.locks[i].Acquire(context.Background(), 1)) + assert.NoError(resolver.locks[i].Lock(context.Background())) } ctx, cancel := context.WithCancel(context.Background()) diff --git a/go.mod b/go.mod index 01a34b3f..4bf3ce8c 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/mattn/go-sqlite3 v1.14.24 github.com/stretchr/testify v1.10.0 golang.org/x/image v0.23.0 - golang.org/x/sync v0.10.0 ) require ( diff --git a/go.sum b/go.sum index c8b437aa..0644a40a 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,6 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68= golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY= -golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= -golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/lock/lock.go b/lock/lock.go new file mode 100644 index 00000000..3fae73ee --- /dev/null +++ b/lock/lock.go @@ -0,0 +1,42 @@ +/* +Copyright 2024 Dima Krasner + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package lock provides synchronization primitives. +package lock + +import "context" + +// Lock is similar to [sync.Mutex] but locking is cancellable through a [context.Context]. +type Lock chan struct{} + +func New() Lock { + c := make(chan struct{}, 1) + c <- struct{}{} + return c +} + +func (l Lock) Lock(ctx context.Context) error { + select { + case <-ctx.Done(): + return ctx.Err() + case <-l: + return nil + } +} + +func (l Lock) Unlock() { + l <- struct{}{} +}