Skip to content

Commit 05c0727

Browse files
committed
Merge remote-tracking branch 'origin/main' into GH-8-metrics
2 parents c65dfe8 + 184ea38 commit 05c0727

File tree

7 files changed

+59
-14
lines changed

7 files changed

+59
-14
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ $(BINARY): $(GO_FILES)
1313
go mod tidy
1414
go build -o $(BINARY) ./cmd/$(BINARY)
1515

16-
.PHONY: build image
16+
.PHONY: build clean image
1717

1818
# Alias for building program binary
1919
build: $(BINARY)
2020

21+
clean:
22+
rm -f $(BINARY)
23+
2124
# Build docker image
2225
image: build
2326
docker build -t cloudstruct/$(BINARY) .

cmd/cardano-submit-api/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/cloudstruct/go-cardano-submit-api/internal/api"
77
"github.com/cloudstruct/go-cardano-submit-api/internal/config"
88
"github.com/cloudstruct/go-cardano-submit-api/internal/logging"
9+
"net/http"
10+
_ "net/http/pprof"
911
"os"
1012
)
1113

@@ -36,6 +38,17 @@ func main() {
3638
}
3739
}()
3840

41+
// Start debug listener
42+
if cfg.Debug.ListenPort > 0 {
43+
logger.Infof("starting debug listener on %s:%d", cfg.Debug.ListenAddress, cfg.Debug.ListenPort)
44+
go func() {
45+
err := http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Debug.ListenAddress, cfg.Debug.ListenPort), nil)
46+
if err != nil {
47+
logger.Fatalf("failed to start debug listener: %s", err)
48+
}
49+
}()
50+
}
51+
3952
// Start API listener
4053
logger.Infof("starting API listener on %s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort)
4154
if err := api.Start(cfg); err != nil {

config.yaml.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ api:
1919
# This can also be set via the API_LISTEN_PORT environment variable
2020
port: 8090
2121

22+
# The debug endpoint provides access to pprof for debugging purposes. This is
23+
# disabled by default, but it can be enabled by setting the port to a non-zero
24+
# value
25+
debug:
26+
# Listen address for the debug endpoint
27+
#
28+
# This can also be set via the DEBUG_ADDRESS environment variable
29+
address: localhost
30+
31+
# Listen port for the debug endpoint
32+
#
33+
# This can also be set via the DEBUG_PORT environment variable
34+
port: 0
35+
2236
node:
2337
# Path to UNIX socket file for cardano-node
2438
#

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/cloudstruct/go-cardano-submit-api
33
go 1.17
44

55
require (
6-
github.com/cloudstruct/go-ouroboros-network v0.13.0
6+
github.com/cloudstruct/go-ouroboros-network v0.13.1
77
github.com/fxamacker/cbor/v2 v2.4.0
88
github.com/gin-contrib/zap v0.0.2
99
github.com/gin-gonic/gin v1.7.7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
5454
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
5555
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
5656
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
57-
github.com/cloudstruct/go-ouroboros-network v0.13.0 h1:q6l4QQocuDTz0TOynEo5jwe9R+9tCL6vLJpWdMer9kc=
58-
github.com/cloudstruct/go-ouroboros-network v0.13.0/go.mod h1:06ggUMerJLwKJkVi5DLX1QMDz8r+R8o92rht7Z4ss9k=
57+
github.com/cloudstruct/go-ouroboros-network v0.13.1 h1:/F9fXKXLmGPdMQrqyBovbTbLUZdj/7cG0qjA/a31cZk=
58+
github.com/cloudstruct/go-ouroboros-network v0.13.1/go.mod h1:06ggUMerJLwKJkVi5DLX1QMDz8r+R8o92rht7Z4ss9k=
5959
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
6060
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6161
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

internal/api/api.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,13 @@ func handleSubmitTx(c *gin.Context) {
106106
},
107107
},
108108
}
109-
go func() {
110-
err, ok := <-errorChan
111-
if ok {
112-
logger.Errorf("failure communicating with node: %s", err)
113-
c.String(500, "failure communicating with node")
114-
doneChan <- true
115-
}
116-
}()
117109
oConn, err := ouroboros.New(oOpts)
110+
defer func() {
111+
// We have to close the channel to break out of the async error handler goroutine
112+
close(errorChan)
113+
// Close Ouroboros connection
114+
oConn.Close()
115+
}()
118116
if err != nil {
119117
logger.Errorf("failure creating Ouroboros connection: %s", err)
120118
c.String(500, "failure communicating with node")
@@ -133,6 +131,15 @@ func handleSubmitTx(c *gin.Context) {
133131
return
134132
}
135133
}
134+
// Start async error handler
135+
go func() {
136+
err, ok := <-errorChan
137+
if ok {
138+
logger.Errorf("failure communicating with node: %s", err)
139+
c.String(500, "failure communicating with node")
140+
doneChan <- true
141+
}
142+
}()
136143
// TODO: figure out better way to determine era
137144
if err = oConn.LocalTxSubmission.SubmitTx(block.TX_TYPE_ALONZO, rawTx); err != nil {
138145
logger.Errorf("failure submitting transaction: %s", err)
@@ -141,6 +148,4 @@ func handleSubmitTx(c *gin.Context) {
141148
}
142149
// Wait for async process to finish
143150
<-doneChan
144-
// We have to close the channel to break out of the Goroutine waiting on it
145-
close(errorChan)
146151
}

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
type Config struct {
1717
Logging LoggingConfig `yaml:"logging"`
1818
Api ApiConfig `yaml:"api"`
19+
Debug DebugConfig `yaml:"debug"`
1920
Node NodeConfig `yaml:"node"`
2021
}
2122

@@ -30,6 +31,11 @@ type ApiConfig struct {
3031
MetricsPort uint `yaml:"metricsPort" envconfig:"METRICS_LISTEN_PORT"`
3132
}
3233

34+
type DebugConfig struct {
35+
ListenAddress string `yaml:"address" envconfig:"DEBUG_ADDRESS"`
36+
ListenPort uint `yaml:"port" envconfig:"DEBUG_PORT"`
37+
}
38+
3339
type NodeConfig struct {
3440
Network string `yaml:"network" envconfig:"NETWORK"`
3541
NetworkMagic uint32 `yaml:"networkMagic" envconfig:"CARDANO_NODE_NETWORK_MAGIC"`
@@ -49,6 +55,10 @@ var globalConfig = &Config{
4955
MetricsAddress: "",
5056
MetricsPort: 8091,
5157
},
58+
Debug: DebugConfig{
59+
ListenAddress: "localhost",
60+
ListenPort: 0,
61+
},
5262
Node: NodeConfig{
5363
Network: "mainnet",
5464
SocketPath: "/node-ipc/node.socket",

0 commit comments

Comments
 (0)