优化服务启动流程
This commit is contained in:
91
server/report/report.go
Normal file
91
server/report/report.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"proxy-server/client/core"
|
||||
"proxy-server/server/pkg/env"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Online(ctx context.Context, name string) (id int32, err error) {
|
||||
var resp string
|
||||
resp, err = repeat(ctx, env.EndpointOnline, map[string]any{
|
||||
"name": name,
|
||||
"version": core.Version,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Id int32 `json:"id"`
|
||||
}
|
||||
err = json.Unmarshal([]byte(resp), &body)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if body.Id == 0 {
|
||||
return 0, errors.New("服务注册返回 ID 有误")
|
||||
} else {
|
||||
return body.Id, nil
|
||||
}
|
||||
}
|
||||
|
||||
func Offline(ctx context.Context, name string) (err error) {
|
||||
_, err = repeat(ctx, env.EndpointOffline, map[string]any{
|
||||
"name": name,
|
||||
"version": core.Version,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func Assigned(ctx context.Context, id int32, edgeId int32, port int16) (err error) {
|
||||
_, err = repeat(ctx, env.EndpointAssigned, map[string]any{
|
||||
"proxy": id,
|
||||
"edge": edgeId,
|
||||
"port": port,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func repeat(ctx context.Context, endpoint string, body any) (string, error) {
|
||||
bodyStr, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for {
|
||||
req, err := http.NewRequest("POST", endpoint, strings.NewReader(string(bodyStr)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Basic "+base64.RawURLEncoding.EncodeToString([]byte("proxy:proxy")))
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if resp != nil && resp.StatusCode == http.StatusOK {
|
||||
var body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
slog.Warn("服务注册失败,五秒后重试", "err", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user