generated from xmidt-org/.go-template
-
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.
refactored into a PacketConn-based server with a custom wire format
- Loading branch information
Showing
9 changed files
with
399 additions
and
0 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,11 @@ | ||
package hashy | ||
|
||
type Config struct { | ||
Address string | ||
Network string | ||
MaxPacketSize int | ||
MaxConcurrentRequests int | ||
|
||
Datacenters map[Datacenter][]string | ||
Vnodes int | ||
} |
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,23 @@ | ||
package hashy | ||
|
||
type Handler interface { | ||
ServeHash(ResponseWriter, *Request) | ||
} | ||
|
||
type HandlerFunc func(ResponseWriter, *Request) | ||
|
||
func (hf HandlerFunc) ServeHash(rw ResponseWriter, r *Request) { hf(rw, r) } | ||
|
||
type DefaultHandler struct { | ||
Datacenters map[Datacenter]Hasher | ||
} | ||
|
||
func (dh *DefaultHandler) ServeHash(rw ResponseWriter, r *Request) { | ||
for _, name := range r.DeviceNames { | ||
for dc, hasher := range dh.Datacenters { | ||
// TODO: error handling | ||
value, _ := hasher.Get(name.GetHashBytes()) | ||
rw.Add(name, dc, value) | ||
} | ||
} | ||
} |
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,36 @@ | ||
package hashy | ||
|
||
import ( | ||
"github.com/billhathaway/consistentHash" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
type Hasher interface { | ||
Get([]byte) (string, error) | ||
} | ||
|
||
func NewHasher(vnodes int, values []string) (Hasher, error) { | ||
ch := consistentHash.New() | ||
if err := ch.SetVnodeCount(vnodes); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, v := range values { | ||
ch.Add(v) | ||
} | ||
|
||
return ch, nil | ||
} | ||
|
||
func NewDatacenterHashers(cfg Config) (m map[Datacenter]Hasher, err error) { | ||
m = make(map[Datacenter]Hasher, len(cfg.Datacenters)) | ||
for dc, values := range cfg.Datacenters { | ||
h, hErr := NewHasher(cfg.Vnodes, values) | ||
err = multierr.Append(err, hErr) | ||
if hErr == nil { | ||
m[dc] = h | ||
} | ||
} | ||
|
||
return | ||
} |
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,58 @@ | ||
package hashy | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
var msgpackHandle = codec.MsgpackHandle{} | ||
|
||
type Datacenter string | ||
|
||
type DeviceName string | ||
|
||
func (dn DeviceName) GetHashBytes() []byte { | ||
i := strings.IndexRune(string(dn), ':') | ||
if i >= 0 { | ||
return []byte(dn[i+1:]) | ||
} | ||
|
||
return []byte(dn) | ||
} | ||
|
||
type DeviceNames []DeviceName | ||
|
||
type DeviceHashes map[DeviceName]map[Datacenter][]string | ||
|
||
func (dh *DeviceHashes) Add(name DeviceName, dc Datacenter, value string) { | ||
if *dh == nil { | ||
*dh = DeviceHashes{ | ||
name: map[Datacenter][]string{ | ||
dc: []string{value}, | ||
}, | ||
} | ||
|
||
return | ||
} | ||
|
||
if datacenters, exists := (*dh)[name]; exists { | ||
datacenters[dc] = append(datacenters[dc], value) | ||
} else { | ||
(*dh)[name] = map[Datacenter][]string{ | ||
dc: []string{value}, | ||
} | ||
} | ||
} | ||
|
||
func UnmarshalBytes[T any](b []byte) (t T, err error) { | ||
decoder := codec.NewDecoderBytes(b, &msgpackHandle) | ||
err = decoder.Decode(&t) | ||
return | ||
} | ||
|
||
func MarshalBytes[T any](t T) (b []byte, err error) { | ||
encoder := codec.NewEncoderBytes(&b, &msgpackHandle) | ||
err = encoder.Encode(t) | ||
return | ||
} |
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,28 @@ | ||
package hashy | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type Request struct { | ||
RemoteAddr net.Addr | ||
DeviceNames DeviceNames | ||
|
||
ctx context.Context | ||
} | ||
|
||
func (r *Request) Context() context.Context { | ||
if r.ctx == nil { | ||
return context.Background() | ||
} | ||
|
||
return r.ctx | ||
} | ||
|
||
func NewRequest(names DeviceNames) *Request { | ||
return &Request{ | ||
DeviceNames: names, | ||
ctx: context.Background(), | ||
} | ||
} |
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,52 @@ | ||
package hashy | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
) | ||
|
||
type Flusher interface { | ||
Flush() error | ||
} | ||
|
||
// Writer defines the behavior of a packet-oriented writer. | ||
type Writer interface { | ||
// WriteTo has the same contract as net.PacketConn.WriteTo. | ||
WriteTo([]byte, net.Addr) (int, error) | ||
} | ||
|
||
type syncWriter struct { | ||
lock sync.Mutex | ||
w Writer | ||
} | ||
|
||
func (sw *syncWriter) WriteTo(b []byte, a net.Addr) (int, error) { | ||
defer sw.lock.Unlock() | ||
sw.lock.Lock() | ||
return sw.w.WriteTo(b, a) | ||
} | ||
|
||
type ResponseWriter interface { | ||
Flusher | ||
Add(DeviceName, Datacenter, string) | ||
} | ||
|
||
type responseWriter struct { | ||
remoteAddr net.Addr | ||
writer Writer | ||
hashes DeviceHashes | ||
} | ||
|
||
func (rw *responseWriter) Add(name DeviceName, dc Datacenter, value string) { | ||
rw.hashes.Add(name, dc, value) | ||
} | ||
|
||
func (rw *responseWriter) Flush() (err error) { | ||
var message []byte | ||
message, err = MarshalBytes(rw.hashes) | ||
if err == nil { | ||
_, err = rw.writer.WriteTo(message, rw.remoteAddr) | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.