Пример #1
0
func newViewServer(addr string) *ViewServer {
	logger.Info("Creating new view server...")

	vs := &ViewServer{
		cView:      View{0, "", ""},
		addr:       addr,
		lastPings:  newpingTimeMap(),
		candidates: newcandidateManager(),
		ack:        false,
		alive:      atomic.NewAtomicBool(true),
		done:       make(chan bool),
		mu:         sync.RWMutex{},

		rpcCount: atomic.NewAtomicInt(0),
	}
	vs.lastPings.Set(Primary, -1)
	vs.lastPings.Set(Backup, -1)

	initVsStates(vs)
	vs.state = getvsState("vsInitialState")

	logger.Info("Create new view server done")

	return vs
}
func NewGroup(t int64) *Group {
	return &Group{
		calls:    make(map[string]*call),
		overTime: t,
		alive:    atomic.NewAtomicBool(true),
		done:     make(chan bool),
		lock:     sync.Mutex{},
	}
}
Пример #3
0
// Register a new server.
func Register(v interface{}, addr string) error {
	server := rpc.NewServer()
	if err := server.Register(v); err != nil {
		return err
	}

	os.Remove(addr)
	listener, err := net.Listen(network, addr)
	if err != nil {
		return err
	}
	servers[v] = &entry{listener, atomic.NewAtomicBool(false)}

	// Start server deamon.
	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				break
			} else {
				if IsUnreliable(v) {
					if rand.Int63()%1000 < 100 {
						// Discard current request.
						conn.Close()
					} else if rand.Int63()%1000 < 200 {
						// Process current request, but discard according reply.
						unixConn := conn.(*net.UnixConn)
						file, _ := unixConn.File()
						// CLose the write side to discard reoply.
						syscall.Shutdown(int(file.Fd()), syscall.SHUT_WR)
						go server.ServeConn(conn)
					} else {
						go server.ServeConn(conn)
					}
				} else {
					go server.ServeConn(conn)
				}
			}
		}
	}()

	return nil
}
Пример #4
0
func newPBServer(addr, vs string) *PBServer {
	logger.Info(fmt.Sprintf("Creating pbserver %s", addr))

	s := &PBServer{
		addr:       addr,
		view:       View{},
		viewServer: vs,
		data:       make(map[string]string),
		group:      sf.NewGroup(time.Second.Nanoseconds()),

		alive: atomic.NewAtomicBool(true),
		done:  make(chan bool, 1),

		mu: sync.Mutex{},
	}

	logger.Info(fmt.Sprintf("Create pbserver %s done", addr))

	return s
}