Skip to content

Commit

Permalink
Merge pull request deis#3348 from aledbf/services_directory_with_ttl
Browse files Browse the repository at this point in the history
fix(publisher): use TTL in services application etcd directories
  • Loading branch information
aledbf committed Mar 30, 2015
2 parents 652cb41 + 25ebb4d commit dfd4e81
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions publisher/server/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ func (s *Server) publishContainer(container *docker.APIContainers, ttl time.Dura
appName := match[1]
appPath := fmt.Sprintf("%s/%s", appName, containerName)
keyPath := fmt.Sprintf("/deis/services/%s", appPath)
dirPath := fmt.Sprintf("/deis/services/%s", appName)
for _, p := range container.Ports {
port := strconv.Itoa(int(p.PublicPort))
hostAndPort := host + ":" + port
if s.IsPublishableApp(containerName) && s.IsPortOpen(hostAndPort) {
s.setEtcd(keyPath, hostAndPort, uint64(ttl.Seconds()))
s.updateDir(dirPath, uint64(ttl.Seconds()))
safeMap.Lock()
safeMap.data[container.ID] = appPath
safeMap.Unlock()
Expand Down Expand Up @@ -146,6 +148,7 @@ func (s *Server) IsPublishableApp(name string) bool {
return false
}

// IsPortOpen checks if the given port is accepting tcp connections
func (s *Server) IsPortOpen(hostAndPort string) bool {
portOpen := false
conn, err := net.Dial("tcp", hostAndPort)
Expand Down Expand Up @@ -215,3 +218,12 @@ func (s *Server) removeEtcd(key string, recursive bool) {
}
log.Println("del", key)
}

// updateDir updates the given directory for a given ttl. It succeeds
// only if the given directory already exists.
func (s *Server) updateDir(directory string, ttl uint64) {
if _, err := s.EtcdClient.UpdateDir(directory, ttl); err != nil {
log.Println(err)
}
log.Println("updateDir", directory)
}

0 comments on commit dfd4e81

Please sign in to comment.