2018-07-19 15:48:08 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
2018-10-25 17:11:50 +01:00
|
|
|
package node
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
2018-10-08 16:09:37 +01:00
|
|
|
func NewConnectionPool() *ConnectionPool {
|
|
|
|
return &ConnectionPool{
|
|
|
|
cache: make(map[string]interface{}),
|
|
|
|
mu: sync.RWMutex{},
|
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add takes a node ID as the key and a node client as the value to store
|
2018-10-25 17:11:50 +01:00
|
|
|
func (pool *ConnectionPool) Add(key string, value interface{}) error {
|
|
|
|
pool.mu.Lock()
|
|
|
|
defer pool.mu.Unlock()
|
|
|
|
pool.cache[key] = value
|
2018-07-19 15:48:08 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a node connection with the provided nodeID
|
|
|
|
// nil is returned if the NodeID is not in the connection pool
|
2018-10-25 17:11:50 +01:00
|
|
|
func (pool *ConnectionPool) Get(key string) (interface{}, error) {
|
|
|
|
pool.mu.Lock()
|
|
|
|
defer pool.mu.Unlock()
|
|
|
|
return pool.cache[key], nil
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a connection associated with the provided NodeID
|
2018-10-25 17:11:50 +01:00
|
|
|
func (pool *ConnectionPool) Remove(key string) error {
|
|
|
|
pool.mu.Lock()
|
|
|
|
defer pool.mu.Unlock()
|
|
|
|
pool.cache[key] = nil
|
2018-07-19 15:48:08 +01:00
|
|
|
return nil
|
|
|
|
}
|