MCPcopy Create free account
hub / github.com/PasarGuard/node / SyncUsersChunked

Method SyncUsersChunked

controller/rest/user.go:82–147  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

80}
81
82func (s *Service) SyncUsersChunked(w http.ResponseWriter, r *http.Request) {
83 reader := bufio.NewReader(r.Body)
84 defer r.Body.Close()
85
86 chunks := make(map[uint64][]*common.User)
87 var (
88 lastIndex uint64
89 sawLast bool
90 )
91
92 for {
93 size, err := binary.ReadUvarint(reader)
94 if errors.Is(err, io.EOF) {
95 break
96 }
97 if err != nil {
98 http.Error(w, fmt.Sprintf("failed to read chunk length: %v", err), http.StatusBadRequest)
99 return
100 }
101 if size == 0 {
102 continue
103 }
104
105 payload := make([]byte, size)
106 if _, err = io.ReadFull(reader, payload); err != nil {
107 http.Error(w, fmt.Sprintf("failed to read chunk payload: %v", err), http.StatusBadRequest)
108 return
109 }
110
111 chunk := &common.UsersChunk{}
112 if err = proto.Unmarshal(payload, chunk); err != nil {
113 http.Error(w, fmt.Sprintf("failed to decode chunk: %v", err), http.StatusBadRequest)
114 return
115 }
116
117 chunks[chunk.GetIndex()] = append(chunks[chunk.GetIndex()], chunk.GetUsers()...)
118
119 if chunk.GetLast() {
120 sawLast = true
121 lastIndex = chunk.GetIndex()
122 break
123 }
124 }
125
126 users, err := controller.BuildUsersFromChunks(chunks, lastIndex, sawLast)
127 if err != nil {
128 http.Error(w, err.Error(), http.StatusBadRequest)
129 return
130 }
131
132 // Large chunk: update in-memory then restart (no API calls).
133 if len(users) > 100 {
134 if err := s.Backend().UpdateUsersAndRestart(r.Context(), users); err != nil {
135 http.Error(w, err.Error(), http.StatusInternalServerError)
136 return
137 }
138 } else {
139 // Small chunk: update via API without restart.

Callers

nothing calls this directly

Implementers 1

Controllercontroller/controller.go

Calls 10

GetIndexMethod · 0.95
GetUsersMethod · 0.95
GetLastMethod · 0.95
BuildUsersFromChunksFunction · 0.92
SendProtoResponseFunction · 0.92
ErrorMethod · 0.80
BackendMethod · 0.80
CloseMethod · 0.65
UpdateUsersAndRestartMethod · 0.65
UpdateUsersMethod · 0.65

Tested by

no test coverage detected