Skip to content

Commit 604b5f6

Browse files
committed
Allow to specify a connection timeout
1 parent bdc4547 commit 604b5f6

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

core/cli/explorer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package cli
22

33
import (
44
"context"
5+
"time"
56

67
cliContext "github.com/mudler/LocalAI/core/cli/context"
78
"github.com/mudler/LocalAI/core/explorer"
89
"github.com/mudler/LocalAI/core/http"
910
)
1011

1112
type ExplorerCMD struct {
12-
Address string `env:"LOCALAI_ADDRESS,ADDRESS" default:":8080" help:"Bind address for the API server" group:"api"`
13-
PoolDatabase string `env:"LOCALAI_POOL_DATABASE,POOL_DATABASE" default:"explorer.json" help:"Path to the pool database" group:"api"`
13+
Address string `env:"LOCALAI_ADDRESS,ADDRESS" default:":8080" help:"Bind address for the API server" group:"api"`
14+
PoolDatabase string `env:"LOCALAI_POOL_DATABASE,POOL_DATABASE" default:"explorer.json" help:"Path to the pool database" group:"api"`
15+
ConnectionTimeout string `env:"LOCALAI_CONNECTION_TIMEOUT,CONNECTION_TIMEOUT" default:"2m" help:"Connection timeout for the explorer" group:"api"`
1416
}
1517

1618
func (e *ExplorerCMD) Run(ctx *cliContext.Context) error {
@@ -20,7 +22,11 @@ func (e *ExplorerCMD) Run(ctx *cliContext.Context) error {
2022
return err
2123
}
2224

23-
ds := explorer.NewDiscoveryServer(db)
25+
dur, err := time.ParseDuration(e.ConnectionTimeout)
26+
if err != nil {
27+
return err
28+
}
29+
ds := explorer.NewDiscoveryServer(db, dur)
2430

2531
go ds.Start(context.Background())
2632
appHTTP := http.Explorer(db, ds)

core/explorer/discovery.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313

1414
type DiscoveryServer struct {
1515
sync.Mutex
16-
database *Database
17-
networkState *NetworkState
16+
database *Database
17+
networkState *NetworkState
18+
connectionTime time.Duration
1819
}
1920

2021
type NetworkState struct {
@@ -29,9 +30,13 @@ func (s *DiscoveryServer) NetworkState() *NetworkState {
2930

3031
// NewDiscoveryServer creates a new DiscoveryServer with the given Database.
3132
// it keeps the db state in sync with the network state
32-
func NewDiscoveryServer(db *Database) *DiscoveryServer {
33+
func NewDiscoveryServer(db *Database, dur time.Duration) *DiscoveryServer {
34+
if dur == 0 {
35+
dur = 50 * time.Second
36+
}
3337
return &DiscoveryServer{
34-
database: db,
38+
database: db,
39+
connectionTime: dur,
3540
networkState: &NetworkState{
3641
Networks: map[string]Network{},
3742
},
@@ -49,7 +54,7 @@ func (s *DiscoveryServer) runBackground() {
4954
}
5055

5156
for _, token := range s.database.TokenList() {
52-
c, cancel := context.WithTimeout(context.Background(), 50*time.Second)
57+
c, cancel := context.WithTimeout(context.Background(), s.connectionTime)
5358
defer cancel()
5459

5560
// Connect to the network

0 commit comments

Comments
 (0)