Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop the golang.org/x/sync/semaphore dependency #103

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions fed/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -53,7 +53,7 @@ type Resolver struct {
sender
BlockedDomains *BlockList
db *sql.DB
locks []*semaphore.Weighted
locks []lock.Lock
}

var (
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion fed/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
42 changes: 42 additions & 0 deletions lock/lock.go
Original file line number Diff line number Diff line change
@@ -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{}{}
}
Loading