mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 10:06:24 +05:00
restruturing
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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!")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user