| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"log" |
| 22 |
"math/rand" |
| 23 |
"net" |
| 24 |
"net/http" |
| 25 |
"net/rpc" |
| 26 |
"strconv" |
| 27 |
"sync" |
| 28 |
"time" |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
const LockMaintenanceLoop = 1 * time.Second |
| 38 |
const LockCheckValidityInterval = 5 * time.Second |
| 39 |
|
| 40 |
func startRPCServer(port int) { |
| 41 |
log.SetPrefix(fmt.Sprintf("[%d] ", port)) |
| 42 |
log.SetFlags(log.Lmicroseconds) |
| 43 |
|
| 44 |
server := rpc.NewServer() |
| 45 |
locker := &lockServer{ |
| 46 |
mutex: sync.Mutex{}, |
| 47 |
lockMap: make(map[string][]lockRequesterInfo), |
| 48 |
|
| 49 |
} |
| 50 |
go func() { |
| 51 |
|
| 52 |
time.Sleep(time.Duration(rand.Float64() * float64(LockMaintenanceLoop))) |
| 53 |
for { |
| 54 |
time.Sleep(LockMaintenanceLoop) |
| 55 |
locker.lockMaintenance(LockCheckValidityInterval) |
| 56 |
} |
| 57 |
}() |
| 58 |
server.RegisterName("Dsync", locker) |
| 59 |
|
| 60 |
rpcPath := rpcPathPrefix + "-" + strconv.Itoa(port) |
| 61 |
server.HandleHTTP(rpcPath, fmt.Sprintf("%s-debug", rpcPath)) |
| 62 |
l, e := net.Listen("tcp", ":"+strconv.Itoa(port)) |
| 63 |
if e != nil { |
| 64 |
log.Fatal("listen error:", e) |
| 65 |
} |
| 66 |
log.Println("RPC server listening at port", port, "under", rpcPath) |
| 67 |
http.Serve(l, nil) |
| 68 |
} |