func (s *server) Serve(ctx context.Context) error {
	defer s.servingWG.Done()
	derivedCtx, derivedCancel := context.WithCancel(ctx)
	defer derivedCancel()
	go s.notifyClients(derivedCtx)
	s.servingWG.Add(1)  
	go func() {
		<-derivedCtx.Done()
		s.unixListener.Close()
		close(s.eventC)
		s.servingWG.Done()
	}()
	var err error
	for derivedCtx.Err() == nil {
		var conn net.Conn
		conn, err = s.unixListener.Accept()
		if err != nil {
			log.Printf("Could not Accept on socket %q: %s\n", s.filename, err)
			continue
		}
		s.addClient(conn)
	}
	return err
}
Serve all clients that connect to this server until the context is canceled.
It is expected that this will be called in a goroutine, after Listen has been
called.  This function should only be called once for a given server.
package eventsocket
import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net"
	"os"
	"sync"
	"time"
	"github.com/m-lab/tcp-info/inetdiag"
)
type TCPEvent int
const (
	Open = TCPEvent(iota)
	Close
)
type FlowEvent struct {
	Event     TCPEvent
	Timestamp time.Time
	UUID      string
	ID        *inetdiag.SockID  
}
type Server interface {
	Listen() error
	Serve(context.Context) error
	FlowCreated(timestamp time.Time, uuid string, sockid inetdiag.SockID)
	FlowDeleted(timestamp time.Time, uuid string)
}
type server struct {
	eventC       chan *FlowEvent
	filename     string
	clients      map[net.Conn]struct{}
	unixListener net.Listener
	mutex        sync.Mutex
	servingWG    sync.WaitGroup
}
func (s *server) addClient(c net.Conn) {
	log.Println("Adding new TCP event client", c)
	s.mutex.Lock()
	defer s.mutex.Unlock()
	s.clients[c] = struct{}{}
}
func (s *server) removeClient(c net.Conn) {
	s.servingWG.Add(1)
	defer s.servingWG.Done()
	s.mutex.Lock()
	defer s.mutex.Unlock()
	_, ok := s.clients[c]
	if !ok {
		log.Println("Tried to remove TCP event client", c, "that was not present")
		return
	}
	delete(s.clients, c)
}
func (s *server) sendToAllListeners(data string) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	for c := range s.clients {
		_, err := fmt.Fprintln(c, data)
		if err != nil {
			log.Println("Write to client", c, "failed with error", err, " - removing the client.")
			go s.removeClient(c)
			go c.Close()
		}
	}
}
func (s *server) notifyClients(ctx context.Context) {
	s.servingWG.Add(1)
	defer s.servingWG.Done()
	for ctx.Err() == nil {
		event := <-s.eventC
		var b []byte
		var err error
		if event != nil {
			b, err = json.Marshal(*event)
		}
		if event == nil || err != nil {
			log.Printf("WARNING: Bad event received %v (err: %v)\n", event, err)
			continue
		}
		s.sendToAllListeners(string(b))
	}
}
func (s *server) Listen() error {
	s.servingWG.Add(1)
	var err error
	os.Remove(s.filename)
	s.unixListener, err = net.Listen("unix", s.filename)
	return err
}