forked from distribworks/dkron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(WIP - consul like approach) add server_lookup to make dkron independ…
…ent from server node IP on raft layer
- Loading branch information
1 parent
f829f54
commit 9ef70de
Showing
3 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package dkron | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/hashicorp/raft" | ||
) | ||
|
||
// ServerLookup encapsulates looking up servers by id and address | ||
type ServerLookup struct { | ||
lock sync.RWMutex | ||
addressToServer map[raft.ServerAddress]*ServerParts | ||
idToServer map[raft.ServerID]*ServerParts | ||
} | ||
|
||
func NewServerLookup() *ServerLookup { | ||
return &ServerLookup{ | ||
addressToServer: make(map[raft.ServerAddress]*ServerParts), | ||
idToServer: make(map[raft.ServerID]*ServerParts), | ||
} | ||
} | ||
|
||
func (sl *ServerLookup) AddServer(server *ServerParts) { | ||
sl.lock.Lock() | ||
defer sl.lock.Unlock() | ||
sl.addressToServer[raft.ServerAddress(server.RPCAddr.String())] = server | ||
sl.idToServer[raft.ServerID(server.ID)] = server | ||
} | ||
|
||
func (sl *ServerLookup) RemoveServer(server *ServerParts) { | ||
sl.lock.Lock() | ||
defer sl.lock.Unlock() | ||
delete(sl.addressToServer, raft.ServerAddress(server.RPCAddr.String())) | ||
delete(sl.idToServer, raft.ServerID(server.ID)) | ||
} | ||
|
||
// Implements the ServerAddressProvider interface | ||
func (sl *ServerLookup) ServerAddr(id raft.ServerID) (raft.ServerAddress, error) { | ||
sl.lock.RLock() | ||
defer sl.lock.RUnlock() | ||
svr, ok := sl.idToServer[id] | ||
if !ok { | ||
return "", fmt.Errorf("Could not find address for server id %v", id) | ||
} | ||
return raft.ServerAddress(svr.RPCAddr.String()), nil | ||
} | ||
|
||
// Server looks up the server by address, returns a boolean if not found | ||
func (sl *ServerLookup) Server(addr raft.ServerAddress) *ServerParts { | ||
sl.lock.RLock() | ||
defer sl.lock.RUnlock() | ||
return sl.addressToServer[addr] | ||
} | ||
|
||
func (sl *ServerLookup) Servers() []*ServerParts { | ||
sl.lock.RLock() | ||
defer sl.lock.RUnlock() | ||
var ret []*ServerParts | ||
for _, svr := range sl.addressToServer { | ||
ret = append(ret, svr) | ||
} | ||
return ret | ||
} | ||
|
||
func (sl *ServerLookup) CheckServers(fn func(srv *ServerParts) bool) { | ||
sl.lock.RLock() | ||
defer sl.lock.RUnlock() | ||
|
||
for _, srv := range sl.addressToServer { | ||
if !fn(srv) { | ||
return | ||
} | ||
} | ||
} |