Skip to content

Commit f9ddbf4

Browse files
committed
update logic for disconnection detection
1 parent 1cbdce1 commit f9ddbf4

File tree

10 files changed

+76
-25
lines changed

10 files changed

+76
-25
lines changed

src/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
build/
1+
build/
2+
bin/

src/.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
run:
2+
timeout: 5m
3+
modules-download-mode: mod
4+
go: "1.23"
5+
6+
linters:
7+
disable:
8+
- typecheck
9+
10+
issues:
11+
exclude-dirs-use-default: true
12+

src/Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ endif
2424

2525
# Dependency versions
2626
GOTESTSUM_VERSION ?= 0.4.2
27-
GOLANGCI_VERSION ?= 1.60.3
27+
GOLANGCI_VERSION ?= 1.61.0
28+
# Default flags for golangci-lint
29+
GOLANGCI_FLAGS ?= --timeout=5m
30+
LINT_PKGS ?= ./internal/common/... ./entry/... ./bin/...
2831

2932
GOLANG_VERSION ?= 1.14
3033

@@ -111,7 +114,7 @@ post-build-release: ${POST_BUILD_RELEASE_TARGETS}
111114
build-release: build-release-deps pre-build-release
112115
build-release: ## Build binaries without debug information
113116
@${MAKE} LDFLAGS="-w ${LDFLAGS}" GOARGS="${GOARGS} -trimpath" BUILD_DIR="${BUILD_DIR}/release" build
114-
117+
mv build/release/entry build/release/ocf-amd64
115118
@${MAKE} post-build-release
116119

117120
.PHONY: build-debug-deps
@@ -157,12 +160,12 @@ bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION}
157160
@ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint
158161
bin/golangci-lint-${GOLANGCI_VERSION}:
159162
@mkdir -p bin
160-
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION}
163+
GOBIN=$(abspath bin) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v${GOLANGCI_VERSION}
161164
@mv bin/golangci-lint $@
162165

163166
.PHONY: lint
164167
lint: bin/golangci-lint ## Run linters
165-
bin/golangci-lint run ./...
168+
GOTOOLCHAIN=go1.23.0 bin/golangci-lint run ${GOLANGCI_FLAGS} ${LINT_PKGS}
166169

167170
release-%: TAG_PREFIX = v
168171
release-%:

src/entry/cmd/update.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func doUpdate() error {
2020
return err
2121
}
2222
defer resp.Body.Close()
23-
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
24-
if err != nil {
25-
// error handling
26-
}
27-
return err
23+
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
24+
if err != nil {
25+
return err
26+
}
27+
return nil
2828
}
2929

3030
var updateCmd = &cobra.Command{

src/entry/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ var (
1010
version = "dev"
1111
commitHash = "?"
1212
buildDate = ""
13-
buildSecret = ""
13+
// buildSecret left for future use to verify official builds
14+
buildSecret string
1415
)
1516

1617
func main() {
1718
common.JSONVersion.Version = version
1819
common.JSONVersion.Commit = commitHash
1920
common.JSONVersion.Date = buildDate
21+
_ = buildSecret
2022
cmd.Execute()
2123
}

src/internal/common/logger.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ func init() {
1616
}
1717
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
1818
config.Level.SetLevel(zapcore.InfoLevel)
19-
zapLogger, err := config.Build()
20-
// trunk-ignore(golangci-lint/errcheck)
21-
defer zapLogger.Sync()
19+
zapLogger, err := config.Build()
20+
defer func() { _ = zapLogger.Sync() }()
2221
if err != nil {
2322
panic(err)
2423
}

src/internal/protocol/clock.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package protocol
22

33
import (
4+
"context"
45
"encoding/json"
56
"math/rand"
67
"ocf/internal/common"
@@ -11,6 +12,7 @@ import (
1112
ds "github.com/ipfs/go-datastore"
1213
"github.com/jasonlvhit/gocron"
1314
"github.com/libp2p/go-libp2p/core/network"
15+
libpeer "github.com/libp2p/go-libp2p/core/peer"
1416
)
1517

1618
// var verificationKey = "ocf-verification-key"
@@ -33,16 +35,29 @@ func StartTicker() {
3335
// updateMyself()
3436
for _, peer_id := range peers {
3537
// check if peer is still connected
36-
peer, error := GetPeerFromTable(peer_id.String())
38+
p, error := GetPeerFromTable(peer_id.String())
3739
if error == nil {
38-
peer.Connected = true
40+
p.Connected = true
3941
if peer_id != host.ID() && host.Network().Connectedness(peer_id) != network.Connected {
40-
common.Logger.Info("Peer:" + peer_id.String() + " got disconnected!")
41-
peer.Connected = false
42+
// try to dial the peer, if cannot dial, then mark it as disconnected
43+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
44+
defer cancel()
45+
addrInfo := libpeer.AddrInfo{ID: peer_id, Addrs: host.Peerstore().Addrs(peer_id)}
46+
if len(addrInfo.Addrs) == 0 {
47+
common.Logger.Warnf("No known addresses for peer %s; marking disconnected", peer_id)
48+
p.Connected = false
49+
} else if err := host.Connect(ctx, addrInfo); err != nil {
50+
common.Logger.With("err", err).Warnf("Failed to dial peer %s; marking disconnected", peer_id)
51+
p.Connected = false
52+
} else {
53+
// Successfully reconnected
54+
common.Logger.Infof("Reconnected to peer %s", peer_id)
55+
p.Connected = true
56+
}
4257
}
4358
// update last seen timestamp
44-
peer.LastSeen = time.Now().Unix()
45-
value, err := json.Marshal(peer)
59+
p.LastSeen = time.Now().Unix()
60+
value, err := json.Marshal(p)
4661
if err == nil {
4762
UpdateNodeTableHook(ds.NewKey(peer_id.String()), value)
4863
}

src/internal/protocol/host.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,21 @@ func newHost(ctx context.Context, seed int64, ds datastore.Batching) (host.Host,
7272
if err != nil {
7373
return nil, err
7474
}
75+
writeKeyToFile(priv)
7576
}
7677
} else {
7778
r := mrand.New(mrand.NewSource(seed))
7879
priv, _, err = crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
7980
if err != nil {
8081
return nil, err
8182
}
83+
writeKeyToFile(priv)
8284
}
83-
// persist private key
84-
writeKeyToFile(priv)
8585
if err != nil {
8686
return nil, err
8787
}
88-
8988
// Configure resource manager with higher limits
9089
limits := rcmgr.DefaultLimits.AutoScale()
91-
9290
// Increase connection limits significantly for a distributed system
9391
systemLimits := rcmgr.ResourceLimits{
9492
ConnsInbound: 1000, // Allow up to 1000 inbound connections
@@ -97,7 +95,7 @@ func newHost(ctx context.Context, seed int64, ds datastore.Batching) (host.Host,
9795
StreamsInbound: 10000, // Increase stream limits
9896
StreamsOutbound: 10000,
9997
Streams: 20000,
100-
Memory: 1 << 30, // 1GB memory limit
98+
Memory: 16 << 30, // 16GB memory limit
10199
}
102100

103101
// Apply the custom limits

src/internal/protocol/key.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func loadKeyFromFile() crypto.PrivKey {
4040
return nil
4141
}
4242
keyPath := path.Join(home, ".ocfcore", "keys", "id")
43+
common.Logger.Info("Looking for keys under: ", keyPath)
4344
keyData, err := os.ReadFile(keyPath)
4445
if err != nil {
4546
return nil

tools/establish_connection.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import subprocess
3+
4+
command = """curl http://148.187.108.172:8092/v1/p2p/<address>/v1/_service/llm/v1/chat/completions \
5+
-H 'Authorization: Bearer YOUR_API_KEY' \
6+
-H 'Content-Type: application/json' \
7+
-d '{
8+
"model": "Qwen/Qwen3-32B",
9+
"messages": [
10+
{ "role": "system", "content": "You are a helpful assistant." },
11+
{ "role": "user", "content": "What is the capital of France?" }
12+
],
13+
"temperature": 0.7
14+
}'"""
15+
16+
address = "QmNNRmz2etg76yFhc15cqvVPEKjCywSLSRFiN3brzYEV6u"
17+
cmd = command.replace("<address>", address)
18+
print(cmd)
19+
# Use subprocess to avoid shell interpretation issues
20+
subprocess.run(cmd, shell=True, check=True)

0 commit comments

Comments
 (0)