go-libp2p-kad-dht/ctx_mutex.go
Steven Allen 0b029388bd fix: obey the context when sending messages to peers
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.
2020-02-22 16:34:56 -05:00

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")
}
}