Skip to content

Commit

Permalink
fix: reserve servers with any upstream error (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 authored Aug 23, 2021
1 parent 023a3d9 commit 7747782
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
34 changes: 18 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package config

import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/Qv2ray/mmp-go/cipher"
"github.com/Qv2ray/mmp-go/infra/lru"
"log"
"net"
"os"
"sync"
"time"
Expand Down Expand Up @@ -36,25 +34,23 @@ type Group struct {
UserContextPool *UserContextPool `json:"-"`
}

type UpstreamConf map[string]string
type UpstreamConf map[string]interface{}

const (
PullingErrorKey = "__pulling_error__"
PullingErrorNetError = "net_error"
PullingErrorKey = "__pulling_error__"
)

func (uc UpstreamConf) InitPullingError() {
if _, ok := uc[PullingErrorKey]; !ok {
uc[PullingErrorKey] = ""
func (uc UpstreamConf) GetPullingError() error {
if v, ok := uc[PullingErrorKey]; ok {
return v.(error)
}
return nil
}
func (uc UpstreamConf) SetPullingError(err error) {
uc[PullingErrorKey] = err
}

func (uc UpstreamConf) Equal(that UpstreamConf) bool {
uc.InitPullingError()
that.InitPullingError()
if len(uc) != len(that) {
return false
}
for k, v := range uc {
if k == PullingErrorKey {
continue
Expand All @@ -63,6 +59,14 @@ func (uc UpstreamConf) Equal(that UpstreamConf) bool {
return false
}
}
for k, v := range that {
if k == PullingErrorKey {
continue
}
if vv, ok := uc[k]; !ok || vv != v {
return false
}
}
return true
}

Expand Down Expand Up @@ -158,9 +162,7 @@ func parseUpstreams(config *Config) (err error) {
defer wg.Done()
servers, err := pullFromUpstream(upstream, upstreamConf)
if err != nil {
if netError := new(net.Error); errors.As(err, netError) {
(*upstreamConf)[PullingErrorKey] = PullingErrorNetError
}
upstreamConf.SetPullingError(err)
log.Printf("[warning] Failed to pull from group %s upstream %s: %v\n", group.Name, upstream.GetName(), err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions config/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Upstream interface {

var InvalidUpstreamErr = fmt.Errorf("invalid upstream")

func Map2Upstream(m map[string]string, upstream interface{}) error {
func Map2Upstream(m UpstreamConf, upstream interface{}) error {
v := reflect.ValueOf(upstream)
if !v.IsValid() {
return fmt.Errorf("upstream should not be nil")
Expand All @@ -27,7 +27,7 @@ func Map2Upstream(m map[string]string, upstream interface{}) error {
tag := f.Tag
key := tag.Get("json")
vf := v.Field(i)
vf.SetString(m[key])
vf.SetString(m[key].(string))
}
return nil
}
45 changes: 23 additions & 22 deletions reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,32 @@ func ReloadConfig() {
newGroup := &newConf.Groups[i]
for j := range newGroup.Upstreams {
newUpstream := newGroup.Upstreams[j]
if newUpstream[config.PullingErrorKey] != config.PullingErrorNetError {
continue
}
// net error, remain those servers
pErr := newUpstream.GetPullingError()
if pErr != nil {
log.Printf("skip to update some servers in group %v , error on upstream %v: %v", newGroup.Name, newUpstream["name"], pErr)
// error occurred, remain those servers

// find the group in the oldConf
var oldGroup *config.Group
for k := range oldConf.Groups {
// they should have the same port
if oldConf.Groups[k].Port != newGroup.Port {
// find the group in the oldConf
var oldGroup *config.Group
for k := range oldConf.Groups {
// they should have the same port
if oldConf.Groups[k].Port != newGroup.Port {
continue
}
oldGroup = &oldConf.Groups[k]
break
}
if oldGroup == nil {
// cannot find the corresponding old group
continue
}
oldGroup = &oldConf.Groups[k]
break
}
if oldGroup == nil {
// cannot find the corresponding old group
continue
}
// check if upstreamConf can match
for k := range oldGroup.Servers {
oldServer := oldGroup.Servers[k]
if oldServer.UpstreamConf != nil && newUpstream.Equal(*oldServer.UpstreamConf) {
// remain the server
newGroup.Servers = append(newGroup.Servers, oldServer)
// check if upstreamConf can match
for k := range oldGroup.Servers {
oldServer := oldGroup.Servers[k]
if oldServer.UpstreamConf != nil && newUpstream.Equal(*oldServer.UpstreamConf) {
// remain the server
newGroup.Servers = append(newGroup.Servers, oldServer)
}
}
}
}
Expand Down

0 comments on commit 7747782

Please sign in to comment.