Home / File/ locker.go — fiber Source File

locker.go — fiber Source File

Architecture documentation for locker.go, a go file in the fiber codebase. 1 imports, 0 dependents.

File go FiberCore Adapters 1 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  aba2691e_ad3a_4c6f_420f_49db09a07c7b["locker.go"]
  4a89612e_cb9d_437c_e107_9ce21cddffbe["sync"]
  aba2691e_ad3a_4c6f_420f_49db09a07c7b --> 4a89612e_cb9d_437c_e107_9ce21cddffbe
  style aba2691e_ad3a_4c6f_420f_49db09a07c7b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

package idempotency

import (
	"sync"
)

// Locker implements a spinlock for a string key.
type Locker interface {
	Lock(key string) error
	Unlock(key string) error
}

type countedLock struct {
	mu     sync.Mutex
	locked int
}

// MemoryLock coordinates access to idempotency keys using in-memory locks.
type MemoryLock struct {
	keys map[string]*countedLock
	mu   sync.Mutex
}

// Lock acquires the lock for the provided key, creating it when necessary.
func (l *MemoryLock) Lock(key string) error {
	l.mu.Lock()
	lock, ok := l.keys[key]
	if !ok {
		lock = new(countedLock)
		l.keys[key] = lock
	}
	lock.locked++
	l.mu.Unlock()

	lock.mu.Lock()

	return nil
}

// Unlock releases the lock associated with the provided key.
func (l *MemoryLock) Unlock(key string) error {
	l.mu.Lock()
	lock, ok := l.keys[key]
	if !ok {
		// This happens if we try to unlock an unknown key
		l.mu.Unlock()
		return nil
	}
	l.mu.Unlock()

	lock.mu.Unlock()

	l.mu.Lock()
	lock.locked--
	if lock.locked <= 0 {
		// This happens if countedLock is used to Lock and Unlock the same number of times
		// So, we can delete the key to prevent memory leak
		delete(l.keys, key)
	}
	l.mu.Unlock()

	return nil
}

// NewMemoryLock creates a MemoryLock ready for use.
func NewMemoryLock() *MemoryLock {
	return &MemoryLock{
		keys: make(map[string]*countedLock),
	}
}

var _ Locker = (*MemoryLock)(nil)

Domain

Subdomains

Functions

Dependencies

  • sync

Frequently Asked Questions

What does locker.go do?
locker.go is a source file in the fiber codebase, written in go. It belongs to the FiberCore domain, Adapters subdomain.
What functions are defined in locker.go?
locker.go defines 1 function(s): NewMemoryLock.
What does locker.go depend on?
locker.go imports 1 module(s): sync.
Where is locker.go in the architecture?
locker.go is located at middleware/idempotency/locker.go (domain: FiberCore, subdomain: Adapters, directory: middleware/idempotency).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free