further transport rework

This commit is contained in:
Asim
2015-05-21 19:24:57 +01:00
parent b555109fbb
commit 9d514f0e60
11 changed files with 209 additions and 137 deletions

View File

@@ -14,11 +14,11 @@ import (
)
type RpcServer struct {
mtx sync.RWMutex
address string
transport transport.Transport
rpc *rpc.Server
exit chan chan error
mtx sync.RWMutex
address string
opts options
rpc *rpc.Server
exit chan chan error
}
var (
@@ -96,7 +96,7 @@ func (s *RpcServer) Register(r Receiver) error {
func (s *RpcServer) Start() error {
registerHealthChecker(http.DefaultServeMux)
ts, err := s.transport.NewServer(Name, s.address)
ts, err := s.opts.transport.NewServer(s.address)
if err != nil {
return err
}
@@ -123,11 +123,21 @@ func (s *RpcServer) Stop() error {
return <-ch
}
func NewRpcServer(address string) *RpcServer {
func NewRpcServer(address string, opt ...Options) *RpcServer {
var opts options
for _, o := range opt {
o(&opts)
}
if opts.transport == nil {
opts.transport = transport.DefaultTransport
}
return &RpcServer{
address: address,
transport: transport.DefaultTransport,
rpc: rpc.NewServer(),
exit: make(chan chan error),
opts: opts,
address: address,
rpc: rpc.NewServer(),
exit: make(chan chan error),
}
}

View File

@@ -10,6 +10,7 @@ import (
"code.google.com/p/go-uuid/uuid"
log "github.com/golang/glog"
"github.com/myodc/go-micro/registry"
"github.com/myodc/go-micro/transport"
)
type Server interface {
@@ -22,6 +23,12 @@ type Server interface {
Stop() error
}
type options struct {
transport transport.Transport
}
type Options func(*options)
var (
Address string
Name string
@@ -65,16 +72,26 @@ func Run() error {
}
// parse address for host, port
var host string
var port int
parts := strings.Split(DefaultServer.Address(), ":")
host := strings.Join(parts[:len(parts)-1], ":")
port, _ := strconv.Atoi(parts[len(parts)-1])
if len(parts) > 1 {
host = strings.Join(parts[:len(parts)-1], ":")
port, _ = strconv.Atoi(parts[len(parts)-1])
} else {
host = parts[0]
}
// register service
node := registry.NewNode(Id, host, port)
service := registry.NewService(Name, node)
log.Infof("Registering %s", node.Id())
registry.Register(service)
log.Infof("Registering node: %s", node.Id())
err := registry.Register(service)
if err != nil {
log.Fatal("Failed to register: %v", err)
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)