Skip to content

Commit

Permalink
Merge pull request #456 from tonkeeper/unix_sockets
Browse files Browse the repository at this point in the history
Support connections via unix sockets
  • Loading branch information
mr-tron authored Aug 30, 2024
2 parents b4700f3 + d98178f commit 661e733
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func main() {
storageBlockCh,
})

server, err := api.NewServer(log, h, fmt.Sprintf(":%d", cfg.API.Port),
server, err := api.NewServer(log, h,
api.WithTransactionSource(source),
api.WithBlockHeadersSource(source),
api.WithTraceSource(tracer),
Expand All @@ -110,5 +110,5 @@ func main() {
}()

log.Warn("start server", zap.Int("port", cfg.API.Port))
server.Run()
server.Run(fmt.Sprintf(":%d", cfg.API.Port), cfg.API.UnixSockets)
}
48 changes: 39 additions & 9 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package api

import (
"errors"
"fmt"
"net"
"net/http"
"os"

"github.com/tonkeeper/tongo/config"
"go.uber.org/zap"
Expand Down Expand Up @@ -93,7 +96,7 @@ func WithMemPool(memPool sources.MemPoolSource) ServerOption {
}
}

func NewServer(log *zap.Logger, handler *Handler, address string, opts ...ServerOption) (*Server, error) {
func NewServer(log *zap.Logger, handler *Handler, opts ...ServerOption) (*Server, error) {
options := &ServerOptions{}
for _, o := range opts {
o(options)
Expand Down Expand Up @@ -137,7 +140,6 @@ func NewServer(log *zap.Logger, handler *Handler, address string, opts ...Server
mux: mux,
asyncMiddlewares: asyncMiddlewares,
httpServer: &http.Server{
Addr: address,
Handler: mux,
},
}
Expand All @@ -161,11 +163,39 @@ func (s *Server) RegisterAsyncHandler(pattern string, handler AsyncHandler, conn
s.mux.Handle(pattern, wrapAsync(connectionType, allowTokenInQuery, chainMiddlewares(handler, s.asyncMiddlewares...)))
}

func (s *Server) Run() {
err := s.httpServer.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
s.logger.Warn("opentonapi quit")
return
}
s.logger.Fatal("ListedAndServe() failed", zap.Error(err))
func (s *Server) Run(address string, unixSockets []string) {
go func() {
tcpListener, err := net.Listen("tcp", address)
if err != nil {
s.logger.Fatal("Failed to listen on tcp address", zap.Error(err))
}
err = s.httpServer.Serve(tcpListener)

if errors.Is(err, http.ErrServerClosed) {
s.logger.Warn("opentonapi quit")
return
}
s.logger.Fatal("ListenAndServe() failed", zap.Error(err))
}()

for _, socketPath := range unixSockets {
go func(socketPath string) {
if _, err := os.Stat(socketPath); err == nil {
os.Remove(socketPath)
}

unixListener, err := net.Listen("unix", socketPath)
if err != nil {
s.logger.Fatal(fmt.Sprintf("Failed to listen on Unix socket %v", socketPath), zap.Error(err))
}

err = s.httpServer.Serve(unixListener)
if errors.Is(err, http.ErrServerClosed) {
s.logger.Warn("opentonapi quit")
return
}
s.logger.Fatal(fmt.Sprintf("ListenAndServe() failed for %v", socketPath), zap.Error(err))
}(socketPath)
}
<-make(chan struct{})
}
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

type Config struct {
API struct {
Port int `env:"PORT" envDefault:"8081"`
Port int `env:"PORT" envDefault:"8081"`
UnixSockets []string `env:"UNIX_SOCKETS" envSeparator:","`
}
App struct {
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
Expand Down

0 comments on commit 661e733

Please sign in to comment.