Skip to content

Commit

Permalink
Remove socket server, closes #304 (#410)
Browse files Browse the repository at this point in the history
* Remove unix socket server

* Update docs

* avoid breaking change

* do not share the socket path to the daemon and the service
  • Loading branch information
krhubert authored and antho1404 committed Aug 30, 2018
1 parent d191696 commit ef0b0c5
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 73 deletions.
25 changes: 8 additions & 17 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ import (

// All the configuration keys.
const (
APIServerAddress = "Api.Server.Address"
APIServerSocket = "Api.Server.Socket"
APIClientTarget = "Api.Client.Target"
APIServiceTargetPath = "Api.Service.TargetPath"
APIServiceTargetSocket = "Api.Service.TargetSocket"
APIServiceSocketPath = "Api.Service.SocketPath"
LogFormat = "Log.Format"
LogLevel = "Log.Level"
ServicePathHost = "Service.Path.Host"
ServicePathDocker = "Service.Path.Docker"
MESGPath = "MESG.Path"
CoreImage = "Core.Image"
APIServerAddress = "Api.Server.Address"
APIClientTarget = "Api.Client.Target"
LogFormat = "Log.Format"
LogLevel = "Log.Level"
ServicePathHost = "Service.Path.Host"
ServicePathDocker = "Service.Path.Docker"
MESGPath = "MESG.Path"
CoreImage = "Core.Image"
)

func setAPIDefault() {
Expand All @@ -31,15 +27,10 @@ func setAPIDefault() {
viper.SetDefault(MESGPath, configPath)

viper.SetDefault(APIServerAddress, ":50052")
viper.SetDefault(APIServerSocket, "/mesg/server.sock")
os.MkdirAll("/mesg", os.ModePerm)

viper.SetDefault(APIClientTarget, viper.GetString(APIServerAddress))

viper.SetDefault(APIServiceSocketPath, filepath.Join(viper.GetString(MESGPath), "server.sock"))
viper.SetDefault(APIServiceTargetPath, "/mesg/server.sock")
viper.SetDefault(APIServiceTargetSocket, "unix://"+viper.GetString(APIServiceTargetPath))

viper.SetDefault(LogFormat, "text")
viper.SetDefault(LogLevel, "info")

Expand Down
15 changes: 5 additions & 10 deletions config/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ func assertViperDefault(t *testing.T, key string, expected string) {

func TestAPIDefault(t *testing.T) {
defaults := map[string]string{
APIServerAddress: ":50052",
APIServerSocket: "/mesg/server.sock",
APIClientTarget: viper.GetString(APIServerAddress),
APIServiceSocketPath: filepath.Join(viper.GetString(MESGPath), "server.sock"),
APIServiceTargetPath: "/mesg/server.sock",
APIServiceTargetSocket: "unix://" + viper.GetString(APIServiceTargetPath),
LogFormat: "text",
LogLevel: "info",
ServicePathHost: filepath.Join(viper.GetString(MESGPath), "services"),
ServicePathDocker: filepath.Join("/mesg", "services"),
APIServerAddress: ":50052",
LogFormat: "text",
LogLevel: "info",
ServicePathHost: filepath.Join(viper.GetString(MESGPath), "services"),
ServicePathDocker: filepath.Join("/mesg", "services"),
}
for key, defaultValue := range defaults {
assertViperDefault(t, key, defaultValue)
Expand Down
31 changes: 6 additions & 25 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,12 @@ func main() {
Address: viper.GetString(config.APIServerAddress),
}

unixServer := &grpc.Server{
Network: "unix",
Address: viper.GetString(config.APIServerSocket),
}

go startServer(tcpServer)
go startServer(unixServer)

closing := make(chan struct{}, 2)
go func() {
if err := tcpServer.Serve(); err != nil {
logrus.Fatalln(err)
}
}()

<-xsignal.WaitForInterrupt()

go closeServer(tcpServer, closing)
go closeServer(unixServer, closing)
<-closing
<-closing
}

func startServer(server *grpc.Server) {
if err := server.Serve(); err != nil {
logrus.Fatalln(err)
}
}

func closeServer(server *grpc.Server, closing chan struct{}) {
server.Close()
closing <- struct{}{}
tcpServer.Close()
}
9 changes: 4 additions & 5 deletions daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ func serviceSpec() (spec container.ServiceOptions, err error) {
Namespace: Namespace(),
Image: viper.GetString(config.CoreImage),
Env: container.MapToEnv(map[string]string{
config.ToEnv(config.MESGPath): "/mesg",
config.ToEnv(config.LogFormat): viper.GetString(config.LogFormat),
config.ToEnv(config.LogLevel): viper.GetString(config.LogLevel),
config.ToEnv(config.APIServiceSocketPath): filepath.Join(viper.GetString(config.MESGPath), "server.sock"),
config.ToEnv(config.ServicePathHost): filepath.Join(viper.GetString(config.MESGPath), "services"),
config.ToEnv(config.MESGPath): "/mesg",
config.ToEnv(config.LogFormat): viper.GetString(config.LogFormat),
config.ToEnv(config.LogLevel): viper.GetString(config.LogLevel),
config.ToEnv(config.ServicePathHost): filepath.Join(viper.GetString(config.MESGPath), "services"),
}),
Mounts: []container.Mount{
{
Expand Down
3 changes: 2 additions & 1 deletion daemon/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func TestStartConfig(t *testing.T) {
require.Nil(t, err)
// Make sure that the config directory is passed in parameter to write on the same folder
require.True(t, contains(spec.Env, "MESG_MESG_PATH=/mesg"))
require.True(t, contains(spec.Env, "MESG_API_SERVICE_SOCKETPATH="+filepath.Join(viper.GetString(config.MESGPath), "server.sock")))
require.True(t, contains(spec.Env, "MESG_LOG_LEVEL=info"))
require.True(t, contains(spec.Env, "MESG_LOG_FORMAT=text"))
require.True(t, contains(spec.Env, "MESG_SERVICE_PATH_HOST="+filepath.Join(viper.GetString(config.MESGPath), "services")))
// Ensure that the port is shared
require.Equal(t, spec.Ports[0].Published, uint32(50052))
Expand Down
5 changes: 0 additions & 5 deletions interface/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package grpc

import (
"net"
"os"
"sync"

"github.com/grpc-ecosystem/go-grpc-middleware"
Expand Down Expand Up @@ -34,10 +33,6 @@ func (s *Server) listen() (net.Listener, error) {
return nil, &alreadyClosedError{}
}

if s.Network == "unix" {
os.Remove(s.Address)
}

ln, err := net.Listen(s.Network, s.Address)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions interface/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const (

func TestServerServe(t *testing.T) {
s := Server{
Network: "unix",
Address: "TestServerServe.sock",
Network: "tcp",
Address: "localhost:50052",
}
go func() {
time.Sleep(waitForServe)
Expand All @@ -36,8 +36,8 @@ func TestServerServeNoAddress(t *testing.T) {

func TestServerListenAfterClose(t *testing.T) {
s := Server{
Network: "unix",
Address: "TestServerListenAfterClose.sock",
Network: "tcp",
Address: "localhost:50052",
}
go s.Serve()
time.Sleep(waitForServe)
Expand Down
10 changes: 4 additions & 6 deletions service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (dependency *DependencyFromService) Start(networkID string) (containerServi
if err != nil {
return "", err
}
endpoint := "mesg-core:50052" // TODO: should get this from daemon namespace and config
return defaultContainer.StartService(container.ServiceOptions{
Namespace: dependency.namespace(),
Labels: map[string]string{
Expand All @@ -84,13 +85,10 @@ func (dependency *DependencyFromService) Start(networkID string) (containerServi
Args: strings.Fields(dependency.Command),
Env: container.MapToEnv(map[string]string{
"MESG_TOKEN": service.Hash(),
"MESG_ENDPOINT": viper.GetString(config.APIServiceTargetSocket),
"MESG_ENDPOINT_TCP": "mesg-core:50052", // TODO: should get this from daemon namespace and config
}),
Mounts: append(mounts, container.Mount{
Source: viper.GetString(config.APIServiceSocketPath),
Target: viper.GetString(config.APIServiceTargetPath),
"MESG_ENDPOINT": endpoint,
"MESG_ENDPOINT_TCP": endpoint,
}),
Mounts: mounts,
Ports: dependency.extractPorts(),
NetworksID: []string{networkID, sharedNetworkID},
})
Expand Down

0 comments on commit ef0b0c5

Please sign in to comment.