How go-zookeeper/zk works
Project type and purpose
• This is a native Go client library for Apache Zookeeper. It provides session management, command encoding/decoding, watches, locks, and other ZK primitives for Go applications.
File structure
• Top-level files
• conn.go, flw.go, structs.go, constants.go, util.go, lock.go, create_mode.go, dnshostprovider.go – the core of the library.
• *_test.go files – unit tests for each component.
• _examples directory – sample Go programs showing how to use the API.
• Internal directory
• internal/assert – assertion helpers used in tests and internal code.
• Configuration and tooling
• .github/workflows – CI definitions (linting, testing).
• Makefile, go.mod – build and dependency management.
README for additional insights
• Confirms that this is purely a client library (no server component).
a. Main components
1. Public API Layer
– Exposed functions and types that application authors import.
2. Session & Connection Manager (conn.go)
– Establishes and maintains TCP connections to ZK servers.
– Handles session creation, heartbeats, reconnect logic.
3. Protocol Frame Layer (flw.go)
– Encodes requests into the wire format.
– Decodes response frames back into Go structs.
4. Data Structures & Constants (structs.go, constants.go)
– Defines request/response types, ACLs, error codes, create modes.
5. Watch & Event Dispatcher (util.go, structs.go)
– Registers watchers, dispatches events back to user callbacks.
6. Host Provider (dnshostprovider.go)
– Resolves ZK hostnames, handles DNS-based discovery.
7. Lock Implementation (lock.go)
– High-level distributed lock abstraction built on top of the core primitives.
8. Examples (/_examples)
– Illustrate basic usage and patterns.
b. Relationships and interactions
• User Application → Public API Layer → Session & Connection Manager
• Sends operations (create, get, set, delete, sync, exists)
• Session & Connection Manager ↔ Zookeeper Cluster
• Over TCP, uses Protocol Frame Layer to marshal/unmarshal
• Incoming Frames → Protocol Layer → Event Dispatcher → User callbacks
• Lock Implementation builds on Public API calls (create, delete, watches)
• Host Provider feeds server addresses into Connection Manager
c. Architectural patterns and principles
• Layered architecture: API → Session → Protocol → Transport
• Separation of concerns: encoding/decoding is isolated from connection logic
• Pluggable host resolution (allows custom DNS or static lists)
• Minimal global state; uses per-conn session objects
d. Key technologies and libraries
• Go’s net package for TCP connections
• Standard library sync primitives for concurrent event dispatch
• Go modules (go.mod) for dependency management