-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from cloudstruct/feat/protocol-resource-cleanup
feat: cleanup protocol resources on shutdown
- Loading branch information
Showing
10 changed files
with
213 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
BINARY=go-ouroboros-network | ||
|
||
# Determine root directory | ||
ROOT_DIR=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) | ||
|
||
# Gather all .go files for use in dependencies below | ||
GO_FILES=$(shell find $(ROOT_DIR) -name '*.go') | ||
|
||
# Build our program binary | ||
# Depends on GO_FILES to determine when rebuild is needed | ||
$(BINARY): $(GO_FILES) | ||
# Needed to fetch new dependencies and add them to go.mod | ||
go mod tidy | ||
go build -o $(BINARY) ./cmd/$(BINARY) | ||
# Gather list of expected binaries | ||
BINARIES=$(shell cd $(ROOT_DIR)/cmd && ls -1) | ||
|
||
.PHONY: build clean test | ||
.PHONY: build mod-tidy clean test | ||
|
||
# Alias for building program binary | ||
build: $(BINARY) | ||
build: $(BINARIES) | ||
|
||
mod-tidy: | ||
# Needed to fetch new dependencies and add them to go.mod | ||
go mod tidy | ||
|
||
clean: | ||
rm -f $(BINARY) | ||
rm -f $(BINARIES) | ||
|
||
test: | ||
go test -v ./... | ||
|
||
# Build our program binaries | ||
# Depends on GO_FILES to determine when rebuild is needed | ||
$(BINARIES): mod-tidy $(GO_FILES) | ||
go build -o $(@) ./cmd/$(@) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
_ "net/http/pprof" | ||
"os" | ||
"runtime" | ||
"runtime/pprof" | ||
"time" | ||
|
||
ouroboros "github.com/cloudstruct/go-ouroboros-network" | ||
) | ||
|
||
type memUsageFlags struct { | ||
flagset *flag.FlagSet | ||
startEra string | ||
tip bool | ||
debugPort int | ||
} | ||
|
||
func newMemUsageFlags() *memUsageFlags { | ||
f := &memUsageFlags{ | ||
flagset: flag.NewFlagSet("mem-usage", flag.ExitOnError), | ||
} | ||
f.flagset.StringVar(&f.startEra, "start-era", "genesis", "era which to start chain-sync at") | ||
f.flagset.BoolVar(&f.tip, "tip", false, "start chain-sync at current chain tip") | ||
f.flagset.IntVar(&f.debugPort, "debug-port", 8080, "pprof port") | ||
return f | ||
} | ||
|
||
func testMemUsage(f *globalFlags) { | ||
memUsageFlags := newMemUsageFlags() | ||
err := memUsageFlags.flagset.Parse(f.flagset.Args()[1:]) | ||
if err != nil { | ||
fmt.Printf("failed to parse subcommand args: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Start pprof listener | ||
log.Printf("Starting pprof listener on http://0.0.0.0:%d/debug/pprof\n", memUsageFlags.debugPort) | ||
go func() { | ||
log.Println(http.ListenAndServe(fmt.Sprintf(":%d", memUsageFlags.debugPort), nil)) | ||
}() | ||
|
||
for i := 0; i < 10; i++ { | ||
showMemoryStats("open") | ||
|
||
conn := createClientConnection(f) | ||
errorChan := make(chan error) | ||
go func() { | ||
for { | ||
err, ok := <-errorChan | ||
if !ok { | ||
return | ||
} | ||
fmt.Printf("ERROR: %s\n", err) | ||
os.Exit(1) | ||
} | ||
}() | ||
o, err := ouroboros.New( | ||
ouroboros.WithConnection(conn), | ||
ouroboros.WithNetworkMagic(uint32(f.networkMagic)), | ||
ouroboros.WithErrorChan(errorChan), | ||
ouroboros.WithNodeToNode(f.ntnProto), | ||
ouroboros.WithKeepAlive(true), | ||
) | ||
if err != nil { | ||
fmt.Printf("ERROR: %s\n", err) | ||
os.Exit(1) | ||
} | ||
o.ChainSync.Client.Start() | ||
|
||
tip, err := o.ChainSync.Client.GetCurrentTip() | ||
if err != nil { | ||
fmt.Printf("ERROR: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
log.Printf("tip: slot = %d, hash = %x\n", tip.Point.Slot, tip.Point.Hash) | ||
|
||
if err := o.Close(); err != nil { | ||
fmt.Printf("ERROR: %s\n", err) | ||
} | ||
|
||
showMemoryStats("close") | ||
|
||
time.Sleep(5 * time.Second) | ||
|
||
runtime.GC() | ||
|
||
showMemoryStats("after GC") | ||
} | ||
|
||
if err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1); err != nil { | ||
fmt.Printf("ERROR: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Printf("waiting forever") | ||
select {} | ||
} | ||
|
||
func showMemoryStats(tag string) { | ||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
log.Printf("[%s] HeapAlloc: %dKiB\n", tag, m.HeapAlloc/1024) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters