Created
July 18, 2023 06:32
-
-
Save ethinx/3903014144acc578e0565a0b3b8ef7d0 to your computer and use it in GitHub Desktop.
consistent hash based on the _hash within cache ttl
This file contains hidden or 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
| ( | |
| ( | |
| _balancerArray = new Array(100).fill().map((_,i)=>i++), | |
| _balancer = new algo.RoundRobinLoadBalancer(_balancerArray), | |
| _service = { | |
| cache: new algo.Cache( | |
| ()=>_balancer.next(), | |
| null, | |
| { | |
| ttl: 10, | |
| } | |
| ), | |
| balancer: _balancer, | |
| } | |
| )=> | |
| pipy({ | |
| _selectKey: null, | |
| _hash: null, | |
| _serviceCache: null, | |
| _select: (service, key, hash) => ( | |
| service.cache && key ? ( | |
| service.cache.get(key) | |
| ) : ( | |
| hash ? ( | |
| service.balancer.next(__inbound, hash) | |
| ) : ( | |
| service.balancer.next() | |
| ) | |
| ) | |
| ), | |
| _selectResult: null, | |
| }) | |
| .listen(39090) | |
| .handleStreamStart( | |
| () => ( | |
| _serviceCache = new algo.Cache( | |
| // k is a service, v is a target | |
| (k) => _select(k, _hash, _hash), | |
| (k, v) => k.balancer.deselect(v), | |
| ) | |
| ) | |
| ) | |
| .decodeHTTPRequest() | |
| .handleMessage( | |
| ()=>( | |
| _hash = "v1", | |
| _selectResult = _serviceCache.get(_service), | |
| console.log("cache", _selectResult) | |
| ) | |
| ) | |
| .replaceMessage( | |
| ()=>new Message(`result: ${_selectResult.id}\n`) | |
| ) | |
| .encodeHTTPResponse() | |
| )() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment