Skip to content

Instantly share code, notes, and snippets.

@gillesdemey
Last active July 12, 2020 15:22
Show Gist options
  • Save gillesdemey/c43f27230adda282fa8add33fdff0890 to your computer and use it in GitHub Desktop.
Save gillesdemey/c43f27230adda282fa8add33fdff0890 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/hashicorp/serf/serf"
"log"
"os"
"os/signal"
"syscall"
)
type Server struct {
Agent *serf.Serf
Config *serf.Config
done chan os.Signal
}
func (s *Server) StopGraceful() {
if err := s.Agent.Leave(); err != nil {
log.Fatal(err)
}
if err := s.Agent.Shutdown(); err != nil {
log.Fatal(err)
}
}
func CreateServer(seed string) (*Server, error) {
signals := make(chan os.Signal, 1)
config := serf.DefaultConfig()
agent, err := serf.Create(config)
if err != nil {
return nil, err
}
if _, err = agent.Join([]string{seed}, false); err != nil {
return nil, err
}
return &Server{
Agent: agent,
Config: config,
done: signals,
}, nil
}
func main() {
server, err := CreateServer("0.0.0.0:7947")
if err != nil {
log.Fatalf("Failed to create Serf server: %s\n", err)
}
signal.Notify(server.done, os.Interrupt, syscall.SIGTERM)
<-server.done
server.StopGraceful()
os.Exit(0)
}
@gillesdemey
Copy link
Author

This requires a seed node

serf agent -node seed-node -bind 0.0.0.0:7947 -rpc-addr 127.0.0.1:7374

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment