mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-04-24 14:22:13 +00:00
Related to #453 but not a fix. This will cause us to actually return early when we start blocking on sending to some peers, but it won't really _unblock_ those peers. For that, we need to write with a context.
29 lines
354 B
Go
29 lines
354 B
Go
package dht
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type ctxMutex chan struct{}
|
|
|
|
func newCtxMutex() ctxMutex {
|
|
return make(ctxMutex, 1)
|
|
}
|
|
|
|
func (m ctxMutex) Lock(ctx context.Context) error {
|
|
select {
|
|
case m <- struct{}{}:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
func (m ctxMutex) Unlock() {
|
|
select {
|
|
case <-m:
|
|
default:
|
|
panic("not locked")
|
|
}
|
|
}
|