MCPcopy
hub / github.com/rclone/rclone / start

Function start

fstest/testserver/testserver.go:79–148  ·  view source on GitHub ↗

Start the server and env vars so rclone can use it

(name string)

Source from the content-addressed store, hash-verified

77
78// Start the server and env vars so rclone can use it
79func start(name string) error {
80 fs.Logf(name, "Starting server")
81 out, err := run(name, "start")
82 if err != nil {
83 return err
84 }
85 // parse the output and set environment vars from it
86 var connect string
87 var connectDelay time.Duration
88 for line := range bytes.SplitSeq(out, []byte("\n")) {
89 line = bytes.TrimSpace(line)
90 part := matchLine.FindSubmatch(line)
91 if part != nil {
92 key, value := part[1], part[2]
93 if string(key) == "_connect" {
94 connect = string(value)
95 continue
96 } else if string(key) == "_connect_delay" {
97 connectDelay, err = time.ParseDuration(string(value))
98 if err != nil {
99 return fmt.Errorf("bad _connect_delay: %w", err)
100 }
101 continue
102 }
103
104 // fs.Debugf(name, "key = %q, envKey = %q, value = %q", key, envKey(name, string(key)), value)
105 err = os.Setenv(envKey(name, string(key)), string(value))
106 if err != nil {
107 return err
108 }
109 }
110 }
111 if connect == "" {
112 fs.Logf(name, "Started server")
113 return nil
114 }
115 // If we got a _connect value then try to connect to it
116 const maxTries = 100
117 var rdBuf = make([]byte, 1)
118 for i := 1; i <= maxTries; i++ {
119 if i != 0 {
120 time.Sleep(time.Second)
121 }
122 fs.Logf(name, "Attempting to connect to %q try %d/%d", connect, i, maxTries)
123 conn, err := net.DialTimeout("tcp", connect, time.Second)
124 if err != nil {
125 fs.Debugf(name, "Connection to %q failed try %d/%d: %v", connect, i, maxTries, err)
126 continue
127 }
128
129 err = conn.SetReadDeadline(time.Now().Add(time.Second))
130 if err != nil {
131 return fmt.Errorf("failed to set deadline: %w", err)
132 }
133 n, err := conn.Read(rdBuf)
134 _ = conn.Close()
135 fs.Debugf(name, "Read %d, error: %v", n, err)
136 if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {

Callers 1

StartFunction · 0.70

Calls 11

LogfFunction · 0.92
DebugfFunction · 0.92
envKeyFunction · 0.85
SetReadDeadlineMethod · 0.80
IsMethod · 0.80
runFunction · 0.70
SetenvMethod · 0.65
AddMethod · 0.65
ReadMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…