// NewNode creates a new go-doudou node.
// service related custom data (<= 512 bytes after being marshalled as json format) can be passed into it by data parameter.
// it is made as a variadic function only for backward compatibility purposes,
// only first parameter will be used.
NewNode creates a new go-doudou node.
service related custom data (<= 512 bytes after being marshalled as json format) can be passed into it by data parameter.
it is made as a variadic function only for backward compatibility purposes,
only first parameter will be used.
// NewNode creates a new go - doudou node . // service related custom data ( < = 512 bytes after being marshalled as json format ) can be passed into it by data parameter . // it is made as a variadic function only for backward compatibility purposes // only first parameter will be used .
func NewNode(data ...map[string]interface{}) error {
	mconf := newConf()
	service := config.GddServiceName.Load()
	if stringutils.IsEmpty(service) {
		return errors.New(fmt.Sprintf("NewNode() error: No env variable %s found", config.GddServiceName))
	}
	port := cast.ToInt(config.GddPort.Load())
	if port == 0 {
		port, _ = getFreePort()
	}
	config.GddPort.Write(fmt.Sprint(port))
	now := time.Now()
	var buildTime string
	if stringutils.IsNotEmpty(config.BuildTime) {
		if t, err := time.Parse(constants.FORMAT15, config.BuildTime); err == nil {
			buildTime = t.Local().Format(constants.FORMAT)
		}
	}
	mmeta := mergedMeta{
		Meta: nodeMeta{
			Service:       service,
			RouteRootPath: config.GddRouteRootPath.Load(),
			Port:          port,
			RegisterAt:    &now,
			GoVer:         runtime.Version(),
			GddVer:        config.GddVer,
			BuildUser:     config.BuildUser,
			BuildTime:     buildTime,
			Weight:        cast.ToInt(config.GddMemWeight.Load()),
		},
		Data: make(map[string]interface{}),
	}
	if len(data) > 0 {
		mmeta.Data = data[0]
	}
	queue := &memberlist.TransmitLimitedQueue{
		NumNodes: func() int {
			if mlist == nil {
				return 0
			}
			return len(mlist.Members())
		},
		RetransmitMult: mconf.RetransmitMult,
	}
	BroadcastQueue = queue
	mconf.Delegate = &delegate{
		mmeta: mmeta,
		queue: queue,
	}
	mconf.Events = events
	var err error
	if mlist, err = memberlist.Create(mconf); err != nil {
		return errors.Wrap(err, "NewNode() error: Failed to create memberlist")
	}
	if err = join(); err != nil {
		mlist.Shutdown()
		return errors.Wrap(err, "NewNode() error: Node register failed")
	}
	local := mlist.LocalNode()
	baseUrl, _ := BaseUrl(local)
	logrus.Infof("memberlist created. local node is Node %s, providing %s service at %s, memberlist port %s",
		local.Name, mmeta.Meta.Service, baseUrl, fmt.Sprint(local.Port))
	return nil
}
func NewNode ( data ... map [ string ] interface { } ) error { mconf := newConf ( ) service := config . GddServiceName . Load ( ) if stringutils . IsEmpty ( service ) { return errors . New ( fmt . Sprintf ( " " , config . GddServiceName ) ) } port := cast . ToInt ( config . GddPort . Load ( ) ) if port == 0 { port , _ = getFreePort ( ) } config . GddPort . Write ( fmt . Sprint ( port ) ) now := time . Now ( ) var buildTime string if stringutils . IsNotEmpty ( config . BuildTime ) { if t , err := time . Parse ( constants . FORMAT15 , config . BuildTime ) ; err == nil { buildTime = t . Local ( ) . Format ( constants . FORMAT ) } } mmeta := mergedMeta { Meta : nodeMeta { Service : service , RouteRootPath : config . GddRouteRootPath . Load ( ) , Port : port , RegisterAt : & now , GoVer : runtime . Version ( ) , GddVer : config . GddVer , BuildUser : config . BuildUser , BuildTime : buildTime , Weight : cast . ToInt ( config . GddMemWeight . Load ( ) ) , } , Data : make ( map [ string ] interface { } ) , } if len ( data ) > 0 { mmeta . Data = data [ 0 ] } queue := & memberlist . TransmitLimitedQueue { NumNodes : func ( ) int { if mlist == nil { return 0 } return len ( mlist . Members ( ) ) } , RetransmitMult : mconf . RetransmitMult , } BroadcastQueue = queue mconf . Delegate = & delegate { mmeta : mmeta , queue : queue , } mconf . Events = events var err error if mlist , err = memberlist . Create ( mconf ) ; err != nil { return errors . Wrap ( err , " " ) } if err = join ( ) ; err != nil { mlist . Shutdown ( ) return errors . Wrap ( err , " " ) } local := mlist . LocalNode ( ) baseUrl , _ := BaseUrl ( local ) logrus . Infof ( " " , local . Name , mmeta . Meta . Service , baseUrl , fmt . Sprint ( local . Port ) ) return nil }
NewNode creates a new go-doudou node.
NewNode creates a new go - doudou node .