Skip to content

Commit b140d11

Browse files
committed
Implement lazy connection pattern
1 parent 5fcf41f commit b140d11

File tree

10 files changed

+287
-146
lines changed

10 files changed

+287
-146
lines changed

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
## Features
1414

1515
- **Simple API**: Fluent, chainable API design
16+
- **Lazy Connection**: Automatic connection establishment on first operation
1617
- **XML Manipulation**: Path-based XML operations using [xmldot](https://github.com/netascode/xmldot) (like gjson/sjson for JSON)
1718
- **Complete NETCONF Support**: All standard operations (Get, GetConfig, EditConfig, Lock, Commit, etc.)
1819
- **Robust Transport**: Built on [scrapligo](https://github.com/scrapli/scrapligo) for reliable SSH connectivity and NETCONF protocol handling
@@ -47,7 +48,7 @@ import (
4748
)
4849

4950
func main() {
50-
// Create client
51+
// Create client (connection established lazily on first operation)
5152
client, err := netconf.NewClient(
5253
"192.168.1.1",
5354
netconf.Username("admin"),
@@ -59,7 +60,7 @@ func main() {
5960
}
6061
defer client.Close()
6162

62-
// Get configuration with filter
63+
// Connection opens automatically on first operation
6364
ctx := context.Background()
6465
filter := netconf.SubtreeFilter("<interfaces/>")
6566
res, err := client.GetConfig(ctx, "running", filter)
@@ -77,7 +78,10 @@ func main() {
7778

7879
### Client Creation
7980

81+
The client uses lazy connection - the connection is established automatically on the first operation:
82+
8083
```go
84+
// Create client without connecting
8185
client, err := netconf.NewClient(
8286
"192.168.1.1",
8387
netconf.Username("admin"),
@@ -86,6 +90,14 @@ client, err := netconf.NewClient(
8690
netconf.MaxRetries(5),
8791
netconf.OperationTimeout(120*time.Second),
8892
)
93+
94+
// Connection established automatically on first operation
95+
res, err := client.GetConfig(ctx, "running", filter)
96+
97+
// Or open connection explicitly if needed
98+
if err := client.Open(); err != nil {
99+
log.Fatal(err)
100+
}
89101
```
90102

91103
### Get Configuration
@@ -167,10 +179,10 @@ client, err := netconf.NewClient(
167179

168180
### Connection Lifecycle Management
169181

170-
The connection is opened automatically when creating a client with `NewClient()`. For use cases that require explicit connection management, use `Reopen()` to reestablish closed connections and `IsClosed()` to check connection state:
182+
The client uses lazy connection - the connection is established automatically on the first operation. For explicit connection control, use `Open()` to establish the connection immediately, and `IsClosed()` to check connection state:
171183

172184
```go
173-
// Create client (connection opens automatically)
185+
// Create client without connecting
174186
client, err := netconf.NewClient(
175187
"192.168.1.1",
176188
netconf.Username("admin"),
@@ -180,18 +192,17 @@ if err != nil {
180192
log.Fatal(err)
181193
}
182194

183-
// Use client for first operation
195+
// Connection opens automatically on first operation
184196
res, err := client.GetConfig(ctx, "running", filter)
185-
client.Close() // Close after operation
186197

187-
// Later - check and reopen if needed
188-
if client.IsClosed() {
189-
if err := client.Reopen(); err != nil {
190-
log.Fatal(err)
191-
}
198+
// Close connection
199+
client.Close()
200+
201+
// Reopen connection (Open is idempotent - safe to call even if already open)
202+
if err := client.Open(); err != nil {
203+
log.Fatal(err)
192204
}
193205
res, err = client.EditConfig(ctx, "candidate", config)
194-
client.Close() // Close after operation
195206
```
196207

197208
### Capability Checking

0 commit comments

Comments
 (0)