Debian Codegraph @

chaos/chaos-server.go

raw symbols SCIP
1 /*
2 * Minio Cloud Storage, (C) 2016 Minio, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 // For this test framework we have short lock timings
32 // Production should use eg following values
33 //
34 // const LockMaintenanceLoop = 1 * time.Minute
35 // const LockCheckValidityInterval = 2 * time.Minute
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 // timestamp: leave uninitialized for testing (set to real timestamp for actual usage)
49 }
50 go func() {
51 // Start with random sleep time, so as to avoid "synchronous checks" between servers
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 // For some reason the registration paths need to be different (even for different server objs)
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 }