mirror of https://github.com/mainnika/nodesync.git
parent
5726a1ba0d
commit
8c9aefd6c1
@ -0,0 +1,5 @@ |
||||
module code.tokarch.uk/mainnika/nodesync |
||||
|
||||
go 1.17 |
||||
|
||||
require github.com/go-zookeeper/zk v1.0.2 |
@ -0,0 +1,2 @@ |
||||
github.com/go-zookeeper/zk v1.0.2 h1:4mx0EYENAdX/B/rbunjlt5+4RTA/a9SMHBRuSKdGxPM= |
||||
github.com/go-zookeeper/zk v1.0.2/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw= |
@ -0,0 +1,48 @@ |
||||
package sync |
||||
|
||||
import ( |
||||
"github.com/go-zookeeper/zk" |
||||
|
||||
"fmt" |
||||
"strings" |
||||
) |
||||
|
||||
// createRecursively creates zookeeper path recursively
|
||||
func (s *NodeSync) createRecursively(path string, acl []zk.ACL) error { |
||||
|
||||
parts := strings.Split(path, "/") |
||||
|
||||
if len(parts) < 1 { |
||||
return fmt.Errorf("path does not contain valid path") |
||||
} |
||||
|
||||
for i := range parts { |
||||
Ensuring: |
||||
for { |
||||
child := fmt.Sprintf("%s", strings.Join(parts[:i+1], "/")) |
||||
|
||||
if len(child) == 0 { |
||||
break |
||||
} |
||||
|
||||
exists, _, err := s.Zk.Exists(child) |
||||
switch true { |
||||
case err == zk.ErrSessionExpired: |
||||
return err |
||||
case err != nil: |
||||
continue |
||||
case exists: |
||||
break Ensuring |
||||
default: |
||||
} |
||||
|
||||
if _, err = s.Zk.Create(child, nil, 0, acl); err != nil { |
||||
continue |
||||
} |
||||
|
||||
break |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
} |
@ -0,0 +1,28 @@ |
||||
package sync |
||||
|
||||
import ( |
||||
"github.com/go-zookeeper/zk" |
||||
) |
||||
|
||||
// NodeSync structure
|
||||
type NodeSync struct { |
||||
Zk *zk.Conn |
||||
|
||||
rootPath string |
||||
} |
||||
|
||||
// Inside private environment keep everything open
|
||||
var defaultAcl = zk.WorldACL(zk.PermAll) |
||||
|
||||
// New creates a new nodesync instance
|
||||
func New(zkconn *zk.Conn, rootPath string) (nodeSync *NodeSync, err error) { |
||||
|
||||
nodeSync = &NodeSync{Zk: zkconn, rootPath: rootPath} |
||||
|
||||
err = nodeSync.createRecursively(rootPath, defaultAcl) |
||||
if err != nil { |
||||
nodeSync = nil |
||||
} |
||||
|
||||
return |
||||
} |
Loading…
Reference in new issue