mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
Merge pull request #575 from hermeznetwork/fix/httpserve
In http servers, first listen, then serve
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -38,8 +39,8 @@ type Pendinger interface {
|
|||||||
New() Pendinger
|
New() Pendinger
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiPort = ":4010"
|
const apiAddr = ":4010"
|
||||||
const apiURL = "http://localhost" + apiPort + "/"
|
const apiURL = "http://localhost" + apiAddr + "/"
|
||||||
|
|
||||||
var SetBlockchain = `
|
var SetBlockchain = `
|
||||||
Type: Blockchain
|
Type: Blockchain
|
||||||
@@ -241,9 +242,14 @@ func TestMain(m *testing.M) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
// Start server
|
// Start server
|
||||||
server := &http.Server{Addr: apiPort, Handler: apiGin}
|
listener, err := net.Listen("tcp", apiAddr) //nolint:gosec
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
server := &http.Server{Handler: apiGin}
|
||||||
go func() {
|
go func() {
|
||||||
if err := server.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
|
if err := server.Serve(listener); err != nil &&
|
||||||
|
tracerr.Unwrap(err) != http.ErrServerClosed {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -609,9 +615,12 @@ func TestTimeout(t *testing.T) {
|
|||||||
<-finishWait
|
<-finishWait
|
||||||
})
|
})
|
||||||
// Start server
|
// Start server
|
||||||
serverTO := &http.Server{Addr: ":4444", Handler: apiGinTO}
|
serverTO := &http.Server{Handler: apiGinTO}
|
||||||
|
listener, err := net.Listen("tcp", ":4444") //nolint:gosec
|
||||||
|
require.NoError(t, err)
|
||||||
go func() {
|
go func() {
|
||||||
if err := serverTO.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
|
if err := serverTO.Serve(listener); err != nil &&
|
||||||
|
tracerr.Unwrap(err) != http.ErrServerClosed {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
11
node/node.go
11
node/node.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -465,16 +466,20 @@ func NewNodeAPI(
|
|||||||
// cancelation.
|
// cancelation.
|
||||||
func (a *NodeAPI) Run(ctx context.Context) error {
|
func (a *NodeAPI) Run(ctx context.Context) error {
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: a.addr,
|
|
||||||
Handler: a.engine,
|
Handler: a.engine,
|
||||||
// TODO: Figure out best parameters for production
|
// TODO: Figure out best parameters for production
|
||||||
ReadTimeout: 30 * time.Second, //nolint:gomnd
|
ReadTimeout: 30 * time.Second, //nolint:gomnd
|
||||||
WriteTimeout: 30 * time.Second, //nolint:gomnd
|
WriteTimeout: 30 * time.Second, //nolint:gomnd
|
||||||
MaxHeaderBytes: 1 << 20, //nolint:gomnd
|
MaxHeaderBytes: 1 << 20, //nolint:gomnd
|
||||||
}
|
}
|
||||||
|
listener, err := net.Listen("tcp", a.addr)
|
||||||
|
if err != nil {
|
||||||
|
return tracerr.Wrap(err)
|
||||||
|
}
|
||||||
|
log.Infof("NodeAPI is ready at %v", a.addr)
|
||||||
go func() {
|
go func() {
|
||||||
log.Infof("NodeAPI is ready at %v", a.addr)
|
if err := server.Serve(listener); err != nil &&
|
||||||
if err := server.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
|
tracerr.Unwrap(err) != http.ErrServerClosed {
|
||||||
log.Fatalf("Listen: %s\n", err)
|
log.Fatalf("Listen: %s\n", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package debugapi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -121,16 +122,20 @@ func (a *DebugAPI) Run(ctx context.Context) error {
|
|||||||
debugAPI.GET("sync/stats", a.handleSyncStats)
|
debugAPI.GET("sync/stats", a.handleSyncStats)
|
||||||
|
|
||||||
debugAPIServer := &http.Server{
|
debugAPIServer := &http.Server{
|
||||||
Addr: a.addr,
|
|
||||||
Handler: api,
|
Handler: api,
|
||||||
// Use some hardcoded numberes that are suitable for testing
|
// Use some hardcoded numbers that are suitable for testing
|
||||||
ReadTimeout: 30 * time.Second, //nolint:gomnd
|
ReadTimeout: 30 * time.Second, //nolint:gomnd
|
||||||
WriteTimeout: 30 * time.Second, //nolint:gomnd
|
WriteTimeout: 30 * time.Second, //nolint:gomnd
|
||||||
MaxHeaderBytes: 1 << 20, //nolint:gomnd
|
MaxHeaderBytes: 1 << 20, //nolint:gomnd
|
||||||
}
|
}
|
||||||
|
listener, err := net.Listen("tcp", a.addr)
|
||||||
|
if err != nil {
|
||||||
|
return tracerr.Wrap(err)
|
||||||
|
}
|
||||||
|
log.Infof("DebugAPI is ready at %v", a.addr)
|
||||||
go func() {
|
go func() {
|
||||||
log.Infof("DebugAPI is ready at %v", a.addr)
|
if err := debugAPIServer.Serve(listener); err != nil &&
|
||||||
if err := debugAPIServer.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
|
tracerr.Unwrap(err) != http.ErrServerClosed {
|
||||||
log.Fatalf("Listen: %s\n", err)
|
log.Fatalf("Listen: %s\n", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -202,16 +203,20 @@ func (s *Mock) Run(ctx context.Context) error {
|
|||||||
apiGroup.POST("/cancel", s.handleCancel)
|
apiGroup.POST("/cancel", s.handleCancel)
|
||||||
|
|
||||||
debugAPIServer := &http.Server{
|
debugAPIServer := &http.Server{
|
||||||
Addr: s.addr,
|
|
||||||
Handler: api,
|
Handler: api,
|
||||||
// Use some hardcoded numberes that are suitable for testing
|
// Use some hardcoded numberes that are suitable for testing
|
||||||
ReadTimeout: 30 * time.Second, //nolint:gomnd
|
ReadTimeout: 30 * time.Second, //nolint:gomnd
|
||||||
WriteTimeout: 30 * time.Second, //nolint:gomnd
|
WriteTimeout: 30 * time.Second, //nolint:gomnd
|
||||||
MaxHeaderBytes: 1 << 20, //nolint:gomnd
|
MaxHeaderBytes: 1 << 20, //nolint:gomnd
|
||||||
}
|
}
|
||||||
|
listener, err := net.Listen("tcp", s.addr)
|
||||||
|
if err != nil {
|
||||||
|
return tracerr.Wrap(err)
|
||||||
|
}
|
||||||
|
log.Infof("prover.MockServer is ready at %v", s.addr)
|
||||||
go func() {
|
go func() {
|
||||||
log.Infof("prover.MockServer is ready at %v", s.addr)
|
if err := debugAPIServer.Serve(listener); err != nil &&
|
||||||
if err := debugAPIServer.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
|
tracerr.Unwrap(err) != http.ErrServerClosed {
|
||||||
log.Fatalf("Listen: %s\n", err)
|
log.Fatalf("Listen: %s\n", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user