Documentation
¶
Index ¶
- Constants
- func ExtractSourceAddr(conn net.Conn) string
- type Channel
- func (c *Channel) Close()
- func (c *Channel) Pull()
- func (c *Channel) Push(r *Request) bool
- func (c *Channel) PushContext(ctx context.Context, r *Request) bool
- func (c *Channel) Report() *ChannelReport
- func (c *Channel) Reset(sourceAddr string)
- func (c *Channel) Restore(r *Request) bool
- func (c *Channel) TryPush(r *Request) bool
- type ChannelReport
- type ChannelReports
- type Entry
- type Holder
- type Lock
- func (l *Lock) Compactions() uint64
- func (l *Lock) Keys() ChannelReports
- func (l *Lock) Lock(key string, sourceAddr string, remoteAddr net.Addr) (bool, error)
- func (l *Lock) LockContext(ctx context.Context, key string, sourceAddr string, remoteAddr net.Addr) (bool, error)
- func (l *Lock) Persistent() bool
- func (l *Lock) ResetByKey(key string) error
- func (l *Lock) ResetBySource(sourceAddr string) error
- func (l *Lock) StoreWriteErrors() uint64
- func (l *Lock) TryLock(key string, sourceAddr string, remoteAddr net.Addr) (bool, error)
- func (l *Lock) Unlock(key string) error
- type Request
- type RestoredAddr
- type Store
- type WALStore
Constants ¶
const ( MaxKeySize = 127 MaxSourceAddrSize = 255 MaxRemoteAddrSize = 255 )
Wire limits of the values that end up in the table. The mutex port reads a key size as a signed byte and the manager port writes every size as an unsigned one, a value beyond these cannot travel back to a client.
Variables ¶
This section is empty.
Functions ¶
func ExtractSourceAddr ¶
Types ¶
type Channel ¶
type Channel struct {
Key string
// contains filtered or unexported fields
}
func NewChannel ¶
func (*Channel) Push ¶
Push waits for the key to become free and takes the ownership of it. It reports false when the request is revoked before it can own the key, the caller is expected to retry on a fresh channel in that case.
func (*Channel) PushContext ¶
PushContext is Push for a requester that may go away while it waits. When ctx is done before the key is won, the request is withdrawn from the queue and false is reported, the key is never handed to a requester that is gone. A request that is already the owner is not affected, the context only matters while waiting.
func (*Channel) Report ¶
func (c *Channel) Report() *ChannelReport
func (*Channel) Restore ¶
Restore puts a fresh channel straight into the held state, for a key that was held when the previous run ended. It reports false on a channel that is already held.
func (*Channel) TryPush ¶
TryPush takes the ownership of the key only if it is free right now, without ever waiting. It reports false when the key is already held or is being reset, so the caller can decide what to do instead of queueing. A waiter is never registered, a try that does not win simply leaves no trace.
type ChannelReport ¶
type ChannelReports ¶
type ChannelReports []*ChannelReport
func (ChannelReports) Len ¶
func (c ChannelReports) Len() int
func (ChannelReports) Less ¶
func (c ChannelReports) Less(i, j int) bool
func (ChannelReports) Swap ¶
func (c ChannelReports) Swap(i, j int)
type Entry ¶
type Entry struct {
Kind entryKind `json:"kind"`
Holder Holder `json:"holder"`
}
Entry is one durable change to the lock table. An acquire carries the whole holder, a release carries only the key of the Holder.
func AcquireEntry ¶
func AcquireEntry(report *ChannelReport) Entry
AcquireEntry records that a key became held.
func ReleaseEntry ¶
ReleaseEntry records that a key was released.
type Holder ¶
type Holder struct {
Key rawString `json:"key"`
Id string `json:"id"`
Stamp time.Time `json:"stamp"`
SourceAddr rawString `json:"source_addr"`
RemoteAddr string `json:"remote_addr"`
}
Holder is the durable form of a held key. Waiting requests are not kept, they are blocked connections that die with the process and retry on their own.
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
func NewLockWithStore ¶
NewLockWithStore brings back the holders that the store kept from the previous run and keeps the store up to date from then on. The restored state is compacted straight back, so a store that cannot be written fails the start up rather than the first lock request, and a torn tail from the previous crash is healed before anything new is appended.
func (*Lock) Compactions ¶
Compactions counts the times the log has been rewritten since the start up.
func (*Lock) Keys ¶
func (l *Lock) Keys() ChannelReports
func (*Lock) Lock ¶
Lock reports false when the request is reset before it can own the key, the caller is expected to try again to contend for the fresh channel. An error means the key could not be made durable, it is released again and the caller should not blindly retry.
func (*Lock) LockContext ¶
func (l *Lock) LockContext(ctx context.Context, key string, sourceAddr string, remoteAddr net.Addr) (bool, error)
LockContext is Lock for a requester that may go away while it waits, a client whose connection drops while its request is queued. When ctx is done before the key is won and made durable, nothing is acquired and the context's error is returned, so the caller can tell "the requester is gone" from "reset, try again". Without this, a request left behind by a dead client would still win the key later and hold it with nobody to release it.
func (*Lock) Persistent ¶
Persistent reports whether the holders survive a restart.
func (*Lock) ResetByKey ¶
func (*Lock) ResetBySource ¶
func (*Lock) StoreWriteErrors ¶
StoreWriteErrors counts the appends and compactions that failed since the start up.
func (*Lock) TryLock ¶
TryLock takes the key only if it is free right now and never waits. It reports false, and takes nothing, when the key is already held. An acquire is made durable before it is reported, exactly like Lock, so a true survives a restart. An error means the key could not be made durable and was released again.
type RestoredAddr ¶
type RestoredAddr string
RestoredAddr stands in for the peer address of a connection that ended before the restart.
func (RestoredAddr) Network ¶
func (r RestoredAddr) Network() string
func (RestoredAddr) String ¶
func (r RestoredAddr) String() string
type Store ¶
type Store interface {
// Load returns the holders of the previous run, none when there was no
// previous run.
Load() ([]Holder, error)
// Append records the entries durably. They are on disk by the time it
// returns, the client is acknowledged right after. It reports whether the
// log has grown enough that the caller should Compact.
Append(entries []Entry) (compact bool, err error)
// Compact rewrites the log from the store's own durable view, dropping the
// released keys, so it never grows without bound. It works from what has
// actually been made durable, not from the caller's in-memory table, which
// can momentarily disagree with the disk on either side of an append.
Compact() error
}
Store keeps the holders across restarts.
type WALStore ¶
type WALStore struct {
// contains filtered or unexported fields
}
WALStore is an append only log of the lock table changes, compacted in place when it outgrows the live set.
The store keeps its own view of the durably recorded holders, updated only after an append has been synced. A compaction rewrites the log from that view, never from the caller's in-memory table, so a compaction can never disagree with what has actually been made durable, whichever way the caller's memory and the disk are momentarily out of step.