dee2c137c8
* slowly but surely * hardcode ID for tests so we can get predictable results * skipping bad test * removing tests of bkad * wip * new algorithm for worker * clean up * remove skipped test * changes * uncomment * fixed conflicts * maybe done ? * cleanup * boot bkad * wip * cleanup * undo change * fixes * wip * wip * moving nodeID around * wip * wip * fixes * fixes after merge * added TODO * fixed tests post identity * linter fixes * wip * PR review comments * wip * fixing tests * fix tests * force db directory * bad test * fixes race condition * small cleanups * adding db folder * testing * wip * cleanup * cleanup * linters * export Restrict * add timeout * testing * linters * forgot one * moar fixes from master merge * PR comments * moar PR comments * removed stun flag * remove duplicate declaration * remove old tests * remove timeout * fix tests * missed one * changed StringToID >> IDFromString * PR comments * stupid linter * moevd overlay mock * fixed merge conflicts * fixes * linter
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information
|
|
|
|
package pool
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
// ConnectionPool is the in memory implementation of a connection Pool
|
|
type ConnectionPool struct {
|
|
mu sync.RWMutex
|
|
cache map[string]interface{}
|
|
}
|
|
|
|
// NewConnectionPool initializes a new in memory pool
|
|
func NewConnectionPool() *ConnectionPool {
|
|
return &ConnectionPool{
|
|
cache: make(map[string]interface{}),
|
|
mu: sync.RWMutex{},
|
|
}
|
|
}
|
|
|
|
// Add takes a node ID as the key and a node client as the value to store
|
|
func (mp *ConnectionPool) Add(ctx context.Context, key string, value interface{}) error {
|
|
mp.mu.Lock()
|
|
defer mp.mu.Unlock()
|
|
mp.cache[key] = value
|
|
|
|
return nil
|
|
}
|
|
|
|
// Get retrieves a node connection with the provided nodeID
|
|
// nil is returned if the NodeID is not in the connection pool
|
|
func (mp *ConnectionPool) Get(ctx context.Context, key string) (interface{}, error) {
|
|
mp.mu.Lock()
|
|
defer mp.mu.Unlock()
|
|
|
|
return mp.cache[key], nil
|
|
}
|
|
|
|
// Remove deletes a connection associated with the provided NodeID
|
|
func (mp *ConnectionPool) Remove(ctx context.Context, key string) error {
|
|
mp.mu.Lock()
|
|
defer mp.mu.Unlock()
|
|
mp.cache[key] = nil
|
|
|
|
return nil
|
|
}
|