restruturing

This commit is contained in:
Radu Ioan Fericean
2012-01-27 16:31:54 +02:00
parent db4812cd51
commit 3d1fb32ad8
21 changed files with 0 additions and 102 deletions

View File

@@ -1,69 +0,0 @@
package main
import (
"fmt"
"log"
"net/rpc"
"time"
"sync"
)
type RaterList struct {
Clients map[string]*rpc.Client
Balancer chan *rpc.Client
balancer_mutex sync.Mutex
}
func NewRaterList() *RaterList {
r:= &RaterList{
Clients: make(map[string]*rpc.Client),
Balancer: make(chan *rpc.Client),
}
r.startBalance()
return r
}
func (rl *RaterList) RegisterRater(clientAddress string, replay *byte) error {
time.Sleep(1 * time.Second) // wait a second for Rater to start serving
client, err := rpc.Dial("tcp", clientAddress)
if err != nil {
log.Print("Could not connect to client!")
return err
}
rl.Clients[clientAddress] = client
log.Print(fmt.Sprintf("Rater %v registered succesfully.", clientAddress))
if len(rl.Clients) == 1 {
// unlock the balancer on first rater
rl.balancer_mutex.Unlock()
}
return nil
}
func (rl *RaterList) UnRegisterRater(clientAddress string, replay *byte) error {
client, ok := rl.Clients[clientAddress]
if ok {
client.Close()
delete(rl.Clients, clientAddress)
log.Print(fmt.Sprintf("Rater %v unregistered succesfully.", clientAddress))
} else {
log.Print(fmt.Sprintf("Server %v was not on my watch!", clientAddress))
}
return nil
}
func (rl *RaterList) startBalance() {
rl.balancer_mutex.Lock()
go func(){
for {
rl.balancer_mutex.Lock()
for _, client := range rl.Clients {
//log.Printf("using server %s:", addr)
rl.Balancer <- client
}
if len(rl.Clients) != 0 {
rl.balancer_mutex.Unlock()
}
}
}()
}

View File

@@ -1,13 +0,0 @@
package main
import (
"fmt"
)
type Responder int
func (r *Responder) Get(args string, replay *string) error {
*replay = fmt.Sprintf("{'response': %s}", callRater(args))
return nil
}

View File

@@ -1,20 +0,0 @@
package main
import (
"fmt"
"github.com/fsouza/gokabinet/kc"
"flag"
)
var (
fileName = flag.String("fileName", "storage.kch", "kyoto storage file")
)
func main() {
flag.Parse()
db, _ := kc.Open(*fileName, kc.WRITE)
defer db.Close()
db.Set("test", "12223")
fmt.Println("Done!")
}